6 #include <linux/input.h>
7 #include <linux/input-event-codes.h>
10 #include "signal_exit.h"
11 #include "json_helpers.h"
13 static void gpio_cb(EV_P_ ev_io *w_, int revents)
15 static char *type = "gpio";
17 ev_io_gpio *w = (ev_io_gpio *)w_;
18 struct gpiod_line_event e;
22 if (gpiod_line_event_read_fd(w->w.fd, &e) == -1) {
23 perror("gpiod_line_event_read_fd");
34 int mt_gpio_init(mt_gpio_t *self, struct ev_loop *loop, int fd)
36 struct gpiod_chip *chip;
37 struct gpiod_line *line;
43 for (i = 0; i < GPIO_COUNT; i++) {
44 chip = gpiod_chip_open_by_number(pins[i].chip);
46 perror("gpiod_chip_open");
49 line = gpiod_chip_get_line(chip, pins[i].offset);
51 gpiod_chip_close(chip);
52 perror("gpiod_chip_get_line");
55 if (gpiod_line_event_request_rising(line, GPIO_CONSUMER, GPIO_ACTIVE_LOW) == -1) {
56 gpiod_chip_close(chip);
57 perror("gpiod_line_event_request_rising");
60 lfd = gpiod_line_event_get_fd(line);
62 perror("gpiod_line_event_get_fd");
63 gpiod_chip_close(chip);
71 ev_io_init(&(w->w), gpio_cb, lfd, EV_READ);
72 ev_io_start(loop, (ev_io *)w);
76 for (size_t j = 0; j < i; j++) {
77 gpiod_chip_close(self->chip[j]);
85 void mt_gpio_deinit(mt_gpio_t *self)
87 for (size_t i = 0; i < GPIO_COUNT; i++) {
88 gpiod_chip_close(self->chip[i]);
93 int main(int argc, char **argv)
95 struct ev_loop *loop = EV_DEFAULT;
98 set_signal_exit(loop);
100 if (mt_gpio_init(&gpio, loop, STDOUT_FILENO) != 0) {
106 mt_gpio_deinit(&gpio);
107 ev_loop_destroy(loop);