Debian Codegraph @

bindings/glib/examples/toggle_multiple_line_values_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 /* Minimal example of periodically toggling multiple lines. */
5
6 #include <glib.h>
7 #include <gpiod-glib.h>
8 #include <stdlib.h>
9
10 typedef struct {
11 GpiodglibLineRequest *request;
12 GArray *offsets;
13 GArray *values;
14 } ToggleData;
15
16 static void toggle_values(GArray *values)
17 {
18 GpiodglibLineValue *value;
19 guint i;
20
21 for (i = 0; i < values->len; i++) {
22 value = &g_array_index(values, GpiodglibLineValue, i);
23 *value = *value == GPIODGLIB_LINE_VALUE_ACTIVE ?
24 GPIODGLIB_LINE_VALUE_INACTIVE :
25 GPIODGLIB_LINE_VALUE_ACTIVE;
26 }
27 }
28
29 static gboolean toggle_lines(gpointer user_data)
30 {
31 ToggleData *data = user_data;
32 g_autoptr(GError) err = NULL;
33 gboolean ret;
34 guint i;
35
36 toggle_values(data->values);
37
38 ret = gpiodglib_line_request_set_values_subset(data->request,
39 data->offsets,
40 data->values, &err);
41 if (!ret) {
42 g_printerr("failed to set line values: %s\n", err->message);
43 exit(EXIT_FAILURE);
44 }
45
46 for (i = 0; i < data->offsets->len; i++)
47 g_print("%u=%s ",
48 g_array_index(data->offsets, guint, i),
49 g_array_index(data->values,
50 GpiodglibLineValue,
51 i) == GPIODGLIB_LINE_VALUE_ACTIVE ?
52 "active" : "inactive");
53 g_print("\n");
54
55 return G_SOURCE_CONTINUE;
56 }
57
58 int main(void)
59 {
60 /* Example configuration - customize to suit your situation. */
61 static const gchar *const chip_path = "/dev/gpiochip1";
62 static const guint line_offsets[] = { 5, 3, 7 };
63 static const GpiodglibLineValue line_values[] = {
64 GPIODGLIB_LINE_VALUE_ACTIVE,
65 GPIODGLIB_LINE_VALUE_ACTIVE,
66 GPIODGLIB_LINE_VALUE_INACTIVE
67 };
68 static const gsize num_lines = 3;
69
70 g_autoptr(GpiodglibRequestConfig) req_cfg = NULL;
71 g_autoptr(GpiodglibLineSettings) settings = NULL;
72 g_autoptr(GpiodglibLineRequest) request = NULL;
73 g_autoptr(GpiodglibLineConfig) line_cfg = NULL;
74 g_autoptr(GpiodglibChip) chip = NULL;
75 g_autoptr(GArray) offsets = NULL;
76 g_autoptr(GMainLoop) loop = NULL;
77 g_autoptr(GArray) values = NULL;
78 g_autoptr(GError) err = NULL;
79 ToggleData data;
80 gboolean ret;
81
82 offsets = g_array_new(FALSE, TRUE, sizeof(guint));
83 g_array_append_vals(offsets, line_offsets, num_lines);
84
85 values = g_array_new(FALSE, TRUE, sizeof(GpiodglibLineValue));
86 g_array_append_vals(values, line_values, num_lines);
87
88 chip = gpiodglib_chip_new(chip_path, &err);
89 if (!chip) {
90 g_printerr("unable to open %s: %s\n", chip_path, err->message);
91 return EXIT_FAILURE;
92 }
93
94 settings = gpiodglib_line_settings_new("direction",
95 GPIODGLIB_LINE_DIRECTION_OUTPUT,
96 NULL);
97 line_cfg = gpiodglib_line_config_new();
98 ret = gpiodglib_line_config_add_line_settings(line_cfg, offsets,
99 settings, &err);
100 if (!ret) {
101 g_printerr("failed to add line settings to line config: %s\n",
102 err->message);
103 return EXIT_FAILURE;
104 }
105
106 ret = gpiodglib_line_config_set_output_values(line_cfg, values, &err);
107 if (!ret) {
108 g_printerr("failed to set output values: %s\n", err->message);
109 return EXIT_FAILURE;
110 }
111
112 req_cfg = gpiodglib_request_config_new("consumer", "toggle-line-value",
113 NULL);
114
115 request = gpiodglib_chip_request_lines(chip, req_cfg, line_cfg, &err);
116 if (!request) {
117 g_printerr("failed to request lines: %s\n", err->message);
118 return EXIT_FAILURE;
119 }
120
121 data.request = request;
122 data.offsets = offsets;
123 data.values = values;
124
125 loop = g_main_loop_new(NULL, FALSE);
126 /* Do the GLib way: add a callback to be invoked from the main loop. */
127 g_timeout_add_seconds(1, toggle_lines, &data);
128
129 g_main_loop_run(loop);
130
131 return EXIT_SUCCESS;
132 }