From 6fb61a85bb9504a5f2fed774884f9bea7aceb123 Mon Sep 17 00:00:00 2001 From: Adam Wonak Date: Sun, 20 Jul 2025 10:39:44 -0700 Subject: [PATCH] When loading EEPROM, if the metadata is not valid, erase all EEPROM. --- firmware/Gravity/save_state.cpp | 12 +++++++++++- firmware/Gravity/save_state.h | 6 ++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/firmware/Gravity/save_state.cpp b/firmware/Gravity/save_state.cpp index 052779b..16ef342 100644 --- a/firmware/Gravity/save_state.cpp +++ b/firmware/Gravity/save_state.cpp @@ -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); diff --git a/firmware/Gravity/save_state.h b/firmware/Gravity/save_state.h index 354253a..29ae845 100644 --- a/firmware/Gravity/save_state.h +++ b/firmware/Gravity/save_state.h @@ -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 {