Files
libGravity/examples/Gravity/save_state.h
Adam Wonak dd1228be00 Vendorize uClock (#10)
Add copy of uClock to the repo including memory optimization changes.

Also add user config setting for changing Pulse Out resolution.

Reviewed-on: https://git.pinkduck.xyz/adam/libGravity/pulls/10
Co-authored-by: Adam Wonak <adam.wonak@gmail.com>
Co-committed-by: Adam Wonak <adam.wonak@gmail.com>
2025-07-02 02:45:39 +00:00

70 lines
2.0 KiB
C++

#ifndef SAVE_STATE_H
#define SAVE_STATE_H
#include <Arduino.h>
#include <gravity.h>
// Forward-declare AppState to avoid circular dependencies.
struct AppState;
// Define the constants for the current firmware.
const char SKETCH_NAME[] = "Gravity";
const byte SKETCH_VERSION = 5;
// Define the minimum amount of time between EEPROM writes.
static const unsigned long SAVE_DELAY_MS = 2000;
/**
* @brief Manages saving and loading of the application state to and from EEPROM.
*/
class StateManager {
public:
StateManager();
// Populate the AppState instance with values from EEPROM if they exist.
bool initialize(AppState& app);
// Reset AppState instance back to default values.
void reset(AppState& app);
// Call from main loop, check if state has changed and needs to be saved.
void update(const AppState& app);
// Indicate that state has changed and we should save.
void markDirty();
private:
// This struct holds the data that identifies the firmware version.
struct Metadata {
char sketch_name[16];
byte version;
};
struct ChannelState {
byte base_clock_mod_index;
byte base_probability;
byte base_duty_cycle;
byte base_offset;
byte base_shuffle;
byte cv_source; // Cast the CvSource enum to a byte for storage
byte cv_destination; // Cast the CvDestination enum as a byte for storage
byte euc_steps;
byte euc_hits;
};
// This struct holds all the parameters we want to save.
struct EepromData {
int tempo;
bool encoder_reversed;
byte selected_param;
byte selected_channel;
byte selected_source;
byte selected_pulse;
ChannelState channel_data[Gravity::OUTPUT_COUNT];
};
void _save(const AppState& app);
bool _isDataValid();
void _saveState(const AppState& app);
void _saveMetadata();
bool _isDirty;
unsigned long _lastChangeTime;
};
#endif // SAVE_STATE_H