From e0bb95ca94b85ea8c5ef91e70db192ace57e9146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 18 Apr 2023 14:22:37 +0200 Subject: [PATCH] implement dynamic userbutton overwrite. fix #2434 --- src/ButtonThread.h | 10 ++++++---- src/PowerFSM.cpp | 2 +- src/main.cpp | 14 ++++++++------ src/platform/esp32/main-esp32.cpp | 2 +- src/sleep.cpp | 6 ++++-- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/ButtonThread.h b/src/ButtonThread.h index 0e9d060dc..0a8b2afa5 100644 --- a/src/ButtonThread.h +++ b/src/ButtonThread.h @@ -45,10 +45,10 @@ class ButtonThread : public concurrency::OSThread ButtonThread() : OSThread("Button") { #ifdef BUTTON_PIN - userButton = OneButton(BUTTON_PIN, true, true); + userButton = OneButton(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN, true, true); #ifdef INPUT_PULLUP_SENSE // Some platforms (nrf52) have a SENSE variant which allows wake from sleep - override what OneButton did - pinMode(BUTTON_PIN, INPUT_PULLUP_SENSE); + pinMode(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN, INPUT_PULLUP_SENSE); #endif userButton.attachClick(userButtonPressed); userButton.setClickTicks(300); @@ -57,7 +57,7 @@ class ButtonThread : public concurrency::OSThread userButton.attachMultiClick(userButtonMultiPressed); userButton.attachLongPressStart(userButtonPressedLongStart); userButton.attachLongPressStop(userButtonPressedLongStop); - wakeOnIrq(BUTTON_PIN, FALLING); + wakeOnIrq(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN, FALLING); #endif #ifdef BUTTON_PIN_ALT userButtonAlt = OneButton(BUTTON_PIN_ALT, true, true); @@ -115,7 +115,9 @@ class ButtonThread : public concurrency::OSThread { // LOG_DEBUG("press!\n"); #ifdef BUTTON_PIN - if ((BUTTON_PIN != moduleConfig.canned_message.inputbroker_pin_press) || !moduleConfig.canned_message.enabled) { + if (((config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN) != + moduleConfig.canned_message.inputbroker_pin_press) || + !moduleConfig.canned_message.enabled) { powerFSM.trigger(EVENT_PRESS); } #endif diff --git a/src/PowerFSM.cpp b/src/PowerFSM.cpp index e6ebdcc93..7babc2067 100644 --- a/src/PowerFSM.cpp +++ b/src/PowerFSM.cpp @@ -99,7 +99,7 @@ static void lsIdle() LOG_INFO("wakeCause2 %d\n", wakeCause2); #ifdef BUTTON_PIN - bool pressed = !digitalRead(BUTTON_PIN); + bool pressed = !digitalRead(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN); #else bool pressed = false; #endif diff --git a/src/main.cpp b/src/main.cpp index 53ed53b9d..7af41116c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -218,10 +218,10 @@ void setup() // If the button is connected to GPIO 12, don't enable the ability to use // meshtasticAdmin on the device. - pinMode(BUTTON_PIN, INPUT); + pinMode(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN, INPUT); #ifdef BUTTON_NEED_PULLUP - gpio_pullup_en((gpio_num_t)BUTTON_PIN); + gpio_pullup_en((gpio_num_t)(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN)); delay(10); #endif @@ -389,10 +389,7 @@ void setup() // scanEInkDevice(); #endif -#if HAS_BUTTON - // Buttons & LED - buttonThread = new ButtonThread(); -#endif + // LED init #ifdef LED_PIN pinMode(LED_PIN, OUTPUT); @@ -417,6 +414,11 @@ void setup() if (config.device.role == meshtastic_Config_DeviceConfig_Role_REPEATER) router = new FloodingRouter(); +#if HAS_BUTTON + // Buttons. Moved here cause we need NodeDB to be initialized + buttonThread = new ButtonThread(); +#endif + playStartMelody(); // fixed screen override? diff --git a/src/platform/esp32/main-esp32.cpp b/src/platform/esp32/main-esp32.cpp index 2068025c2..4cb7f4443 100644 --- a/src/platform/esp32/main-esp32.cpp +++ b/src/platform/esp32/main-esp32.cpp @@ -202,7 +202,7 @@ void cpuDeepSleep(uint32_t msecToWake) #ifdef BUTTON_PIN // Only GPIOs which are have RTC functionality can be used in this bit map: 0,2,4,12-15,25-27,32-39. #if SOC_RTCIO_HOLD_SUPPORTED - uint64_t gpioMask = (1ULL << BUTTON_PIN); + uint64_t gpioMask = (1ULL << config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN); #endif #ifdef BUTTON_NEED_PULLUP diff --git a/src/sleep.cpp b/src/sleep.cpp index 9468b7d09..c25e3473e 100644 --- a/src/sleep.cpp +++ b/src/sleep.cpp @@ -309,7 +309,8 @@ esp_sleep_wakeup_cause_t doLightSleep(uint64_t sleepMsec) // FIXME, use a more r // assert(esp_sleep_enable_uart_wakeup(0) == ESP_OK); #endif #ifdef BUTTON_PIN - esp_sleep_enable_ext0_wakeup((gpio_num_t)BUTTON_PIN, LOW); // when user presses, this button goes low + esp_sleep_enable_ext0_wakeup((gpio_num_t)(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN), + LOW); // when user presses, this button goes low #endif #if defined(LORA_DIO1) && (LORA_DIO1 != RADIOLIB_NC) gpio_wakeup_enable((gpio_num_t)LORA_DIO1, GPIO_INTR_HIGH_LEVEL); // SX126x/SX128x interrupt, active high @@ -338,7 +339,8 @@ esp_sleep_wakeup_cause_t doLightSleep(uint64_t sleepMsec) // FIXME, use a more r esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause(); #ifdef BUTTON_PIN if (cause == ESP_SLEEP_WAKEUP_GPIO) - LOG_INFO("Exit light sleep gpio: btn=%d\n", !digitalRead(BUTTON_PIN)); + LOG_INFO("Exit light sleep gpio: btn=%d\n", + !digitalRead(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN)); #endif return cause;