From 2d3a5a9031ef3212d15655579ca4bb6da9c164de Mon Sep 17 00:00:00 2001 From: Ashley Strahle Date: Tue, 30 Jun 2026 21:59:55 +0000 Subject: [PATCH] queue MIDI sends with delay(1) to avoid race; force legacy advertising --- include/app_task.h | 5 +++++ platformio.ini | 1 + src/app_task.cpp | 21 ++++++++++++++++----- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/app_task.h b/include/app_task.h index 5f7f7c4..a3752c4 100644 --- a/include/app_task.h +++ b/include/app_task.h @@ -38,6 +38,11 @@ private: uint8_t sysex_len = 0; bool sysex_active = false; + bool pending_cc = false; + uint8_t pending_channel = 0; + uint8_t pending_cc_num = 0; + uint8_t pending_value = 0; + void process_switch_event(uint8_t switch_id, bool pressed); void run_palette_test(); void handle_sysex(const uint8_t* data, uint8_t len); diff --git a/platformio.ini b/platformio.ini index f21db17..41477e8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,6 +18,7 @@ build_flags = -DARDUINO_USB_CDC_ON_BOOT=1 -DUSE_TINYUSB=1 -Wno-macro-redefined + -DMYNEWT_VAL_BLE_EXT_ADV=0 monitor_speed = 115200 diff --git a/src/app_task.cpp b/src/app_task.cpp index 24fd29b..cc3d152 100644 --- a/src/app_task.cpp +++ b/src/app_task.cpp @@ -49,6 +49,13 @@ void AppTask::update() { last_switch_state[i] = false; } } + + if (pending_cc) { + pending_cc = false; + delay(1); + usb_midi->send_cc(pending_channel, pending_cc_num, pending_value); + if (ble_midi) ble_midi->send_cc(pending_channel, pending_cc_num, pending_value); + } } void AppTask::process_midi_event(const MidiEvent& event) { @@ -136,14 +143,18 @@ void AppTask::process_switch_event(uint8_t switch_id, bool pressed) { uint8_t cc_num = cc_map[i]; uint8_t value = pressed ? 127 : 0; - if (pressed) { - usb_midi->send_cc(channel, cc_num, value); - if (ble_midi) ble_midi->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"); + + if (pressed) { + // Send MIDI via a flag that loop() processes + pending_cc = true; + pending_channel = channel; + pending_cc_num = cc_num; + pending_value = value; + } + break; } }