| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <errno.h> |
| 7 |
#include <gpiod.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <unistd.h> |
| 12 |
|
| 13 |
#define NUM_LINES 3 |
| 14 |
|
| 15 |
static struct gpiod_line_request * |
| 16 |
request_output_lines(const char *chip_path, const unsigned int *offsets, |
| 17 |
enum gpiod_line_value *values, unsigned int num_lines, |
| 18 |
const char *consumer) |
| 19 |
{ |
| 20 |
struct gpiod_request_config *rconfig = NULL; |
| 21 |
struct gpiod_line_request *request = NULL; |
| 22 |
struct gpiod_line_settings *settings; |
| 23 |
struct gpiod_line_config *lconfig; |
| 24 |
struct gpiod_chip *chip; |
| 25 |
unsigned int i; |
| 26 |
int ret; |
| 27 |
|
| 28 |
chip = gpiod_chip_open(chip_path); |
| 29 |
if (!chip) |
| 30 |
return NULL; |
| 31 |
|
| 32 |
settings = gpiod_line_settings_new(); |
| 33 |
if (!settings) |
| 34 |
goto close_chip; |
| 35 |
|
| 36 |
gpiod_line_settings_set_direction(settings, |
| 37 |
GPIOD_LINE_DIRECTION_OUTPUT); |
| 38 |
|
| 39 |
lconfig = gpiod_line_config_new(); |
| 40 |
if (!lconfig) |
| 41 |
goto free_settings; |
| 42 |
|
| 43 |
for (i = 0; i < num_lines; i++) { |
| 44 |
ret = gpiod_line_config_add_line_settings(lconfig, &offsets[i], |
| 45 |
1, settings); |
| 46 |
if (ret) |
| 47 |
goto free_line_config; |
| 48 |
} |
| 49 |
gpiod_line_config_set_output_values(lconfig, values, num_lines); |
| 50 |
|
| 51 |
if (consumer) { |
| 52 |
rconfig = gpiod_request_config_new(); |
| 53 |
if (!rconfig) |
| 54 |
goto free_line_config; |
| 55 |
|
| 56 |
gpiod_request_config_set_consumer(rconfig, consumer); |
| 57 |
} |
| 58 |
|
| 59 |
request = gpiod_chip_request_lines(chip, rconfig, lconfig); |
| 60 |
|
| 61 |
gpiod_request_config_free(rconfig); |
| 62 |
|
| 63 |
free_line_config: |
| 64 |
gpiod_line_config_free(lconfig); |
| 65 |
|
| 66 |
free_settings: |
| 67 |
gpiod_line_settings_free(settings); |
| 68 |
|
| 69 |
close_chip: |
| 70 |
gpiod_chip_close(chip); |
| 71 |
|
| 72 |
return request; |
| 73 |
} |
| 74 |
|
| 75 |
static enum gpiod_line_value toggle_line_value(enum gpiod_line_value value) |
| 76 |
{ |
| 77 |
return (value == GPIOD_LINE_VALUE_ACTIVE) ? GPIOD_LINE_VALUE_INACTIVE : |
| 78 |
GPIOD_LINE_VALUE_ACTIVE; |
| 79 |
} |
| 80 |
|
| 81 |
static void toggle_line_values(enum gpiod_line_value *values, |
| 82 |
unsigned int num_lines) |
| 83 |
{ |
| 84 |
unsigned int i; |
| 85 |
|
| 86 |
for (i = 0; i < num_lines; i++) |
| 87 |
values[i] = toggle_line_value(values[i]); |
| 88 |
} |
| 89 |
|
| 90 |
static void print_values(const unsigned int *offsets, |
| 91 |
const enum gpiod_line_value *values, |
| 92 |
unsigned int num_lines) |
| 93 |
{ |
| 94 |
unsigned int i; |
| 95 |
|
| 96 |
for (i = 0; i < num_lines; i++) { |
| 97 |
if (values[i] == GPIOD_LINE_VALUE_ACTIVE) |
| 98 |
printf("%d=Active ", offsets[i]); |
| 99 |
else |
| 100 |
printf("%d=Inactive ", offsets[i]); |
| 101 |
} |
| 102 |
|
| 103 |
printf("\n"); |
| 104 |
} |
| 105 |
|
| 106 |
int main(void) |
| 107 |
{ |
| 108 |
|
| 109 |
static const char *const chip_path = "/dev/gpiochip0"; |
| 110 |
static const unsigned int line_offsets[NUM_LINES] = { 5, 3, 7 }; |
| 111 |
|
| 112 |
enum gpiod_line_value values[NUM_LINES] = { GPIOD_LINE_VALUE_ACTIVE, |
| 113 |
GPIOD_LINE_VALUE_ACTIVE, |
| 114 |
GPIOD_LINE_VALUE_INACTIVE }; |
| 115 |
struct gpiod_line_request *request; |
| 116 |
|
| 117 |
request = request_output_lines(chip_path, line_offsets, values, |
| 118 |
NUM_LINES, |
| 119 |
"toggle-multiple-line-values"); |
| 120 |
if (!request) { |
| 121 |
fprintf(stderr, "failed to request line: %s\n", |
| 122 |
strerror(errno)); |
| 123 |
return EXIT_FAILURE; |
| 124 |
} |
| 125 |
|
| 126 |
for (;;) { |
| 127 |
print_values(line_offsets, values, NUM_LINES); |
| 128 |
sleep(1); |
| 129 |
toggle_line_values(values, NUM_LINES); |
| 130 |
gpiod_line_request_set_values(request, values); |
| 131 |
} |
| 132 |
|
| 133 |
gpiod_line_request_release(request); |
| 134 |
|
| 135 |
return EXIT_SUCCESS; |
| 136 |
} |