diff --git a/src/main.cpp b/src/main.cpp index ad42692..a76906d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,9 +26,49 @@ TaskHandle_t midi_task_handle = NULL; void midi_task(void* parameter) { Serial.println("[TASK] MIDI task started on core 0"); + // Beat timing fixes for precise beat alignment + const uint32_t BEAT_MS = 500; // 120 BPM target (500ms per beat) + uint32_t last_beat_time = 0; + uint32_t current_real_beat = 0; + uint32_t flash_start = 0; + while (true) { midi_transport.update(); exp_pedal.update(midi_transport); + + uint32_t tick = midi_tick_count; + uint32_t now = millis(); + + // Precise beat detection: calculate which real-time beat we're on + uint32_t real_beat = now / BEAT_MS; + + // Check if we've crossed a real-time beat boundary + if (real_beat != current_real_beat) { + // Track the current real-time beat + current_real_beat = real_beat; + last_beat_time = now; + + // Flash pixel 6 on the precise real-time beat + mux.set_led_color(6, 255, 255, 255); + mux.show(); + flash_start = now; + } + + // Handle flash animation + if (flash_start > 0) { + uint32_t elapsed = now - flash_start; + if (elapsed >= 80) { + mux.set_led_color(6, 20, 20, 20); + mux.show(); + flash_start = 0; + } else { + float b = 1.0f - (float)elapsed / 80.0f; + uint8_t v = (uint8_t)(255 * b * b); + mux.set_led_color(6, v, v, v); + mux.show(); + } + } + vTaskDelay(1); } }