Using EEPROMHelper to check the boot number

This commit is contained in:
Stefan Kremser
2019-05-14 08:34:35 +02:00
parent 92eb4f2b14
commit c8a9ad50e9

View File

@@ -11,13 +11,14 @@ extern "C" {
// And be sure to have the right board selected // And be sure to have the right board selected
#include "user_interface.h" #include "user_interface.h"
} }
#include <EEPROM.h>
#include "EEPROMHelper.h"
#include <ArduinoJson.h> #include <ArduinoJson.h>
#if ARDUINOJSON_VERSION_MAJOR != 5 #if ARDUINOJSON_VERSION_MAJOR != 5
// The software was build using ArduinoJson v5.x // The software was build using ArduinoJson v5.x
// version 6 is still in beta at the time of writing // version 6 is still in beta at the time of writing
// go to tools -> manage libraries, search for ArduinoJSON and install the latest version 5 // go to tools -> manage libraries, search for ArduinoJSON and install version 5
#error Please upgrade/downgrade ArduinoJSON library to version 5! #error Please upgrade/downgrade ArduinoJSON library to version 5!
#endif // if ARDUINOJSON_VERSION_MAJOR != 5 #endif // if ARDUINOJSON_VERSION_MAJOR != 5
@@ -65,21 +66,18 @@ void setup() {
// start SPIFFS // start SPIFFS
prnt(SETUP_MOUNT_SPIFFS); prnt(SETUP_MOUNT_SPIFFS);
prntln(SPIFFS.begin() ? SETUP_OK : SETUP_ERROR); bool spiffsError = !SPIFFS.begin();
prntln(spiffsError ? SETUP_ERROR : SETUP_OK);
// Start EEPROM // Start EEPROM
EEPROM.begin(4096); EEPROMHelper::begin(EEPROM_SIZE);
// auto repair when in boot-loop // Format SPIFFS when in boot-loop
uint8_t bootCounter = EEPROM.read(0); if (spiffsError || !EEPROMHelper::checkBootNum(BOOT_COUNTER_ADDR)) {
if (bootCounter >= 3) {
prnt(SETUP_FORMAT_SPIFFS); prnt(SETUP_FORMAT_SPIFFS);
SPIFFS.format(); SPIFFS.format();
prntln(SETUP_OK); prntln(SETUP_OK);
} else { EEPROMHelper::resetBootNum(BOOT_COUNTER_ADDR);
EEPROM.write(0, bootCounter + 1); // add 1 to the boot counter
EEPROM.commit();
} }
// get time // get time
@@ -169,10 +167,8 @@ void loop() {
} }
if (!booted) { if (!booted) {
// reset boot counter
EEPROM.write(0, 0);
EEPROM.commit();
booted = true; booted = true;
EEPROMHelper::resetBootNum(BOOT_COUNTER_ADDR);
#ifdef HIGHLIGHT_LED #ifdef HIGHLIGHT_LED
displayUI.setupLED(); displayUI.setupLED();
#endif // ifdef HIGHLIGHT_LED #endif // ifdef HIGHLIGHT_LED