Stability fix

This commit is contained in:
Dejvino 2026-02-19 07:24:59 +01:00
parent de6775a7d7
commit 4ec306b8b9
2 changed files with 14 additions and 10 deletions

View File

@ -4,12 +4,15 @@
#include "config.h" #include "config.h"
#include "SharedState.h" #include "SharedState.h"
static Step local_sequence[NUM_TRACKS][NUM_STEPS];
static Step local_nextSequence[NUM_TRACKS][NUM_STEPS];
bool wasPlaying = false; bool wasPlaying = false;
static void handlePlayback() { static void handlePlayback() {
bool nowPlaying = isPlaying; bool nowPlaying = isPlaying;
int tracksToPlay = (playMode == MODE_POLY) ? NUM_TRACKS : 1; int tracksToPlay = (playMode == MODE_POLY) ? NUM_TRACKS : 1;
if (!wasPlaying && !nowPlaying) { if (!wasPlaying && nowPlaying) {
midi.sendRealtime(0xFA); // MIDI Start midi.sendRealtime(0xFA); // MIDI Start
} else if (wasPlaying && !nowPlaying) { } else if (wasPlaying && !nowPlaying) {
midi.sendRealtime(0xFC); // MIDI Stop midi.sendRealtime(0xFC); // MIDI Stop
@ -17,12 +20,18 @@ static void handlePlayback() {
} }
wasPlaying = nowPlaying; wasPlaying = nowPlaying;
if (!nowPlaying) return; if (!nowPlaying) {
delay(1); // yield
return;
}
unsigned long currentMicros = micros(); unsigned long currentMicros = micros();
unsigned long clockInterval = 2500000 / tempo; // 60s * 1000000us / (tempo * 24ppqn) unsigned long clockInterval = 2500000 / tempo; // 60s * 1000000us / (tempo * 24ppqn)
if (currentMicros - lastClockTime >= clockInterval) { if (currentMicros - lastClockTime < clockInterval) {
delay(1); // yield
return;
} else {
lastClockTime += clockInterval; lastClockTime += clockInterval;
midi.sendRealtime(0xF8); // MIDI Clock midi.sendRealtime(0xF8); // MIDI Clock
@ -32,9 +41,7 @@ static void handlePlayback() {
clockCount = 0; clockCount = 0;
midi.lock(); midi.lock();
Step local_sequence[NUM_TRACKS][NUM_STEPS];
memcpy(local_sequence, sequence, sizeof(local_sequence)); memcpy(local_sequence, sequence, sizeof(local_sequence));
Step local_nextSequence[NUM_TRACKS][NUM_STEPS];
memcpy(local_nextSequence, nextSequence, sizeof(local_nextSequence)); memcpy(local_nextSequence, nextSequence, sizeof(local_nextSequence));
midi.unlock(); midi.unlock();

View File

@ -15,7 +15,7 @@
#include "UIThread.h" #include "UIThread.h"
#include "SharedState.h" #include "SharedState.h"
static Step local_sequence[NUM_TRACKS][NUM_STEPS];
static void handleInput(); static void handleInput();
static void drawUI(); static void drawUI();
@ -106,8 +106,7 @@ static void generateSequenceData(int themeType, Step (*target)[NUM_STEPS]) {
} }
void generateTheme(int themeType) { void generateTheme(int themeType) {
Step local_sequence[NUM_TRACKS][NUM_STEPS]; generateSequenceData(themeType, local_sequence);
generateSequenceData(themeType, sequence);
midi.lock(); midi.lock();
memcpy(sequence, local_sequence, sizeof(local_sequence)); memcpy(sequence, local_sequence, sizeof(local_sequence));
@ -353,7 +352,6 @@ static void drawUI() {
bool local_trackMute[NUM_TRACKS]; bool local_trackMute[NUM_TRACKS];
int local_midiChannel; int local_midiChannel;
MelodyStrategy* local_strategy; MelodyStrategy* local_strategy;
Step local_sequence[NUM_TRACKS][NUM_STEPS];
int local_playbackStep; int local_playbackStep;
int local_scaleNotes[12]; int local_scaleNotes[12];
@ -401,7 +399,6 @@ static void drawUI() {
static void updateLeds() { static void updateLeds() {
// Make local copies of shared data inside a critical section // Make local copies of shared data inside a critical section
// to avoid holding the lock during slow LED update operations. // to avoid holding the lock during slow LED update operations.
Step local_sequence[NUM_TRACKS][NUM_STEPS];
int local_playbackStep; int local_playbackStep;
bool local_isPlaying; bool local_isPlaying;
UIState local_currentState; UIState local_currentState;