From 2db09b76bf3404dc56d95690e8a2c07550633279 Mon Sep 17 00:00:00 2001 From: Ashley Strahle Date: Tue, 30 Jun 2026 06:43:58 +0000 Subject: [PATCH] Add BT controller diagnostics, init BLE before USB MIDI --- platformio.ini | 1 - pre_build.py | 63 ++++++++++++++++++++++++++++++++++++-- src/ble_midi_transport.cpp | 16 ++++++++++ src/main.cpp | 10 +++--- 4 files changed, 81 insertions(+), 9 deletions(-) diff --git a/platformio.ini b/platformio.ini index 3ad6ae5..f5c9f42 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,7 +17,6 @@ build_flags = -DARDUINO_USB_MODE=0 -DARDUINO_USB_CDC_ON_BOOT=1 -DUSE_TINYUSB=1 - -DCONFIG_ARDUINO_LOOP_STACK_SIZE=16384 monitor_speed = 115200 diff --git a/pre_build.py b/pre_build.py index 603e007..6bc5ab2 100644 --- a/pre_build.py +++ b/pre_build.py @@ -1,5 +1,6 @@ import os -import fileinput +import re + def patch_usb_ids(): # Find the core's pins_arduino.h for ESP32-S3 DevKitC-1 @@ -15,7 +16,7 @@ def patch_usb_ids(): pins_file = os.path.join(core_variants, "esp32s3", "pins_arduino.h") if os.path.exists(pins_file): - print(f"Patching {pins_file} with Launchpad X VID/PID") + print(f"Patching {pins_file} with JOC Midi USB descriptors") # Read and replace with open(pins_file, 'r') as f: @@ -46,4 +47,60 @@ def patch_usb_ids(): else: print(f"WARNING: Could not find pins_arduino.h at {pins_file}") -patch_usb_ids() \ No newline at end of file + +def patch_sdkconfig_bt(): + # Find the precompiled sdkconfig.h for this board variant + sdk_dir = os.path.expanduser( + "~/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32s3/qio_opi/include" + ) + alt_name = os.path.join(os.environ.get("PLATFORMIO_PACKAGES_DIR", ""), + "framework-arduinoespressif32/tools/sdk/esp32s3/qio_opi/include") + if not os.path.exists(sdk_dir): + sdk_dir = alt_name + + sdkconfig_file = os.path.join(sdk_dir, "sdkconfig.h") + + if not os.path.exists(sdkconfig_file): + print(f"WARNING: sdkconfig.h not found at {sdkconfig_file}") + return + + print(f"Patching {sdkconfig_file} with BLE/NimBLE support") + + with open(sdkconfig_file, 'r') as f: + content = f.read() + + defines = { + "CONFIG_BT_ENABLED": 1, + "CONFIG_BT_NIMBLE_ENABLED": 1, + "CONFIG_BTDM_CTRL_MODE_BLE_ONLY": 1, + "CONFIG_BT_NIMBLE_MAX_CONNECTIONS": 1, + "CONFIG_BT_NIMBLE_TASK_STACK_SIZE": 6144, + "CONFIG_BT_NIMBLE_ROLE_PERIPHERAL": 1, + "CONFIG_BT_NIMBLE_ROLE_BROADCASTER": 1, + } + + changed = False + for name, value in defines.items(): + pattern = re.compile( + r'^[#/]*\s*#?\s*(define\s+' + re.escape(name) + r'\b).*$', + re.MULTILINE + ) + if pattern.search(content): + # Already defined, replace the line + content = pattern.sub(r'#define ' + name + ' ' + str(value), content) + changed = True + elif f'#define {name}' not in content: + # Not present at all, add it + content += f'\n#define {name} {value}' + changed = True + + if changed: + with open(sdkconfig_file, 'w') as f: + f.write(content) + print("BLE/NimBLE sdkconfig patched successfully") + else: + print("BLE/NimBLE already configured in sdkconfig") + + +patch_usb_ids() +patch_sdkconfig_bt() \ No newline at end of file diff --git a/src/ble_midi_transport.cpp b/src/ble_midi_transport.cpp index 5780bb5..3f281fd 100644 --- a/src/ble_midi_transport.cpp +++ b/src/ble_midi_transport.cpp @@ -1,6 +1,8 @@ #include "ble_midi_transport.h" #include #include +#include +#include #define BLE_MIDI_SERVICE_UUID "03B80E5A-EDE8-4B33-A751-6CE34EC4C700" #define BLE_MIDI_CHAR_UUID "7772E5DB-3868-4112-A1A9-F2669D106BF3" @@ -44,6 +46,20 @@ BleMidiTransport::~BleMidiTransport() { bool BleMidiTransport::begin() { Serial.println("[BLE] Initializing BLE MIDI..."); + esp_err_t err; + err = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); + Serial.printf("[BLE] bt_mem_release: %d (%s)\n", err, esp_err_to_name(err)); + + esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); + err = esp_bt_controller_init(&bt_cfg); + Serial.printf("[BLE] bt_controller_init: %d (%s)\n", err, esp_err_to_name(err)); + if (err != ESP_OK) { initialized = false; return false; } + + err = esp_bt_controller_enable(ESP_BT_MODE_BTDM); + Serial.printf("[BLE] bt_controller_enable: %d (%s)\n", err, esp_err_to_name(err)); + if (err != ESP_OK) { initialized = false; return false; } + + Serial.println("[BLE] NimBLEDevice init..."); NimBLEDevice::init("JOC Midi"); Serial.println("[BLE] NimBLEDevice initialized"); diff --git a/src/main.cpp b/src/main.cpp index 5d8f9d1..4aee710 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -346,13 +346,13 @@ void setup() { switch_driver.set_mux(&mux); switch_driver.begin(); + Serial.println("[INIT] Initializing BLE MIDI..."); + delay(1000); + ble_midi_transport.begin(); + Serial.println("[INIT] Initializing USB MIDI..."); midi_transport.begin(); - delay(1500); - - Serial.println("[INIT] Initializing BLE MIDI..."); - delay(2000); - ble_midi_transport.begin(); + delay(500); Serial.println("[INIT] Registering MIDI callbacks..."); controller.begin();