Debian Codegraph @

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