Debian Codegraph @

bindings/glib/examples/reconfigure_input_to_output_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 * Example of a bi-directional line requested as input and then switched
6 * to output.
7 */
8
9 #include <glib.h>
10 #include <gpiod-glib.h>
11 #include <stdlib.h>
12
13 int main(void)
14 {
15 /* Example configuration - customize to suit your situation */
16 static const gchar *const chip_path = "/dev/gpiochip1";
17 static const guint line_offset = 5;
18
19 g_autoptr(GpiodglibRequestConfig) req_cfg = NULL;
20 g_autoptr(GpiodglibLineSettings) settings = NULL;
21 g_autoptr(GpiodglibLineRequest) request = NULL;
22 g_autoptr(GpiodglibLineConfig) line_cfg = NULL;
23 g_autoptr(GpiodglibChip) chip = NULL;
24 g_autoptr(GArray) offsets = NULL;
25 g_autoptr(GError) err = NULL;
26 GpiodglibLineValue value;
27 gboolean ret;
28
29 offsets = g_array_new(FALSE, TRUE, sizeof(guint));
30 g_array_append_val(offsets, line_offset);
31
32 chip = gpiodglib_chip_new(chip_path, &err);
33 if (!chip) {
34 g_printerr("unable to open %s: %s\n", chip_path, err->message);
35 return EXIT_FAILURE;
36 }
37
38 settings = gpiodglib_line_settings_new("direction",
39 GPIODGLIB_LINE_DIRECTION_INPUT,
40 NULL);
41
42 line_cfg = gpiodglib_line_config_new();
43 ret = gpiodglib_line_config_add_line_settings(line_cfg, offsets,
44 settings, &err);
45 if (!ret) {
46 g_printerr("failed to add line settings to line config: %s\n",
47 err->message);
48 return EXIT_FAILURE;
49 }
50
51 req_cfg = gpiodglib_request_config_new("consumer",
52 "reconfigure-input-to-output",
53 NULL);
54
55 request = gpiodglib_chip_request_lines(chip, req_cfg, line_cfg, &err);
56 if (!request) {
57 g_printerr("failed to request lines: %s\n", err->message);
58 return EXIT_FAILURE;
59 }
60
61 /* Read the current line value. */
62 ret = gpiodglib_line_request_get_value(request, line_offset,
63 &value, &err);
64 if (!ret) {
65 g_printerr("failed to read line value: %s\n", err->message);
66 return EXIT_FAILURE;
67 }
68
69 g_print("%s (input)\n",
70 value == GPIODGLIB_LINE_VALUE_ACTIVE ? "Active" : "Inactive");
71
72 /* Switch the line to an output and drive it high. */
73 gpiodglib_line_settings_set_direction(settings,
74 GPIODGLIB_LINE_DIRECTION_OUTPUT);
75 gpiodglib_line_settings_set_output_value(settings,
76 GPIODGLIB_LINE_VALUE_ACTIVE);
77 ret = gpiodglib_line_config_add_line_settings(line_cfg, offsets,
78 settings, &err);
79 if (!ret) {
80 g_printerr("failed to add line settings to line config: %s\n",
81 err->message);
82 return EXIT_FAILURE;
83 }
84
85 /* Reconfigure lines. */
86 ret = gpiodglib_line_request_reconfigure_lines(request, line_cfg, &err);
87 if (!ret) {
88 g_printerr("failed to reconfigure lines: %s\n", err->message);
89 return EXIT_FAILURE;
90 }
91
92 /* Report the current driven value. */
93 ret = gpiodglib_line_request_get_value(request, line_offset,
94 &value, &err);
95 if (!ret) {
96 g_printerr("failed to read line value: %s\n", err->message);
97 return EXIT_FAILURE;
98 }
99
100 g_print("%s (output)\n",
101 value == GPIODGLIB_LINE_VALUE_ACTIVE ? "Active" : "Inactive");
102
103 return EXIT_SUCCESS;
104 }