Debian Codegraph @

bindings/glib/examples/get_line_info_glib.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2022-2024 Bartosz Golaszewski <[email protected]>
3
4 /* Minimal example of reading the info for a line. */
5
6 #include <glib.h>
7 #include <gpiod-glib.h>
8 #include <stdlib.h>
9
10 static GString *make_flags(GpiodglibLineInfo *info)
11 {
12 g_autofree gchar *drive_str = NULL;
13 g_autofree gchar *edge_str = NULL;
14 g_autofree gchar *bias_str = NULL;
15 GpiodglibLineDrive drive;
16 GpiodglibLineEdge edge;
17 GpiodglibLineBias bias;
18 GString *ret;
19
20 edge = gpiodglib_line_info_get_edge_detection(info);
21 bias = gpiodglib_line_info_get_bias(info);
22 drive = gpiodglib_line_info_get_drive(info);
23
24 edge_str = g_enum_to_string(GPIODGLIB_LINE_EDGE_TYPE, edge);
25 bias_str = g_enum_to_string(GPIODGLIB_LINE_BIAS_TYPE, bias);
26 drive_str = g_enum_to_string(GPIODGLIB_LINE_DRIVE_TYPE, drive);
27
28 ret = g_string_new(NULL);
29 g_string_printf(ret, "%s, %s, %s", edge_str, bias_str, drive_str);
30 g_string_replace(ret, "GPIODGLIB_LINE_", "", 0);
31
32 return ret;
33 }
34
35 int main(void)
36 {
37 /* Example configuration - customize to suit your situation. */
38 static const gchar *const chip_path = "/dev/gpiochip0";
39 static const guint line_offset = 4;
40
41 g_autoptr(GpiodglibLineInfo) info = NULL;
42 g_autoptr(GpiodglibChip) chip = NULL;
43 g_autofree gchar *consumer = NULL;
44 GpiodglibLineDirection direction;
45 g_autoptr(GString) flags = NULL;
46 g_autofree gchar *name = NULL;
47 g_autoptr(GError) err = NULL;
48 gboolean active_low;
49
50 chip = gpiodglib_chip_new(chip_path, &err);
51 if (err) {
52 g_printerr("Failed to open the GPIO chip at '%s': %s\n",
53 chip_path, err->message);
54 return EXIT_FAILURE;
55 }
56
57 info = gpiodglib_chip_get_line_info(chip, line_offset, &err);
58 if (err) {
59 g_printerr("Failed to retrieve GPIO line info: %s\n",
60 err->message);
61 return EXIT_FAILURE;
62 }
63
64 name = gpiodglib_line_info_dup_name(info);
65 consumer = gpiodglib_line_info_dup_consumer(info);
66 direction = gpiodglib_line_info_get_direction(info);
67 active_low = gpiodglib_line_info_is_active_low(info);
68 flags = make_flags(info);
69
70 g_print("\tline: %u %s %s %s %s [%s]\n",
71 line_offset,
72 name ?: "unnamed",
73 consumer ?: "unused",
74 direction == GPIODGLIB_LINE_DIRECTION_INPUT ?
75 "input" : "output",
76 active_low ? "active-low" : "active-high",
77 flags->str);
78
79 return EXIT_SUCCESS;
80 }