- add reverse encoder menu option and save state - improve usage of EncoderDir in ISR with pointer to instance and static isr() method. - reduce u8g2 memory usage by using single page buffer - improve save state behavor by using a mutex flag and update check with debounce in main loop - make saving to EEPROM safer by wrapping put calls with noInterrupts()
120 lines
3.9 KiB
C++
120 lines
3.9 KiB
C++
#include "save_state.h"
|
|
|
|
#include <EEPROM.h>
|
|
|
|
#include "app_state.h"
|
|
|
|
StateManager::StateManager() : _isDirty(false), _lastChangeTime(0) {}
|
|
|
|
bool StateManager::initialize(AppState& app) {
|
|
if (isDataValid()) {
|
|
static EepromData load_data;
|
|
EEPROM.get(sizeof(Metadata), load_data);
|
|
|
|
// Restore main app state
|
|
app.tempo = load_data.tempo;
|
|
app.encoder_reversed = load_data.encoder_reversed;
|
|
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);
|
|
ch.setDutyCycle(saved_ch_state.base_duty_cycle);
|
|
ch.setOffset(saved_ch_state.base_offset);
|
|
ch.setCvSource(static_cast<CvSource>(saved_ch_state.cv_source));
|
|
ch.setCvDestination(static_cast<CvDestination>(saved_ch_state.cv_destination));
|
|
}
|
|
|
|
return true;
|
|
} else {
|
|
reset(app);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void StateManager::save(const AppState& app) {
|
|
// Ensure interrupts do not cause corrupt data writes.
|
|
noInterrupts();
|
|
_save_worker(app);
|
|
interrupts();
|
|
}
|
|
|
|
void StateManager::reset(AppState& app) {
|
|
app.tempo = Clock::DEFAULT_TEMPO;
|
|
app.encoder_reversed = false;
|
|
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();
|
|
}
|
|
|
|
noInterrupts();
|
|
_metadata_worker(); // Write the new metadata
|
|
_save_worker(app); // Write the new (default) app state
|
|
interrupts();
|
|
|
|
_isDirty = false;
|
|
}
|
|
|
|
void StateManager::update(const AppState& app) {
|
|
// Check if a save is pending and if enough time has passed.
|
|
if (_isDirty && (millis() - _lastChangeTime > SAVE_DELAY_MS)) {
|
|
save(app);
|
|
_isDirty = false; // Clear the flag, we are now "clean".
|
|
}
|
|
}
|
|
|
|
void StateManager::markDirty() {
|
|
_isDirty = true;
|
|
_lastChangeTime = millis();
|
|
}
|
|
|
|
bool StateManager::isDataValid() {
|
|
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::_save_worker(const AppState& app) {
|
|
static EepromData save_data;
|
|
|
|
// Populate main app state
|
|
save_data.tempo = app.tempo;
|
|
save_data.encoder_reversed = app.encoder_reversed;
|
|
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& save_ch = save_data.channel_data[i];
|
|
|
|
// Use the getters with 'withCvMod = false' to get the base values
|
|
save_ch.base_clock_mod_index = ch.getClockModIndex(false);
|
|
save_ch.base_probability = ch.getProbability(false);
|
|
save_ch.base_duty_cycle = ch.getDutyCycle(false);
|
|
save_ch.base_offset = ch.getOffset(false);
|
|
save_ch.cv_source = static_cast<byte>(ch.getCvSource());
|
|
save_ch.cv_destination = static_cast<byte>(ch.getCvDestination());
|
|
}
|
|
EEPROM.put(sizeof(Metadata), save_data);
|
|
}
|
|
|
|
void StateManager::_metadata_worker() {
|
|
Metadata currentMeta;
|
|
strcpy(currentMeta.sketchName, CURRENT_SKETCH_NAME);
|
|
currentMeta.version = CURRENT_SKETCH_VERSION;
|
|
EEPROM.put(0, currentMeta);
|
|
}
|