Skip to content

Commit

Permalink
Add software reboot and switch to EasyButton to handle button navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
andreock committed Jan 10, 2025
1 parent 755b174 commit 4723ac5
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 30 deletions.
18 changes: 10 additions & 8 deletions lib/Peripherals/Peripherals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 10 additions & 3 deletions lib/Peripherals/Peripherals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,29 @@
#ifndef PERIPHERALS_H
#define PERIPHERALS_H
#include <Arduino.h>
#include <EasyButton.h>
#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
Original file line number Diff line number Diff line change
Expand Up @@ -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() {};
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,29 @@
#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() {};
void init_sd() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 7 additions & 5 deletions lib/UI/navigation/buttons/btn_routines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <RotaryEncoder.h>
Expand Down Expand Up @@ -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; }

Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 4723ac5

Please sign in to comment.