Debian Codegraph @

examples/async_watch_line_value.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]>
3
4 /* Minimal example of asynchronously watching for edges on a single line. */
5
6 #include <errno.h>
7 #include <gpiod.h>
8 #include <poll.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 /* Request a line as input with edge detection. */
14 static struct gpiod_line_request *request_input_line(const char *chip_path,
15 unsigned int offset,
16 const char *consumer)
17 {
18 struct gpiod_request_config *req_cfg = NULL;
19 struct gpiod_line_request *request = NULL;
20 struct gpiod_line_settings *settings;
21 struct gpiod_line_config *line_cfg;
22 struct gpiod_chip *chip;
23 int ret;
24
25 chip = gpiod_chip_open(chip_path);
26 if (!chip)
27 return NULL;
28
29 settings = gpiod_line_settings_new();
30 if (!settings)
31 goto close_chip;
32
33 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
34 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
35 /* Assume a button connecting the pin to ground, so pull it up... */
36 gpiod_line_settings_set_bias(settings, GPIOD_LINE_BIAS_PULL_UP);
37 /* ... and provide some debounce. */
38 gpiod_line_settings_set_debounce_period_us(settings, 10000);
39
40 line_cfg = gpiod_line_config_new();
41 if (!line_cfg)
42 goto free_settings;
43
44 ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
45 settings);
46 if (ret)
47 goto free_line_config;
48
49 if (consumer) {
50 req_cfg = gpiod_request_config_new();
51 if (!req_cfg)
52 goto free_line_config;
53
54 gpiod_request_config_set_consumer(req_cfg, consumer);
55 }
56
57 request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
58
59 gpiod_request_config_free(req_cfg);
60
61 free_line_config:
62 gpiod_line_config_free(line_cfg);
63
64 free_settings:
65 gpiod_line_settings_free(settings);
66
67 close_chip:
68 gpiod_chip_close(chip);
69
70 return request;
71 }
72
73 static const char *edge_event_type_str(struct gpiod_edge_event *event)
74 {
75 switch (gpiod_edge_event_get_event_type(event)) {
76 case GPIOD_EDGE_EVENT_RISING_EDGE:
77 return "Rising";
78 case GPIOD_EDGE_EVENT_FALLING_EDGE:
79 return "Falling";
80 default:
81 return "Unknown";
82 }
83 }
84
85 int main(void)
86 {
87 /* Example configuration - customize to suit your situation. */
88 static const char *const chip_path = "/dev/gpiochip0";
89 static const unsigned int line_offset = 5;
90
91 struct gpiod_edge_event_buffer *event_buffer;
92 struct gpiod_line_request *request;
93 struct gpiod_edge_event *event;
94 int i, ret, event_buf_size;
95 struct pollfd pollfd;
96
97 request = request_input_line(chip_path, line_offset,
98 "async-watch-line-value");
99 if (!request) {
100 fprintf(stderr, "failed to request line: %s\n",
101 strerror(errno));
102 return EXIT_FAILURE;
103 }
104
105 /*
106 * A larger buffer is an optimisation for reading bursts of events from
107 * the kernel, but that is not necessary in this case, so 1 is fine.
108 */
109 event_buf_size = 1;
110 event_buffer = gpiod_edge_event_buffer_new(event_buf_size);
111 if (!event_buffer) {
112 fprintf(stderr, "failed to create event buffer: %s\n",
113 strerror(errno));
114 return EXIT_FAILURE;
115 }
116
117 pollfd.fd = gpiod_line_request_get_fd(request);
118 pollfd.events = POLLIN;
119
120 for (;;) {
121 ret = poll(&pollfd, 1, -1);
122 if (ret == -1) {
123 fprintf(stderr, "error waiting for edge events: %s\n",
124 strerror(errno));
125 return EXIT_FAILURE;
126 }
127 ret = gpiod_line_request_read_edge_events(request, event_buffer,
128 event_buf_size);
129 if (ret == -1) {
130 fprintf(stderr, "error reading edge events: %s\n",
131 strerror(errno));
132 return EXIT_FAILURE;
133 }
134 for (i = 0; i < ret; i++) {
135 event = gpiod_edge_event_buffer_get_event(event_buffer,
136 i);
137 printf("offset: %d type: %-7s event #%ld\n",
138 gpiod_edge_event_get_line_offset(event),
139 edge_event_type_str(event),
140 gpiod_edge_event_get_line_seqno(event));
141 }
142 }
143 }