Compare commits

...

4 Commits

Author SHA1 Message Date
Dejvino
46ecc57cde Fix deadlock in loading + configurable steps count 2026-02-19 13:34:06 +01:00
Dejvino
617251ffc2 playback menu entry 2026-02-19 11:15:47 +01:00
Dejvino
d70e30d76f menu redesign: hierarchical entries 2026-02-19 11:10:09 +01:00
Dejvino
8ae1f5f501 Drop mono mode 2026-02-19 10:48:55 +01:00
9 changed files with 301 additions and 229 deletions

View File

@ -11,12 +11,11 @@ bool wasPlaying = false;
static void handlePlayback() {
bool nowPlaying = isPlaying;
int tracksToPlay = (playMode == MODE_POLY) ? NUM_TRACKS : 1;
if (!wasPlaying && nowPlaying) {
midi.sendRealtime(0xFA); // MIDI Start
} else if (wasPlaying && !nowPlaying) {
midi.sendRealtime(0xFC); // MIDI Stop
for (int i=0; i<tracksToPlay; i++) midi.panic(midiChannels[i]);
for (int i=0; i<NUM_TRACKS; i++) midi.panic(midiChannels[i]);
}
wasPlaying = nowPlaying;
@ -45,10 +44,10 @@ static void handlePlayback() {
memcpy(local_nextSequence, nextSequence, sizeof(local_nextSequence));
midi.unlock();
for(int t=0; t<tracksToPlay; t++) {
int trackChannel = playMode == MODE_POLY ? midiChannels[t] : midiChannels[0];
for(int t=0; t<NUM_TRACKS; t++) {
int trackChannel = midiChannels[t];
int nextStep = playbackStep + 1;
if (nextStep >= NUM_STEPS) nextStep = 0;
if (nextStep >= numSteps) nextStep = 0;
// Determine if we are tying to the next note
bool isTied = local_sequence[t][playbackStep].tie && (local_sequence[t][nextStep].note != -1);
@ -61,7 +60,7 @@ static void handlePlayback() {
}
playbackStep++;
if (playbackStep >= NUM_STEPS) {
if (playbackStep >= numSteps) {
playbackStep = 0;
// Theme change
@ -83,7 +82,7 @@ static void handlePlayback() {
midi.unlock();
}
for (int i=0; i<tracksToPlay; i++) midi.panic(midiChannels[i]);
for (int i=0; i<NUM_TRACKS; i++) midi.panic(midiChannels[i]);
if (sequenceChangeScheduled) {
memcpy(local_sequence, local_nextSequence, sizeof(local_sequence));
@ -111,14 +110,14 @@ static void handlePlayback() {
}
// Note On for new step
for(int t=0; t<tracksToPlay; t++) {
int trackChannel = playMode == MODE_POLY ? midiChannels[t] : midiChannels[0];
for(int t=0; t<NUM_TRACKS; t++) {
int trackChannel = midiChannels[t];
if (!trackMute[t] && local_sequence[t][playbackStep].note != -1) {
uint8_t velocity = local_sequence[t][playbackStep].accent ? 127 : 100;
midi.sendNoteOn(local_sequence[t][playbackStep].note, velocity, trackChannel);
}
int prevStep = (playbackStep == 0) ? NUM_STEPS - 1 : playbackStep - 1;
int prevStep = (playbackStep == 0) ? numSteps - 1 : playbackStep - 1;
bool wasTied = local_sequence[t][prevStep].tie && (local_sequence[t][playbackStep].note != -1);
int prevNote = local_sequence[t][prevStep].note;
// Note Off for previous step (if tied - delayed Note Off)
@ -131,10 +130,8 @@ static void handlePlayback() {
void loopPlayback() {
if (needsPanic) {
if (playMode == MODE_POLY) {
for (int i=0; i<NUM_TRACKS; i++) midi.panic(midiChannels[i]);
} else {
midi.panic(midiChannels[0]);
for (int i=0; i<NUM_TRACKS; i++) {
midi.panic(midiChannels[i]);
}
needsPanic = false;
}

View File

@ -58,15 +58,19 @@ void setup() {
// 5. Init Sequence
randomSeed(micros());
Serial.println(F("Loading sequence."));
EEPROM.begin(512);
if (!loadSequence()) {
generateRandomScale();
Serial.println(F("Starting fresh instead."));
numSteps = NUM_STEPS;
for(int i=0; i<NUM_TRACKS; i++) {
midiChannels[i] = i + 1;
melodySeeds[i] = random(10000);
currentStrategyIndices[i] = 0;
trackMute[i] = false;
}
generateRandomScale();
generateTheme(1);
}
isPlaying = false; // Don't start playing on boot
@ -80,6 +84,10 @@ void setup() {
}
void loop1() {
if (!watchdogActive) {
delay(100);
return;
}
unsigned long now = millis();
lastLoop1Time = now;
if (watchdogActive && (now - lastLoop0Time > 1000)) {

View File

@ -12,26 +12,49 @@ Step nextSequence[NUM_TRACKS][NUM_STEPS];
volatile bool sequenceChangeScheduled = false;
volatile bool needsPanic = false;
UIState currentState = UI_MENU_RANDOMIZE;
UIState currentState = UI_MENU_MAIN;
// Menus
const char* mainMenu[] = { "Randomize", "Setup" };
extern const int mainMenuCount = sizeof(mainMenu) / sizeof(char*);
MenuItem menuItems[] = {
{ "Main", MENU_ID_GROUP_MAIN, true, true, 0 },
{ "Playback", MENU_ID_PLAYBACK, false, false, 1 },
{ "Melody", MENU_ID_MELODY, false, false, 1 },
{ "Scale", MENU_ID_SCALE, false, false, 1 },
{ "Steps", MENU_ID_STEPS, false, false, 1 },
{ "Tempo", MENU_ID_TEMPO, false, false, 1 },
{ "Song Mode", MENU_ID_SONG_MODE, false, false, 1 },
{ "Track", MENU_ID_GROUP_TRACK, true, true, 0 },
{ "Track", MENU_ID_TRACK_SELECT, false, false, 1 },
{ "Mute", MENU_ID_MUTE, false, false, 1 },
{ "Flavour", MENU_ID_FLAVOUR, false, false, 1 },
{ "Mutation", MENU_ID_MUTATION, false, false, 1 },
{ "Theme 1", MENU_ID_THEME_1, false, false, 1 },
{ "Theme 2", MENU_ID_THEME_2, false, false, 1 },
{ "Theme 3", MENU_ID_THEME_3, false, false, 1 },
{ "Theme 4", MENU_ID_THEME_4, false, false, 1 },
{ "Theme 5", MENU_ID_THEME_5, false, false, 1 },
{ "Theme 6", MENU_ID_THEME_6, false, false, 1 },
{ "Theme 7", MENU_ID_THEME_7, false, false, 1 },
{ "Setup", MENU_ID_GROUP_SETUP, true, false, 0 },
{ "Channel", MENU_ID_CHANNEL, false, false, 1 },
{ "Reset", MENU_ID_RESET, false, false, 1 }
};
extern const int menuItemsCount = sizeof(menuItems) / sizeof(MenuItem);
const char* randomizeMenuMono[] = { "Setup", "Melody", "Flavour", "Scale", "Tempo", "Mutation", "Song Mode", "Theme 1", "Theme 2", "Theme 3", "Theme 4", "Theme 5", "Theme 6", "Theme 7" };
extern const int randomizeMenuMonoCount = sizeof(randomizeMenuMono) / sizeof(char*);
extern const int THEME_1_INDEX_MONO = 7;
const char* randomizeMenuPoly[] = { "Setup", "Track", "Mute", "Melody", "Flavour", "Scale", "Tempo", "Mutation", "Song Mode", "Theme 1", "Theme 2", "Theme 3", "Theme 4", "Theme 5", "Theme 6", "Theme 7" };
extern const int randomizeMenuPolyCount = sizeof(randomizeMenuPoly) / sizeof(char*);
extern const int THEME_1_INDEX_POLY = 9;
const char* setupMenu[] = { "Back", "Play Mode", "Channel", "Factory Reset" };
extern const int setupMenuCount = sizeof(setupMenu) / sizeof(char*);
bool isItemVisible(int index) {
if (menuItems[index].indentLevel == 0) return true;
for (int i = index - 1; i >= 0; i--) {
if (menuItems[i].indentLevel < menuItems[index].indentLevel) {
return menuItems[i].expanded;
}
}
return true;
}
int menuSelection = 0;
volatile bool trackMute[NUM_TRACKS];
int randomizeTrack = 0;
volatile int numSteps = NUM_STEPS;
volatile int playbackStep = 0;
volatile int midiChannels[NUM_TRACKS];
int scaleNotes[12];
@ -39,7 +62,7 @@ int numScaleNotes = 0;
int melodySeeds[NUM_TRACKS];
volatile int queuedTheme = -1;
volatile int currentThemeIndex = 1;
extern const uint32_t EEPROM_MAGIC = 0x4242424B;
extern const uint32_t EEPROM_MAGIC = 0x4242424D;
MelodyStrategy* strategies[] = {
new LuckyStrategy(), new ArpStrategy(), new EuclideanStrategy(),
@ -47,7 +70,7 @@ MelodyStrategy* strategies[] = {
extern const int numStrategies = 6;
int currentStrategyIndices[NUM_TRACKS];
volatile PlayMode playMode = MODE_MONO;
volatile PlayMode playMode = MODE_POLY;
volatile bool mutationEnabled = false;
volatile bool songModeEnabled = false;
volatile int songRepeatsRemaining = 0;

View File

@ -14,20 +14,49 @@ extern volatile bool needsPanic;
extern UIState currentState;
// Menus
extern const char* mainMenu[];
extern const int mainMenuCount;
extern const char* randomizeMenuMono[];
extern const int randomizeMenuMonoCount;
extern const int THEME_1_INDEX_MONO;
extern const char* randomizeMenuPoly[];
extern const int randomizeMenuPolyCount;
extern const int THEME_1_INDEX_POLY;
extern const char* setupMenu[];
extern const int setupMenuCount;
enum MenuItemID {
MENU_ID_GROUP_MAIN,
MENU_ID_PLAYBACK,
MENU_ID_MELODY,
MENU_ID_SCALE,
MENU_ID_STEPS,
MENU_ID_TEMPO,
MENU_ID_SONG_MODE,
MENU_ID_GROUP_TRACK,
MENU_ID_TRACK_SELECT,
MENU_ID_MUTE,
MENU_ID_FLAVOUR,
MENU_ID_MUTATION,
MENU_ID_THEME_1,
MENU_ID_THEME_2,
MENU_ID_THEME_3,
MENU_ID_THEME_4,
MENU_ID_THEME_5,
MENU_ID_THEME_6,
MENU_ID_THEME_7,
MENU_ID_GROUP_SETUP,
MENU_ID_CHANNEL,
MENU_ID_RESET
};
struct MenuItem {
const char* label;
MenuItemID id;
bool isGroup;
bool expanded;
int indentLevel;
};
extern MenuItem menuItems[];
extern const int menuItemsCount;
bool isItemVisible(int index);
extern int menuSelection;
extern volatile bool trackMute[NUM_TRACKS];
extern int randomizeTrack;
extern volatile int numSteps;
extern volatile int playbackStep;
extern volatile int midiChannels[NUM_TRACKS];
extern int scaleNotes[12];

View File

@ -17,10 +17,9 @@ enum PlayMode {
enum UIState {
UI_MENU_MAIN,
UI_MENU_RANDOMIZE,
UI_MENU_SETUP,
UI_SETUP_CHANNEL_EDIT,
UI_EDIT_TEMPO,
UI_EDIT_STEPS,
UI_EDIT_FLAVOUR,
UI_SETUP_PLAYMODE_EDIT,
UI_RANDOMIZE_TRACK_EDIT

View File

@ -1,5 +1,6 @@
#include "UIManager.h"
#include "config.h"
#include "SharedState.h"
// --- HARDWARE CONFIGURATION ---
#define SCREEN_WIDTH 128
@ -48,13 +49,10 @@ void UIManager::showMessage(const char* msg) {
void UIManager::draw(UIState currentState, int menuSelection,
int midiChannel, int tempo, MelodyStrategy* currentStrategy,
int queuedTheme, int currentThemeIndex,
int numScaleNotes, const int* scaleNotes, int melodySeed,
int numScaleNotes, const int* scaleNotes, int melodySeed, int numSteps,
bool mutationEnabled, bool songModeEnabled,
const Step sequence[][NUM_STEPS], int playbackStep, bool isPlaying,
const char* mainMenu[], int mainMenuCount,
const char* randomizeMenu[], int randomizeMenuCount,
const char* setupMenu[], int setupMenuCount, int theme1Index,
PlayMode playMode, int randomizeTrack, const bool* trackMute) {
int randomizeTrack, const bool* trackMute) {
display.clearDisplay();
display.setTextSize(1);
@ -63,13 +61,7 @@ void UIManager::draw(UIState currentState, int menuSelection,
switch(currentState) {
case UI_MENU_MAIN:
drawMenu("MAIN MENU", mainMenu, mainMenuCount, menuSelection, currentState, midiChannel, tempo, currentStrategy->getName(), queuedTheme, currentThemeIndex, numScaleNotes, scaleNotes, melodySeed, mutationEnabled, songModeEnabled, theme1Index, playMode, randomizeTrack, trackMute);
break;
case UI_MENU_RANDOMIZE:
drawMenu("PLAY", randomizeMenu, randomizeMenuCount, menuSelection, currentState, midiChannel, tempo, currentStrategy->getName(), queuedTheme, currentThemeIndex, numScaleNotes, scaleNotes, melodySeed, mutationEnabled, songModeEnabled, theme1Index, playMode, randomizeTrack, trackMute);
break;
case UI_MENU_SETUP:
drawMenu("SETUP", setupMenu, setupMenuCount, menuSelection, currentState, midiChannel, tempo, currentStrategy->getName(), queuedTheme, currentThemeIndex, numScaleNotes, scaleNotes, melodySeed, mutationEnabled, songModeEnabled, theme1Index, playMode, randomizeTrack, trackMute);
drawMenu(menuSelection, currentState, midiChannel, tempo, currentStrategy->getName(), queuedTheme, currentThemeIndex, numScaleNotes, scaleNotes, melodySeed, numSteps, mutationEnabled, songModeEnabled, isPlaying, randomizeTrack, trackMute);
break;
case UI_SETUP_CHANNEL_EDIT:
display.println(F("SET MIDI CHANNEL"));
@ -94,6 +86,17 @@ void UIManager::draw(UIState currentState, int menuSelection,
display.setCursor(0, 50);
display.println(F(" (Press to confirm)"));
break;
case UI_EDIT_STEPS:
display.println(F("SET STEPS"));
display.drawLine(0, 8, 128, 8, SSD1306_WHITE);
display.setCursor(20, 25);
display.setTextSize(2);
display.print(F("LEN: "));
display.print(numSteps);
display.setTextSize(1);
display.setCursor(0, 50);
display.println(F(" (Press to confirm)"));
break;
case UI_EDIT_FLAVOUR:
display.println(F("SET FLAVOUR"));
display.drawLine(0, 8, 128, 8, SSD1306_WHITE);
@ -104,16 +107,6 @@ void UIManager::draw(UIState currentState, int menuSelection,
display.setCursor(0, 50);
display.println(F(" (Press to confirm)"));
break;
case UI_SETUP_PLAYMODE_EDIT:
display.println(F("SET PLAY MODE"));
display.drawLine(0, 8, 128, 8, SSD1306_WHITE);
display.setCursor(20, 25);
display.setTextSize(2);
display.print(playMode == MODE_MONO ? "Mono" : "Poly");
display.setTextSize(1);
display.setCursor(0, 50);
display.println(F(" (Press to confirm)"));
break;
case UI_RANDOMIZE_TRACK_EDIT:
display.println(F("SET TRACK"));
display.drawLine(0, 8, 128, 8, SSD1306_WHITE);
@ -129,55 +122,59 @@ void UIManager::draw(UIState currentState, int menuSelection,
display.display();
}
void UIManager::drawMenu(const char* title, const char* items[], int count, int selection,
UIState currentState, int midiChannel, int tempo, const char* flavourName,
void UIManager::drawMenu(int selection, UIState currentState, int midiChannel, int tempo, const char* flavourName,
int queuedTheme, int currentThemeIndex, int numScaleNotes,
const int* scaleNotes, int melodySeed, bool mutationEnabled,
bool songModeEnabled, int theme1Index, PlayMode playMode, int randomizeTrack, const bool* trackMute) {
display.println(title);
display.drawLine(0, 8, 128, 8, SSD1306_WHITE);
const int* scaleNotes, int melodySeed, int numSteps, bool mutationEnabled,
bool songModeEnabled, bool isPlaying, int randomizeTrack, const bool* trackMute) {
int start = 0;
if (selection >= 5) start = selection - 4;
// Calculate visual cursor position and scroll offset
int visualCursor = 0;
for(int i=0; i<selection; i++) {
if(isItemVisible(i)) visualCursor++;
}
int y = 10;
for (int i = start; i < count; i++) {
if (y > 55) break;
const int MAX_LINES = 7; // No title, so we have more space
int startVisualIndex = 0;
if (visualCursor >= MAX_LINES) {
startVisualIndex = visualCursor - (MAX_LINES - 1);
}
int currentVisualIndex = 0;
int y = 0;
for (int i = 0; i < menuItemsCount; i++) {
if (!isItemVisible(i)) continue;
if (currentVisualIndex >= startVisualIndex) {
if (y > 55) break;
if (i == selection) {
display.fillRect(0, y, 128, 8, SSD1306_WHITE);
display.fillRect(0, y, 128, 9, SSD1306_WHITE);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
} else {
display.setTextColor(SSD1306_WHITE);
}
display.setCursor(2, y);
display.print(items[i]);
if (currentState == UI_MENU_SETUP && i == 1) {
display.print(F(": ")); display.print(playMode == MODE_MONO ? "Mono" : "Poly");
int x = 2 + (menuItems[i].indentLevel * 6);
display.setCursor(x, y + 1);
if (menuItems[i].isGroup) {
display.print(menuItems[i].expanded ? F("v ") : F("> "));
}
if (currentState == UI_MENU_SETUP && i == 2) {
display.print(menuItems[i].label);
MenuItemID id = menuItems[i].id;
if (id == MENU_ID_CHANNEL) {
display.print(F(": ")); display.print(midiChannel);
}
if (currentState == UI_MENU_RANDOMIZE && playMode == MODE_POLY && i == 1) {
display.print(F(": ")); display.print(randomizeTrack + 1);
}
if (currentState == UI_MENU_RANDOMIZE && playMode == MODE_POLY && i == 2) {
display.print(F(": ")); display.print(trackMute[randomizeTrack] ? F("YES") : F("NO"));
}
if (currentState == UI_MENU_RANDOMIZE && i >= theme1Index && queuedTheme == (i - theme1Index + 1)) {
display.print(F(" [NEXT]"));
}
if (currentState == UI_MENU_RANDOMIZE && i >= theme1Index && currentThemeIndex == (i - theme1Index + 1)) {
display.print(F(" *"));
}
if (currentState == UI_MENU_RANDOMIZE) {
int track_offset = (playMode == MODE_POLY) ? 2 : 0;
if (i == 1 + track_offset) { // Melody
// Dynamic values
if (id == MENU_ID_PLAYBACK) { display.print(F(": ")); display.print(isPlaying ? F("ON") : F("OFF")); }
else if (id == MENU_ID_MELODY) {
display.print(F(": ")); display.print(melodySeed);
} else if (i == 2 + track_offset) { // Flavour
display.print(F(": ")); display.print(flavourName);
} else if (i == 3 + track_offset) { // Scale
} else if (id == MENU_ID_SCALE) {
display.print(F(": "));
if (numScaleNotes > 0) {
const char* noteNames[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
@ -186,11 +183,24 @@ void UIManager::drawMenu(const char* title, const char* items[], int count, int
if (j < min(numScaleNotes, 6) - 1) display.print(F(" "));
}
}
} else if (i == 4 + track_offset) { display.print(F(": ")); display.print(tempo); }
else if (i == 5 + track_offset) { display.print(F(": ")); display.print(mutationEnabled ? F("ON") : F("OFF")); }
else if (i == 6 + track_offset) { display.print(F(": ")); display.print(songModeEnabled ? F("ON") : F("OFF")); }
}
} else if (id == MENU_ID_TEMPO) { display.print(F(": ")); display.print(tempo); }
else if (id == MENU_ID_STEPS) { display.print(F(": ")); display.print(numSteps); }
else if (id == MENU_ID_SONG_MODE) { display.print(F(": ")); display.print(songModeEnabled ? F("ON") : F("OFF")); }
else if (id == MENU_ID_TRACK_SELECT) { display.print(F(": ")); display.print(randomizeTrack + 1); }
else if (id == MENU_ID_MUTE) { display.print(F(": ")); display.print(trackMute[randomizeTrack] ? F("YES") : F("NO")); }
else if (id == MENU_ID_FLAVOUR) { display.print(F(": ")); display.print(flavourName); }
else if (id == MENU_ID_MUTATION) { display.print(F(": ")); display.print(mutationEnabled ? F("ON") : F("OFF")); }
if (id >= MENU_ID_THEME_1 && id <= MENU_ID_THEME_7) {
int themeIdx = id - MENU_ID_THEME_1 + 1;
if (queuedTheme == themeIdx) display.print(F(" [NEXT]"));
if (currentThemeIndex == themeIdx) display.print(F(" *"));
}
y += 9;
}
currentVisualIndex++;
}
}
@ -207,7 +217,7 @@ int UIManager::getPixelIndex(int x, int y) {
void UIManager::updateLeds(const Step sequence[][NUM_STEPS], int playbackStep, bool isPlaying,
UIState currentState, bool songModeEnabled,
int songRepeatsRemaining, bool sequenceChangeScheduled, PlayMode playMode,
int numScaleNotes, const int* scaleNotes, const bool* trackMute) {
int selectedTrack, int numSteps, int numScaleNotes, const int* scaleNotes, const bool* trackMute) {
pixels.clear();
const uint32_t COLOR_PLAYHEAD = pixels.Color(0, 255, 0);
@ -219,8 +229,10 @@ void UIManager::updateLeds(const Step sequence[][NUM_STEPS], int playbackStep, b
if(playMode == MODE_POLY) {
for(int t=0; t<NUM_TRACKS; t++) {
for(int s=0; s<NUM_STEPS; s++) {
int col = t * 2 + (s / 8);
int row = s % 8;
if (s >= numSteps) continue;
int row = t * 2 + (s / 8);
int col = s % 8;
uint32_t color = 0;
int note = sequence[t][s].note;
@ -242,21 +254,24 @@ void UIManager::updateLeds(const Step sequence[][NUM_STEPS], int playbackStep, b
}
} else {
// --- Mono Mode (original) ---
const Step* trackSequence = sequence[selectedTrack];
for (int s = 0; s < NUM_STEPS; s++) {
if (s >= numSteps) continue;
int x = s % 8;
int yBase = (s / 8) * 4;
uint32_t color = 0, dimColor = 0;
bool isCursorHere = (isPlaying && s == playbackStep);
if (sequence[0][s].note != -1) {
color = getNoteColor(sequence[0][s].note, sequence[0][s].tie);
dimColor = getNoteColor(sequence[0][s].note, true);
if (trackSequence[s].note != -1) {
color = getNoteColor(trackSequence[s].note, trackSequence[s].tie);
dimColor = getNoteColor(trackSequence[s].note, true);
}
uint32_t c[4] = {0};
if (sequence[0][s].note != -1) {
int octave = sequence[0][s].note / 12;
if (octave > 4) { c[0] = color; if (sequence[0][s].accent) c[1] = dimColor; }
else if (octave < 4) { c[2] = color; if (sequence[0][s].accent) c[1] = dimColor; }
else { c[1] = color; if (sequence[0][s].accent) { c[0] = dimColor; c[2] = dimColor; } }
if (trackSequence[s].note != -1) {
int octave = trackSequence[s].note / 12;
if (octave > 4) { c[0] = color; if (trackSequence[s].accent) c[1] = dimColor; }
else if (octave < 4) { c[2] = color; if (trackSequence[s].accent) c[1] = dimColor; }
else { c[1] = color; if (trackSequence[s].accent) { c[0] = dimColor; c[2] = dimColor; } }
}
uint32_t cursorColor = pixels.Color(0, 0, 50);
if (isPlaying) {

View File

@ -17,29 +17,25 @@ public:
void draw(UIState currentState, int menuSelection,
int midiChannel, int tempo, MelodyStrategy* currentStrategy,
int queuedTheme, int currentThemeIndex,
int numScaleNotes, const int* scaleNotes, int melodySeed,
int numScaleNotes, const int* scaleNotes, int melodySeed, int numSteps,
bool mutationEnabled, bool songModeEnabled,
const Step sequence[][NUM_STEPS], int playbackStep, bool isPlaying,
const char* mainMenu[], int mainMenuCount,
const char* randomizeMenu[], int randomizeMenuCount,
const char* setupMenu[], int setupMenuCount, int theme1Index,
PlayMode playMode, int randomizeTrack, const bool* trackMute);
int randomizeTrack, const bool* trackMute);
void updateLeds(const Step sequence[][NUM_STEPS], int playbackStep, bool isPlaying,
UIState currentState, bool songModeEnabled,
int songRepeatsRemaining, bool sequenceChangeScheduled, PlayMode playMode,
int numScaleNotes, const int* scaleNotes, const bool* trackMute);
int selectedTrack, int numSteps, int numScaleNotes, const int* scaleNotes, const bool* trackMute);
private:
Adafruit_SSD1306 display;
Adafruit_NeoPixel pixels;
uint32_t leds_buffer[8][8]; // For piano roll
void drawMenu(const char* title, const char* items[], int count, int selection,
UIState currentState, int midiChannel, int tempo, const char* flavourName,
void drawMenu(int selection, UIState currentState, int midiChannel, int tempo, const char* flavourName,
int queuedTheme, int currentThemeIndex,
int numScaleNotes, const int* scaleNotes, int melodySeed,
bool mutationEnabled, bool songModeEnabled, int theme1Index, PlayMode playMode, int randomizeTrack, const bool* trackMute);
int numScaleNotes, const int* scaleNotes, int melodySeed, int numSteps,
bool mutationEnabled, bool songModeEnabled, bool isPlaying, int randomizeTrack, const bool* trackMute);
uint32_t getNoteColor(int note, bool dim);
int getPixelIndex(int x, int y);

View File

@ -36,7 +36,7 @@ void saveSequence(bool quiet) {
for(int i=0; i<NUM_TRACKS; i++) mutes[i] = trackMute[i];
EEPROM.put(addr, mutes); addr += sizeof(mutes);
EEPROM.put(addr, (int)tempo); addr += sizeof(int);
EEPROM.put(addr, (int)playMode); addr += sizeof(int);
EEPROM.put(addr, (int)numSteps); addr += sizeof(int);
EEPROM.put(addr, numScaleNotes); addr += sizeof(numScaleNotes);
for (int i = 0; i<12; i++) {
@ -54,7 +54,10 @@ bool loadSequence() {
int addr = 0;
uint32_t magic;
EEPROM.get(addr, magic); addr += sizeof(magic);
if (magic != EEPROM_MAGIC) return false;
if (magic != EEPROM_MAGIC) {
midi.unlock();
return false;
}
int channels[NUM_TRACKS];
EEPROM.get(addr, channels); addr += sizeof(channels);
@ -68,7 +71,8 @@ bool loadSequence() {
EEPROM.get(addr, t); addr += sizeof(int);
tempo = t;
EEPROM.get(addr, t); addr += sizeof(int);
playMode = (PlayMode)t;
numSteps = t;
if (numSteps <= 0 || numSteps >= NUM_STEPS) numSteps = NUM_STEPS;
EEPROM.get(addr, numScaleNotes); addr += sizeof(numScaleNotes);
for (int i = 0; i<12; i++) {
@ -91,15 +95,17 @@ void factoryReset() {
static void generateTrackData(int track, int themeType, Step (*target)[NUM_STEPS]) {
randomSeed(melodySeeds[track] + themeType * 12345);
strategies[currentStrategyIndices[track]]->generate(target, track, NUM_STEPS, scaleNotes, numScaleNotes, melodySeeds[track] + themeType * 12345);
strategies[currentStrategyIndices[track]]->generate(target, track, numSteps, scaleNotes, numScaleNotes, melodySeeds[track] + themeType * 12345);
}
void generateRandomScale() {
Serial.println(F("Generating new scale."));
// All tracks share the same scale for now
strategies[currentStrategyIndices[0]]->generateScale(scaleNotes, numScaleNotes);
}
static void generateSequenceData(int themeType, Step (*target)[NUM_STEPS]) {
Serial.println(F("Generating sequence."));
for(int i=0; i<NUM_TRACKS; i++) {
generateTrackData(i, themeType, target);
}
@ -108,11 +114,13 @@ static void generateSequenceData(int themeType, Step (*target)[NUM_STEPS]) {
void generateTheme(int themeType) {
generateSequenceData(themeType, local_sequence);
Serial.println(F("Generating theme."));
midi.lock();
memcpy(sequence, local_sequence, sizeof(local_sequence));
needsPanic = true;
midi.unlock();
Serial.println(F("Theme ready."));
currentThemeIndex = themeType;
clockCount = 0;
lastClockTime = micros();
@ -121,11 +129,7 @@ void generateTheme(int themeType) {
}
void mutateSequence(Step (*target)[NUM_STEPS]) {
if (playMode == MODE_POLY) {
for(int i=0; i<NUM_TRACKS; i++) strategies[currentStrategyIndices[i]]->mutate(target, i, NUM_STEPS, scaleNotes, numScaleNotes);
} else {
strategies[currentStrategyIndices[0]]->mutate(target, 0, NUM_STEPS, scaleNotes, numScaleNotes);
}
for(int i=0; i<NUM_TRACKS; i++) strategies[currentStrategyIndices[i]]->mutate(target, i, numSteps, scaleNotes, numScaleNotes);
}
static void handleInput() {
@ -139,29 +143,23 @@ static void handleInput() {
if (delta != 0) {
switch(currentState) {
case UI_MENU_MAIN:
menuSelection += (delta > 0 ? 1 : -1);
if (menuSelection < 0) menuSelection = mainMenuCount - 1;
if (menuSelection >= mainMenuCount) menuSelection = 0;
break;
case UI_MENU_RANDOMIZE:
{
menuSelection += (delta > 0 ? 1 : -1);
int count = (playMode == MODE_POLY) ? randomizeMenuPolyCount : randomizeMenuMonoCount;
if (menuSelection < 0) menuSelection = count - 1;
if (menuSelection >= count) menuSelection = 0;
int next = menuSelection;
int count = 0;
do {
next += (delta > 0 ? 1 : -1);
if (next < 0) next = menuItemsCount - 1;
if (next >= menuItemsCount) next = 0;
count++;
} while (!isItemVisible(next) && count < menuItemsCount);
menuSelection = next;
}
break;
case UI_MENU_SETUP:
menuSelection += (delta > 0 ? 1 : -1);
if (menuSelection < 0) menuSelection = setupMenuCount - 1;
if (menuSelection >= setupMenuCount) menuSelection = 0;
break;
case UI_SETUP_CHANNEL_EDIT:
{
int trackToEdit = (playMode == MODE_POLY) ? randomizeTrack : 0;
midiChannels[trackToEdit] += (delta > 0 ? 1 : -1);
if (midiChannels[trackToEdit] < 1) midiChannels[trackToEdit] = 16;
if (midiChannels[trackToEdit] > 16) midiChannels[trackToEdit] = 1;
midiChannels[randomizeTrack] += (delta > 0 ? 1 : -1);
if (midiChannels[randomizeTrack] < 1) midiChannels[randomizeTrack] = 16;
if (midiChannels[randomizeTrack] > 16) midiChannels[randomizeTrack] = 1;
}
break;
case UI_EDIT_TEMPO:
@ -169,17 +167,18 @@ static void handleInput() {
if (tempo < 40) tempo = 40;
if (tempo > 240) tempo = 240;
break;
case UI_EDIT_STEPS:
numSteps += delta;
if (numSteps < 1) numSteps = 1;
if (numSteps > NUM_STEPS) numSteps = NUM_STEPS;
break;
case UI_EDIT_FLAVOUR:
{
int trackToEdit = playMode == MODE_POLY ? randomizeTrack : 0;
currentStrategyIndices[trackToEdit] += (delta > 0 ? 1 : -1);
if (currentStrategyIndices[trackToEdit] < 0) currentStrategyIndices[trackToEdit] = numStrategies - 1;
if (currentStrategyIndices[trackToEdit] >= numStrategies) currentStrategyIndices[trackToEdit] = 0;
currentStrategyIndices[randomizeTrack] += (delta > 0 ? 1 : -1);
if (currentStrategyIndices[randomizeTrack] < 0) currentStrategyIndices[randomizeTrack] = numStrategies - 1;
if (currentStrategyIndices[randomizeTrack] >= numStrategies) currentStrategyIndices[randomizeTrack] = 0;
}
break;
case UI_SETUP_PLAYMODE_EDIT:
playMode = (playMode == MODE_MONO) ? MODE_POLY : MODE_MONO;
break;
}
if (currentState == UI_RANDOMIZE_TRACK_EDIT) {
randomizeTrack += (delta > 0 ? 1 : -1);
@ -211,37 +210,38 @@ static void handleInput() {
if (!buttonConsumed) { // Short press action
switch(currentState) {
case UI_MENU_MAIN:
if (menuSelection == 0) { currentState = UI_MENU_RANDOMIZE; menuSelection = 0; break; }
if (menuSelection == 1) { currentState = UI_MENU_SETUP; menuSelection = 0; break; }
break;
case UI_MENU_RANDOMIZE:
{
int track_offset = (playMode == MODE_POLY) ? 2 : 0;
int theme_1_index = (playMode == MODE_POLY) ? THEME_1_INDEX_POLY : THEME_1_INDEX_MONO;
if (menuSelection == 0) { currentState = UI_MENU_SETUP; menuSelection = 0; break; }
if (playMode == MODE_POLY) {
if (menuSelection == 1) { currentState = UI_RANDOMIZE_TRACK_EDIT; break; }
if (menuSelection == 2) { trackMute[randomizeTrack] = !trackMute[randomizeTrack]; break; }
if (menuItems[menuSelection].isGroup) {
menuItems[menuSelection].expanded = !menuItems[menuSelection].expanded;
break;
}
if (menuSelection == 1 + track_offset) { // Melody
int track = playMode == MODE_POLY ? randomizeTrack : 0;
switch(menuItems[menuSelection].id) {
case MENU_ID_PLAYBACK:
isPlaying = !isPlaying;
if (isPlaying) {
playbackStep = 0;
clockCount = 0;
lastClockTime = micros();
} else {
queuedTheme = -1;
}
break;
case MENU_ID_MELODY:
midi.lock();
melodySeeds[track] = random(10000);
melodySeeds[randomizeTrack] = random(10000);
if (isPlaying) {
int theme = (queuedTheme != -1) ? queuedTheme : currentThemeIndex;
if (!sequenceChangeScheduled) {
memcpy(nextSequence, sequence, sizeof(sequence));
}
generateTrackData(track, theme, nextSequence);
generateTrackData(randomizeTrack, theme, nextSequence);
sequenceChangeScheduled = true;
}
midi.unlock();
saveSequence(true);
break;
}
if (menuSelection == 2 + track_offset) { currentState = UI_EDIT_FLAVOUR; break; } // Flavour
if (menuSelection == 3 + track_offset) { // Scale
case MENU_ID_SCALE:
generateRandomScale();
if (isPlaying) {
int theme = (queuedTheme != -1) ? queuedTheme : currentThemeIndex;
@ -253,18 +253,28 @@ static void handleInput() {
}
saveSequence(true);
break;
}
if (menuSelection == 4 + track_offset) { currentState = UI_EDIT_TEMPO; break; }
if (menuSelection == 5 + track_offset) { mutationEnabled = !mutationEnabled; break; }
if (menuSelection == 6 + track_offset) {
case MENU_ID_TEMPO: currentState = UI_EDIT_TEMPO; break;
case MENU_ID_STEPS: currentState = UI_EDIT_STEPS; break;
case MENU_ID_SONG_MODE:
songModeEnabled = !songModeEnabled;
if (songModeEnabled) {
songModeNeedsNext = true;
}
break;
}
if (menuSelection >= theme_1_index) { // Themes
const int selectedTheme = menuSelection - theme_1_index + 1;
case MENU_ID_TRACK_SELECT: currentState = UI_RANDOMIZE_TRACK_EDIT; break;
case MENU_ID_MUTE: trackMute[randomizeTrack] = !trackMute[randomizeTrack]; break;
case MENU_ID_FLAVOUR: currentState = UI_EDIT_FLAVOUR; break;
case MENU_ID_MUTATION: mutationEnabled = !mutationEnabled; break;
case MENU_ID_CHANNEL: currentState = UI_SETUP_CHANNEL_EDIT; break;
case MENU_ID_RESET: factoryReset(); break;
default:
if (menuItems[menuSelection].id >= MENU_ID_THEME_1 && menuItems[menuSelection].id <= MENU_ID_THEME_7) {
const int selectedTheme = menuItems[menuSelection].id - MENU_ID_THEME_1 + 1;
if (isPlaying) {
queuedTheme = selectedTheme;
midi.lock();
@ -276,43 +286,37 @@ static void handleInput() {
}
break;
}
break;
}
break;
case UI_MENU_SETUP:
if (menuSelection == 0) { currentState = UI_MENU_RANDOMIZE; menuSelection = 0; break; }
if (menuSelection == 1) { currentState = UI_SETUP_PLAYMODE_EDIT; break; }
if (menuSelection == 2) { currentState = UI_SETUP_CHANNEL_EDIT; break; }
if (menuSelection == 3) { factoryReset(); break; }
break;
case UI_SETUP_CHANNEL_EDIT:
currentState = UI_MENU_SETUP;
currentState = UI_MENU_MAIN;
saveSequence(true);
break;
case UI_EDIT_TEMPO:
currentState = UI_MENU_RANDOMIZE;
currentState = UI_MENU_MAIN;
saveSequence(true);
break;
case UI_EDIT_STEPS:
currentState = UI_MENU_MAIN;
saveSequence(true);
break;
case UI_EDIT_FLAVOUR:
currentState = UI_MENU_RANDOMIZE;
currentState = UI_MENU_MAIN;
if (isPlaying) {
int theme = (queuedTheme != -1) ? queuedTheme : currentThemeIndex;
int track = playMode == MODE_POLY ? randomizeTrack : 0;
midi.lock();
if (!sequenceChangeScheduled) {
memcpy(nextSequence, sequence, sizeof(sequence));
}
generateTrackData(track, theme, nextSequence);
generateTrackData(randomizeTrack, theme, nextSequence);
sequenceChangeScheduled = true;
midi.unlock();
}
saveSequence(true);
break;
case UI_SETUP_PLAYMODE_EDIT:
currentState = UI_MENU_SETUP;
saveSequence(true);
break;
case UI_RANDOMIZE_TRACK_EDIT:
currentState = UI_MENU_RANDOMIZE;
currentState = UI_MENU_MAIN;
saveSequence(true);
break;
}
@ -338,16 +342,11 @@ static void handleInput() {
}
static void drawUI() {
const char **randMenu;
int randMenuCount;
int themeIndex;
// Make local copies of shared data inside a critical section
// to avoid holding the lock during slow display operations.
UIState local_currentState;
PlayMode local_playMode;
int local_menuSelection, local_randomizeTrack, local_tempo, local_currentThemeIndex, local_queuedTheme, local_numScaleNotes;
int local_melodySeed;
int local_melodySeed, local_numSteps;
bool local_mutationEnabled, local_songModeEnabled, local_isPlaying;
bool local_trackMute[NUM_TRACKS];
int local_midiChannel;
@ -356,30 +355,19 @@ static void drawUI() {
int local_scaleNotes[12];
midi.lock();
local_playMode = playMode;
local_randomizeTrack = randomizeTrack;
int ui_track = (local_playMode == MODE_POLY) ? local_randomizeTrack : 0;
if (local_playMode == MODE_POLY) {
randMenu = randomizeMenuPoly;
randMenuCount = randomizeMenuPolyCount;
themeIndex = THEME_1_INDEX_POLY;
} else {
randMenu = randomizeMenuMono;
randMenuCount = randomizeMenuMonoCount;
themeIndex = THEME_1_INDEX_MONO;
}
local_currentState = currentState;
local_menuSelection = menuSelection;
local_midiChannel = midiChannels[ui_track];
local_midiChannel = midiChannels[local_randomizeTrack];
local_tempo = tempo;
local_strategy = strategies[currentStrategyIndices[ui_track]];
local_numSteps = numSteps;
local_strategy = strategies[currentStrategyIndices[local_randomizeTrack]];
local_queuedTheme = queuedTheme;
local_currentThemeIndex = currentThemeIndex;
local_numScaleNotes = numScaleNotes;
memcpy(local_scaleNotes, scaleNotes, sizeof(local_scaleNotes));
local_melodySeed = melodySeeds[ui_track];
local_melodySeed = melodySeeds[local_randomizeTrack];
local_mutationEnabled = mutationEnabled;
local_songModeEnabled = songModeEnabled;
memcpy(local_sequence, sequence, sizeof(local_sequence));
@ -390,10 +378,8 @@ static void drawUI() {
ui.draw(local_currentState, local_menuSelection,
local_midiChannel, local_tempo, local_strategy,
local_queuedTheme, local_currentThemeIndex, local_numScaleNotes, local_scaleNotes, local_melodySeed,
local_mutationEnabled, local_songModeEnabled, (const Step (*)[NUM_STEPS])local_sequence, local_playbackStep, local_isPlaying,
mainMenu, mainMenuCount, randMenu, randMenuCount, setupMenu, setupMenuCount,
themeIndex, local_playMode, local_randomizeTrack, (const bool*)local_trackMute);
local_queuedTheme, local_currentThemeIndex, local_numScaleNotes, local_scaleNotes, local_melodySeed, local_numSteps,
local_mutationEnabled, local_songModeEnabled, (const Step (*)[NUM_STEPS])local_sequence, local_playbackStep, local_isPlaying, local_randomizeTrack, (const bool*)local_trackMute);
}
static void updateLeds() {
@ -402,31 +388,50 @@ static void updateLeds() {
int local_playbackStep;
bool local_isPlaying;
UIState local_currentState;
int local_menuSelection;
bool local_songModeEnabled;
int local_songRepeatsRemaining;
bool local_sequenceChangeScheduled;
PlayMode local_playMode;
int local_numScaleNotes;
int local_numScaleNotes, local_numSteps;
int local_scaleNotes[12];
bool local_trackMute[NUM_TRACKS];
int local_randomizeTrack;
midi.lock();
memcpy(local_sequence, sequence, sizeof(local_sequence));
local_playbackStep = playbackStep;
local_isPlaying = isPlaying;
local_currentState = currentState;
local_menuSelection = menuSelection;
local_songModeEnabled = songModeEnabled;
local_songRepeatsRemaining = songRepeatsRemaining;
local_sequenceChangeScheduled = sequenceChangeScheduled;
local_playMode = playMode;
local_numScaleNotes = numScaleNotes;
local_numSteps = numSteps;
local_randomizeTrack = randomizeTrack;
memcpy(local_scaleNotes, scaleNotes, sizeof(local_scaleNotes));
memcpy(local_trackMute, (const void*)trackMute, sizeof(local_trackMute));
midi.unlock();
PlayMode ledDisplayMode = MODE_POLY; // Default to POLY (MAIN section view)
if (local_currentState == UI_MENU_MAIN) {
MenuItemID id = menuItems[local_menuSelection].id;
// Check if we are in the Track group (IDs between TRACK_SELECT and THEME_7)
if (id >= MENU_ID_TRACK_SELECT && id <= MENU_ID_THEME_7) {
// It's a TRACK section item (Track, Mute, Flavour, Mutation, Themes)
ledDisplayMode = MODE_MONO;
}
} else if (local_currentState == UI_EDIT_FLAVOUR || local_currentState == UI_RANDOMIZE_TRACK_EDIT) {
// These are entered from TRACK section items
ledDisplayMode = MODE_MONO;
}
ui.updateLeds((const Step (*)[NUM_STEPS])local_sequence, local_playbackStep, local_isPlaying,
local_currentState, local_songModeEnabled, local_songRepeatsRemaining,
local_sequenceChangeScheduled, local_playMode, local_numScaleNotes,
local_sequenceChangeScheduled, ledDisplayMode, local_randomizeTrack, local_numSteps, local_numScaleNotes,
local_scaleNotes, (const bool*)local_trackMute);
}

View File

@ -16,7 +16,7 @@
#define ENC_SW 14
// --- TRACKER DATA ---
#define NUM_STEPS 16
#define NUM_STEPS 8
#define NUM_TRACKS 4
#endif