Debian Codegraph @

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