Debian Codegraph @

bindings/glib/examples/watch_multiple_edge_rising_glib.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023-2024 Bartosz Golaszewski <[email protected]>
3
4 /*
5 * Minimal example of asynchronously watching for rising edges on multiple
6 * lines.
7 */
8
9 #include <glib.h>
10 #include <gpiod-glib.h>
11 #include <stdlib.h>
12
13 static void on_edge_event(GpiodglibLineRequest *request G_GNUC_UNUSED,
14 GpiodglibEdgeEvent *event,
15 gpointer data G_GNUC_UNUSED)
16 {
17 g_autoptr(GString) event_name = NULL;
18 guint64 timestamp;
19 guint offset;
20
21 event_name = g_string_new(
22 g_enum_to_string(GPIODGLIB_EDGE_EVENT_TYPE_TYPE,
23 gpiodglib_edge_event_get_event_type(event)));
24 g_string_replace(event_name, "GPIODGLIB_EDGE_EVENT_", "", 0);
25 timestamp = gpiodglib_edge_event_get_timestamp_ns(event);
26 offset = gpiodglib_edge_event_get_line_offset(event);
27
28 g_print("%s %lu %u\n", event_name->str, timestamp, offset);
29 }
30
31 int main(void)
32 {
33 /* Example configuration - customize to suit your situation. */
34 static const gchar *const chip_path = "/dev/gpiochip1";
35 static const guint line_offsets[] = { 5, 3, 7 };
36 static const gsize num_lines = 3;
37
38 g_autoptr(GpiodglibRequestConfig) req_cfg = NULL;
39 g_autoptr(GpiodglibLineSettings) settings = NULL;
40 g_autoptr(GpiodglibLineRequest) request = NULL;
41 g_autoptr(GpiodglibLineConfig) line_cfg = NULL;
42 g_autoptr(GpiodglibChip) chip = NULL;
43 g_autoptr(GArray) offsets = NULL;
44 g_autoptr(GMainLoop) loop = NULL;
45 g_autoptr(GError) err = NULL;
46 gboolean ret;
47
48 offsets = g_array_new(FALSE, TRUE, sizeof(guint));
49 g_array_append_vals(offsets, line_offsets, num_lines);
50
51 chip = gpiodglib_chip_new(chip_path, &err);
52 if (!chip) {
53 g_printerr("unable to open %s: %s\n", chip_path, err->message);
54 return EXIT_FAILURE;
55 }
56
57 /*
58 * Assume a button connecting the pin to ground, so pull it up and
59 * provide some debounce.
60 */
61 settings = gpiodglib_line_settings_new(
62 "direction", GPIODGLIB_LINE_DIRECTION_INPUT,
63 "edge-detection", GPIODGLIB_LINE_EDGE_RISING,
64 "bias", GPIODGLIB_LINE_BIAS_PULL_UP,
65 "debounce-period-us", 1000,
66 NULL);
67
68 line_cfg = gpiodglib_line_config_new();
69 ret = gpiodglib_line_config_add_line_settings(line_cfg, offsets,
70 settings, &err);
71 if (!ret) {
72 g_printerr("failed to add line settings to line config: %s",
73 err->message);
74 return EXIT_FAILURE;
75 }
76
77 req_cfg = gpiodglib_request_config_new(NULL);
78 gpiodglib_request_config_set_consumer(req_cfg, "watch-multiline-value");
79
80 request = gpiodglib_chip_request_lines(chip, req_cfg, line_cfg, &err);
81 if (!request) {
82 g_printerr("failed to request lines: %s", err->message);
83 return EXIT_FAILURE;
84 }
85
86 loop = g_main_loop_new(NULL, FALSE);
87
88 /* Connect to the edge-event signal on the line-request. */
89 g_signal_connect(request, "edge-event",
90 G_CALLBACK(on_edge_event), NULL);
91
92 g_main_loop_run(loop);
93
94 return EXIT_SUCCESS;
95 }