When loading EEPROM, if the metadata is not valid, erase all EEPROM.

This commit is contained in:
2025-07-20 10:39:44 -07:00
parent 385ce85da3
commit 6fb61a85bb
2 changed files with 15 additions and 3 deletions

View File

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

View File

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