13 Commits

Author SHA1 Message Date
527889c1c2 fix factory reset and load state metadata. 2025-07-22 20:29:02 -07:00
ba1a47bfc7 align bootsplash more centered. 2025-07-22 20:27:16 -07:00
d34acd4477 Merge branch 'main' of https://git.pinkduck.xyz/adam/libGravity into bootsplash-version 2025-07-22 09:09:29 -07:00
c5bddef66d Show loading bootsplash with firmware name and version (#18)
Bootsplash is displayed before EEPROM erase, which is a slow operation.

Reviewed-on: https://git.pinkduck.xyz/awonak/libGravity/pulls/18
2025-07-22 05:16:32 +00:00
d66839b12f Merge branch 'main' of https://git.pinkduck.xyz/adam/libGravity into bootsplash-version 2025-07-21 22:13:11 -07:00
b0accdc83a Fix Initial Transient State (#17)
There was an off-by-one error that was not properly loading transient state from the designated memory slot. Also fixes setting the last saved/loaded slot indicator with metadata.

Reviewed-on: https://git.pinkduck.xyz/awonak/libGravity/pulls/17
2025-07-22 05:12:45 +00:00
1ae9f5b545 Display bootsplash with firmware name and version. 2025-07-21 22:11:23 -07:00
1155978e51 Move save slot to metadata to persist when loading transient. 2025-07-21 21:00:00 -07:00
d30e9e2f85 Fix initial transient state 2025-07-21 20:41:47 -07:00
1c0fb86bc1 Reverse the order of clock mod options. (#16)
This now matches original Gravity behavior. Also, now when applying CV mod positive voltages increase clock mod instead of reducing it.

Also fix pulse out, which wasn't previously updated when CLOCK_MOD was moved to program mem.

Reviewed-on: https://git.pinkduck.xyz/awonak/libGravity/pulls/16
2025-07-22 00:00:49 +00:00
4f04137f67 Add global/hardware settings to metadata EEPROM (#15)
Settings like Encoder Direction and Display Orientation should persist when resetting channel state.

Fixes https://github.com/awonak/alt-gravity/issues/7

Reviewed-on: https://git.pinkduck.xyz/awonak/libGravity/pulls/15
2025-07-21 00:27:32 +00:00
1bf90e1674 Mute channel when shift + play pressed (#14)
Fixes https://github.com/awonak/alt-gravity/issues/2

Reviewed-on: https://git.pinkduck.xyz/awonak/libGravity/pulls/14
2025-07-21 00:01:18 +00:00
5729eef037 Factory Reset (#13)
Fixes https://github.com/awonak/alt-gravity/issues/1

Reviewed-on: https://git.pinkduck.xyz/awonak/libGravity/pulls/13
2025-07-21 00:00:47 +00:00
7 changed files with 209 additions and 93 deletions

View File

@ -50,9 +50,6 @@ class Clock {
void Init() {
NeoSerial.begin(31250);
// Static pin definition for pulse out.
pinMode(PULSE_OUT_PIN, OUTPUT);
// Initialize the clock library
uClock.init();
uClock.setClockMode(uClock.INTERNAL_CLOCK);

View File

@ -2,7 +2,7 @@
* @file Gravity.ino
* @author Adam Wonak (https://github.com/awonak/)
* @brief Alt firmware version of Gravity by Sitka Instruments.
* @version v2.0.1 - June 2025 awonak - Full rewrite
* @version v2.0.1 - June 2025 awonak - Full rewrite
* @version v1.0 - August 2023 Oleksiy H - Initial release
* @date 2025-07-04
*
@ -25,7 +25,7 @@
* quantization of features like duty cycle (pulse width) or offset.
* Additionally, this firmware replaces the sequencer with a Euclidean Rhythm
* generator.
*
*
* ENCODER:
* Press: change between selecting a parameter and editing the parameter.
* Hold & Rotate: change current selected output channel.
@ -33,17 +33,17 @@
* BTN1:
* Play/pause - start or stop the internal clock.
*
* BTN2:
* BTN2:
* Shift - hold and rotate encoder to change current selected output channel.
*
* EXT:
* External clock input. When Gravity is set to INTERNAL clock mode, this
* input is used to reset clocks.
*
*
* CV1:
* CV2:
* External analog input used to provide modulation to any channel parameter.
*
*
*/
#include <gravity.h>
@ -64,6 +64,10 @@ void setup() {
// Start Gravity.
gravity.Init();
// Show bootsplash when initializing firmware.
Bootsplash();
delay(2000);
// Initialize the state manager. This will load settings from EEPROM
stateManager.initialize(app);
InitGravity(app);
@ -135,13 +139,12 @@ void HandleIntClockTick(uint32_t tick) {
break;
}
const uint32_t pulse_high_ticks = CLOCK_MOD_PULSES[clock_index];
const uint16_t pulse_high_ticks = pgm_read_word_near(&CLOCK_MOD_PULSES[clock_index]);
const uint32_t pulse_low_ticks = tick + max((pulse_high_ticks / 2), 1L);
if (tick % pulse_high_ticks == 0) {
gravity.pulse.High();
}
if (pulse_low_ticks % pulse_high_ticks == 0) {
} else if (pulse_low_ticks % pulse_high_ticks == 0) {
gravity.pulse.Low();
}
}
@ -168,6 +171,21 @@ void HandleExtClockTick() {
//
void HandlePlayPressed() {
// Check if SHIFT is pressed to mute all/current channel.
if (gravity.shift_button.On()) {
if (app.selected_channel == 0) {
// Mute all channels
for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) {
app.channel[i].toggleMute();
}
} else {
// Mute selected channel
auto& ch = GetSelectedChannel();
ch.toggleMute();
}
return;
}
gravity.clock.IsPaused()
? gravity.clock.Start()
: gravity.clock.Stop();
@ -181,8 +199,8 @@ void HandleEncoderPressed() {
if (app.selected_channel == 0) { // main page
// TODO: rewrite as switch
if (app.selected_param == PARAM_MAIN_ENCODER_DIR) {
bool reversed = app.selected_sub_param == 1;
gravity.encoder.SetReverseDirection(reversed);
app.encoder_reversed = app.selected_sub_param == 1;
gravity.encoder.SetReverseDirection(app.encoder_reversed);
}
if (app.selected_param == PARAM_MAIN_SAVE_DATA) {
if (app.selected_sub_param < MAX_SAVE_SLOTS) {
@ -203,6 +221,13 @@ void HandleEncoderPressed() {
InitGravity(app);
}
}
if (app.selected_param == PARAM_MAIN_FACTORY_RESET) {
if (app.selected_sub_param == 0) { // Erase
Bootsplash();
stateManager.factoryReset(app);
InitGravity(app);
}
}
}
// Only mark dirty and reset selected_sub_param when leaving editing mode.
stateManager.markDirty();
@ -277,6 +302,9 @@ void editMainParameter(int val) {
case PARAM_MAIN_RESET_STATE:
updateSelection(app.selected_sub_param, val, 2);
break;
case PARAM_MAIN_FACTORY_RESET:
updateSelection(app.selected_sub_param, val, 2);
break;
}
}

View File

@ -38,28 +38,4 @@ static Channel& GetSelectedChannel() {
return app.channel[app.selected_channel - 1];
}
enum ParamsMainPage : uint8_t {
PARAM_MAIN_TEMPO,
PARAM_MAIN_SOURCE,
PARAM_MAIN_PULSE,
PARAM_MAIN_ENCODER_DIR,
PARAM_MAIN_SAVE_DATA,
PARAM_MAIN_LOAD_DATA,
PARAM_MAIN_RESET_STATE,
PARAM_MAIN_LAST,
};
enum ParamsChannelPage : uint8_t {
PARAM_CH_MOD,
PARAM_CH_PROB,
PARAM_CH_DUTY,
PARAM_CH_OFFSET,
PARAM_CH_SWING,
PARAM_CH_EUC_STEPS,
PARAM_CH_EUC_HITS,
PARAM_CH_CV1_DEST,
PARAM_CH_CV2_DEST,
PARAM_CH_LAST,
};
#endif // APP_STATE_H

View File

@ -34,28 +34,28 @@ static const byte MOD_CHOICE_SIZE = 25;
// Negative numbers are multipliers, positive are divisors.
static const int CLOCK_MOD[MOD_CHOICE_SIZE] PROGMEM = {
// Multipliers
-24, -16, -12, -8, -6, -4, -3, -2,
// Internal Clock Unity
1,
// Divisors
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 24, 32, 64, 128};
128, 64, 32, 24, 16, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,
// Internal Clock Unity (quarter note)
1,
// Multipliers
-2, -3, -4, -6, -8, -12, -16, -24};
// This represents the number of clock pulses for a 96 PPQN clock source
// that match the above div/mult mods.
static const int CLOCK_MOD_PULSES[MOD_CHOICE_SIZE] PROGMEM = {
// Multiplier Pulses (96 / X)
4, 6, 8, 12, 16, 24, 32, 48,
// Divisor Pulses (96 * X)
12288, 6144, 3072, 2304, 1536, 1152, 1056, 960, 864, 768, 672, 576, 480, 384, 288, 192,
// Internal Clock Pulses
96,
// Divisor Pulses (96 * X)
192, 288, 384, 480, 576, 672, 768, 864, 960, 1056, 1152, 1536, 2304, 3072, 6144, 12288};
// Multiplier Pulses (96 / X)
48, 32, 24, 16, 12, 8, 6, 4};
static const byte DEFAULT_CLOCK_MOD_INDEX = 8; // x1 or 96 PPQN.
static const byte DEFAULT_CLOCK_MOD_INDEX = 16; // x1 or 96 PPQN.
static const byte PULSE_PPQN_24_CLOCK_MOD_INDEX = 0;
static const byte PULSE_PPQN_4_CLOCK_MOD_INDEX = 4;
static const byte PULSE_PPQN_1_CLOCK_MOD_INDEX = 8;
static const byte PULSE_PPQN_24_CLOCK_MOD_INDEX = MOD_CHOICE_SIZE - 1;
static const byte PULSE_PPQN_4_CLOCK_MOD_INDEX = MOD_CHOICE_SIZE - 6;
static const byte PULSE_PPQN_1_CLOCK_MOD_INDEX = MOD_CHOICE_SIZE - 9;
class Channel {
public:
@ -161,6 +161,8 @@ class Channel {
byte getSteps(bool withCvMod = false) const { return withCvMod ? pattern.GetSteps() : base_euc_steps; }
byte getHits(bool withCvMod = false) const { return withCvMod ? pattern.GetHits() : base_euc_hits; }
void toggleMute() { mute = !mute; }
/**
* @brief Processes a clock tick and determines if the output should be high or low.
* Note: this method is called from an ISR and must be kept as simple as possible.
@ -168,6 +170,12 @@ class Channel {
* @param output The output object to be modified.
*/
void processClockTick(uint32_t tick, DigitalOutput& output) {
// Mute check
if (mute) {
output.Low();
return;
}
const uint16_t mod_pulses = pgm_read_word_near(&CLOCK_MOD_PULSES[cvmod_clock_mod_index]);
// Conditionally apply swing on down beats.
@ -298,6 +306,9 @@ class Channel {
// Euclidean pattern
Pattern pattern;
// Mute channel flag
bool mute;
// Pre-calculated pulse values for ISR performance
uint16_t _duty_pulses;
uint16_t _offset_pulses;

View File

@ -96,6 +96,33 @@ constexpr uint8_t CHANNEL_BOXES_Y = 50;
constexpr uint8_t CHANNEL_BOX_WIDTH = 18;
constexpr uint8_t CHANNEL_BOX_HEIGHT = 14;
// Menu items for editing global parameters.
enum ParamsMainPage : uint8_t {
PARAM_MAIN_TEMPO,
PARAM_MAIN_SOURCE,
PARAM_MAIN_PULSE,
PARAM_MAIN_ENCODER_DIR,
PARAM_MAIN_SAVE_DATA,
PARAM_MAIN_LOAD_DATA,
PARAM_MAIN_RESET_STATE,
PARAM_MAIN_FACTORY_RESET,
PARAM_MAIN_LAST,
};
// Menu items for editing channel parameters.
enum ParamsChannelPage : uint8_t {
PARAM_CH_MOD,
PARAM_CH_PROB,
PARAM_CH_DUTY,
PARAM_CH_OFFSET,
PARAM_CH_SWING,
PARAM_CH_EUC_STEPS,
PARAM_CH_EUC_HITS,
PARAM_CH_CV1_DEST,
PARAM_CH_CV2_DEST,
PARAM_CH_LAST,
};
// Helper function to draw centered text
void drawCenteredText(const char* text, int y, const uint8_t* font) {
gravity.display.setFont(font);
@ -278,13 +305,23 @@ void DisplayMainPage() {
mainText = F("x");
subText = F("BACK TO MAIN");
}
break;
case PARAM_MAIN_FACTORY_RESET:
if (app.selected_sub_param == 0) {
mainText = F("DEL");
subText = F("FACTORY RESET");
} else {
mainText = F("x");
subText = F("BACK TO MAIN");
}
break;
}
drawCenteredText(mainText.c_str(), MAIN_TEXT_Y, LARGE_FONT);
drawCenteredText(subText.c_str(), SUB_TEXT_Y, TEXT_FONT);
// Draw Main Page menu items
String menu_items[PARAM_MAIN_LAST] = {F("TEMPO"), F("SOURCE"), F("PULSE OUT"), F("ENCODER DIR"), F("SAVE"), F("LOAD"), F("RESET")};
String menu_items[PARAM_MAIN_LAST] = {F("TEMPO"), F("SOURCE"), F("PULSE OUT"), F("ENCODER DIR"), F("SAVE"), F("LOAD"), F("RESET"), F("ERASE")};
drawMenuItems(menu_items, PARAM_MAIN_LAST);
}
@ -432,4 +469,21 @@ void UpdateDisplay() {
} while (gravity.display.nextPage());
}
void Bootsplash() {
gravity.display.firstPage();
do {
int textWidth;
gravity.display.setFont(TEXT_FONT);
textWidth = gravity.display.getStrWidth(SKETCH_NAME);
gravity.display.drawStr(16 + (textWidth / 2), 20, SKETCH_NAME);
textWidth = gravity.display.getStrWidth(SEMANTIC_VERSION);
gravity.display.drawStr(16 + (textWidth / 2), 32, SEMANTIC_VERSION);
textWidth = gravity.display.getStrWidth("LOADING....");
gravity.display.drawStr(26 + (textWidth / 2), 44, "LOADING....");
} while (gravity.display.nextPage());
}
#endif // DISPLAY_H

View File

@ -16,64 +16,74 @@
#include "app_state.h"
// Calculate the starting address for EepromData, leaving space for metadata.
static const int METADATA_START_ADDR = 0;
static const int EEPROM_DATA_START_ADDR = sizeof(StateManager::Metadata);
StateManager::StateManager() : _isDirty(false), _lastChangeTime(0) {}
bool StateManager::initialize(AppState& app) {
if (_isDataValid()) {
// Load data from the transient slot.
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.
reset(app);
_saveMetadata();
// MAX_SAVE_SLOTS slot is reserved for transient state.
for (int i = 0; i <= MAX_SAVE_SLOTS; i++) {
app.selected_save_slot = i;
_saveState(app, i);
}
// Load global settings.
_loadMetadata(app);
// Load app data from the transient slot.
_loadState(app, TRANSIENT_SLOT);
return true;
}
// EEPROM does not contain save data for this firmware & version.
else {
// Erase EEPROM and initialize state. Save default pattern to all save slots.
factoryReset(app);
return false;
}
}
bool StateManager::loadData(AppState& app, byte slot_index) {
if (slot_index >= MAX_SAVE_SLOTS) return false;
// Check if slot_index is within max range + 1 for transient.
if (slot_index >= MAX_SAVE_SLOTS + 1) return false;
// Load the state data from the specified EEPROM slot and update the app state save slot.
_loadState(app, slot_index);
app.selected_save_slot = slot_index;
// Persist this change in the global metadata.
_saveMetadata(app);
return true;
}
// Save app state to user specified save slot.
void StateManager::saveData(const AppState& app) {
if (app.selected_save_slot >= MAX_SAVE_SLOTS) return;
// Check if slot_index is within max range + 1 for transient.
if (app.selected_save_slot >= MAX_SAVE_SLOTS + 1) return;
_saveState(app, app.selected_save_slot);
_saveMetadata(app);
_isDirty = false;
}
// Save transient state if it has changed and enough time has passed since last save.
void StateManager::update(const AppState& app) {
if (_isDirty && (millis() - _lastChangeTime > SAVE_DELAY_MS)) {
// MAX_SAVE_SLOTS slot is reserved for transient state.
_saveState(app, MAX_SAVE_SLOTS);
_saveState(app, TRANSIENT_SLOT);
_saveMetadata(app);
_isDirty = false;
}
}
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;
app.selected_pulse = Clock::PULSE_PPQN_24;
app.selected_save_slot = 0;
AppState default_app;
app.tempo = default_app.tempo;
app.selected_param = default_app.selected_param;
app.selected_channel = default_app.selected_channel;
app.selected_source = default_app.selected_source;
app.selected_pulse = default_app.selected_pulse;
for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) {
app.channel[i].Init();
}
// Load global settings from Metadata
_loadMetadata(app);
_isDirty = false;
}
@ -82,28 +92,48 @@ void StateManager::markDirty() {
_lastChangeTime = millis();
}
// Erases all data in the EEPROM by writing 0 to every address.
void StateManager::factoryReset(AppState& app) {
noInterrupts();
for (unsigned int i = 0; i < EEPROM.length(); i++) {
EEPROM.write(i, 0);
}
// Initialize eeprom and save default patter to all save slots.
_saveMetadata(app);
reset(app);
for (int i = 0; i < MAX_SAVE_SLOTS; i++) {
app.selected_save_slot = i;
_saveState(app, i);
}
_saveState(app, TRANSIENT_SLOT);
interrupts();
}
bool StateManager::_isDataValid() {
Metadata load_meta;
EEPROM.get(0, load_meta);
bool name_match = (strcmp(load_meta.sketch_name, SKETCH_NAME) == 0);
bool version_match = (load_meta.version == SKETCH_VERSION);
Metadata metadata;
EEPROM.get(METADATA_START_ADDR, metadata);
bool name_match = (strcmp(metadata.sketch_name, SKETCH_NAME) == 0);
bool version_match = (strcmp(metadata.version, SEMANTIC_VERSION) == 0);
return name_match && version_match;
}
void StateManager::_saveState(const AppState& app, byte slot_index) {
if (app.selected_save_slot >= MAX_SAVE_SLOTS) return;
// Check if slot_index is within max range + 1 for transient.
if (app.selected_save_slot >= MAX_SAVE_SLOTS + 1) return;
noInterrupts();
static EepromData save_data;
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);
save_data.selected_pulse = static_cast<byte>(app.selected_pulse);
save_data.selected_save_slot = app.selected_save_slot;
// TODO: break this out into a separate function. Save State should be
// broken out into global / per-channel save methods. When saving via
// "update" only save state for the current channel since other channels
// will not have changed when saving user edits.
for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) {
const auto& ch = app.channel[i];
auto& save_ch = save_data.channel_data[i];
@ -124,6 +154,9 @@ void StateManager::_saveState(const AppState& app, byte slot_index) {
}
void StateManager::_loadState(AppState& app, byte slot_index) {
// Check if slot_index is within max range + 1 for transient.
if (slot_index >= MAX_SAVE_SLOTS + 1) return;
noInterrupts();
static EepromData load_data;
int address = EEPROM_DATA_START_ADDR + (slot_index * sizeof(EepromData));
@ -131,12 +164,10 @@ void StateManager::_loadState(AppState& app, byte slot_index) {
// Restore app state from loaded data.
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);
app.selected_pulse = static_cast<Clock::Pulse>(load_data.selected_pulse);
app.selected_save_slot = slot_index;
for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) {
auto& ch = app.channel[i];
@ -155,11 +186,25 @@ void StateManager::_loadState(AppState& app, byte slot_index) {
interrupts();
}
void StateManager::_saveMetadata() {
void StateManager::_saveMetadata(const AppState& app) {
noInterrupts();
Metadata current_meta;
strcpy(current_meta.sketch_name, SKETCH_NAME);
current_meta.version = SKETCH_VERSION;
EEPROM.put(0, current_meta);
strcpy(current_meta.version, SEMANTIC_VERSION);
// Global user settings
current_meta.selected_save_slot = app.selected_save_slot;
current_meta.encoder_reversed = app.encoder_reversed;
EEPROM.put(METADATA_START_ADDR, current_meta);
interrupts();
}
void StateManager::_loadMetadata(AppState& app) {
noInterrupts();
Metadata metadata;
EEPROM.get(METADATA_START_ADDR, metadata);
app.selected_save_slot = metadata.selected_save_slot;
app.encoder_reversed = metadata.encoder_reversed;
interrupts();
}

View File

@ -19,11 +19,12 @@
struct AppState;
// Define the constants for the current firmware.
const char SKETCH_NAME[] = "Gravity";
const byte SKETCH_VERSION = 7;
const char SKETCH_NAME[] = "ALT GRAVITY";
const char SEMANTIC_VERSION[] = "V2.0.0BETA2";
// Number of available save slots.
const byte MAX_SAVE_SLOTS = 10;
const byte MAX_SAVE_SLOTS = 10; // Count of save slots 0 - 9 to save/load presets.
const byte TRANSIENT_SLOT = 10; // Transient slot index to persist state when powered off.
// Define the minimum amount of time between EEPROM writes.
static const unsigned long SAVE_DELAY_MS = 2000;
@ -52,11 +53,16 @@ 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(AppState& app);
// This struct holds the data that identifies the firmware version.
struct Metadata {
byte version;
char sketch_name[16];
char version[16];
// Additional global/hardware settings
byte selected_save_slot;
bool encoder_reversed;
};
struct ChannelState {
byte base_clock_mod_index;
@ -72,18 +78,17 @@ class StateManager {
// 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;
byte selected_save_slot;
ChannelState channel_data[Gravity::OUTPUT_COUNT];
};
private:
bool _isDataValid();
void _saveMetadata();
void _saveMetadata(const AppState& app);
void _loadMetadata(AppState& app);
void _saveState(const AppState& app, byte slot_index);
void _loadState(AppState& app, byte slot_index);