pin_control: add probe_pin()

This commit is contained in:
Alejandro Soto 2023-09-12 23:37:22 -06:00
parent 4104ba296f
commit 5b7ec4c2e8
No known key found for this signature in database
GPG key ID: 570448E3382BDEC5
2 changed files with 31 additions and 0 deletions

View file

@ -9,6 +9,8 @@ int turn_on_pin(int pin);
int turn_off_pin(int pin);
int probe_pin(int pin);
int turn_on_all_pins();
int turn_off_all_pins();

View file

@ -73,6 +73,35 @@ int turn_off_pin(int pin) {
return set_pin_state(pin, 0);
}
int probe_pin(int pin) {
int ret = -1;
if (!chip) {
perror("[pin_control] attempt to read GPIO line for invalid chip");
goto exit;
}
struct gpiod_line *line = gpiod_chip_get_line(chip, pin);
if (!line) {
perror("[pin_control] gpiod_chip_get_line()");
goto exit;
} else if (gpiod_line_request_input(line, "house-leds") < 0) {
perror("[pin_control] gpiod_line_request_input()");
goto exit_line;
}
ret = gpiod_line_get_value(line);
if (ret < 0) {
perror("[pin_control] gpiod_line_get_value()");
goto exit_line;
}
exit_line:
// https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_line_release.html
gpiod_line_release(line);
exit:
return ret;
}
int turn_on_all_pins() {
for (int pin = 0; pin <= 5; pin++)
if (turn_on_pin(pin) < 0)