Debian Codegraph @

bindings/glib/examples/get_line_value_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 reading a single line. */
5
6 #include <glib.h>
7 #include <gpiod-glib.h>
8 #include <stdlib.h>
9
10 int main(void)
11 {
12 /* Example configuration - customize to suit your situation. */
13 static const gchar *const chip_path = "/dev/gpiochip1";
14 static const guint line_offset = 5;
15
16 g_autoptr(GpiodglibRequestConfig) req_cfg = NULL;
17 g_autoptr(GpiodglibLineSettings) settings = NULL;
18 g_autoptr(GpiodglibLineRequest) request = NULL;
19 g_autoptr(GpiodglibLineConfig) line_cfg = NULL;
20 g_autoptr(GpiodglibChip) chip = NULL;
21 g_autoptr(GArray) offsets = NULL;
22 g_autoptr(GError) err = NULL;
23 guint offset;
24 gboolean ret;
25
26 chip = gpiodglib_chip_new(chip_path, &err);
27 if (!chip) {
28 g_printerr("unable to open %s: %s\n", chip_path, err->message);
29 return EXIT_FAILURE;
30 }
31
32 offsets = g_array_new(FALSE, TRUE, sizeof(guint));
33 g_array_append_val(offsets, line_offset);
34
35 settings = gpiodglib_line_settings_new("direction",
36 GPIODGLIB_LINE_DIRECTION_INPUT,
37 NULL);
38
39 line_cfg = gpiodglib_line_config_new();
40 ret = gpiodglib_line_config_add_line_settings(line_cfg, offsets,
41 settings, &err);
42 if (!ret) {
43 g_printerr("failed to add line settings to line config: %s\n",
44 err->message);
45 return EXIT_FAILURE;
46 }
47
48 req_cfg = gpiodglib_request_config_new("consumer",
49 "get-line-value-glib",
50 NULL);
51
52 request = gpiodglib_chip_request_lines(chip, req_cfg, line_cfg, &err);
53 if (!request) {
54 g_printerr("failed to request lines: %s\n", err->message);
55 return EXIT_FAILURE;
56 }
57
58 ret = gpiodglib_line_request_get_value(request, line_offset,
59 &offset, &err);
60 if (!ret) {
61 g_printerr("failed to read line values: %s\n", err->message);
62 return EXIT_FAILURE;
63 }
64
65 g_print("%u\n", offset);
66
67 return EXIT_SUCCESS;
68 }