From 6dfc00d6aec63171f464e2c3c15feff2c8e80d19 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Sun, 20 Jul 2025 14:41:00 -0500 Subject: [PATCH] Unify the shutdown proceedure --- src/Power.cpp | 13 ++++++++++++- src/buzz/BuzzerFeedbackThread.cpp | 4 ---- src/graphics/draw/MenuHandler.cpp | 5 ++--- src/input/ExpressLRSFiveWay.cpp | 9 +-------- src/modules/SystemCommandsModule.cpp | 6 +----- src/shutdown.h | 14 -------------- 6 files changed, 16 insertions(+), 35 deletions(-) diff --git a/src/Power.cpp b/src/Power.cpp index 9c67977bd..8ff2cb48a 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -681,7 +681,14 @@ bool Power::setup() void Power::shutdown() { - LOG_INFO("Shutting Down"); + +#if HAS_SCREEN + if (screen) { + screen->showSimpleBanner("Shutting Down...", 0); // stays on screen + } +#endif + nodeDB->saveToDisk(); + playShutdownMelody(); #if defined(ARCH_NRF52) || defined(ARCH_ESP32) || defined(ARCH_RP2040) #ifdef PIN_LED1 @@ -694,6 +701,10 @@ void Power::shutdown() ledOff(PIN_LED3); #endif doDeepSleep(DELAY_FOREVER, false, false); +#elif defined(ARCH_PORTDUINO) + exit(EXIT_SUCCESS); +#else + LOG_WARN("FIXME implement shutdown for this platform"); #endif } diff --git a/src/buzz/BuzzerFeedbackThread.cpp b/src/buzz/BuzzerFeedbackThread.cpp index b644ea8f4..ce762c764 100644 --- a/src/buzz/BuzzerFeedbackThread.cpp +++ b/src/buzz/BuzzerFeedbackThread.cpp @@ -47,10 +47,6 @@ int BuzzerFeedbackThread::handleInputEvent(const InputEvent *event) playComboTune(); // Ping sent feedback break; - case INPUT_BROKER_SHUTDOWN: - playShutdownMelody(); // Shutdown feedback - break; - default: // For other events, check if it's a printable character if (event->kbchar >= 32 && event->kbchar <= 126) { diff --git a/src/graphics/draw/MenuHandler.cpp b/src/graphics/draw/MenuHandler.cpp index 7ed9c4ea1..83198a7c5 100644 --- a/src/graphics/draw/MenuHandler.cpp +++ b/src/graphics/draw/MenuHandler.cpp @@ -815,9 +815,8 @@ void menuHandler::shutdownMenu() bannerOptions.optionsCount = 2; bannerOptions.bannerCallback = [](int selected) -> void { if (selected == 1) { - IF_SCREEN(screen->showSimpleBanner("Shutting Down...", 0)); - nodeDB->saveToDisk(); - power->shutdown(); + InputEvent event = {.inputEvent = (input_broker_event)INPUT_BROKER_SHUTDOWN, .kbchar = 0, .touchX = 0, .touchY = 0}; + inputBroker->injectInputEvent(&event); } else { menuQueue = power_menu; screen->runNow(); diff --git a/src/input/ExpressLRSFiveWay.cpp b/src/input/ExpressLRSFiveWay.cpp index 53bcedc63..77f9e9993 100644 --- a/src/input/ExpressLRSFiveWay.cpp +++ b/src/input/ExpressLRSFiveWay.cpp @@ -233,14 +233,7 @@ void ExpressLRSFiveWay::sendAdhocPing() // Contained as one method for easier remapping of buttons by user void ExpressLRSFiveWay::shutdown() { - LOG_INFO("Shutdown from long press"); - powerFSM.trigger(EVENT_PRESS); - screen->startAlert("Shutting Down..."); - // Don't set alerting = true. We don't want to auto-dismiss this alert. - - playShutdownMelody(); // In case user adds a buzzer - - shutdownAtMsec = millis() + 3000; + sendKey(INPUT_BROKER_SHUTDOWN); } void ExpressLRSFiveWay::click() diff --git a/src/modules/SystemCommandsModule.cpp b/src/modules/SystemCommandsModule.cpp index ab9439b39..2d534bd67 100644 --- a/src/modules/SystemCommandsModule.cpp +++ b/src/modules/SystemCommandsModule.cpp @@ -107,11 +107,7 @@ int SystemCommandsModule::handleInputEvent(const InputEvent *event) return true; // Power control case INPUT_BROKER_SHUTDOWN: - LOG_ERROR("Shutting Down"); - IF_SCREEN(screen->showSimpleBanner("Shutting Down...")); - nodeDB->saveToDisk(); - shutdownAtMsec = millis() + DEFAULT_SHUTDOWN_SECONDS * 1000; - // runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; + shutdownAtMsec = millis(); return true; default: diff --git a/src/shutdown.h b/src/shutdown.h index 7e2120149..46148599e 100644 --- a/src/shutdown.h +++ b/src/shutdown.h @@ -40,21 +40,7 @@ void powerCommandsCheck() #endif } -#if defined(ARCH_ESP32) || defined(ARCH_NRF52) - if (shutdownAtMsec && screen) { - screen->showSimpleBanner("Shutting Down...", 0); // stays on screen - } -#endif - if (shutdownAtMsec && millis() > shutdownAtMsec) { - LOG_INFO("Shut down from admin command"); -#if defined(ARCH_NRF52) || defined(ARCH_ESP32) || defined(ARCH_RP2040) - playShutdownMelody(); power->shutdown(); -#elif defined(ARCH_PORTDUINO) - exit(EXIT_SUCCESS); -#else - LOG_WARN("FIXME implement shutdown for this platform"); -#endif } } \ No newline at end of file