From c44bfb033b96f73a8f9bd897142fcf651ab36bb4 Mon Sep 17 00:00:00 2001 From: Ashley Strahle Date: Thu, 2 Jul 2026 06:22:23 +0000 Subject: [PATCH] Fix button-to-MIDI latency: remove blocking serial, reduce debounce, faster loop Latency sources fixed: - debounce 50ms -> 5ms (switch_stub.cpp:15) - Removed [MIDI OUT] Serial.printf from send_cc() (midi_transport.cpp:93-98) - Removed [APP] Switch pressed/released prints from update() (app_task.cpp:39,43) - Removed [APP] Switch -> CC print from process_switch_event() (app_task.cpp:140-143) - loop() delay 10ms -> 1ms (main.cpp:390) Serial output at 115200 baud was taking 2-5ms per printf call in the critical button-to-MIDI path, adding 7-15ms+ total latency per button press. All removed from hot path. --- src/app_task.cpp | 6 ------ src/main.cpp | 2 +- src/midi_transport.cpp | 1 - src/switch_stub.cpp | 2 +- 4 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/app_task.cpp b/src/app_task.cpp index b3ad99c..eb90559 100644 --- a/src/app_task.cpp +++ b/src/app_task.cpp @@ -36,11 +36,9 @@ void AppTask::update() { bool is_pressed = switch_driver->is_pressed(i); if (is_pressed && !last_switch_state[i]) { - Serial.printf("[APP] Switch %d pressed\n", i); process_switch_event(i, true); last_switch_state[i] = true; } else if (!is_pressed && last_switch_state[i]) { - Serial.printf("[APP] Switch %d released\n", i); process_switch_event(i, false); last_switch_state[i] = false; } @@ -136,10 +134,6 @@ void AppTask::process_switch_event(uint8_t switch_id, bool pressed) { if (pressed) { midi_transport->send_cc(channel, cc_num, value); } - - Serial.printf("[APP] Switch %d -> Ch%d CC%d Val%d (%s)\n", - switch_id, channel, cc_num, value, - pressed ? "PRESS" : "RELEASE"); break; } } diff --git a/src/main.cpp b/src/main.cpp index a0563ab..ad42692 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -387,5 +387,5 @@ void loop() { handle_serial_command(cmd); } - delay(10); + delay(1); } diff --git a/src/midi_transport.cpp b/src/midi_transport.cpp index 91502d1..8276b18 100644 --- a/src/midi_transport.cpp +++ b/src/midi_transport.cpp @@ -94,7 +94,6 @@ void UsbMidiTransport::send_cc(uint8_t channel, uint8_t cc, uint8_t value) { if (!initialized) return; uint8_t packet[4] = {0x0B, (uint8_t)(0xB0 | (channel - 1)), cc, value}; usb_midi.writePacket(packet); - Serial.printf("[MIDI OUT] Ch:%d CC:%d:%d\n", channel, cc, value); } bool UsbMidiTransport::is_connected() { diff --git a/src/switch_stub.cpp b/src/switch_stub.cpp index 3255f00..d33cad7 100644 --- a/src/switch_stub.cpp +++ b/src/switch_stub.cpp @@ -11,7 +11,7 @@ DefaultSwitchStub::DefaultSwitchStub() : initialized(false) { switch_states[i].current_state = false; switch_states[i].previous_state = false; switch_states[i].last_change_time = 0; - switch_states[i].debounce_time = 50; + switch_states[i].debounce_time = 5; } }