From 72496d2daf9efb5b62a2ab81b7d669582ee900ec Mon Sep 17 00:00:00 2001 From: Ashley Strahle Date: Fri, 3 Jul 2026 07:13:38 +0000 Subject: [PATCH] Fix MIDI clock phase: handle 0xFA START to reset tick counter to downbeat --- src/main.cpp | 5 +++-- src/midi_transport.cpp | 9 +++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5b353cc..262177d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,7 +27,7 @@ void midi_task(void* parameter) { Serial.println("[TASK] MIDI task started on core 0"); // Beat timing fixes for precise beat alignment - sync to real MIDI time, not hardcoded tempo - uint32_t last_beat = 0; + uint32_t last_beat = UINT32_MAX; uint32_t flash_start = 0; while (true) { @@ -40,7 +40,8 @@ void midi_task(void* parameter) { // Calculate beat using MIDI tick count (24 ticks per beat/PPQN) uint32_t current_beat = tick / 24; - // Check if we've crossed a beat boundary + // Check if we've crossed a beat boundary. + // last_beat starts as UINT32_MAX so tick 0 (START) always triggers if (current_beat != last_beat) { last_beat = current_beat; flash_start = now; diff --git a/src/midi_transport.cpp b/src/midi_transport.cpp index a6fa11f..f76975a 100644 --- a/src/midi_transport.cpp +++ b/src/midi_transport.cpp @@ -54,8 +54,13 @@ void UsbMidiTransport::update() { uint8_t packet[4]; if (usb_midi.readPacket(packet)) { uint8_t cin = packet[0] & 0x0F; - if (cin == 0x0F && packet[1] == 0xF8) { - midi_tick_count++; + if (cin == 0x0F) { + if (packet[1] == 0xF8) { + midi_tick_count++; + } else if (packet[1] == 0xFA) { + midi_tick_count = 0; + Serial.println("[CLK] START - reset tick to 0"); + } continue; }