Debian Codegraph @

examples/get_multiple_line_values.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]>
3
4 /* Minimal example of reading multiple lines. */
5
6 #include <errno.h>
7 #include <gpiod.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #define NUM_LINES 3
13
14 /* Request a line as input. */
15 static struct gpiod_line_request *
16 request_input_lines(const char *chip_path, const unsigned int *offsets,
17 unsigned int num_lines, const char *consumer)
18 {
19 struct gpiod_request_config *req_cfg = NULL;
20 struct gpiod_line_request *request = NULL;
21 struct gpiod_line_settings *settings;
22 struct gpiod_line_config *line_cfg;
23 struct gpiod_chip *chip;
24 unsigned int i;
25 int ret;
26
27 chip = gpiod_chip_open(chip_path);
28 if (!chip)
29 return NULL;
30
31 settings = gpiod_line_settings_new();
32 if (!settings)
33 goto close_chip;
34
35 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
36
37 line_cfg = gpiod_line_config_new();
38 if (!line_cfg)
39 goto free_settings;
40
41 for (i = 0; i < num_lines; i++) {
42 ret = gpiod_line_config_add_line_settings(line_cfg, &offsets[i],
43 1, settings);
44 if (ret)
45 goto free_line_config;
46 }
47
48 if (consumer) {
49 req_cfg = gpiod_request_config_new();
50 if (!req_cfg)
51 goto free_line_config;
52
53 gpiod_request_config_set_consumer(req_cfg, consumer);
54 }
55
56 request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
57
58 gpiod_request_config_free(req_cfg);
59
60 free_line_config:
61 gpiod_line_config_free(line_cfg);
62
63 free_settings:
64 gpiod_line_settings_free(settings);
65
66 close_chip:
67 gpiod_chip_close(chip);
68
69 return request;
70 }
71
72 static int print_values(const unsigned int *offsets, unsigned int num_lines,
73 enum gpiod_line_value *values)
74 {
75 unsigned int i;
76
77 for (i = 0; i < num_lines; i++) {
78 if (values[i] == GPIOD_LINE_VALUE_ACTIVE)
79 printf("%d=Active ", offsets[i]);
80 else if (values[i] == GPIOD_LINE_VALUE_INACTIVE) {
81 printf("%d=Inactive ", offsets[i]);
82 } else {
83 fprintf(stderr, "error reading value: %s\n",
84 strerror(errno));
85 return EXIT_FAILURE;
86 }
87 }
88
89 printf("\n");
90
91 return EXIT_SUCCESS;
92 }
93
94 int main(void)
95 {
96 /* Example configuration - customize to suit your situation. */
97 static const char *const chip_path = "/dev/gpiochip0";
98 static const unsigned int line_offsets[NUM_LINES] = { 5, 3, 7 };
99
100 enum gpiod_line_value values[NUM_LINES];
101 struct gpiod_line_request *request;
102 int ret;
103
104 request = request_input_lines(chip_path, line_offsets, NUM_LINES,
105 "get-multiple-line-values");
106 if (!request) {
107 fprintf(stderr, "failed to request lines: %s\n",
108 strerror(errno));
109 return EXIT_FAILURE;
110 }
111
112 ret = gpiod_line_request_get_values(request, values);
113 if (ret == -1) {
114 fprintf(stderr, "failed to get values: %s\n", strerror(errno));
115 return EXIT_FAILURE;
116 }
117
118 return print_values(line_offsets, NUM_LINES, values);
119 }