diff --git a/pin_control/include/pin_control.h b/pin_control/include/pin_control.h index cbfce67..13467e4 100644 --- a/pin_control/include/pin_control.h +++ b/pin_control/include/pin_control.h @@ -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(); diff --git a/pin_control/src/pin_control.c b/pin_control/src/pin_control.c index 80d1ebd..0ad62b0 100644 --- a/pin_control/src/pin_control.c +++ b/pin_control/src/pin_control.c @@ -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)