Add the ability to reset state from the main menu. Refactor gravity.h global const definitions to be static and more readable.

This commit is contained in:
2025-06-15 11:23:57 -07:00
parent 696229cfe1
commit 0cef942f2c
8 changed files with 185 additions and 140 deletions

View File

@ -1,23 +1,24 @@
// File: save_state.cpp
#include "save_state.h"
#include <EEPROM.h>
#include "save_state.h"
#include "app_state.h" // Includes AppState and Channel definitions
#include "app_state.h"
bool StateManager::initialize(AppState& app) {
if (isDataValid()) {
EepromData loadedState;
EEPROM.get(sizeof(Metadata), loadedState);
EepromData load_data;
EEPROM.get(sizeof(Metadata), load_data);
// Restore main app state
app.selected_param = loadedState.selected_param;
app.selected_channel = loadedState.selected_channel;
app.selected_source = static_cast<Source>(loadedState.selected_source);
// --- NEW: Loop through and restore each channel's state ---
for (int i = 0; i < OUTPUT_COUNT; i++) {
auto& ch = app.channel[i]; // Get a reference to the channel object
const auto& saved_ch_state = loadedState.channel_data[i]; // Get a const reference to the saved data
app.tempo = load_data.tempo;
app.selected_param = load_data.selected_param;
app.selected_channel = load_data.selected_channel;
app.selected_source = static_cast<Clock::Source>(load_data.selected_source);
// Loop through and restore each channel's state.
for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) {
auto& ch = app.channel[i];
const auto& saved_ch_state = load_data.channel_data[i];
ch.setClockMod(saved_ch_state.base_clock_mod_index);
ch.setProbability(saved_ch_state.base_probability);
@ -26,27 +27,28 @@ bool StateManager::initialize(AppState& app) {
ch.setCvSource(static_cast<CvSource>(saved_ch_state.cv_source));
ch.setCvDestination(static_cast<CvDestination>(saved_ch_state.cv_destination));
}
return true;
} else {
writeMetadata();
save(app); // Save the initial default state
save(app); // Save the initial default state
return false;
}
}
void StateManager::save(const AppState& app) {
EepromData stateToSave;
// Populate main app state
stateToSave.selected_param = app.selected_param;
stateToSave.selected_channel = app.selected_channel;
stateToSave.selected_source = static_cast<byte>(app.selected_source);
EepromData save_data;
// --- NEW: Loop through and populate each channel's state ---
for (int i = 0; i < OUTPUT_COUNT; i++) {
const auto& ch = app.channel[i]; // Get a const reference to the channel object
auto& saved_ch_state = stateToSave.channel_data[i]; // Get a reference to the struct we're saving to
// Populate main app state
save_data.tempo = app.tempo;
save_data.selected_param = app.selected_param;
save_data.selected_channel = app.selected_channel;
save_data.selected_source = static_cast<byte>(app.selected_source);
// Loop through and populate each channel's state
for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) {
const auto& ch = app.channel[i];
auto& saved_ch_state = save_data.channel_data[i];
// Use the getters with 'withCvMod = false' to get the base values
saved_ch_state.base_clock_mod_index = ch.getClockModIndex(false);
@ -58,28 +60,34 @@ void StateManager::save(const AppState& app) {
}
// Write the entire state struct to EEPROM
EEPROM.put(sizeof(Metadata), stateToSave);
EEPROM.put(sizeof(Metadata), save_data);
}
void StateManager::reset(AppState& app) {
AppState defaultState;
app = defaultState;
app.tempo = Clock::DEFAULT_TEMPO;
app.selected_param = 0;
app.selected_channel = 0;
app.selected_source = Clock::SOURCE_INTERNAL;
for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) {
app.channel[i].Init();
}
writeMetadata();
save(app);
}
// isDataValid() and writeMetadata() remain unchanged
bool StateManager::isDataValid() {
Metadata storedMeta;
EEPROM.get(0, storedMeta);
bool nameMatch = (strcmp(storedMeta.sketchName, CURRENT_SKETCH_NAME) == 0);
bool versionMatch = (storedMeta.version == CURRENT_SKETCH_VERSION);
Metadata load_meta;
EEPROM.get(0, load_meta);
bool nameMatch = (strcmp(load_meta.sketchName, CURRENT_SKETCH_NAME) == 0);
bool versionMatch = (load_meta.version == CURRENT_SKETCH_VERSION);
return nameMatch && versionMatch;
}
void StateManager::writeMetadata() {
Metadata currentMeta;
strcpy(currentMeta.sketchName, CURRENT_SKETCH_NAME);
currentMeta.version = CURRENT_SKETCH_VERSION;
EEPROM.put(0, currentMeta);
Metadata save_meta;
strcpy(save_meta.sketchName, CURRENT_SKETCH_NAME);
save_meta.version = CURRENT_SKETCH_VERSION;
EEPROM.put(0, save_meta);
}