When loading EEPROM, if the metadata is not valid, erase all EEPROM.
This commit is contained in:
@ -26,7 +26,8 @@ bool StateManager::initialize(AppState& app) {
|
||||
return loadData(app, MAX_SAVE_SLOTS);
|
||||
} else {
|
||||
// EEPROM does not contain save data for this firmware & version.
|
||||
// Initialize eeprom and save default patter to all save slots.
|
||||
// Erase EEPROM and initialize state. Save default pattern to all save slots.
|
||||
factoryReset();
|
||||
reset(app);
|
||||
_saveMetadata();
|
||||
// MAX_SAVE_SLOTS slot is reserved for transient state.
|
||||
@ -82,6 +83,15 @@ void StateManager::markDirty() {
|
||||
_lastChangeTime = millis();
|
||||
}
|
||||
|
||||
// Erases all data in the EEPROM by writing 0 to every address.
|
||||
void StateManager::factoryReset() {
|
||||
noInterrupts();
|
||||
for (unsigned int i = 0 ; i < EEPROM.length() ; i++) {
|
||||
EEPROM.write(i, 0);
|
||||
}
|
||||
interrupts();
|
||||
}
|
||||
|
||||
bool StateManager::_isDataValid() {
|
||||
Metadata load_meta;
|
||||
EEPROM.get(0, load_meta);
|
||||
|
||||
@ -19,8 +19,8 @@
|
||||
struct AppState;
|
||||
|
||||
// Define the constants for the current firmware.
|
||||
const char SKETCH_NAME[] = "Gravity";
|
||||
const byte SKETCH_VERSION = 7;
|
||||
const char SKETCH_NAME[] = "AltGravity";
|
||||
const byte SKETCH_VERSION = 1;
|
||||
|
||||
// Number of available save slots.
|
||||
const byte MAX_SAVE_SLOTS = 10;
|
||||
@ -52,6 +52,8 @@ class StateManager {
|
||||
void update(const AppState& app);
|
||||
// Indicate that state has changed and we should save.
|
||||
void markDirty();
|
||||
// Erase all data stored in the EEPROM.
|
||||
void factoryReset();
|
||||
|
||||
// This struct holds the data that identifies the firmware version.
|
||||
struct Metadata {
|
||||
|
||||
Reference in New Issue
Block a user