Debian Codegraph @

examples/watch_line_rising.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 rising 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_RISING);
34
35 line_cfg = gpiod_line_config_new();
36 if (!line_cfg)
37 goto free_settings;
38
39 ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
40 settings);
41 if (ret)
42 goto free_line_config;
43
44 if (consumer) {
45 req_cfg = gpiod_request_config_new();
46 if (!req_cfg)
47 goto free_line_config;
48
49 gpiod_request_config_set_consumer(req_cfg, consumer);
50 }
51
52 request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
53
54 gpiod_request_config_free(req_cfg);
55
56 free_line_config:
57 gpiod_line_config_free(line_cfg);
58
59 free_settings:
60 gpiod_line_settings_free(settings);
61
62 close_chip:
63 gpiod_chip_close(chip);
64
65 return request;
66 }
67
68 static const char *edge_event_type_str(struct gpiod_edge_event *event)
69 {
70 switch (gpiod_edge_event_get_event_type(event)) {
71 case GPIOD_EDGE_EVENT_RISING_EDGE:
72 return "Rising";
73 case GPIOD_EDGE_EVENT_FALLING_EDGE:
74 return "Falling";
75 default:
76 return "Unknown";
77 }
78 }
79
80 int main(void)
81 {
82 /* Example configuration - customize to suit your situation. */
83 static const char *const chip_path = "/dev/gpiochip0";
84 static const unsigned int line_offset = 5;
85
86 struct gpiod_edge_event_buffer *event_buffer;
87 struct gpiod_line_request *request;
88 struct gpiod_edge_event *event;
89 int i, ret, event_buf_size;
90
91 request = request_input_line(chip_path, line_offset,
92 "watch-line-value");
93 if (!request) {
94 fprintf(stderr, "failed to request line: %s\n",
95 strerror(errno));
96 return EXIT_FAILURE;
97 }
98
99 /*
100 * A larger buffer is an optimisation for reading bursts of events from
101 * the kernel, but that is not necessary in this case, so 1 is fine.
102 */
103 event_buf_size = 1;
104 event_buffer = gpiod_edge_event_buffer_new(event_buf_size);
105 if (!event_buffer) {
106 fprintf(stderr, "failed to create event buffer: %s\n",
107 strerror(errno));
108 return EXIT_FAILURE;
109 }
110
111 for (;;) {
112 /* Blocks until at least one event is available. */
113 ret = gpiod_line_request_read_edge_events(request, event_buffer,
114 event_buf_size);
115 if (ret == -1) {
116 fprintf(stderr, "error reading edge events: %s\n",
117 strerror(errno));
118 return EXIT_FAILURE;
119 }
120 for (i = 0; i < ret; i++) {
121 event = gpiod_edge_event_buffer_get_event(event_buffer,
122 i);
123 printf("offset: %d type: %-7s event #%ld\n",
124 gpiod_edge_event_get_line_offset(event),
125 edge_event_type_str(event),
126 gpiod_edge_event_get_line_seqno(event));
127 }
128 }
129 }