From 30488e6f675fece372c58af5c07090574f06c57d Mon Sep 17 00:00:00 2001 From: seagull9000 Date: Tue, 27 May 2025 11:07:51 +1200 Subject: [PATCH] Connect RTTTL shutdown melody to shutdown procedure Added a new UITask shutdown method to run non-board specific shutdown code. This avoids having to update all the board files for different hardware. UITask::shutdown(bool restart = false); Where the buzzer is available and defined, the RTTTL shutdown melody is played when the button is held down for >5s. --- examples/companion_radio/UITask.cpp | 25 ++++++++++++++++++++++++- examples/companion_radio/UITask.h | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/examples/companion_radio/UITask.cpp b/examples/companion_radio/UITask.cpp index f97b47f4..1932512f 100644 --- a/examples/companion_radio/UITask.cpp +++ b/examples/companion_radio/UITask.cpp @@ -256,7 +256,7 @@ void UITask::buttonHandler() { digitalWrite(PIN_STATUS_LED, LOW); delay(10); #endif - _board->powerOff(); + shutdown(); } } btn_state_change_time = millis(); @@ -267,6 +267,29 @@ void UITask::buttonHandler() { #endif } +/* hardware-agnostic pre-shutdown activity should be done here +*/ +void UITask::shutdown(bool restart){ + + #ifdef PIN_BUZZER + /* note: we have a choice here - + we can do a blocking buzzer.loop() with non-deterministic consequences + or we can set a flag and delay the shutdown for a couple of seconds + while a non-blocking buzzer.loop() plays out in UITask::loop() + */ + buzzer.shutdown(); + uint32_t buzzer_timer = millis(); // fail-safe shutdown + while (buzzer.isPlaying() && (millis() - 2500) < buzzer_timer) + buzzer.loop(); + + #endif // PIN_BUZZER + + if (restart) + _board->reboot(); + else + _board->powerOff(); +} + void UITask::loop() { buttonHandler(); userLedHandler(); diff --git a/examples/companion_radio/UITask.h b/examples/companion_radio/UITask.h index 134b5a16..d774e54c 100644 --- a/examples/companion_radio/UITask.h +++ b/examples/companion_radio/UITask.h @@ -39,7 +39,6 @@ class UITask { void buttonHandler(); void userLedHandler(); void renderBatteryIndicator(uint16_t batteryMilliVolts); - public: @@ -55,5 +54,6 @@ public: void msgRead(int msgcount); void newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount); void soundBuzzer(UIEventType bet = UIEventType::none); + void shutdown(bool restart = false); void loop(); };