Add support for the power button #528

This commit is contained in:
Jm Casler 2020-11-22 18:50:14 -08:00
parent 3ab9d2a50e
commit 2bd40b7053
3 changed files with 48 additions and 12 deletions

View File

@ -134,6 +134,11 @@ bool Power::setup()
return found; return found;
} }
void Power::shutdown() {
DEBUG_MSG("Shutting down\n");
axp.shutdown();
}
/// Reads power status to powerStatus singleton. /// Reads power status to powerStatus singleton.
// //
// TODO(girts): move this and other axp stuff to power.h/power.cpp. // TODO(girts): move this and other axp stuff to power.h/power.cpp.

View File

@ -167,29 +167,35 @@ class ButtonThread : public OSThread
#endif #endif
public: public:
static uint32_t longPressTime;
// callback returns the period for the next callback invocation (or 0 if we should no longer be called) // callback returns the period for the next callback invocation (or 0 if we should no longer be called)
ButtonThread() : OSThread("Button") ButtonThread() : OSThread("Button")
{ {
#ifdef BUTTON_PIN #ifdef BUTTON_PIN
userButton = OneButton(BUTTON_PIN, true, true); userButton = OneButton(BUTTON_PIN, true, true);
#ifdef INPUT_PULLUP_SENSE #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); pinMode(BUTTON_PIN, INPUT_PULLUP_SENSE);
#endif #endif
userButton.attachClick(userButtonPressed); userButton.attachClick(userButtonPressed);
userButton.attachDuringLongPress(userButtonPressedLong); userButton.attachDuringLongPress(userButtonPressedLong);
userButton.attachDoubleClick(userButtonDoublePressed); userButton.attachDoubleClick(userButtonDoublePressed);
userButton.attachLongPressStart(userButtonPressedLongStart);
userButton.attachLongPressStop(userButtonPressedLongStop);
wakeOnIrq(BUTTON_PIN, FALLING); wakeOnIrq(BUTTON_PIN, FALLING);
#endif #endif
#ifdef BUTTON_PIN_ALT #ifdef BUTTON_PIN_ALT
userButtonAlt = OneButton(BUTTON_PIN_ALT, true, true); userButtonAlt = OneButton(BUTTON_PIN_ALT, true, true);
#ifdef INPUT_PULLUP_SENSE #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); pinMode(BUTTON_PIN_ALT, INPUT_PULLUP_SENSE);
#endif #endif
userButtonAlt.attachClick(userButtonPressed); userButtonAlt.attachClick(userButtonPressed);
userButtonAlt.attachDuringLongPress(userButtonPressedLong); userButtonAlt.attachDuringLongPress(userButtonPressedLong);
userButtonAlt.attachDoubleClick(userButtonDoublePressed); userButtonAlt.attachDoubleClick(userButtonDoublePressed);
userButtonAlt.attachLongPressStart(userButtonPressedLongStart);
userButtonAlt.attachLongPressStop(userButtonPressedLongStop);
wakeOnIrq(BUTTON_PIN_ALT, FALLING); wakeOnIrq(BUTTON_PIN_ALT, FALLING);
#endif #endif
} }
@ -209,7 +215,7 @@ class ButtonThread : public OSThread
canSleep &= userButtonAlt.isIdle(); canSleep &= userButtonAlt.isIdle();
#endif #endif
// if (!canSleep) DEBUG_MSG("Supressing sleep!\n"); // if (!canSleep) DEBUG_MSG("Supressing sleep!\n");
//else DEBUG_MSG("sleep ok\n"); // else DEBUG_MSG("sleep ok\n");
return 5; return 5;
} }
@ -222,20 +228,44 @@ class ButtonThread : public OSThread
} }
static void userButtonPressedLong() static void userButtonPressedLong()
{ {
DEBUG_MSG("Long press!\n"); // DEBUG_MSG("Long press!\n");
screen->adjustBrightness(); screen->adjustBrightness();
}
// If user button is held down for 10 seconds, shutdown the device.
static void userButtonDoublePressed() if (millis() - longPressTime > 10 * 1000) {
{ #ifdef AXP192_SLAVE_ADDRESS
#ifndef NO_ESP32 if (axp192_found == true) {
disablePin(); power->shutdown();
}
#endif #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 Periodic *ledPeriodic;
static OSThread *powerFSMthread, *buttonThread; static OSThread *powerFSMthread, *buttonThread;
uint32_t ButtonThread::longPressTime = 0;
RadioInterface *rIf = NULL; RadioInterface *rIf = NULL;

View File

@ -23,6 +23,7 @@ class Power : private concurrency::OSThread
Power(); Power();
void shutdown();
void readPowerStatus(); void readPowerStatus();
virtual bool setup(); virtual bool setup();
virtual int32_t runOnce(); virtual int32_t runOnce();