diff --git a/lib/Peripherals/Peripherals.cpp b/lib/Peripherals/Peripherals.cpp index aad133c..beb0fb5 100644 --- a/lib/Peripherals/Peripherals.cpp +++ b/lib/Peripherals/Peripherals.cpp @@ -71,13 +71,15 @@ void Peripherals::init_navigation_btn(int pin, void callback(), int input_mode, attachInterrupt(pin, callback, isr_mode); } -void Peripherals::common_init_navigation(size_t up, size_t down, size_t left, - size_t right, size_t ok) { -#if BTN_NAVIGATION - init_navigation_btn(up, handle_up_button); - init_navigation_btn(down, handle_down_button); - init_navigation_btn(left, handle_left_button); - init_navigation_btn(right, handle_right_button); - init_navigation_btn(ok, handle_ok_button); +void Peripherals::common_init_navigation(EasyButton *up, EasyButton *down, EasyButton *left, EasyButton *right) { +#ifdef BTN_NAVIGATION + up->begin(); + up->onPressed(handle_up_button); + down->begin(); + down->onPressed(handle_down_button); + left->begin(); + left->onPressed(handle_left_button); + right->begin(); + right->onPressed(handle_right_button); #endif } \ No newline at end of file diff --git a/lib/Peripherals/Peripherals.hpp b/lib/Peripherals/Peripherals.hpp index 080ddb8..dfefe6a 100644 --- a/lib/Peripherals/Peripherals.hpp +++ b/lib/Peripherals/Peripherals.hpp @@ -31,22 +31,29 @@ #ifndef PERIPHERALS_H #define PERIPHERALS_H #include +#include +#include "navigation/buttons/btn_routines.hpp" class Peripherals { protected: bool common_init_sd(size_t sck, size_t miso, size_t mosi, size_t cs); - void common_init_navigation(size_t up, size_t down, size_t left, size_t right, - size_t ok); + void common_init_navigation(EasyButton *up, EasyButton *down, EasyButton *left, EasyButton *right); void init_navigation_btn(int pin, void callback(), int input_mode = INPUT_PULLUP, int isr_mode = FALLING); - + EasyButton ok_btn = EasyButton(OK_BTN_PIN); + void init_ok_btn() { + ok_btn.begin(); + ok_btn.onPressed(handle_ok_button); + ok_btn.onPressedFor(5000, []() {ESP.restart();}); + } public: Peripherals() {}; ~Peripherals() {}; virtual void init_i2c_bus() = 0; virtual void init_sd() = 0; virtual void init_navigation() = 0; + virtual void loop_code() = 0; }; #endif \ No newline at end of file diff --git a/lib/Peripherals/boards/arduino_nano_esp32/Peripherals_Arduino_Nano_ESP32.hpp b/lib/Peripherals/boards/arduino_nano_esp32/Peripherals_Arduino_Nano_ESP32.hpp index 3b7bd55..1aab5d6 100644 --- a/lib/Peripherals/boards/arduino_nano_esp32/Peripherals_Arduino_Nano_ESP32.hpp +++ b/lib/Peripherals/boards/arduino_nano_esp32/Peripherals_Arduino_Nano_ESP32.hpp @@ -62,7 +62,10 @@ class Peripherals_Arduino_Nano_ESP32 : public Peripherals { } return status; }; - + EasyButton up_btn = EasyButton(UP_BTN_PIN); + EasyButton down_btn = EasyButton(DOWN_BTN_PIN); + EasyButton left_btn = EasyButton(LEFT_BTN_PIN); + EasyButton right_btn = EasyButton(RIGHT_BTN_PIN); public: Peripherals_Arduino_Nano_ESP32(/* args */) {}; ~Peripherals_Arduino_Nano_ESP32() {}; @@ -78,9 +81,16 @@ class Peripherals_Arduino_Nano_ESP32 : public Peripherals { } }; void init_navigation() { - common_init_navigation(UP_BTN_PIN, DOWN_BTN_PIN, LEFT_BTN_PIN, - RIGHT_BTN_PIN, OK_BTN_PIN); + common_init_navigation(&up_btn, &down_btn, &left_btn, + &right_btn); + init_ok_btn(); }; + void loop_code() { + up_btn.read(); + down_btn.read(); + left_btn.read(); + right_btn.read(); + } }; #endif #endif \ No newline at end of file diff --git a/lib/Peripherals/boards/esp32_s3_devkitc/Peripherals_ESP32S3_DevKitC.hpp b/lib/Peripherals/boards/esp32_s3_devkitc/Peripherals_ESP32S3_DevKitC.hpp index 401cf30..c292393 100644 --- a/lib/Peripherals/boards/esp32_s3_devkitc/Peripherals_ESP32S3_DevKitC.hpp +++ b/lib/Peripherals/boards/esp32_s3_devkitc/Peripherals_ESP32S3_DevKitC.hpp @@ -22,7 +22,12 @@ #include "../../Peripherals.hpp" class Peripherals_ESP32S3_DevKitC : public Peripherals { - public: +private: + EasyButton up_btn = EasyButton(UP_BTN_PIN); + EasyButton down_btn = EasyButton(DOWN_BTN_PIN); + EasyButton left_btn = EasyButton(LEFT_BTN_PIN); + EasyButton right_btn = EasyButton(RIGHT_BTN_PIN); +public: Peripherals_ESP32S3_DevKitC(/* args */) {}; ~Peripherals_ESP32S3_DevKitC() {}; void init_i2c_bus() {}; @@ -30,9 +35,16 @@ class Peripherals_ESP32S3_DevKitC : public Peripherals { common_init_sd(SD_CARD_SCK, SD_CARD_MISO, SD_CARD_MOSI, SD_CARD_CS); }; void init_navigation() { - common_init_navigation(UP_BTN_PIN, DOWN_BTN_PIN, LEFT_BTN_PIN, - RIGHT_BTN_PIN, OK_BTN_PIN); + common_init_navigation(&up_btn, &down_btn, &left_btn, + &right_btn); + init_ok_btn(); }; + void loop_code() { + up_btn.read(); + down_btn.read(); + left_btn.read(); + right_btn.read(); + } }; #endif diff --git a/lib/Peripherals/boards/lilygo_t_embed_cc1101/Peripherals_Lilygo_t_embed_cc1101.hpp b/lib/Peripherals/boards/lilygo_t_embed_cc1101/Peripherals_Lilygo_t_embed_cc1101.hpp index 1430fea..14b4f36 100644 --- a/lib/Peripherals/boards/lilygo_t_embed_cc1101/Peripherals_Lilygo_t_embed_cc1101.hpp +++ b/lib/Peripherals/boards/lilygo_t_embed_cc1101/Peripherals_Lilygo_t_embed_cc1101.hpp @@ -78,8 +78,12 @@ class Peripherals_Lilygo_t_embed_cc1101 : public Peripherals { }; void init_navigation() { init_rotary_encoder(); - init_navigation_btn(OK_BTN_PIN, handle_ok_button, INPUT, RISING); + init_ok_btn(); }; + void loop_code() { + handle_encoder(); + ok_btn.read(); + } }; #endif \ No newline at end of file diff --git a/lib/UI/navigation/buttons/btn_routines.cpp b/lib/UI/navigation/buttons/btn_routines.cpp index ab65932..200b18e 100644 --- a/lib/UI/navigation/buttons/btn_routines.cpp +++ b/lib/UI/navigation/buttons/btn_routines.cpp @@ -24,13 +24,13 @@ static Buttons btn_pressed = NULL_BTN; #ifdef BTN_NAVIGATION /* ISR routines */ -void IRAM_ATTR handle_up_button() { btn_pressed = UP_BTN; } +void handle_up_button() { btn_pressed = UP_BTN; } -void IRAM_ATTR handle_down_button() { btn_pressed = DOWN_BTN; } +void handle_down_button() { btn_pressed = DOWN_BTN; } -void IRAM_ATTR handle_left_button() { btn_pressed = LEFT_BTN; } +void handle_left_button() { btn_pressed = LEFT_BTN; } -void IRAM_ATTR handle_right_button() { btn_pressed = RIGHT_BTN; } +void handle_right_button() { btn_pressed = RIGHT_BTN; } #elif defined(ENCODER_NAVIGATION) #include @@ -60,7 +60,9 @@ void handle_encoder() { } #endif -void IRAM_ATTR handle_ok_button() { btn_pressed = OK_BTN; } +void IRAM_ATTR handle_ok_button() { + btn_pressed = OK_BTN; +} Buttons get_btn_pressed() { return btn_pressed; } diff --git a/platformio.ini b/platformio.ini index 78a1e60..ed48c3e 100755 --- a/platformio.ini +++ b/platformio.ini @@ -36,11 +36,11 @@ lib_deps_external = https://github.com/spacehuhn/ArduinoPcap.git bblanchon/ArduinoJson@^7.1.0 esphome/AsyncTCP-esphome@^2.0.0 + https://github.com/vdccc/EasyButton.git https://github.com/andreock/RadioLib.git build_flags = -DCONFIG_USE_LOGIN_CALLBACK=1 -DUSE_NIMBLE=true -DNO_SERIAL_PRINT_BLESNIFFER=true - -DCORE_DEBUG_LEVEL=0 monitor_speed = 115200 monitor_rts = 0 monitor_dtr = 0 diff --git a/src/main.cpp b/src/main.cpp index de2e943..6197180 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -90,10 +90,5 @@ void setup() { } void loop() { -#if defined(ENCODER_NAVIGATION) - handle_encoder(); -#else - LOG_INFO("Loop\n"); // Avoid FreeRTOS watchdog trigger - delay(1000); -#endif + peripherals.loop_code(); }