46#define GPIO_CHIP_COUNT 4
49#define GPIO_LINES_PER_CHIP 32
52#define GPIO_DEBOUNCE_DELAY_US 50
62static gpio_t *gpio_get_line(
int gpio_number) {
67 if (
gpio_chip[gpio_chip_number] == NULL) {
74 return gpiod_chip_get_line(
gpio_chip[gpio_chip_number], gpio_line_index);
80 gpio_chip[i] = gpiod_chip_open_by_number(i);
83 "Failed opening GPIO chip %d. %d message: %s\n", i, errno,
105 gpio_t *gpio_line = gpio_get_line(gpio_number);
106 if (gpio_line == NULL) {
108 "Failed to get GPIO line for GPIO %s. %d message: %s\n", name,
109 errno, strerror(errno));
116 ret = gpiod_line_request_output(gpio_line, name, 0);
118 ret = gpiod_line_request_input(gpio_line, name);
122 "Failed to configure for GPIO %s. %d message: %s\n", name, errno,
131 gpiod_line_release(gpio);
137 if (value < 0 || value > 1) {
143 return gpiod_line_set_value(gpio, value);
151 gpio_t *gpio_line = gpio_get_line(gpio_number);
152 if (gpio_line == NULL) {
154 "Failed to get GPIO line for GPIO %s. %d message: %s\n", name,
155 errno, strerror(errno));
165 ret = gpiod_line_request_rising_edge_events(gpio_line, name);
168 ret = gpiod_line_request_falling_edge_events(gpio_line, name);
171 ret = gpiod_line_request_both_edges_events(gpio_line, name);
179 "Failed to configure interrupt for GPIO %s. %d message: %s\n",
180 name, errno, strerror(errno));
189 struct gpiod_line_event event;
192 ret = gpiod_line_event_wait(gpio, timeout);
195 if (errno != EINTR) {
197 "Failed waiting for interrupt of GPIO %s. %d message: %s\n",
198 gpiod_line_consumer(gpio), errno, strerror(errno));
207 ret = gpiod_line_event_read(gpio, &event);
210 "Failed reading event for GPIO %s. %d message: %s\n",
211 gpiod_line_consumer(gpio), errno, strerror(errno));
216 if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE) {
219 if (event.event_type == GPIOD_LINE_EVENT_FALLING_EDGE) {
struct gpiod_chip * gpio_chip[GPIO_CHIP_COUNT]
#define GPIO_CHIP_COUNT
Number of GPIO chips.
#define GPIO_LINES_PER_CHIP
Number of GPIO lines per GPIO chip.
int gpio_set_value(gpio_t *gpio, int value)
#define GPIO_DEBOUNCE_DELAY_US
Minimal time a button needs to be pressed (in microseconds)
int gpio_get_value(gpio_t *gpio)
void gpio_release(gpio_t *gpio)
int gpio_wait_interrupt(gpio_t *gpio, const struct timespec *timeout)
gpio_t * gpio_setup_interrupt(int gpio_number, gpio_interrupt_t edge, const char *name)
gpio_t * gpio_setup(int gpio_number, gpio_mode_t mode, const char *name)
@ GPIO_INTERRUPT_FALLING
Interrupt on falling edge.
@ GPIO_INTERRUPT_BOTH
Interrupt on both edges.
@ GPIO_INTERRUPT_NONE
No interrupt.
@ GPIO_INTERRUPT_RISING
Interrupt on rising edge.
enum gpio_interrupt gpio_interrupt_t
@ GPIO_MODE_OUT
GPIO write mode.
@ GPIO_MODE_IN
GPIO read mode.
enum gpio_mode gpio_mode_t
int rl_log(rl_log_level_t log_level, char const *const format,...)
#define ERROR
Function return value for errors (use errno to indicate the error)
#define SUCCESS
Function return value for successful completion.