|
7 | 7 | #include "esp32-hal-tinyusb.h"
|
8 | 8 |
|
9 | 9 | // Initialize static members
|
10 |
| -const char* USBMIDI::deviceName = nullptr; |
11 |
| -char USBMIDI::nameBuffer[32] = {0}; |
| 10 | +char* USBMIDI::midiUserDeviceName = nullptr; |
| 11 | +// Weak definition of getUSBMIDIDefaultDeviceName to provide a default name |
| 12 | +__attribute__((weak)) const char* getUSBMIDIDefaultDeviceName() { |
| 13 | + return ESP32_USB_MIDI_DEFAULT_NAME; |
| 14 | +} |
12 | 15 |
|
13 | 16 | // Default Cable Number (for simplified APIs that do not expose this)
|
14 | 17 | #define DEFAULT_CN 0
|
@@ -45,21 +48,63 @@ USBMIDI::USBMIDI() {
|
45 | 48 | }
|
46 | 49 | }
|
47 | 50 |
|
| 51 | + |
| 52 | +void USBMIDI::setDeviceName(const char* name) { |
| 53 | + const uint8_t maxNameLength = 32; // tinyUSB Descriptor limit |
| 54 | + if (name != nullptr && strlen(name) > 0) { |
| 55 | + if (strlen(name) > maxNameLength) { |
| 56 | + log_w("USBMIDI: Device name too long, truncating to %d characters.", maxNameLength); |
| 57 | + } |
| 58 | + midiUserDeviceName = new char[maxNameLength + 1]; // +1 for null-terminator |
| 59 | + if (midiUserDeviceName) { |
| 60 | + strncpy(midiUserDeviceName, name, maxNameLength); |
| 61 | + // Ensure null-termination when overflowing |
| 62 | + midiUserDeviceName[maxNameLength] = '\0'; |
| 63 | + } else { |
| 64 | + log_e("USBMIDI: Failed to allocate memory for device name, using default name."); |
| 65 | + } |
| 66 | + } else { |
| 67 | + log_w("USBMIDI: No device name provided, using default name [%s].", getUSBMIDIDefaultDeviceName()); |
| 68 | + } |
| 69 | +} |
| 70 | + |
48 | 71 | USBMIDI::USBMIDI(const char* name) {
|
49 | 72 | if (!tinyusb_midi_interface_enabled) {
|
50 |
| - strncpy(nameBuffer, name, sizeof(nameBuffer) - 1); |
51 |
| - nameBuffer[sizeof(nameBuffer) - 1] = '\0'; |
52 |
| - deviceName = nameBuffer; |
| 73 | + setDeviceName(name); |
53 | 74 | tinyusb_midi_interface_enabled = true;
|
54 | 75 | tinyusb_enable_interface(USB_INTERFACE_MIDI, TUD_MIDI_DESC_LEN, tusb_midi_load_descriptor);
|
55 | 76 | } else {
|
56 | 77 | log_e("USBMIDI: Multiple instances of USBMIDI not supported!");
|
57 | 78 | }
|
58 | 79 | }
|
59 | 80 |
|
| 81 | +USBMIDI::~USBMIDI() { |
| 82 | + if (midiUserDeviceName) { |
| 83 | + delete[] midiUserDeviceName; |
| 84 | + midiUserDeviceName = nullptr; |
| 85 | + } |
| 86 | +} |
| 87 | + |
60 | 88 | void USBMIDI::begin() {}
|
61 | 89 | void USBMIDI::end() {}
|
62 | 90 |
|
| 91 | +/** |
| 92 | +* @brief Get the current device name |
| 93 | +* @return The device name in order of precedence: |
| 94 | +* 1. Name set via constructor (if any) |
| 95 | +* 2. Name set via SET_USB_MIDI_DEVICE_NAME() macro (if defined) |
| 96 | +* 3. Default name "TinyUSB MIDI" |
| 97 | +* If device name is set as "", it will be ignored |
| 98 | +*/ |
| 99 | +const char* USBMIDI::getCurrentDeviceName(void) { |
| 100 | + if (midiUserDeviceName) { |
| 101 | + return midiUserDeviceName; |
| 102 | + } |
| 103 | + // If no user name set, use the compile-time default name limited to 32 characters |
| 104 | + setDeviceName(getUSBMIDIDefaultDeviceName()); |
| 105 | + return strlen(midiUserDeviceName) ? midiUserDeviceName : "TinyUSB MIDI"; |
| 106 | +} |
| 107 | + |
63 | 108 | // uint compatible version of constrain
|
64 | 109 | #define uconstrain(amt, low, high) ((amt) <= (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
|
65 | 110 |
|
|
0 commit comments