From 404097a13d113282362c8991fe3e7001a19943e3 Mon Sep 17 00:00:00 2001 From: Fabian Montero Date: Mon, 11 Sep 2023 03:33:57 -0600 Subject: [PATCH 1/2] fixes wait_for_button_press function, WIP --- pin_control/pin_control.c | 56 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/pin_control/pin_control.c b/pin_control/pin_control.c index acf604d..b5d5cd9 100644 --- a/pin_control/pin_control.c +++ b/pin_control/pin_control.c @@ -1,6 +1,8 @@ #include #include +// 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 + ) } From 010bb391693f864422804225eeb96489cc9a7cd0 Mon Sep 17 00:00:00 2001 From: Fabian Montero Date: Mon, 11 Sep 2023 23:25:22 -0600 Subject: [PATCH 2/2] add nix derivation for compiling with CMAKE --- pin_control/CMakeLists.txt | 16 ++++++++++++++++ pin_control/default.nix | 10 ++++++++++ pin_control/{ => include}/pin_control.h | 0 pin_control/readme.md | 6 +++++- pin_control/{ => src}/pin_control.c | 18 ++++++++++-------- 5 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 pin_control/CMakeLists.txt create mode 100644 pin_control/default.nix rename pin_control/{ => include}/pin_control.h (100%) rename pin_control/{ => src}/pin_control.c (90%) diff --git a/pin_control/CMakeLists.txt b/pin_control/CMakeLists.txt new file mode 100644 index 0000000..93a5805 --- /dev/null +++ b/pin_control/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.20) + +project(PinControl) +find_package(PkgConfig REQUIRED) +pkg_check_modules(LIBGPIOD REQUIRED libgpiod) + +add_library(pin_control SHARED src/pin_control.c) + +target_include_directories(pin_control PUBLIC include/ PRIVATE ${LIBGPIOD_INCLUDE_DIRS}) +target_link_libraries(pin_control PUBLIC ${LIBGPIOD_LIBRARIES}) +set_target_properties(pin_control PROPERTIES POSITION_INDEPENDENT_CODE ON) + +target_compile_options(pin_control PUBLIC -Wall -Wextra -Werror) + +install(TARGETS pin_control LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) +install(FILES include/pin_control.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) \ No newline at end of file diff --git a/pin_control/default.nix b/pin_control/default.nix new file mode 100644 index 0000000..8c845ff --- /dev/null +++ b/pin_control/default.nix @@ -0,0 +1,10 @@ +{ stdenv, libgpiod, cmake, pkg-config }: +stdenv.mkDerivation { + pname = "pin_control"; + version = "0.0.1"; + src = ./.; + buildInputs = [ libgpiod ]; + nativeBuildInputs = [ cmake pkg-config ]; + + outputs = [ "out" "dev" ]; +} diff --git a/pin_control/pin_control.h b/pin_control/include/pin_control.h similarity index 100% rename from pin_control/pin_control.h rename to pin_control/include/pin_control.h diff --git a/pin_control/readme.md b/pin_control/readme.md index 5f8c1ca..c8d4cba 100644 --- a/pin_control/readme.md +++ b/pin_control/readme.md @@ -1 +1,5 @@ -gcc -o pin_contro_test main.c pin_control.c -lgpiod \ No newline at end of file +Compilation command: + +``` + +``` \ No newline at end of file diff --git a/pin_control/pin_control.c b/pin_control/src/pin_control.c similarity index 90% rename from pin_control/pin_control.c rename to pin_control/src/pin_control.c index b5d5cd9..aa2b386 100644 --- a/pin_control/pin_control.c +++ b/pin_control/src/pin_control.c @@ -74,6 +74,8 @@ int turn_off_all_pins() { } int button_presssed(int event, unsigned int pin, const struct timespec * timestamp, void *data) { + (void) event; + (void) timestamp; * (int *) data = (int) pin; return GPIOD_CTXLESS_EVENT_POLL_RET_STOP; } @@ -83,20 +85,20 @@ int wait_for_button_press(unsigned int pins[]) { int pressed_pin = -1; - struct timespec { - time_t 3600; /* Seconds */ - long 0; /* Nanoseconds */ - } ts; - - gpiod_ctxless_event_monitor_multiple(chip, + struct timespec ts = { + .tv_sec = 3600, + .tv_nsec = 0, + }; + + return gpiod_ctxless_event_monitor_multiple(GPIO_CHIP, GPIOD_CTXLESS_EVENT_FALLING_EDGE, pins, 5, true, "house-leds", - ts, // big timout + &ts, // big timout NULL, button_presssed, &pressed_pin - ) + ); }