From 87819f653e4269ebb863da5b184cabcac8de107a Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月12日 11:21:01 -0300 Subject: [PATCH 01/28] feat(usb): allow the MIDI constructor to define a device name This PR will allow the user to modify the MIDI USB Device Name (device descriptor). Fomer API continues to be valid and the default device name is "TinyUSB MIDI". The user can change the device name by passing a string to the constructor, declaring it like this `USBMIDI myMIDI("MyDeviceName"); --- libraries/USB/src/USBMIDI.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index 8a9571855e1..12b8489e3f8 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -11,14 +11,14 @@ static bool tinyusb_midi_descriptor_loaded = false; static bool tinyusb_midi_interface_enabled = false; +static String deviceDescriptor(""); extern "C" uint16_t tusb_midi_load_descriptor(uint8_t *dst, uint8_t *itf) { if (tinyusb_midi_descriptor_loaded) { return 0; } tinyusb_midi_descriptor_loaded = true; - - uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB MIDI"); + uint8_t str_index = tinyusb_add_string_descriptor(deviceDescriptor.c_str()); uint8_t ep_in = tinyusb_get_free_in_endpoint(); TU_VERIFY(ep_in != 0); uint8_t ep_out = tinyusb_get_free_out_endpoint(); @@ -32,9 +32,10 @@ extern "C" uint16_t tusb_midi_load_descriptor(uint8_t *dst, uint8_t *itf) { return TUD_MIDI_DESC_LEN; } -USBMIDI::USBMIDI() { +USBMIDI::USBMIDI(String devDescName = "TinyUSB MIDI") { if (!tinyusb_midi_interface_enabled) { tinyusb_midi_interface_enabled = true; + deviceDescriptor = devDescName; tinyusb_enable_interface(USB_INTERFACE_MIDI, TUD_MIDI_DESC_LEN, tusb_midi_load_descriptor); } else { log_e("USBMIDI: Multiple instances of USBMIDI not supported!"); From e8d506cd257aab51e9290bec4154a6cc5e3b7738 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月12日 11:28:01 -0300 Subject: [PATCH 02/28] feat(usb): changes the MIDI device descriptor in the example --- libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino b/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino index e3ad1c4e028..a6d0d256abb 100644 --- a/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino +++ b/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino @@ -26,7 +26,9 @@ void loop() {} #include "USB.h" #include "USBMIDI.h" -USBMIDI MIDI; +// Create the MIDI device with specific descriptor +USBMIDI MIDI("ESP MIDI Device"); + #define MIDI_RX 39 #define MIDI_TX 40 From 36ac341273f65d2a9a0d4d6c3d83867dea75e84b Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月12日 11:29:17 -0300 Subject: [PATCH 03/28] feat(usb): changes the MIDI device descriptor in the example --- libraries/USB/examples/MIDI/MidiController/MidiController.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/USB/examples/MIDI/MidiController/MidiController.ino b/libraries/USB/examples/MIDI/MidiController/MidiController.ino index 2871d3b1a52..1628af840c3 100644 --- a/libraries/USB/examples/MIDI/MidiController/MidiController.ino +++ b/libraries/USB/examples/MIDI/MidiController/MidiController.ino @@ -20,7 +20,8 @@ void loop() {} #include "USB.h" #include "USBMIDI.h" -USBMIDI MIDI; +// Create the MIDI device with specific descriptor +USBMIDI MIDI("ESP MIDI Device"); #define MIDI_NOTE_C4 60 From 74fa5308e0295485dd71c87881c211829caa4c62 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月12日 13:10:08 -0300 Subject: [PATCH 04/28] fix(usb): typo in commentary - start CI again --- libraries/USB/examples/MIDI/MidiController/MidiController.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/USB/examples/MIDI/MidiController/MidiController.ino b/libraries/USB/examples/MIDI/MidiController/MidiController.ino index 1628af840c3..9cdd8a805de 100644 --- a/libraries/USB/examples/MIDI/MidiController/MidiController.ino +++ b/libraries/USB/examples/MIDI/MidiController/MidiController.ino @@ -20,7 +20,7 @@ void loop() {} #include "USB.h" #include "USBMIDI.h" -// Create the MIDI device with specific descriptor +// Creates the MIDI device with specific descriptor USBMIDI MIDI("ESP MIDI Device"); #define MIDI_NOTE_C4 60 From 5af16d1496cc0739560333272b3bacd50df1025e Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月12日 13:18:17 -0300 Subject: [PATCH 05/28] feat(usb): allow the MIDI constructor to define a device name --- libraries/USB/src/USBMIDI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/USB/src/USBMIDI.h b/libraries/USB/src/USBMIDI.h index 91a1bfa4be1..a1071ce5188 100644 --- a/libraries/USB/src/USBMIDI.h +++ b/libraries/USB/src/USBMIDI.h @@ -19,7 +19,7 @@ typedef struct { class USBMIDI { public: - USBMIDI(void); + USBMIDI(String devDescName = "TinyUSB MIDI"); void begin(void); void end(void); From 8bb446bc662852f9b5140f7d0a130570b29fc3b9 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月12日 13:19:09 -0300 Subject: [PATCH 06/28] fix(usb): correct constructor declaration --- libraries/USB/src/USBMIDI.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index 12b8489e3f8..407638f2d7d 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -32,7 +32,7 @@ extern "C" uint16_t tusb_midi_load_descriptor(uint8_t *dst, uint8_t *itf) { return TUD_MIDI_DESC_LEN; } -USBMIDI::USBMIDI(String devDescName = "TinyUSB MIDI") { +USBMIDI::USBMIDI(String devDescName) { if (!tinyusb_midi_interface_enabled) { tinyusb_midi_interface_enabled = true; deviceDescriptor = devDescName; From fdd6ccd46eae4d644b778f532435339e48a10a24 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月12日 13:20:45 -0300 Subject: [PATCH 07/28] fix(usb): typo in commentary - start CI again --- libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino b/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino index a6d0d256abb..0abc2aad2d1 100644 --- a/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino +++ b/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino @@ -26,7 +26,7 @@ void loop() {} #include "USB.h" #include "USBMIDI.h" -// Create the MIDI device with specific descriptor +// Creates the MIDI device with specific descriptor USBMIDI MIDI("ESP MIDI Device"); From 473a4c69cf35fec4980d5e7df164d1727a5b7355 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月22日 13:16:09 -0300 Subject: [PATCH 08/28] feat(usb_midi): Enhance USBMIDI with device name --- libraries/USB/src/USBMIDI.h | 45 ++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/libraries/USB/src/USBMIDI.h b/libraries/USB/src/USBMIDI.h index a1071ce5188..7a4a6e52952 100644 --- a/libraries/USB/src/USBMIDI.h +++ b/libraries/USB/src/USBMIDI.h @@ -18,10 +18,53 @@ typedef struct { } midiEventPacket_t; class USBMIDI { +private: + static const char* deviceName; + static char nameBuffer[32]; // Buffer to store the device name + /** + * @brief Get the default device name + * @return Default name "TinyUSB MIDI" if no other name is set + */ + static const char* getDefaultDeviceName() { + return "TinyUSB MIDI"; + } + public: - USBMIDI(String devDescName = "TinyUSB MIDI"); + /** + * @brief Default constructor + * Will use the compile-time name if set via SET_USB_MIDI_DEVICE_NAME(), + * otherwise uses "TinyUSB MIDI" + */ + USBMIDI(void); + + /** + * @brief Constructor with custom device name + * @param name The device name to use. This takes precedence over any + * compile-time name set via SET_USB_MIDI_DEVICE_NAME() + */ + USBMIDI(const char* name); + void begin(void); void end(void); + + /** + * @brief Get the current device name + * @return The device name in order of precedence: + * 1. Name set via constructor (if any) + * 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) + * 3. Default name "TinyUSB MIDI" + */ + static const char* getCurrentDeviceName(void) { + return deviceName ? deviceName : getDefaultDeviceName(); + } + + /** + * @brief Set the default name at compile time + * @param name The name to set as default if no runtime name is provided + */ + static void setDefaultName(const char* name) { + if (!deviceName) deviceName = name; + } /* User-level API */ From 75d04dbea7ba0ad28aa5e9fc81dbe8cae8f21e57 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月22日 13:17:39 -0300 Subject: [PATCH 09/28] feat(usb_midi): Refactor USBMIDI with device name handling --- libraries/USB/src/USBMIDI.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index 407638f2d7d..4a4b1213728 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -6,19 +6,23 @@ #include "Arduino.h" #include "esp32-hal-tinyusb.h" +// Initialize static members +const char* USBMIDI::deviceName = nullptr; +char USBMIDI::nameBuffer[32] = {0}; + // Default Cable Number (for simplified APIs that do not expose this) #define DEFAULT_CN 0 static bool tinyusb_midi_descriptor_loaded = false; static bool tinyusb_midi_interface_enabled = false; -static String deviceDescriptor(""); extern "C" uint16_t tusb_midi_load_descriptor(uint8_t *dst, uint8_t *itf) { if (tinyusb_midi_descriptor_loaded) { return 0; } tinyusb_midi_descriptor_loaded = true; - uint8_t str_index = tinyusb_add_string_descriptor(deviceDescriptor.c_str()); + + uint8_t str_index = tinyusb_add_string_descriptor(USBMIDI::getCurrentDeviceName()); uint8_t ep_in = tinyusb_get_free_in_endpoint(); TU_VERIFY(ep_in != 0); uint8_t ep_out = tinyusb_get_free_out_endpoint(); @@ -32,10 +36,21 @@ extern "C" uint16_t tusb_midi_load_descriptor(uint8_t *dst, uint8_t *itf) { return TUD_MIDI_DESC_LEN; } -USBMIDI::USBMIDI(String devDescName) { +USBMIDI::USBMIDI() { + if (!tinyusb_midi_interface_enabled) { + tinyusb_midi_interface_enabled = true; + tinyusb_enable_interface(USB_INTERFACE_MIDI, TUD_MIDI_DESC_LEN, tusb_midi_load_descriptor); + } else { + log_e("USBMIDI: Multiple instances of USBMIDI not supported!"); + } +} + +USBMIDI::USBMIDI(const char* name) { if (!tinyusb_midi_interface_enabled) { + strncpy(nameBuffer, name, sizeof(nameBuffer) - 1); + nameBuffer[sizeof(nameBuffer) - 1] = '0円'; + deviceName = nameBuffer; tinyusb_midi_interface_enabled = true; - deviceDescriptor = devDescName; tinyusb_enable_interface(USB_INTERFACE_MIDI, TUD_MIDI_DESC_LEN, tusb_midi_load_descriptor); } else { log_e("USBMIDI: Multiple instances of USBMIDI not supported!"); From b3fbce7735488f44f316d4c00bbdc24f71efc3b8 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月22日 13:19:28 -0300 Subject: [PATCH 10/28] feat(usb_midi): Add macro to set USB MIDI device name --- cores/esp32/Arduino.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 9048249a873..478b811e789 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -222,6 +222,16 @@ size_t getArduinoLoopTaskStackSize(void); return sz; \ } +#define SET_USB_MIDI_DEVICE_NAME(name) \ + namespace { \ + static const char* _usb_midi_default_name = name; \ + static struct _USBMIDINameSetter { \ + _USBMIDINameSetter() { \ + USBMIDI::setDefaultName(_usb_midi_default_name); \ + } \ + } _usb_midi_name_setter; \ + } + bool shouldPrintChipDebugReport(void); #define ENABLE_CHIP_DEBUG_REPORT \ bool shouldPrintChipDebugReport(void) { \ From c930bc14a1fd6147cbeda6d3c4866a610324545c Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月22日 13:22:31 -0300 Subject: [PATCH 11/28] feat(usb_midi): demonstrate the use of macro to change the device name Updated MIDI device initialization to use default constructor. --- libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino b/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino index 0abc2aad2d1..86d05d846e9 100644 --- a/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino +++ b/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino @@ -24,10 +24,13 @@ void setup() {} void loop() {} #else +// define a new USB MIDI device name using a macro +SET_USB_MIDI_DEVICE_NAME("ESP MIDI Device") + #include "USB.h" #include "USBMIDI.h" // Creates the MIDI device with specific descriptor -USBMIDI MIDI("ESP MIDI Device"); +USBMIDI MIDI(); #define MIDI_RX 39 From 40fbd4f14111eef5f7182fa8d8c6a8e0f9341022 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月22日 13:24:38 -0300 Subject: [PATCH 12/28] fix(usb_midi): reduce changes to the code --- libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino b/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino index 86d05d846e9..e118d5ddb9a 100644 --- a/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino +++ b/libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino @@ -29,9 +29,8 @@ SET_USB_MIDI_DEVICE_NAME("ESP MIDI Device") #include "USB.h" #include "USBMIDI.h" -// Creates the MIDI device with specific descriptor -USBMIDI MIDI(); - +// Creates the MIDI device with specific name defined with the SET_USB_MIDI_DEVICE_NAME() macro +USBMIDI MIDI; #define MIDI_RX 39 #define MIDI_TX 40 From fb36be8ab5097e1777509c965d83cf13fd145a81 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: 2025年8月22日 16:47:27 -0300 Subject: [PATCH 13/28] fix(usb_midi): Add macro guards --- cores/esp32/Arduino.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 478b811e789..474d3908f80 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -222,6 +222,9 @@ size_t getArduinoLoopTaskStackSize(void); return sz; \ } +#if CONFIG_TINYUSB_MIDI_ENABLE +// Forward declaration of USBMIDI class for the SET_USB_MIDI_DEVICE_NAME macro +#include "USB/USBMIDI.h" #define SET_USB_MIDI_DEVICE_NAME(name) \ namespace { \ static const char* _usb_midi_default_name = name; \ @@ -231,7 +234,17 @@ size_t getArduinoLoopTaskStackSize(void); } \ } _usb_midi_name_setter; \ } - +#else +#define SET_USB_MIDI_DEVICE_NAME(name) \ + namespace { \ + static struct _USBMIDINameSetter { \ + _USBMIDINameSetter() { \ + log_e("USB MIDI is not enabled. Enable it in menuconfig under Component Config -> TinyUSB -> CDC Enable"); \ + } \ + } _usb_midi_name_setter; \ + } +#endif + bool shouldPrintChipDebugReport(void); #define ENABLE_CHIP_DEBUG_REPORT \ bool shouldPrintChipDebugReport(void) { \ From b7b61c353b9d7254e59166181ea51ab4d81a55fa Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:37:21 -0300 Subject: [PATCH 14/28] feat(midi): add midi dev name from macro --- cores/esp32/Arduino.h | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 474d3908f80..b249c43d930 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -222,28 +222,14 @@ size_t getArduinoLoopTaskStackSize(void); return sz; \ } -#if CONFIG_TINYUSB_MIDI_ENABLE -// Forward declaration of USBMIDI class for the SET_USB_MIDI_DEVICE_NAME macro -#include "USB/USBMIDI.h" -#define SET_USB_MIDI_DEVICE_NAME(name) \ - namespace { \ - static const char* _usb_midi_default_name = name; \ - static struct _USBMIDINameSetter { \ - _USBMIDINameSetter() { \ - USBMIDI::setDefaultName(_usb_midi_default_name); \ - } \ - } _usb_midi_name_setter; \ - } -#else -#define SET_USB_MIDI_DEVICE_NAME(name) \ - namespace { \ - static struct _USBMIDINameSetter { \ - _USBMIDINameSetter() { \ - log_e("USB MIDI is not enabled. Enable it in menuconfig under Component Config -> TinyUSB -> CDC Enable"); \ - } \ - } _usb_midi_name_setter; \ - } -#endif +#define ESP32_USB_MIDI_DEFAULT_NAME "TinyUSB MIDI" +#define SET_USB_MIDI_DEVICE_NAME(name) \ + const char* getUSBMIDIDefaultDeviceName() { \ + if (strlen(name) == 0) { \ + return ESP32_USB_MIDI_DEFAULT_NAME; \ + } \ + return name; \ + } bool shouldPrintChipDebugReport(void); #define ENABLE_CHIP_DEBUG_REPORT \ From 1b4aea2201db67fa7569d692a0b1d8aeee3ee11c Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:41:37 -0300 Subject: [PATCH 15/28] feat(midi): Enhance USBMIDI device name handling --- libraries/USB/src/USBMIDI.cpp | 55 +++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index 563efce8935..49707691a05 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -7,8 +7,11 @@ #include "esp32-hal-tinyusb.h" // Initialize static members -const char* USBMIDI::deviceName = nullptr; -char USBMIDI::nameBuffer[32] = {0}; +char* USBMIDI::midiUserDeviceName = nullptr; +// Weak definition of getUSBMIDIDefaultDeviceName to provide a default name +__attribute__((weak)) const char* getUSBMIDIDefaultDeviceName() { + return ESP32_USB_MIDI_DEFAULT_NAME; +} // Default Cable Number (for simplified APIs that do not expose this) #define DEFAULT_CN 0 @@ -45,11 +48,29 @@ USBMIDI::USBMIDI() { } } + +void USBMIDI::setDeviceName(const char* name) { + const uint8_t maxNameLength = 32; // tinyUSB Descriptor limit + if (name != nullptr && strlen(name)> 0) { + if (strlen(name)> maxNameLength) { + log_w("USBMIDI: Device name too long, truncating to %d characters.", maxNameLength); + } + midiUserDeviceName = new char[maxNameLength + 1]; // +1 for null-terminator + if (midiUserDeviceName) { + strncpy(midiUserDeviceName, name, maxNameLength); + // Ensure null-termination when overflowing + midiUserDeviceName[maxNameLength] = '0円'; + } else { + log_e("USBMIDI: Failed to allocate memory for device name, using default name."); + } + } else { + log_w("USBMIDI: No device name provided, using default name [%s].", getUSBMIDIDefaultDeviceName()); + } +} + USBMIDI::USBMIDI(const char* name) { if (!tinyusb_midi_interface_enabled) { - strncpy(nameBuffer, name, sizeof(nameBuffer) - 1); - nameBuffer[sizeof(nameBuffer) - 1] = '0円'; - deviceName = nameBuffer; + setDeviceName(name); tinyusb_midi_interface_enabled = true; tinyusb_enable_interface(USB_INTERFACE_MIDI, TUD_MIDI_DESC_LEN, tusb_midi_load_descriptor); } else { @@ -57,9 +78,33 @@ USBMIDI::USBMIDI(const char* name) { } } +USBMIDI::~USBMIDI() { + if (midiUserDeviceName) { + delete[] midiUserDeviceName; + midiUserDeviceName = nullptr; + } +} + void USBMIDI::begin() {} void USBMIDI::end() {} +/** +* @brief Get the current device name +* @return The device name in order of precedence: +* 1. Name set via constructor (if any) +* 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) +* 3. Default name "TinyUSB MIDI" +* If device name is set as "", it will be ignored +*/ +const char* USBMIDI::getCurrentDeviceName(void) { + if (midiUserDeviceName) { + return midiUserDeviceName; + } + // If no user name set, use the compile-time default name limited to 32 characters + setDeviceName(getUSBMIDIDefaultDeviceName()); + return strlen(midiUserDeviceName) ? midiUserDeviceName : "TinyUSB MIDI"; +} + // uint compatible version of constrain #define uconstrain(amt, low, high) ((amt) <= (low) ? (low) : ((amt)> (high) ? (high) : (amt))) From 60e4a0bb75ff7da9df07d6b25006a7683441ea21 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:46:30 -0300 Subject: [PATCH 16/28] feat(midi): USBMIDI class for device name --- libraries/USB/src/USBMIDI.h | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/libraries/USB/src/USBMIDI.h b/libraries/USB/src/USBMIDI.h index 7a4a6e52952..d85cbc084eb 100644 --- a/libraries/USB/src/USBMIDI.h +++ b/libraries/USB/src/USBMIDI.h @@ -19,15 +19,8 @@ typedef struct { class USBMIDI { private: - static const char* deviceName; - static char nameBuffer[32]; // Buffer to store the device name - /** - * @brief Get the default device name - * @return Default name "TinyUSB MIDI" if no other name is set - */ - static const char* getDefaultDeviceName() { - return "TinyUSB MIDI"; - } + static char* midiUserDeviceName; // user device name + static void setDeviceName(const char* name); // set user device name limites to 32 characters public: /** @@ -38,11 +31,15 @@ class USBMIDI { USBMIDI(void); /** - * @brief Constructor with custom device name - * @param name The device name to use. This takes precedence over any - * compile-time name set via SET_USB_MIDI_DEVICE_NAME() - */ + * @brief Set the current device name + * 1. Name set via constructor (if any) + * 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) + * 3. Default name "TinyUSB MIDI" + * It has no effect if name is set as NULL or "" + */ USBMIDI(const char* name); + + ~USBMIDI(); void begin(void); void end(void); @@ -53,18 +50,8 @@ class USBMIDI { * 1. Name set via constructor (if any) * 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) * 3. Default name "TinyUSB MIDI" - */ - static const char* getCurrentDeviceName(void) { - return deviceName ? deviceName : getDefaultDeviceName(); - } - - /** - * @brief Set the default name at compile time - * @param name The name to set as default if no runtime name is provided - */ - static void setDefaultName(const char* name) { - if (!deviceName) deviceName = name; - } + */ + static const char* getCurrentDeviceName(void); /* User-level API */ From 219f8383e910d81eb128a8e39d29de1ad035d12e Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:47:35 -0300 Subject: [PATCH 17/28] fix(midi): comment typo --- libraries/USB/src/USBMIDI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/USB/src/USBMIDI.h b/libraries/USB/src/USBMIDI.h index d85cbc084eb..056d60bbedf 100644 --- a/libraries/USB/src/USBMIDI.h +++ b/libraries/USB/src/USBMIDI.h @@ -20,7 +20,7 @@ typedef struct { class USBMIDI { private: static char* midiUserDeviceName; // user device name - static void setDeviceName(const char* name); // set user device name limites to 32 characters + static void setDeviceName(const char* name); // set user device name limited to 32 characters public: /** From 10e1948541a348cc86576de5cd857f671cb660d6 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:48:19 -0300 Subject: [PATCH 18/28] fix(rmt): fixes bad commentary formating --- libraries/USB/src/USBMIDI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/USB/src/USBMIDI.h b/libraries/USB/src/USBMIDI.h index 056d60bbedf..e14676deafa 100644 --- a/libraries/USB/src/USBMIDI.h +++ b/libraries/USB/src/USBMIDI.h @@ -27,7 +27,7 @@ class USBMIDI { * @brief Default constructor * Will use the compile-time name if set via SET_USB_MIDI_DEVICE_NAME(), * otherwise uses "TinyUSB MIDI" - */ + */ USBMIDI(void); /** From 62659c9bc3ad14bc55f3cbf306945b14703315d3 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:51:23 -0300 Subject: [PATCH 19/28] feat(rmt): more commentaries --- libraries/USB/src/USBMIDI.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index 49707691a05..aaf3486c063 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -48,7 +48,7 @@ USBMIDI::USBMIDI() { } } - +// private function for setting a not null/empty MIDI device name limited to 32 characters void USBMIDI::setDeviceName(const char* name) { const uint8_t maxNameLength = 32; // tinyUSB Descriptor limit if (name != nullptr && strlen(name)> 0) { From 0d4dce25ef3c603c9320006f400f611dd275eae7 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:52:23 -0300 Subject: [PATCH 20/28] fix(midi): move commentaries --- libraries/USB/src/USBMIDI.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index aaf3486c063..b2076ec537a 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -88,14 +88,6 @@ USBMIDI::~USBMIDI() { void USBMIDI::begin() {} void USBMIDI::end() {} -/** -* @brief Get the current device name -* @return The device name in order of precedence: -* 1. Name set via constructor (if any) -* 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) -* 3. Default name "TinyUSB MIDI" -* If device name is set as "", it will be ignored -*/ const char* USBMIDI::getCurrentDeviceName(void) { if (midiUserDeviceName) { return midiUserDeviceName; From f3bd145c033c80ed5b8280a8091f2d35a5f031a7 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:53:20 -0300 Subject: [PATCH 21/28] fix(midi): move commentaries --- libraries/USB/src/USBMIDI.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index b2076ec537a..7f768eaaccf 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -68,6 +68,14 @@ void USBMIDI::setDeviceName(const char* name) { } } +/** +* @brief Get the current device name +* @return The device name in order of precedence: +* 1. Name set via constructor (if any) +* 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) +* 3. Default name "TinyUSB MIDI" +* If device name is set as "", it will be ignored +*/ USBMIDI::USBMIDI(const char* name) { if (!tinyusb_midi_interface_enabled) { setDeviceName(name); From c5cb5f084f6b958fa168141d457a085f895ccc46 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:55:57 -0300 Subject: [PATCH 22/28] feat(midi): add more commentaries --- cores/esp32/Arduino.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index b249c43d930..025d1c5bdac 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -223,6 +223,13 @@ size_t getArduinoLoopTaskStackSize(void); } #define ESP32_USB_MIDI_DEFAULT_NAME "TinyUSB MIDI" +/** +* @brief Set the current device name +* 1. Name set via constructor (if any) +* 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) +* 3. Default name "TinyUSB MIDI" +* If device name is set as "", it will be ignored +*/ #define SET_USB_MIDI_DEVICE_NAME(name) \ const char* getUSBMIDIDefaultDeviceName() { \ if (strlen(name) == 0) { \ From fa7d041eff36ec869c611fc8fa1d72979839bfb8 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 10:57:09 -0300 Subject: [PATCH 23/28] fix(midi): fixes constructor commentary --- libraries/USB/src/USBMIDI.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index 7f768eaaccf..057c1742ee5 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -69,8 +69,7 @@ void USBMIDI::setDeviceName(const char* name) { } /** -* @brief Get the current device name -* @return The device name in order of precedence: +* @brief Constructor for setting the current device name * 1. Name set via constructor (if any) * 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) * 3. Default name "TinyUSB MIDI" From ce55c9932a6e897ecb30542069db22400635318a Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 11:09:52 -0300 Subject: [PATCH 24/28] fix(midi): safeguard for memory leak --- libraries/USB/src/USBMIDI.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index 057c1742ee5..bf3b34487c5 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -50,12 +50,15 @@ USBMIDI::USBMIDI() { // private function for setting a not null/empty MIDI device name limited to 32 characters void USBMIDI::setDeviceName(const char* name) { + printf("setName called"); const uint8_t maxNameLength = 32; // tinyUSB Descriptor limit if (name != nullptr && strlen(name)> 0) { if (strlen(name)> maxNameLength) { log_w("USBMIDI: Device name too long, truncating to %d characters.", maxNameLength); } - midiUserDeviceName = new char[maxNameLength + 1]; // +1 for null-terminator + if (!midiUserDeviceName) { + midiUserDeviceName = new char[maxNameLength + 1]; // +1 for null-terminator + } if (midiUserDeviceName) { strncpy(midiUserDeviceName, name, maxNameLength); // Ensure null-termination when overflowing From c14e76941b37119a9ec8602eb0c153347bca3d69 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 11:15:46 -0300 Subject: [PATCH 25/28] fix(midi): removes debug logging Co-authored-by: Copilot <175728472+copilot@users.noreply.github.com> --- libraries/USB/src/USBMIDI.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index bf3b34487c5..1d03e380167 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -50,7 +50,6 @@ USBMIDI::USBMIDI() { // private function for setting a not null/empty MIDI device name limited to 32 characters void USBMIDI::setDeviceName(const char* name) { - printf("setName called"); const uint8_t maxNameLength = 32; // tinyUSB Descriptor limit if (name != nullptr && strlen(name)> 0) { if (strlen(name)> maxNameLength) { From 861abb97c455747e5f6ebc5873472c29c69f5076 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 11:16:47 -0300 Subject: [PATCH 26/28] feat(midi): explicit safer test and return Co-authored-by: Copilot <175728472+copilot@users.noreply.github.com> --- libraries/USB/src/USBMIDI.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index 1d03e380167..e1c8fa4835f 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -103,7 +103,11 @@ const char* USBMIDI::getCurrentDeviceName(void) { } // If no user name set, use the compile-time default name limited to 32 characters setDeviceName(getUSBMIDIDefaultDeviceName()); - return strlen(midiUserDeviceName) ? midiUserDeviceName : "TinyUSB MIDI"; + if (midiUserDeviceName && strlen(midiUserDeviceName)) { + return midiUserDeviceName; + } else { + return "TinyUSB MIDI"; + } } // uint compatible version of constrain From 3f59ac650e3fee7f4be63aec40922ff2bb23fe8b Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 9 Sep 2025 16:30:41 -0300 Subject: [PATCH 27/28] feat(midi): avoids possible strlen(NULL) --- cores/esp32/Arduino.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 025d1c5bdac..55af6844ae8 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -232,7 +232,7 @@ size_t getArduinoLoopTaskStackSize(void); */ #define SET_USB_MIDI_DEVICE_NAME(name) \ const char* getUSBMIDIDefaultDeviceName() { \ - if (strlen(name) == 0) { \ + if (!name || strlen(name) == 0) { \ return ESP32_USB_MIDI_DEFAULT_NAME; \ } \ return name; \ From 4bca7060139d5ea7d9fbd7a8bc1186470363df48 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: 2025年9月10日 12:19:41 +0000 Subject: [PATCH 28/28] ci(pre-commit): Apply automatic fixes --- cores/esp32/Arduino.h | 4 +-- libraries/USB/src/USBMIDI.cpp | 46 +++++++++++++++++------------------ libraries/USB/src/USBMIDI.h | 12 ++++----- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 55af6844ae8..b5c5a456abf 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -231,8 +231,8 @@ size_t getArduinoLoopTaskStackSize(void); * If device name is set as "", it will be ignored */ #define SET_USB_MIDI_DEVICE_NAME(name) \ - const char* getUSBMIDIDefaultDeviceName() { \ - if (!name || strlen(name) == 0) { \ + const char *getUSBMIDIDefaultDeviceName() { \ + if (!name || strlen(name) == 0) { \ return ESP32_USB_MIDI_DEFAULT_NAME; \ } \ return name; \ diff --git a/libraries/USB/src/USBMIDI.cpp b/libraries/USB/src/USBMIDI.cpp index e1c8fa4835f..61b75f0144d 100644 --- a/libraries/USB/src/USBMIDI.cpp +++ b/libraries/USB/src/USBMIDI.cpp @@ -7,9 +7,9 @@ #include "esp32-hal-tinyusb.h" // Initialize static members -char* USBMIDI::midiUserDeviceName = nullptr; +char *USBMIDI::midiUserDeviceName = nullptr; // Weak definition of getUSBMIDIDefaultDeviceName to provide a default name -__attribute__((weak)) const char* getUSBMIDIDefaultDeviceName() { +__attribute__((weak)) const char *getUSBMIDIDefaultDeviceName() { return ESP32_USB_MIDI_DEFAULT_NAME; } @@ -49,25 +49,25 @@ USBMIDI::USBMIDI() { } // private function for setting a not null/empty MIDI device name limited to 32 characters -void USBMIDI::setDeviceName(const char* name) { - const uint8_t maxNameLength = 32; // tinyUSB Descriptor limit - if (name != nullptr && strlen(name)> 0) { - if (strlen(name)> maxNameLength) { - log_w("USBMIDI: Device name too long, truncating to %d characters.", maxNameLength); - } - if (!midiUserDeviceName) { - midiUserDeviceName = new char[maxNameLength + 1]; // +1 for null-terminator - } - if (midiUserDeviceName) { - strncpy(midiUserDeviceName, name, maxNameLength); - // Ensure null-termination when overflowing - midiUserDeviceName[maxNameLength] = '0円'; - } else { - log_e("USBMIDI: Failed to allocate memory for device name, using default name."); - } +void USBMIDI::setDeviceName(const char *name) { + const uint8_t maxNameLength = 32; // tinyUSB Descriptor limit + if (name != nullptr && strlen(name)> 0) { + if (strlen(name)> maxNameLength) { + log_w("USBMIDI: Device name too long, truncating to %d characters.", maxNameLength); + } + if (!midiUserDeviceName) { + midiUserDeviceName = new char[maxNameLength + 1]; // +1 for null-terminator + } + if (midiUserDeviceName) { + strncpy(midiUserDeviceName, name, maxNameLength); + // Ensure null-termination when overflowing + midiUserDeviceName[maxNameLength] = '0円'; } else { - log_w("USBMIDI: No device name provided, using default name [%s].", getUSBMIDIDefaultDeviceName()); + log_e("USBMIDI: Failed to allocate memory for device name, using default name."); } + } else { + log_w("USBMIDI: No device name provided, using default name [%s].", getUSBMIDIDefaultDeviceName()); + } } /** @@ -77,7 +77,7 @@ void USBMIDI::setDeviceName(const char* name) { * 3. Default name "TinyUSB MIDI" * If device name is set as "", it will be ignored */ -USBMIDI::USBMIDI(const char* name) { +USBMIDI::USBMIDI(const char *name) { if (!tinyusb_midi_interface_enabled) { setDeviceName(name); tinyusb_midi_interface_enabled = true; @@ -92,17 +92,17 @@ USBMIDI::~USBMIDI() { delete[] midiUserDeviceName; midiUserDeviceName = nullptr; } -} +} void USBMIDI::begin() {} void USBMIDI::end() {} -const char* USBMIDI::getCurrentDeviceName(void) { +const char *USBMIDI::getCurrentDeviceName(void) { if (midiUserDeviceName) { return midiUserDeviceName; } // If no user name set, use the compile-time default name limited to 32 characters - setDeviceName(getUSBMIDIDefaultDeviceName()); + setDeviceName(getUSBMIDIDefaultDeviceName()); if (midiUserDeviceName && strlen(midiUserDeviceName)) { return midiUserDeviceName; } else { diff --git a/libraries/USB/src/USBMIDI.h b/libraries/USB/src/USBMIDI.h index e14676deafa..8f111dbfd4a 100644 --- a/libraries/USB/src/USBMIDI.h +++ b/libraries/USB/src/USBMIDI.h @@ -19,8 +19,8 @@ typedef struct { class USBMIDI { private: - static char* midiUserDeviceName; // user device name - static void setDeviceName(const char* name); // set user device name limited to 32 characters + static char *midiUserDeviceName; // user device name + static void setDeviceName(const char *name); // set user device name limited to 32 characters public: /** @@ -37,13 +37,13 @@ class USBMIDI { * 3. Default name "TinyUSB MIDI" * It has no effect if name is set as NULL or "" */ - USBMIDI(const char* name); - + USBMIDI(const char *name); + ~USBMIDI(); void begin(void); void end(void); - + /** * @brief Get the current device name * @return The device name in order of precedence: @@ -51,7 +51,7 @@ class USBMIDI { * 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) * 3. Default name "TinyUSB MIDI" */ - static const char* getCurrentDeviceName(void); + static const char *getCurrentDeviceName(void); /* User-level API */

AltStyle によって変換されたページ (->オリジナル) /