From e83c5495def506c65280b1a3e8d4c3c8f933b84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Wed, 22 Nov 2017 16:23:58 +0100 Subject: [PATCH] Unify reset calls --- code/espurna/api.ino | 8 ++------ code/espurna/button.ino | 6 ++---- code/espurna/espurna.ino | 6 +++--- code/espurna/mqtt.ino | 3 +-- code/espurna/ota.ino | 3 +-- code/espurna/settings.ino | 5 ++--- code/espurna/utils.ino | 26 +++++++++++++++++++------- code/espurna/web.ino | 7 +------ code/espurna/ws.ino | 6 +++--- 9 files changed, 34 insertions(+), 36 deletions(-) diff --git a/code/espurna/api.ino b/code/espurna/api.ino index 4dc9e259..6270ea06 100644 --- a/code/espurna/api.ino +++ b/code/espurna/api.ino @@ -11,9 +11,8 @@ Copyright (C) 2016-2017 by Xose Pérez #include #include #include -#include +#include -Ticker _api_defer; typedef struct { char * url; char * key; @@ -144,10 +143,7 @@ void _onRPC(AsyncWebServerRequest *request) { if (action.equals("reset")) { response = 200; - _api_defer.once_ms(100, []() { - customReset(CUSTOM_RESET_RPC); - ESP.restart(); - }); + deferredReset(100, CUSTOM_RESET_RPC); } } diff --git a/code/espurna/button.ino b/code/espurna/button.ino index fc1142b4..4ad25b81 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -104,15 +104,13 @@ void buttonEvent(unsigned int id, unsigned char event) { } if (action == BUTTON_MODE_AP) createAP(); if (action == BUTTON_MODE_RESET) { - customReset(CUSTOM_RESET_HARDWARE); - ESP.restart(); + deferredReset(100, CUSTOM_RESET_HARDWARE); } if (action == BUTTON_MODE_PULSE) relayPulseToggle(); if (action == BUTTON_MODE_FACTORY) { DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n")); settingsFactoryReset(); - customReset(CUSTOM_RESET_FACTORY); - ESP.restart(); + deferredReset(100, CUSTOM_RESET_FACTORY); } } diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 3cca1518..bafea82e 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -200,10 +200,10 @@ void welcome() { // ------------------------------------------------------------------------- - unsigned char custom_reset = customReset(); - if (custom_reset > 0) { + unsigned char reason = resetReason(); + if (reason > 0) { char buffer[32]; - strcpy_P(buffer, custom_reset_string[custom_reset-1]); + strcpy_P(buffer, custom_reset_string[reason-1]); DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), buffer); } else { DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str()); diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 69bddde1..82c25fe3 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -216,8 +216,7 @@ void _mqttCallback(unsigned int type, const char * topic, const char * payload) // Actions if (t.equals(MQTT_TOPIC_ACTION)) { if (strcmp(payload, MQTT_ACTION_RESET) == 0) { - customReset(CUSTOM_RESET_MQTT); - ESP.restart(); + deferredReset(100, CUSTOM_RESET_MQTT); } } diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index 280c3b2f..473ee0cf 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -30,12 +30,11 @@ void otaSetup() { }); ArduinoOTA.onEnd([]() { - customReset(CUSTOM_RESET_OTA); DEBUG_MSG_P(PSTR("\n[OTA] End\n")); #if WEB_SUPPORT wsSend_P(PSTR("{\"action\": \"reload\"}")); #endif - delay(100); + deferredReset(100, CUSTOM_RESET_OTA); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index a008d248..bae004ab 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -147,13 +147,12 @@ void settingsSetup() { Embedis::command( F("RESET"), [](Embedis* e) { DEBUG_MSG_P(PSTR("+OK\n")); - customReset(CUSTOM_RESET_TERMINAL); - ESP.restart(); + deferredReset(100, CUSTOM_RESET_TERMINAL); }); Embedis::command( F("ERASE.CONFIG"), [](Embedis* e) { DEBUG_MSG_P(PSTR("+OK\n")); - customReset(CUSTOM_RESET_TERMINAL); + resetReason(CUSTOM_RESET_TERMINAL); ESP.eraseConfig(); *((int*) 0) = 0; // see https://github.com/esp8266/Arduino/issues/1494 }); diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index acc503e1..a094fb3b 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -6,6 +6,9 @@ Copyright (C) 2017 by Xose Pérez */ +#include +Ticker _defer_reset; + String getIdentifier() { char buffer[20]; snprintf_P(buffer, sizeof(buffer), PSTR("%s_%06X"), DEVICE, ESP.getChipId()); @@ -136,21 +139,30 @@ void heartbeat() { // ----------------------------------------------------------------------------- -void customReset(unsigned char status) { - EEPROM.write(EEPROM_CUSTOM_RESET, status); - EEPROM.commit(); -} - -unsigned char customReset() { +unsigned char resetReason() { static unsigned char status = 255; if (status == 255) { status = EEPROM.read(EEPROM_CUSTOM_RESET); - if (status > 0) customReset(0); + if (status > 0) resetReason(0); if (status > CUSTOM_RESET_MAX) status = 0; } return status; } +void resetReason(unsigned char reason) { + EEPROM.write(EEPROM_CUSTOM_RESET, reason); + EEPROM.commit(); +} + +void reset(unsigned char reason) { + resetReason(reason); + ESP.restart(); +} + +void deferredReset(unsigned long delay, unsigned char reason) { + _defer_reset.once_ms(delay, reset, reason); +} + // ----------------------------------------------------------------------------- #if SYSTEM_CHECK_ENABLED diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 27a56275..e1f56867 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -14,7 +14,6 @@ Copyright (C) 2016-2017 by Xose Pérez #include #include #include -#include #if WEB_EMBEDDED #include "static/index.html.gz.h" @@ -29,7 +28,6 @@ Copyright (C) 2016-2017 by Xose Pérez AsyncWebServer * _server; char _last_modified[50]; -Ticker _web_defer; // ----------------------------------------------------------------------------- // HOOKS @@ -177,10 +175,7 @@ void _onUpgrade(AsyncWebServerRequest *request) { AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", buffer); response->addHeader("Connection", "close"); if (!Update.hasError()) { - _web_defer.once_ms(100, []() { - customReset(CUSTOM_RESET_UPGRADE); - ESP.restart(); - }); + deferredReset(100, CUSTOM_RESET_UPGRADE); } request->send(response); diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 408e1e11..f9cb3c62 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -11,10 +11,11 @@ Copyright (C) 2016-2017 by Xose Pérez #include #include #include +#include #include "ws.h" AsyncWebSocket _ws("/ws"); - +Ticker _web_defer; // ----------------------------------------------------------------------------- // Private methods // ----------------------------------------------------------------------------- @@ -55,8 +56,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { DEBUG_MSG_P(PSTR("[WEBSOCKET] Requested action: %s\n"), action.c_str()); if (action.equals("reset")) { - customReset(CUSTOM_RESET_WEB); - ESP.restart(); + deferredReset(100, CUSTOM_RESET_WEB); } #ifdef ITEAD_SONOFF_RFBRIDGE