fixes wait_for_button_press function, WIP
This commit is contained in:
parent
ef16376aba
commit
404097a13d
|
@ -1,6 +1,8 @@
|
|||
#include <stdio.h>
|
||||
#include <gpiod.h>
|
||||
|
||||
// https://docs.kernel.org/driver-api/gpio/using-gpio.html
|
||||
|
||||
#define GPIO_CHIP "gpiochip0"
|
||||
|
||||
struct gpiod_chip *chip;
|
||||
|
@ -47,27 +49,6 @@ int set_pin_state(int pin, int state) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int read_button(int pin) {
|
||||
// https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_chip_get_line.html
|
||||
struct gpiod_line *line = gpiod_chip_get_line(chip, pin + PIN_OFFSET);
|
||||
if (!line) {
|
||||
printf("Error getting GPIO line");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_line_request_input.html
|
||||
if (gpiod_line_request_input(line, "house-leds") < 0) {
|
||||
printf("Error setting GPIO line direction to input");
|
||||
gpiod_line_release(line);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_line_get_value.html
|
||||
int value = gpiod_line_get_value(line);
|
||||
gpiod_line_release(line);
|
||||
return value; // either 0 or 1
|
||||
}
|
||||
|
||||
int turn_on_pin(int pin) {
|
||||
return set_pin_state(pin, 1);
|
||||
}
|
||||
|
@ -92,11 +73,30 @@ int turn_off_all_pins() {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int wait_for_button_press(int button_pin) {
|
||||
while (1) {
|
||||
int button_state = read_button(button_pin); // maybe make it so that this works with a range of pins instead of just one
|
||||
if (button_state == 1) {
|
||||
return button_pin;
|
||||
}
|
||||
}
|
||||
int button_presssed(int event, unsigned int pin, const struct timespec * timestamp, void *data) {
|
||||
* (int *) data = (int) pin;
|
||||
return GPIOD_CTXLESS_EVENT_POLL_RET_STOP;
|
||||
}
|
||||
|
||||
int wait_for_button_press(unsigned int pins[]) {
|
||||
// https://www.lane-fu.com/linuxmirror/libgpiod/doc/html/group______high__level____.html#ga3ac28eb59bbd31b8b2298f76047d377d
|
||||
|
||||
int pressed_pin = -1;
|
||||
|
||||
struct timespec {
|
||||
time_t 3600; /* Seconds */
|
||||
long 0; /* Nanoseconds */
|
||||
} ts;
|
||||
|
||||
gpiod_ctxless_event_monitor_multiple(chip,
|
||||
GPIOD_CTXLESS_EVENT_FALLING_EDGE,
|
||||
pins,
|
||||
5,
|
||||
true,
|
||||
"house-leds",
|
||||
ts, // big timout
|
||||
NULL,
|
||||
button_presssed,
|
||||
&pressed_pin
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue