Browse Source

Unify reset calls

fastled
Xose Pérez 7 years ago
parent
commit
e83c5495de
9 changed files with 34 additions and 36 deletions
  1. +2
    -6
      code/espurna/api.ino
  2. +2
    -4
      code/espurna/button.ino
  3. +3
    -3
      code/espurna/espurna.ino
  4. +1
    -2
      code/espurna/mqtt.ino
  5. +1
    -2
      code/espurna/ota.ino
  6. +2
    -3
      code/espurna/settings.ino
  7. +19
    -7
      code/espurna/utils.ino
  8. +1
    -6
      code/espurna/web.ino
  9. +3
    -3
      code/espurna/ws.ino

+ 2
- 6
code/espurna/api.ino View File

@ -11,9 +11,8 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <Ticker.h>
#include <vector>
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);
}
}


+ 2
- 4
code/espurna/button.ino View File

@ -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);
}
}


+ 3
- 3
code/espurna/espurna.ino View File

@ -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());


+ 1
- 2
code/espurna/mqtt.ino View File

@ -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);
}
}


+ 1
- 2
code/espurna/ota.ino View File

@ -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) {


+ 2
- 3
code/espurna/settings.ino View File

@ -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
});


+ 19
- 7
code/espurna/utils.ino View File

@ -6,6 +6,9 @@ Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
*/
#include <Ticker.h>
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


+ 1
- 6
code/espurna/web.ino View File

@ -14,7 +14,6 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <FS.h>
#include <AsyncJson.h>
#include <ArduinoJson.h>
#include <Ticker.h>
#if WEB_EMBEDDED
#include "static/index.html.gz.h"
@ -29,7 +28,6 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
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);


+ 3
- 3
code/espurna/ws.ino View File

@ -11,10 +11,11 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <Ticker.h>
#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


Loading…
Cancel
Save