diff --git a/code/espurna/system.cpp b/code/espurna/system.cpp index 083fe6eb..9454464d 100644 --- a/code/espurna/system.cpp +++ b/code/espurna/system.cpp @@ -164,43 +164,45 @@ void _systemRtcmemResetReason(CustomResetReason reason) { // // An unstable system will only have serial access, WiFi in AP mode and OTA -bool _systemStable = true; - -void systemCheck(bool stable) { - uint8_t value = 0; - - if (stable) { - value = 0; - DEBUG_MSG_P(PSTR("[MAIN] System OK\n")); - } else { - if (!rtcmemStatus()) { - systemStabilityCounter(1); - return; - } - - value = systemStabilityCounter(); +constexpr unsigned char _systemCheckMin() { + return 0u; +} - if (++value > SYSTEM_CHECK_MAX) { - _systemStable = false; - value = SYSTEM_CHECK_MAX; - DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n")); - } - } +constexpr unsigned char _systemCheckMax() { + return SYSTEM_CHECK_MAX; +} - systemStabilityCounter(value); +constexpr unsigned long _systemCheckTime() { + return SYSTEM_CHECK_TIME; } -bool systemCheck() { - return _systemStable; +static_assert(_systemCheckMax() > 0, ""); + +Ticker _system_stable_timer; +bool _system_stable { true }; + +void _systemStabilityInit() { + auto count = rtcmemStatus() ? systemStabilityCounter() : 1u; + + _system_stable = (count < _systemCheckMax()); + DEBUG_MSG_P(PSTR("[MAIN] System %s\n"), _system_stable ? "OK" : "UNSTABLE"); + + _system_stable_timer.once_ms_scheduled(_systemCheckTime(), []() { + DEBUG_MSG_P(PSTR("[MAIN] System stability counter %hhu / %hhu\n"), + _systemCheckMin(), _systemCheckMax()); + systemStabilityCounter(_systemCheckMin()); + }); + + auto next = count + 1u; + count = next > _systemCheckMax() + ? count + : next; + + systemStabilityCounter(count); } -void _systemCheckLoop() { - static bool checked = false; - if (!checked && (millis() > SYSTEM_CHECK_TIME)) { - // Flag system as stable - systemCheck(true); - checked = true; - } +bool systemCheck() { + return _system_stable; } #endif @@ -536,10 +538,6 @@ void systemLoop() { return; } -#if SYSTEM_CHECK_ENABLED - _systemCheckLoop(); -#endif - _systemUpdateLoadAverage(); } @@ -573,9 +571,8 @@ void systemSetup() { SPIFFS.begin(); #endif - // Question system stability #if SYSTEM_CHECK_ENABLED - systemCheck(false); + _systemStabilityInit(); #endif #if WEB_SUPPORT diff --git a/code/espurna/system.h b/code/espurna/system.h index c330d394..926c6ab7 100644 --- a/code/espurna/system.h +++ b/code/espurna/system.h @@ -146,7 +146,6 @@ uint32_t systemResetReason(); uint8_t systemStabilityCounter(); void systemStabilityCounter(uint8_t count); -void systemCheck(bool stable); bool systemCheck(); void customResetReason(CustomResetReason reason); diff --git a/code/espurna/terminal.cpp b/code/espurna/terminal.cpp index ea3d87aa..b999c7c4 100644 --- a/code/espurna/terminal.cpp +++ b/code/espurna/terminal.cpp @@ -348,10 +348,11 @@ void _terminalInitCommands() { }); terminalRegisterCommand(F("RESET"), [](const terminal::CommandContext& ctx) { + auto count = 1; if (ctx.argc == 2) { - auto arg = ctx.argv[1].toInt(); + count = ctx.argv[1].toInt(); if (arg < SYSTEM_CHECK_MAX) { - systemStabilityCounter(arg); + systemStabilityCounter(count); } }