From 4c1dd9fd4a6fbd3d010244eb58023b2e8312ca29 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 31 Oct 2023 15:47:44 -0400 Subject: [PATCH 1/2] Added a delay to the reboot function --- ESPixelStick/ESPixelStick.ino | 28 ++++++++++++++++++++----- ESPixelStick/src/ESPixelStick.h | 5 ++++- ESPixelStick/src/WebMgr.cpp | 10 ++++----- ESPixelStick/src/input/InputE131.cpp | 5 ++--- ESPixelStick/src/input/InputMQTT.cpp | 11 +++++----- ESPixelStick/src/input/InputMgr.cpp | 11 +++++++--- ESPixelStick/src/network/WiFiDriver.cpp | 4 +--- ESPixelStick/src/output/OutputMgr.cpp | 8 +++---- ESPixelStick/src/output/OutputRmt.cpp | 4 ++-- ESPixelStick/src/output/OutputUart.cpp | 2 +- 10 files changed, 54 insertions(+), 34 deletions(-) diff --git a/ESPixelStick/ESPixelStick.ino b/ESPixelStick/ESPixelStick.ino index afb1a8ecf..612253fc0 100644 --- a/ESPixelStick/ESPixelStick.ino +++ b/ESPixelStick/ESPixelStick.ino @@ -87,7 +87,8 @@ const String BUILD_DATE = String(__DATE__) + " - " + String(__TIME__); const uint8_t CurrentConfigVersion = 1; config_t config; // Current configuration -bool reboot = false; // Reboot flag +static const uint32_t NotRebootingValue = uint32_t(-1); +uint32_t RebootCount = NotRebootingValue; uint32_t lastUpdate; // Update timeout tracker bool ResetWiFi = false; bool IsBooting = true; // Configuration initialization flag @@ -495,11 +496,14 @@ void loop() } // end discard loop // Reboot handler - if (reboot) + if (NotRebootingValue != RebootCount) { - logcon (String(CN_stars) + CN_minussigns + F ("Internal Reboot Requested. Rebooting Now")); - delay (REBOOT_DELAY); - ESP.restart (); + if(0 == --RebootCount) + { + logcon (String(CN_stars) + CN_minussigns + F ("Internal Reboot Requested. Rebooting Now")); + delay (REBOOT_DELAY); + ESP.restart (); + } } if (ConfigLoadNeeded) @@ -516,6 +520,20 @@ void loop() } // loop +bool RebootInProgress() +{ + return RebootCount != NotRebootingValue; +} + +void RequestReboot(uint32_t LoopDelay) +{ + RebootCount = LoopDelay; + + InputMgr.SetOperationalState(false); + OutputMgr.PauseOutputs(true); + +} // RequestReboot + void _logcon (String & DriverName, String Message) { char Spaces[7]; diff --git a/ESPixelStick/src/ESPixelStick.h b/ESPixelStick/src/ESPixelStick.h index 138698b5d..6b5544838 100644 --- a/ESPixelStick/src/ESPixelStick.h +++ b/ESPixelStick/src/ESPixelStick.h @@ -60,6 +60,9 @@ #define STRINGIFY(X) #X #define STRING(X) STRINGIFY(X) +extern void RequestReboot(uint32_t LoopDelay); +extern bool RebootInProgress(); + /// Core configuration structure typedef struct { // Device @@ -72,7 +75,7 @@ void deserializeCoreHandler (DynamicJsonDocument& jsonDoc); bool deserializeCore (JsonObject & json); bool dsDevice (JsonObject & json); bool dsNetwork (JsonObject & json); -extern bool reboot; + extern bool IsBooting; extern bool ResetWiFi; extern const String ConfigFileName; diff --git a/ESPixelStick/src/WebMgr.cpp b/ESPixelStick/src/WebMgr.cpp index 875c6f065..8615ea462 100644 --- a/ESPixelStick/src/WebMgr.cpp +++ b/ESPixelStick/src/WebMgr.cpp @@ -165,7 +165,7 @@ void c_WebMgr::init () } else { - reboot = true; + RequestReboot(100000); request->send (200, CN_textSLASHplain, "Rebooting"); } }); @@ -188,8 +188,7 @@ void c_WebMgr::init () request->send (200, CN_textSLASHplain, "Rebooting"); // DEBUG_V (""); - extern bool reboot; - reboot = true; + RequestReboot(100000);; } }); @@ -364,7 +363,7 @@ void c_WebMgr::init () webServer.on ("/updatefw", HTTP_POST, [](AsyncWebServerRequest* request) { - reboot = true; + RequestReboot(100000);; }, [](AsyncWebServerRequest* request, String filename, uint32_t index, uint8_t* data, uint32_t len, bool final) {WebMgr.FirmwareUpload (request, filename, index, data, len, final); }).setFilter (ON_STA_FILTER); @@ -715,8 +714,7 @@ void c_WebMgr::FirmwareUpload (AsyncWebServerRequest* request, efupdate.end (); // LittleFS.begin (); - extern bool reboot; - reboot = true; + RequestReboot(100000);; } } while (false); diff --git a/ESPixelStick/src/input/InputE131.cpp b/ESPixelStick/src/input/InputE131.cpp index 884b1ce15..13a4c75dd 100644 --- a/ESPixelStick/src/input/InputE131.cpp +++ b/ESPixelStick/src/input/InputE131.cpp @@ -273,7 +273,7 @@ bool c_InputE131::SetConfig (ArduinoJson::JsonObject& jsonConfig) if ((OldPortId != PortId) && (ESPAsyncE131Initialized)) { // ask for a reboot. - reboot = true; + RequestReboot(100000); logcon (String (F ("Requesting reboot on change of UDP port."))); } @@ -390,8 +390,7 @@ void c_InputE131::NetworkStateChanged (bool IsConnected, bool ReBootAllowed) { // handle a disconnect // E1.31 does not do this gracefully. A loss of connection needs a reboot - // extern bool reboot; - reboot = true; + RequestReboot(100000); logcon (String (F ("Input requesting reboot on loss of WiFi connection."))); } diff --git a/ESPixelStick/src/input/InputMQTT.cpp b/ESPixelStick/src/input/InputMQTT.cpp index 7373de762..7d3a895de 100644 --- a/ESPixelStick/src/input/InputMQTT.cpp +++ b/ESPixelStick/src/input/InputMQTT.cpp @@ -157,11 +157,11 @@ void c_InputMQTT::Process () // DEBUG_V (""); pEffectsEngine->Process (); } - } - if (nullptr != pPlayFileEngine) - { - pPlayFileEngine->Poll (); + if (nullptr != pPlayFileEngine) + { + pPlayFileEngine->Poll (); + } } // DEBUG_END; @@ -772,8 +772,7 @@ void c_InputMQTT::NetworkStateChanged (bool IsConnected, bool ReBootAllowed) else if (ReBootAllowed) { // handle a disconnect - extern bool reboot; - reboot = true; + RequestReboot(100000); logcon (String (F ("Requesting reboot on loss of network connection."))); } diff --git a/ESPixelStick/src/input/InputMgr.cpp b/ESPixelStick/src/input/InputMgr.cpp index 8c375ca4b..f2947df6f 100644 --- a/ESPixelStick/src/input/InputMgr.cpp +++ b/ESPixelStick/src/input/InputMgr.cpp @@ -624,7 +624,7 @@ void c_InputMgr::LoadConfig () else { logcon (CN_stars + String (F (" Error loading Input Manager Config File. Rebooting ")) + CN_stars); - reboot = true; + RequestReboot(100000); } } @@ -659,10 +659,15 @@ void c_InputMgr::Process () configInProgress = false; } + if(RebootInProgress()) + { + break; + } + bool aBlankTimerIsRunning = false; for (auto & CurrentInput : InputChannelDrivers) { - // DEBUG_V(""); + // DEBUG_V(String("pInputChannelDriver: 0x") + String(uint32_t(CurrentInput.pInputChannelDriver), HEX)); CurrentInput.pInputChannelDriver->Process (); if (!BlankTimerHasExpired (CurrentInput.pInputChannelDriver->GetInputChannelId())) @@ -683,7 +688,7 @@ void c_InputMgr::Process () if (rebootNeeded) { // DEBUG_V("Requesting Reboot"); - reboot = true; + RequestReboot(100000); } } while (false); diff --git a/ESPixelStick/src/network/WiFiDriver.cpp b/ESPixelStick/src/network/WiFiDriver.cpp index 1077ac3ab..fa9317b9d 100644 --- a/ESPixelStick/src/network/WiFiDriver.cpp +++ b/ESPixelStick/src/network/WiFiDriver.cpp @@ -957,10 +957,8 @@ void fsm_WiFi_state_ConnectionFailed::Init () { if (true == pWiFiDriver->Get_RebootOnWiFiFailureToConnect()) { - extern bool reboot; logcon (F ("WiFi Requesting Reboot")); - - reboot = true; + RequestReboot(100000); } else { diff --git a/ESPixelStick/src/output/OutputMgr.cpp b/ESPixelStick/src/output/OutputMgr.cpp index 103e4384d..ea742aac5 100644 --- a/ESPixelStick/src/output/OutputMgr.cpp +++ b/ESPixelStick/src/output/OutputMgr.cpp @@ -245,7 +245,7 @@ void c_OutputMgr::Begin () if (0 == OutputChannelId_End) { logcon("ERROR: No output Channels defined. Rebooting"); - reboot = true; + RequestReboot(100000); break; } @@ -1030,7 +1030,7 @@ void c_OutputMgr::LoadConfig () else { logcon(CN_stars + String(MN_15) + CN_stars); - reboot = true; + RequestReboot(100000); } } @@ -1297,7 +1297,7 @@ void c_OutputMgr::Poll() LoadConfig (); } // done need to save the current config - if ((false == IsOutputPaused) && (false == ConfigInProgress)) + if ((false == IsOutputPaused) && (false == ConfigInProgress) && (false == RebootInProgress()) ) { // //DEBUG_V(); for (DriverInfo_t & OutputChannel : OutputChannelDrivers) @@ -1326,7 +1326,7 @@ void c_OutputMgr::TaskPoll() // DEBUG_V("Done config processing"); } // done need to save the current config - if ((false == IsOutputPaused) && (false == ConfigInProgress)) + if ((false == IsOutputPaused) && (false == ConfigInProgress) && (false == RebootInProgress()) ) { // PollCount ++; bool FoundAnActiveOutputChannel = false; diff --git a/ESPixelStick/src/output/OutputRmt.cpp b/ESPixelStick/src/output/OutputRmt.cpp index 1bd35e3aa..740747da0 100644 --- a/ESPixelStick/src/output/OutputRmt.cpp +++ b/ESPixelStick/src/output/OutputRmt.cpp @@ -62,7 +62,7 @@ c_OutputRmt::~c_OutputRmt () if (HasBeenInitialized) { logcon(F("Shutting down an RMT channel requires a reboot")); - reboot = true; + RequestReboot(100000); DisableRmtInterrupts; ClearRmtInterrupts; @@ -131,7 +131,7 @@ void c_OutputRmt::Begin (OutputRmtConfig_t config ) { LOG_PORT.println (F("Invalid RMT configuration parameters. Rebooting")); - reboot = true; + RequestReboot(100000);; break; } diff --git a/ESPixelStick/src/output/OutputUart.cpp b/ESPixelStick/src/output/OutputUart.cpp index 86c90f6b1..2e541cc1e 100644 --- a/ESPixelStick/src/output/OutputUart.cpp +++ b/ESPixelStick/src/output/OutputUart.cpp @@ -190,7 +190,7 @@ void c_OutputUart::Begin (OutputUartConfig_t & config ) { LOG_PORT.println (F("Invalid UART configuration parameters. Rebooting")); - reboot = true; + RequestReboot(100000); break; } From 7ce5ed8c2876422483ce4cf977f195417d2adf09 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 31 Oct 2023 15:52:04 -0400 Subject: [PATCH 2/2] Added delayed reboot --- ESPixelStick/src/network/EthernetDriver.cpp | 2 +- ESPixelStick/src/output/OutputSpi.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ESPixelStick/src/network/EthernetDriver.cpp b/ESPixelStick/src/network/EthernetDriver.cpp index abe392ca2..9534f4129 100644 --- a/ESPixelStick/src/network/EthernetDriver.cpp +++ b/ESPixelStick/src/network/EthernetDriver.cpp @@ -353,7 +353,7 @@ bool c_EthernetDriver::SetConfig (JsonObject & json) if (ConfigChanged && HasBeenPreviouslyConfigured) { logcon (F ("Configuration change requires system reboot.")); - reboot = true; + RequestReboot(100000); } HasBeenPreviouslyConfigured = true; diff --git a/ESPixelStick/src/output/OutputSpi.cpp b/ESPixelStick/src/output/OutputSpi.cpp index b2021aedb..df23c829a 100644 --- a/ESPixelStick/src/output/OutputSpi.cpp +++ b/ESPixelStick/src/output/OutputSpi.cpp @@ -90,7 +90,7 @@ c_OutputSpi::~c_OutputSpi () if (OutputPixel) { logcon(CN_stars + String(F(" SPI Interface Shutdown requires a reboot ")) + CN_stars); - reboot = true; + RequestReboot(100000); } } // DEBUG_END;