From 2bd40b7053b4c6ca11c29238416520f069eb1480 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sun, 22 Nov 2020 18:50:14 -0800 Subject: [PATCH] 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();