| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <glib.h> |
| 7 |
#include <gpiod-glib.h> |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
typedef struct { |
| 11 |
GpiodglibLineRequest *request; |
| 12 |
guint line_offset; |
| 13 |
GpiodglibLineValue value; |
| 14 |
} ToggleData; |
| 15 |
|
| 16 |
static gboolean toggle_line(gpointer user_data) |
| 17 |
{ |
| 18 |
ToggleData *data = user_data; |
| 19 |
g_autoptr(GError) err = NULL; |
| 20 |
gboolean ret; |
| 21 |
|
| 22 |
data->value = data->value == GPIODGLIB_LINE_VALUE_ACTIVE ? |
| 23 |
GPIODGLIB_LINE_VALUE_INACTIVE : |
| 24 |
GPIODGLIB_LINE_VALUE_ACTIVE; |
| 25 |
|
| 26 |
ret = gpiodglib_line_request_set_value(data->request, data->line_offset, |
| 27 |
data->value, &err); |
| 28 |
if (!ret) { |
| 29 |
g_printerr("failed to set line value: %s\n", err->message); |
| 30 |
exit(EXIT_FAILURE); |
| 31 |
} |
| 32 |
|
| 33 |
g_print("%u=%s\n", |
| 34 |
data->line_offset, |
| 35 |
data->value == GPIODGLIB_LINE_VALUE_ACTIVE ? |
| 36 |
"active" : "inactive"); |
| 37 |
|
| 38 |
return G_SOURCE_CONTINUE; |
| 39 |
} |
| 40 |
|
| 41 |
int main(void) |
| 42 |
{ |
| 43 |
|
| 44 |
static const gchar *const chip_path = "/dev/gpiochip1"; |
| 45 |
static const guint line_offset = 5; |
| 46 |
|
| 47 |
g_autoptr(GpiodglibRequestConfig) req_cfg = NULL; |
| 48 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 49 |
g_autoptr(GpiodglibLineRequest) request = NULL; |
| 50 |
g_autoptr(GpiodglibLineConfig) line_cfg = NULL; |
| 51 |
g_autoptr(GpiodglibChip) chip = NULL; |
| 52 |
g_autoptr(GArray) offsets = NULL; |
| 53 |
g_autoptr(GMainLoop) loop = NULL; |
| 54 |
g_autoptr(GError) err = NULL; |
| 55 |
ToggleData data; |
| 56 |
gboolean ret; |
| 57 |
|
| 58 |
offsets = g_array_new(FALSE, TRUE, sizeof(guint)); |
| 59 |
g_array_append_val(offsets, line_offset); |
| 60 |
|
| 61 |
chip = gpiodglib_chip_new(chip_path, &err); |
| 62 |
if (!chip) { |
| 63 |
g_printerr("unable to open %s: %s\n", chip_path, err->message); |
| 64 |
return EXIT_FAILURE; |
| 65 |
} |
| 66 |
|
| 67 |
settings = gpiodglib_line_settings_new("direction", |
| 68 |
GPIODGLIB_LINE_DIRECTION_OUTPUT, |
| 69 |
NULL); |
| 70 |
line_cfg = gpiodglib_line_config_new(); |
| 71 |
ret = gpiodglib_line_config_add_line_settings(line_cfg, offsets, |
| 72 |
settings, &err); |
| 73 |
if (!ret) { |
| 74 |
g_printerr("failed to add line settings to line config: %s\n", |
| 75 |
err->message); |
| 76 |
return EXIT_FAILURE; |
| 77 |
} |
| 78 |
|
| 79 |
req_cfg = gpiodglib_request_config_new("consumer", "toggle-line-value", |
| 80 |
NULL); |
| 81 |
|
| 82 |
request = gpiodglib_chip_request_lines(chip, req_cfg, line_cfg, &err); |
| 83 |
if (!request) { |
| 84 |
g_printerr("failed to request lines: %s\n", err->message); |
| 85 |
return EXIT_FAILURE; |
| 86 |
} |
| 87 |
|
| 88 |
data.request = request; |
| 89 |
data.line_offset = line_offset; |
| 90 |
data.value = GPIODGLIB_LINE_VALUE_INACTIVE; |
| 91 |
|
| 92 |
loop = g_main_loop_new(NULL, FALSE); |
| 93 |
|
| 94 |
g_timeout_add_seconds(1, toggle_line, &data); |
| 95 |
|
| 96 |
g_main_loop_run(loop); |
| 97 |
|
| 98 |
return EXIT_SUCCESS; |
| 99 |
} |