From ef16376aba1e224db5cad706257d93ba9d12ced2 Mon Sep 17 00:00:00 2001 From: Fabian Montero Date: Mon, 11 Sep 2023 02:05:35 -0600 Subject: [PATCH] add pin control library --- pin_control/main.c | 13 +++++ pin_control/pin_control.c | 102 ++++++++++++++++++++++++++++++++++++++ pin_control/pin_control.h | 22 ++++++++ pin_control/readme.md | 1 + 4 files changed, 138 insertions(+) create mode 100644 pin_control/main.c create mode 100644 pin_control/pin_control.c create mode 100644 pin_control/pin_control.h create mode 100644 pin_control/readme.md diff --git a/pin_control/main.c b/pin_control/main.c new file mode 100644 index 0000000..2b70c82 --- /dev/null +++ b/pin_control/main.c @@ -0,0 +1,13 @@ +#include "pin_control.h" + +int main() { + int button_pin = 5; + int button_response; + + + button_response = wait_for_button_press(button_pin); + printf("Button press detected on pin: %d\n", button_response); + + cleanup_gpio(); + return 0; +} diff --git a/pin_control/pin_control.c b/pin_control/pin_control.c new file mode 100644 index 0000000..acf604d --- /dev/null +++ b/pin_control/pin_control.c @@ -0,0 +1,102 @@ +#include +#include + +#define GPIO_CHIP "gpiochip0" + +struct gpiod_chip *chip; + +int init_gpio() { + // https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_chip_open_by_name.html + chip = gpiod_chip_open_by_name(GPIO_CHIP); + if (!chip) { + printf("Error opening GPIO chip"); + return -1; + } + return 0; +} + +void cleanup_gpio() { + // https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_chip_close.html + gpiod_chip_close(chip); +} + +int set_pin_state(int pin, int state) { + // https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_chip_get_line.html + struct gpiod_line *line = gpiod_chip_get_line(chip, pin); // this may need an offset + if (!line) { + printf("Error getting GPIO line"); + return -1; + } + + // https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_line_request_output.html + if (gpiod_line_request_output(line, "house-leds", state) < 0) { + printf("Error setting GPIO line direction"); + gpiod_line_release(line); + return -1; + } + + // https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_line_set_value.html + if (gpiod_line_set_value(line, state) < 0) { + printf("Error setting GPIO line value"); + gpiod_line_release(line); + return -1; + } + + // https://libgpiod-dlang.dpldocs.info/gpiod.gpiod_line_release.html + gpiod_line_release(line); + 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); +} + +int turn_off_pin(int pin) { + return set_pin_state(pin, 0); +} + +int turn_on_all_pins() { + for (int pin = 0; pin <= 5; pin++) { + if (turn_on_pin(pin) < 0) + return -1; + } + return 0; +} + +int turn_off_all_pins() { + for (int pin = 0; pin <= 5; pin++) { + if (turn_off_pin(pin) < 0) + return -1; + } + 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; + } + } +} diff --git a/pin_control/pin_control.h b/pin_control/pin_control.h new file mode 100644 index 0000000..bcf2dc7 --- /dev/null +++ b/pin_control/pin_control.h @@ -0,0 +1,22 @@ +#ifndef GPIO_CONTROL_H +#define GPIO_CONTROL_H + +int init_gpio(); + +void cleanup_gpio(); + +int set_pin_state(int pin, int state); + +int read_button(int pin); + +int turn_on_pin(int pin); + +int turn_off_pin(int pin); + +int turn_on_all_pins(); + +int turn_off_all_pins(); + +int wait_for_button_press(int button_pin); + +#endif /* GPIO_CONTROL_H */ diff --git a/pin_control/readme.md b/pin_control/readme.md new file mode 100644 index 0000000..5f8c1ca --- /dev/null +++ b/pin_control/readme.md @@ -0,0 +1 @@ +gcc -o pin_contro_test main.c pin_control.c -lgpiod \ No newline at end of file