From 9309824874fd7e7c9c0264c6b955a828bb00556a Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Wed, 19 Jan 2022 17:10:02 -0600 Subject: [PATCH] RAK / NRF shutdown functionality on user button long press (#1113) --- src/commands.h | 1 + src/graphics/Screen.cpp | 23 +++++++++++++++++++++++ src/graphics/Screen.h | 8 +++++++- src/main.cpp | 5 ++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/commands.h b/src/commands.h index 803ea2b5..13ddc728 100644 --- a/src/commands.h +++ b/src/commands.h @@ -13,4 +13,5 @@ enum class Cmd { STOP_BLUETOOTH_PIN_SCREEN, STOP_BOOT_SCREEN, PRINT, + START_SHUTDOWN_SCREEN, }; \ No newline at end of file diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index c7bc0fe3..d70ee887 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -219,6 +219,14 @@ static void drawFrameBluetooth(OLEDDisplay *display, OLEDDisplayUiState *state, display->drawString(64 + x, 48 + y, buf); } +static void drawFrameShutdown(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) +{ + display->setTextAlignment(TEXT_ALIGN_CENTER); + + display->setFont(FONT_MEDIUM); + display->drawString(64 + x, 26 + y, "Shutting down..."); +} + static void drawFrameFirmware(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { display->setTextAlignment(TEXT_ALIGN_CENTER); @@ -881,6 +889,9 @@ int32_t Screen::runOnce() handlePrint(cmd.print_text); free(cmd.print_text); break; + case Cmd::START_SHUTDOWN_SCREEN: + handleShutdownScreen(); + break; default: DEBUG_MSG("BUG: invalid cmd\n"); } @@ -1047,6 +1058,18 @@ void Screen::handleStartBluetoothPinScreen(uint32_t pin) setFastFramerate(); } +void Screen::handleShutdownScreen() +{ + DEBUG_MSG("showing shutdown screen\n"); + showingNormalScreen = false; + + static FrameCallback shutdownFrames[] = {drawFrameShutdown}; + + ui.disableAllIndicators(); + ui.setFrames(shutdownFrames, 1); + setFastFramerate(); +} + void Screen::handleStartFirmwareUpdateScreen() { DEBUG_MSG("showing firmware screen\n"); diff --git a/src/graphics/Screen.h b/src/graphics/Screen.h index 10b2b6ff..6c50ea2b 100644 --- a/src/graphics/Screen.h +++ b/src/graphics/Screen.h @@ -151,6 +151,12 @@ class Screen : public concurrency::OSThread enqueueCmd(cmd); } + void startShutdownScreen() + { + ScreenCmd cmd; + cmd.cmd = Cmd::START_SHUTDOWN_SCREEN; + enqueueCmd(cmd); + } /// Stops showing the bluetooth PIN screen. void stopBluetoothPinScreen() { enqueueCmd(ScreenCmd{.cmd = Cmd::STOP_BLUETOOTH_PIN_SCREEN}); } @@ -262,7 +268,7 @@ class Screen : public concurrency::OSThread void handleStartBluetoothPinScreen(uint32_t pin); void handlePrint(const char *text); void handleStartFirmwareUpdateScreen(); - + void handleShutdownScreen(); /// Rebuilds our list of frames (screens) to default ones. void setFrames(); diff --git a/src/main.cpp b/src/main.cpp index 031dba8a..be651d20 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -300,8 +300,9 @@ class ButtonThread : public OSThread static void userButtonPressedLong() { // DEBUG_MSG("Long press!\n"); +#ifndef NRF52_SERIES screen->adjustBrightness(); - +#endif // If user button is held down for 5 seconds, shutdown the device. if (millis() - longPressTime > 5 * 1000) { #ifdef TBEAM_V10 @@ -313,6 +314,7 @@ class ButtonThread : public OSThread // Do actual shutdown when button released, otherwise the button release // may wake the board immediatedly. if (!shutdown_on_long_stop) { + screen->startShutdownScreen(); DEBUG_MSG("Shutdown from long press"); playBeep(); ledOff(PIN_LED1); @@ -354,6 +356,7 @@ class ButtonThread : public OSThread longPressTime = 0; if (shutdown_on_long_stop) { playShutdownMelody(); + delay(3000); power->shutdown(); } }