From 2bd40b7053b4c6ca11c29238416520f069eb1480 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sun, 22 Nov 2020 18:50:14 -0800 Subject: [PATCH 1/3] Add support for the power button #528 --- src/Power.cpp | 5 +++++ src/main.cpp | 54 +++++++++++++++++++++++++++++++++++++++------------ src/power.h | 1 + 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/Power.cpp b/src/Power.cpp index ae51e8039..93e3096d0 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -134,6 +134,11 @@ bool Power::setup() return found; } +void Power::shutdown() { + DEBUG_MSG("Shutting down\n"); + axp.shutdown(); +} + /// Reads power status to powerStatus singleton. // // TODO(girts): move this and other axp stuff to power.h/power.cpp. diff --git a/src/main.cpp b/src/main.cpp index acef3b672..228f2b3e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -167,29 +167,35 @@ class ButtonThread : public OSThread #endif public: + static uint32_t longPressTime; + // callback returns the period for the next callback invocation (or 0 if we should no longer be called) ButtonThread() : OSThread("Button") { #ifdef BUTTON_PIN userButton = OneButton(BUTTON_PIN, true, true); #ifdef INPUT_PULLUP_SENSE - // Some platforms (nrf52) have a SENSE variant which allows wake from sleep - override what OneButton did + // Some platforms (nrf52) have a SENSE variant which allows wake from sleep - override what OneButton did pinMode(BUTTON_PIN, INPUT_PULLUP_SENSE); #endif userButton.attachClick(userButtonPressed); userButton.attachDuringLongPress(userButtonPressedLong); userButton.attachDoubleClick(userButtonDoublePressed); + userButton.attachLongPressStart(userButtonPressedLongStart); + userButton.attachLongPressStop(userButtonPressedLongStop); wakeOnIrq(BUTTON_PIN, FALLING); #endif #ifdef BUTTON_PIN_ALT userButtonAlt = OneButton(BUTTON_PIN_ALT, true, true); #ifdef INPUT_PULLUP_SENSE - // Some platforms (nrf52) have a SENSE variant which allows wake from sleep - override what OneButton did + // Some platforms (nrf52) have a SENSE variant which allows wake from sleep - override what OneButton did pinMode(BUTTON_PIN_ALT, INPUT_PULLUP_SENSE); -#endif +#endif userButtonAlt.attachClick(userButtonPressed); userButtonAlt.attachDuringLongPress(userButtonPressedLong); userButtonAlt.attachDoubleClick(userButtonDoublePressed); + userButtonAlt.attachLongPressStart(userButtonPressedLongStart); + userButtonAlt.attachLongPressStop(userButtonPressedLongStop); wakeOnIrq(BUTTON_PIN_ALT, FALLING); #endif } @@ -209,7 +215,7 @@ class ButtonThread : public OSThread canSleep &= userButtonAlt.isIdle(); #endif // if (!canSleep) DEBUG_MSG("Supressing sleep!\n"); - //else DEBUG_MSG("sleep ok\n"); + // else DEBUG_MSG("sleep ok\n"); return 5; } @@ -222,20 +228,44 @@ class ButtonThread : public OSThread } static void userButtonPressedLong() { - DEBUG_MSG("Long press!\n"); + // DEBUG_MSG("Long press!\n"); screen->adjustBrightness(); - } - - static void userButtonDoublePressed() -{ -#ifndef NO_ESP32 - disablePin(); + + // If user button is held down for 10 seconds, shutdown the device. + if (millis() - longPressTime > 10 * 1000) { +#ifdef AXP192_SLAVE_ADDRESS + if (axp192_found == true) { + power->shutdown(); + } #endif -} + } else { + //DEBUG_MSG("Long press %u\n", (millis() - longPressTime)); + } + } + + static void userButtonDoublePressed() + { +#ifndef NO_ESP32 + disablePin(); +#endif + } + + static void userButtonPressedLongStart() + { + DEBUG_MSG("Long press start!\n"); + longPressTime = millis(); + } + + static void userButtonPressedLongStop() + { + DEBUG_MSG("Long press stop!\n"); + longPressTime = 0; + } }; static Periodic *ledPeriodic; static OSThread *powerFSMthread, *buttonThread; +uint32_t ButtonThread::longPressTime = 0; RadioInterface *rIf = NULL; diff --git a/src/power.h b/src/power.h index 8b75834aa..815e22675 100644 --- a/src/power.h +++ b/src/power.h @@ -23,6 +23,7 @@ class Power : private concurrency::OSThread Power(); + void shutdown(); void readPowerStatus(); virtual bool setup(); virtual int32_t runOnce(); From 9801a62d2dc1f722f49013ac57c14385498c7ed2 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sun, 22 Nov 2020 19:01:48 -0800 Subject: [PATCH 2/3] Fix for CI on other boards. --- src/Power.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Power.cpp b/src/Power.cpp index 93e3096d0..4e30c8288 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -134,9 +134,12 @@ bool Power::setup() return found; } -void Power::shutdown() { +void Power::shutdown() +{ +#ifdef AXP192_SLAVE_ADDRESS DEBUG_MSG("Shutting down\n"); axp.shutdown(); +#endif } /// Reads power status to powerStatus singleton. From 817c99e09ccdd9e47ace7e967a6ed2ef02da7a16 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sun, 22 Nov 2020 19:12:11 -0800 Subject: [PATCH 3/3] trying to fix it again --- src/Power.cpp | 2 +- src/main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Power.cpp b/src/Power.cpp index 4e30c8288..ba908e3fb 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -136,7 +136,7 @@ bool Power::setup() void Power::shutdown() { -#ifdef AXP192_SLAVE_ADDRESS +#ifdef TBEAM_V10 DEBUG_MSG("Shutting down\n"); axp.shutdown(); #endif diff --git a/src/main.cpp b/src/main.cpp index 228f2b3e3..4c0af7f7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -233,7 +233,7 @@ class ButtonThread : public OSThread // If user button is held down for 10 seconds, shutdown the device. if (millis() - longPressTime > 10 * 1000) { -#ifdef AXP192_SLAVE_ADDRESS +#ifdef TBEAM_V10 if (axp192_found == true) { power->shutdown(); }