7 Commits

Author SHA1 Message Date
dd7217d04e Fix euclidean hit mod 2025-07-24 18:27:24 -07:00
d1c8ee16a4 EXT will reset clocks in MIDI clock mode.
Add reset behavior for EXT clock input when MIDI clock source is selected.

Fixes: https://git.pinkduck.xyz/awonak/libGravity/issues/22
2025-07-24 08:35:05 -07:00
65dde4d62e Reorganization of library structure to better match Arduino spec (#20)
Note, this will also require to you "uninstall and reinstall" the Arduino library due to the library file location changes.

Reviewed-on: https://git.pinkduck.xyz/awonak/libGravity/pulls/20
2025-07-24 15:07:15 +00:00
c7a3277b5f Memory improvements in bootsplash and StateManager 2025-07-24 07:53:41 -07:00
fb44601707 Merge branch 'main' of https://git.pinkduck.xyz/awonak/libGravity 2025-07-23 18:08:10 -07:00
ec34bc3a7b Fix metadata loading issues with Initialization and refactor Factory Reset. (#19)
Reviewed-on: https://git.pinkduck.xyz/awonak/libGravity/pulls/19
2025-07-23 03:32:16 +00:00
01f32407f6 bump version 2025-07-20 17:53:03 -07:00
20 changed files with 111 additions and 261 deletions

View File

@ -17,7 +17,7 @@
*
*/
#include "gravity.h"
#include <libGravity.h>
// Firmware state variables.
struct Channel {

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.0 - June 2025 awonak - Full rewrite
* @version v1.0 - August 2023 Oleksiy H - Initial release
* @date 2025-07-04
*
@ -37,16 +37,18 @@
* 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.
* External clock input. When Gravity is set to INTERNAL or MIDI clock
* source, this input is used to reset clocks.
*
* CV1:
* External analog input used to provide modulation to any channel parameter.
*
* CV2:
* External analog input used to provide modulation to any channel parameter.
*
*/
#include <gravity.h>
#include <libGravity.h>
#include "app_state.h"
#include "channel.h"
@ -155,13 +157,16 @@ void HandleIntClockTick(uint32_t tick) {
}
void HandleExtClockTick() {
if (gravity.clock.InternalSource()) {
// Use EXT as Reset when internally clocked.
ResetOutputs();
gravity.clock.Reset();
} else {
// Register clock tick.
gravity.clock.Tick();
switch (app.selected_source) {
case Clock::SOURCE_INTERNAL:
case Clock::SOURCE_EXTERNAL_MIDI:
// Use EXT as Reset when not used for clock source.
ResetOutputs();
gravity.clock.Reset();
break;
default:
// Register EXT cv clock tick.
gravity.clock.Tick();
}
app.refresh_screen = true;
}
@ -203,13 +208,13 @@ void HandleEncoderPressed() {
gravity.encoder.SetReverseDirection(app.encoder_reversed);
}
if (app.selected_param == PARAM_MAIN_SAVE_DATA) {
if (app.selected_sub_param < MAX_SAVE_SLOTS) {
if (app.selected_sub_param < StateManager::MAX_SAVE_SLOTS) {
app.selected_save_slot = app.selected_sub_param;
stateManager.saveData(app);
}
}
if (app.selected_param == PARAM_MAIN_LOAD_DATA) {
if (app.selected_sub_param < MAX_SAVE_SLOTS) {
if (app.selected_sub_param < StateManager::MAX_SAVE_SLOTS) {
app.selected_save_slot = app.selected_sub_param;
stateManager.loadData(app, app.selected_save_slot);
InitGravity(app);
@ -222,10 +227,10 @@ void HandleEncoderPressed() {
}
}
if (app.selected_param == PARAM_MAIN_FACTORY_RESET) {
if (app.selected_sub_param == 0) { // Reset
if (app.selected_sub_param == 0) { // Erase
// Show bootsplash during slow erase operation.
Bootsplash();
stateManager.factoryReset();
stateManager.reset(app);
stateManager.factoryReset(app);
InitGravity(app);
}
}
@ -298,7 +303,7 @@ void editMainParameter(int val) {
break;
case PARAM_MAIN_SAVE_DATA:
case PARAM_MAIN_LOAD_DATA:
updateSelection(app.selected_sub_param, val, MAX_SAVE_SLOTS + 1);
updateSelection(app.selected_sub_param, val, StateManager::MAX_SAVE_SLOTS + 1);
break;
case PARAM_MAIN_RESET_STATE:
updateSelection(app.selected_sub_param, val, 2);

View File

@ -12,16 +12,14 @@
#ifndef APP_STATE_H
#define APP_STATE_H
#include <gravity.h>
#include <libGravity.h>
#include "channel.h"
// Global state for settings and app behavior.
struct AppState {
int tempo = Clock::DEFAULT_TEMPO;
bool encoder_reversed = false;
bool refresh_screen = true;
bool editing_param = false;
Channel channel[Gravity::OUTPUT_COUNT];
byte selected_param = 0;
byte selected_sub_param = 0; // Temporary value for editing params.
byte selected_channel = 0; // 0=tempo, 1-6=output channel
@ -29,7 +27,9 @@ struct AppState {
byte selected_save_slot = 0; // The currently active save slot.
Clock::Source selected_source = Clock::SOURCE_INTERNAL;
Clock::Pulse selected_pulse = Clock::PULSE_PPQN_24;
Channel channel[Gravity::OUTPUT_COUNT];
bool editing_param = false;
bool encoder_reversed = false;
bool refresh_screen = true;
};
extern AppState app;

View File

@ -13,7 +13,7 @@
#define CHANNEL_H
#include <Arduino.h>
#include <gravity.h>
#include <libGravity.h>
#include "euclidean.h"
@ -255,7 +255,7 @@ class Channel {
int step_mod = _calculateMod(CV_DEST_EUC_STEPS, cv1_val, cv2_val, 0, MAX_PATTERN_LEN);
pattern.SetSteps(base_euc_steps + step_mod);
int hit_mod = _calculateMod(CV_DEST_EUC_HITS, cv1_val, cv2_val, 0, MAX_PATTERN_LEN);
int hit_mod = _calculateMod(CV_DEST_EUC_HITS, cv1_val, cv2_val, 0, pattern.GetSteps());
pattern.SetHits(base_euc_hits + hit_mod);
// After all cvmod values are updated, recalculate clock pulse modifiers.

View File

@ -214,10 +214,10 @@ void swingDivisionMark() {
// Human friendly display value for save slot.
String displaySaveSlot(int slot) {
if (slot >= 0 && slot < MAX_SAVE_SLOTS / 2) {
if (slot >= 0 && slot < StateManager::MAX_SAVE_SLOTS / 2) {
return String("A") + String(slot + 1);
} else if (slot >= MAX_SAVE_SLOTS / 2 && slot <= MAX_SAVE_SLOTS) {
return String("B") + String(slot - (MAX_SAVE_SLOTS / 2) + 1);
} else if (slot >= StateManager::MAX_SAVE_SLOTS / 2 && slot <= StateManager::MAX_SAVE_SLOTS) {
return String("B") + String(slot - (StateManager::MAX_SAVE_SLOTS / 2) + 1);
}
}
@ -283,7 +283,7 @@ void DisplayMainPage() {
break;
case PARAM_MAIN_SAVE_DATA:
case PARAM_MAIN_LOAD_DATA:
if (app.selected_sub_param == MAX_SAVE_SLOTS) {
if (app.selected_sub_param == StateManager::MAX_SAVE_SLOTS) {
mainText = F("x");
subText = F("BACK TO MAIN");
} else {
@ -465,7 +465,7 @@ void UpdateDisplay() {
DisplayChannelPage();
}
// Global channel select UI.
DisplaySelectedChannel();
DisplaySelectedChannel();
} while (gravity.display.nextPage());
}
@ -473,16 +473,17 @@ void Bootsplash() {
gravity.display.firstPage();
do {
int textWidth;
String loadingText = F("LOADING....");
gravity.display.setFont(TEXT_FONT);
textWidth = gravity.display.getStrWidth(SKETCH_NAME);
gravity.display.drawStr(24 + (textWidth / 2), 24, SKETCH_NAME);
textWidth = gravity.display.getStrWidth(StateManager::SKETCH_NAME);
gravity.display.drawStr(16 + (textWidth / 2), 20, StateManager::SKETCH_NAME);
textWidth = gravity.display.getStrWidth(SEMANTIC_VERSION);
gravity.display.drawStr(24 + (textWidth / 2), 36, SEMANTIC_VERSION);
textWidth = gravity.display.getStrWidth(StateManager::SEMANTIC_VERSION);
gravity.display.drawStr(16 + (textWidth / 2), 32, StateManager::SEMANTIC_VERSION);
textWidth = gravity.display.getStrWidth("LOADING....");
gravity.display.drawStr(34 + (textWidth / 2), 48, "LOADING....");
textWidth = gravity.display.getStrWidth(loadingText.c_str());
gravity.display.drawStr(26 + (textWidth / 2), 44, loadingText.c_str());
} while (gravity.display.nextPage());
}

View File

@ -15,28 +15,35 @@
#include "app_state.h"
// Define the constants for the current firmware.
const char StateManager::SKETCH_NAME[] = "ALT GRAVITY";
const char StateManager::SEMANTIC_VERSION[] = "V2.0.0BETA2"; // NOTE: This should match the version in the library.properties file.
// Number of available save slots.
const byte StateManager::MAX_SAVE_SLOTS = 10;
const byte StateManager::TRANSIENT_SLOT = 10;
// Define the minimum amount of time between EEPROM writes.
const unsigned long StateManager::SAVE_DELAY_MS = 2000;
// 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);
const int StateManager::METADATA_START_ADDR = 0;
const int StateManager::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, TRANSIENT_SLOT);
// 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();
// Initialize eeprom and save default patter to all save slots.
_saveMetadata(app);
reset(app);
for (int i = 0; i < MAX_SAVE_SLOTS; i++) {
_saveState(app, i);
}
_saveState(app, TRANSIENT_SLOT);
factoryReset(app);
return false;
}
}
@ -45,8 +52,11 @@ bool StateManager::loadData(AppState& app, byte slot_index) {
// 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);
_loadMetadata(app);
app.selected_save_slot = slot_index;
// Persist this change in the global metadata.
_saveMetadata(app);
return true;
}
@ -57,6 +67,7 @@ void StateManager::saveData(const AppState& app) {
if (app.selected_save_slot >= MAX_SAVE_SLOTS + 1) return;
_saveState(app, app.selected_save_slot);
_saveMetadata(app);
_isDirty = false;
}
@ -70,12 +81,12 @@ void StateManager::update(const AppState& app) {
}
void StateManager::reset(AppState& app) {
app.tempo = Clock::DEFAULT_TEMPO;
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();
@ -93,19 +104,27 @@ void StateManager::markDirty() {
}
// Erases all data in the EEPROM by writing 0 to every address.
void StateManager::factoryReset() {
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(METADATA_START_ADDR, load_meta);
bool name_match = (strcmp(load_meta.sketch_name, SKETCH_NAME) == 0);
bool version_match = (strcmp(load_meta.version, SEMANTIC_VERSION) == 0);
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;
}
@ -117,7 +136,6 @@ void StateManager::_saveState(const AppState& app, byte slot_index) {
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);
@ -161,7 +179,6 @@ void StateManager::_loadState(AppState& app, byte slot_index) {
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];

View File

@ -13,22 +13,11 @@
#define SAVE_STATE_H
#include <Arduino.h>
#include <gravity.h>
#include <libGravity.h>
// Forward-declare AppState to avoid circular dependencies.
struct AppState;
// Define the constants for the current firmware.
const char SKETCH_NAME[] = "ALT GRAVITY";
const char SEMANTIC_VERSION[] = "V2.0.0BETA1";
// Number of available save slots.
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;
/**
* @brief Manages saving and loading of the application state to and from EEPROM.
* The number of user slots is defined by MAX_SAVE_SLOTS, and one additional slot
@ -39,6 +28,11 @@ static const unsigned long SAVE_DELAY_MS = 2000;
*/
class StateManager {
public:
static const char SKETCH_NAME[];
static const char SEMANTIC_VERSION[];
static const byte MAX_SAVE_SLOTS;
static const byte TRANSIENT_SLOT;
StateManager();
// Populate the AppState instance with values from EEPROM if they exist.
@ -54,7 +48,7 @@ class StateManager {
// Indicate that state has changed and we should save.
void markDirty();
// Erase all data stored in the EEPROM.
void factoryReset();
void factoryReset(AppState& app);
// This struct holds the data that identifies the firmware version.
struct Metadata {
@ -78,7 +72,6 @@ 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;
@ -93,6 +86,10 @@ class StateManager {
void _saveState(const AppState& app, byte slot_index);
void _loadState(AppState& app, byte slot_index);
static const unsigned long SAVE_DELAY_MS;
static const int METADATA_START_ADDR;
static const int EEPROM_DATA_START_ADDR;
bool _isDirty;
unsigned long _lastChangeTime;
};

10
library.properties Normal file
View File

@ -0,0 +1,10 @@
name=libGravity
version=2.0.0beta2
author=Adam Wonak
maintainer=awonak <github.com/awonak>
sentence=Hardware abstraction library for Sitka Instruments Gravity eurorack module
category=Other
license=MIT
url=https://github.com/awonak/libGravity
architectures=avr
depends=uClock,RotaryEncoder,U8g2

View File

@ -15,7 +15,7 @@
#include <NeoHWSerial.h>
#include "peripherials.h"
#include "uClock.h"
#include "uClock/uClock.h"
// MIDI clock, start, stop, and continue byte definitions - based on MIDI 1.0 Standards.
#define MIDI_CLOCK 0xF8

View File

@ -1,5 +1,5 @@
/**
* @file gravity.cpp
* @file libGravity.cpp
* @author Adam Wonak (https://github.com/awonak)
* @brief Library for building custom scripts for the Sitka Instruments Gravity module.
* @version 0.1
@ -9,7 +9,7 @@
*
*/
#include "gravity.h"
#include "libGravity.h"
// Initialize the static pointer for the EncoderDir class to null. We want to
// have a static pointer to decouple the ISR from the global gravity object.

View File

@ -1,5 +1,5 @@
/**
* @file gravity.h
* @file libGravity.h
* @author Adam Wonak (https://github.com/awonak)
* @brief Library for building custom scripts for the Sitka Instruments Gravity module.
* @version 0.1

View File

@ -32,7 +32,7 @@
* DEALINGS IN THE SOFTWARE.
*/
#include "uClock.h"
#include "uClock/platforms/avr.h"
#include "platforms/avr.h"
//
// Platform specific timer setup/control

View File

@ -1,180 +0,0 @@
/*!
* @file uClock.h
* Project BPM clock generator for Arduino
* @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(RPI2040, Teensy, Seedstudio XIAO M0 and ESP32)
* @version 2.2.1
* @author Romulo Silva
* @date 10/06/2017
* @license MIT - (c) 2024 - Romulo Silva - contact@midilab.co
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __U_CLOCK_H__
#define __U_CLOCK_H__
#include <Arduino.h>
#include <inttypes.h>
namespace umodular { namespace clock {
#define MIN_BPM 1
#define MAX_BPM 400
#define PHASE_FACTOR 16
#define PLL_X 220
#define SECS_PER_MIN (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY (SECS_PER_HOUR * 24L)
class uClockClass {
public:
enum ClockMode {
INTERNAL_CLOCK = 0,
EXTERNAL_CLOCK
};
enum ClockState {
PAUSED = 0,
STARTING,
STARTED
};
enum PPQNResolution {
PPQN_1 = 1,
PPQN_2 = 2,
PPQN_4 = 4,
PPQN_8 = 8,
PPQN_12 = 12,
PPQN_24 = 24,
PPQN_48 = 48,
PPQN_96 = 96,
PPQN_384 = 384,
PPQN_480 = 480,
PPQN_960 = 960
};
ClockState clock_state;
uClockClass();
void setOnOutputPPQN(void (*callback)(uint32_t tick)) {
onOutputPPQNCallback = callback;
}
void setOnSync24(void (*callback)(uint32_t tick)) {
onSync24Callback = callback;
}
void setOnClockStart(void (*callback)()) {
onClockStartCallback = callback;
}
void setOnClockStop(void (*callback)()) {
onClockStopCallback = callback;
}
void init();
void setOutputPPQN(PPQNResolution resolution);
void setInputPPQN(PPQNResolution resolution);
void handleTimerInt();
void handleExternalClock();
void resetCounters();
// external class control
void start();
void stop();
void pause();
void setTempo(float bpm);
float getTempo();
// for software timer implementation(fallback for no board support)
void run();
// external timming control
void setClockMode(ClockMode tempo_mode);
ClockMode getClockMode();
void clockMe();
// for smooth slave tempo calculate display you should raise the
// buffer_size of ext_interval_buffer in between 64 to 128. 254 max size.
// note: this doesn't impact on sync time, only display time getTempo()
// if you dont want to use it, it is default set it to 1 for memory save
void setExtIntervalBuffer(uint8_t buffer_size);
// elapsed time support
uint8_t getNumberOfSeconds(uint32_t time);
uint8_t getNumberOfMinutes(uint32_t time);
uint8_t getNumberOfHours(uint32_t time);
uint8_t getNumberOfDays(uint32_t time);
uint32_t getNowTimer();
uint32_t getPlayTime();
uint32_t bpmToMicroSeconds(float bpm);
private:
float inline freqToBpm(uint32_t freq);
float inline constrainBpm(float bpm);
void calculateReferencedata();
void (*onOutputPPQNCallback)(uint32_t tick);
void (*onSync24Callback)(uint32_t tick);
void (*onClockStartCallback)();
void (*onClockStopCallback)();
// clock input/output control
PPQNResolution output_ppqn = PPQN_96;
PPQNResolution input_ppqn = PPQN_24;
// output and internal counters, ticks and references
uint32_t tick;
uint32_t int_clock_tick;
uint8_t mod_clock_counter;
uint16_t mod_clock_ref;
uint8_t mod_sync24_counter;
uint16_t mod_sync24_ref;
uint32_t sync24_tick;
// external clock control
volatile uint32_t ext_clock_us;
volatile uint32_t ext_clock_tick;
volatile uint32_t ext_interval;
uint32_t last_interval;
uint32_t sync_interval;
float tempo;
uint32_t start_timer;
ClockMode clock_mode;
volatile uint32_t * ext_interval_buffer = nullptr;
uint8_t ext_interval_buffer_size;
uint16_t ext_interval_idx;
};
} } // end namespace umodular::clock
extern umodular::clock::uClockClass uClock;
extern "C" {
extern volatile uint32_t _millis;
}
#endif /* __U_CLOCK_H__ */