Debian Codegraph @

examples/reconfigure_input_to_output.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]>
3
4 /*
5 * Example of a bi-directional line requested as input and then switched
6 * to output.
7 */
8
9 #include <errno.h>
10 #include <gpiod.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 /* Request a line as input. */
16 static struct gpiod_line_request *request_input_line(const char *chip_path,
17 unsigned int offset,
18 const char *consumer)
19 {
20 struct gpiod_request_config *req_cfg = NULL;
21 struct gpiod_line_request *request = NULL;
22 struct gpiod_line_settings *settings;
23 struct gpiod_line_config *line_cfg;
24 struct gpiod_chip *chip;
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 ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
42 settings);
43 if (ret)
44 goto free_line_config;
45
46 if (consumer) {
47 req_cfg = gpiod_request_config_new();
48 if (!req_cfg)
49 goto free_line_config;
50
51 gpiod_request_config_set_consumer(req_cfg, consumer);
52 }
53
54 request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
55
56 gpiod_request_config_free(req_cfg);
57
58 free_line_config:
59 gpiod_line_config_free(line_cfg);
60
61 free_settings:
62 gpiod_line_settings_free(settings);
63
64 close_chip:
65 gpiod_chip_close(chip);
66
67 return request;
68 }
69
70 static int reconfigure_as_output_line(struct gpiod_line_request *request,
71 unsigned int offset,
72 enum gpiod_line_value value)
73 {
74 struct gpiod_line_settings *settings;
75 struct gpiod_line_config *line_cfg;
76 int ret = -1;
77
78 settings = gpiod_line_settings_new();
79 if (!settings)
80 return -1;
81
82 gpiod_line_settings_set_direction(settings,
83 GPIOD_LINE_DIRECTION_OUTPUT);
84 gpiod_line_settings_set_output_value(settings, value);
85
86 line_cfg = gpiod_line_config_new();
87 if (!line_cfg)
88 goto free_settings;
89
90 ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
91 settings);
92 if (ret)
93 goto free_line_config;
94
95 ret = gpiod_line_request_reconfigure_lines(request, line_cfg);
96
97 free_line_config:
98 gpiod_line_config_free(line_cfg);
99
100 free_settings:
101 gpiod_line_settings_free(settings);
102
103 return ret;
104 }
105
106 static const char * value_str(enum gpiod_line_value value)
107 {
108 if (value == GPIOD_LINE_VALUE_ACTIVE)
109 return "Active";
110 else if (value == GPIOD_LINE_VALUE_INACTIVE) {
111 return "Inactive";
112 } else {
113 return "Unknown";
114 }
115 }
116
117 int main(void)
118 {
119 /* Example configuration - customize to suit your situation. */
120 static const char *const chip_path = "/dev/gpiochip0";
121 static const unsigned int line_offset = 5;
122
123 struct gpiod_line_request *request;
124 enum gpiod_line_value value;
125 int ret;
126
127 /* request the line initially as an input */
128 request = request_input_line(chip_path, line_offset,
129 "reconfigure-input-to-output");
130 if (!request) {
131 fprintf(stderr, "failed to request line: %s\n",
132 strerror(errno));
133 return EXIT_FAILURE;
134 }
135
136 /* read the current line value */
137 value = gpiod_line_request_get_value(request, line_offset);
138 printf("%d=%s (input)\n", line_offset, value_str(value));
139
140 /* switch the line to an output and drive it low */
141 ret = reconfigure_as_output_line(request, line_offset,
142 GPIOD_LINE_VALUE_INACTIVE);
143
144 /* report the current driven value */
145 value = gpiod_line_request_get_value(request, line_offset);
146 printf("%d=%s (output)\n", line_offset, value_str(value));
147
148 /* not strictly required here, but if the app wasn't exiting... */
149 gpiod_line_request_release(request);
150
151 return ret;
152 }