Skip to content

Commit 75d04db

Browse files
authored
feat(usb_midi): Refactor USBMIDI with device name handling
1 parent 473a4c6 commit 75d04db

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

libraries/USB/src/USBMIDI.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
#include "Arduino.h"
77
#include "esp32-hal-tinyusb.h"
88

9+
// Initialize static members
10+
const char* USBMIDI::deviceName = nullptr;
11+
char USBMIDI::nameBuffer[32] = {0};
12+
913
// Default Cable Number (for simplified APIs that do not expose this)
1014
#define DEFAULT_CN 0
1115

1216
static bool tinyusb_midi_descriptor_loaded = false;
1317
static bool tinyusb_midi_interface_enabled = false;
14-
static String deviceDescriptor("");
1518

1619
extern "C" uint16_t tusb_midi_load_descriptor(uint8_t *dst, uint8_t *itf) {
1720
if (tinyusb_midi_descriptor_loaded) {
1821
return 0;
1922
}
2023
tinyusb_midi_descriptor_loaded = true;
21-
uint8_t str_index = tinyusb_add_string_descriptor(deviceDescriptor.c_str());
24+
25+
uint8_t str_index = tinyusb_add_string_descriptor(USBMIDI::getCurrentDeviceName());
2226
uint8_t ep_in = tinyusb_get_free_in_endpoint();
2327
TU_VERIFY(ep_in != 0);
2428
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) {
3236
return TUD_MIDI_DESC_LEN;
3337
}
3438

35-
USBMIDI::USBMIDI(String devDescName) {
39+
USBMIDI::USBMIDI() {
40+
if (!tinyusb_midi_interface_enabled) {
41+
tinyusb_midi_interface_enabled = true;
42+
tinyusb_enable_interface(USB_INTERFACE_MIDI, TUD_MIDI_DESC_LEN, tusb_midi_load_descriptor);
43+
} else {
44+
log_e("USBMIDI: Multiple instances of USBMIDI not supported!");
45+
}
46+
}
47+
48+
USBMIDI::USBMIDI(const char* name) {
3649
if (!tinyusb_midi_interface_enabled) {
50+
strncpy(nameBuffer, name, sizeof(nameBuffer) - 1);
51+
nameBuffer[sizeof(nameBuffer) - 1] = '\0';
52+
deviceName = nameBuffer;
3753
tinyusb_midi_interface_enabled = true;
38-
deviceDescriptor = devDescName;
3954
tinyusb_enable_interface(USB_INTERFACE_MIDI, TUD_MIDI_DESC_LEN, tusb_midi_load_descriptor);
4055
} else {
4156
log_e("USBMIDI: Multiple instances of USBMIDI not supported!");

0 commit comments

Comments
 (0)