Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
87819f6
feat(usb): allow the MIDI constructor to define a device name
SuGlider Aug 12, 2025
e8d506c
feat(usb): changes the MIDI device descriptor in the example
SuGlider Aug 12, 2025
36ac341
feat(usb): changes the MIDI device descriptor in the example
SuGlider Aug 12, 2025
74fa530
fix(usb): typo in commentary - start CI again
SuGlider Aug 12, 2025
5af16d1
feat(usb): allow the MIDI constructor to define a device name
SuGlider Aug 12, 2025
8bb446b
fix(usb): correct constructor declaration
SuGlider Aug 12, 2025
fdd6ccd
fix(usb): typo in commentary - start CI again
SuGlider Aug 12, 2025
473a4c6
feat(usb_midi): Enhance USBMIDI with device name
SuGlider Aug 22, 2025
75d04db
feat(usb_midi): Refactor USBMIDI with device name handling
SuGlider Aug 22, 2025
b3fbce7
feat(usb_midi): Add macro to set USB MIDI device name
SuGlider Aug 22, 2025
c930bc1
feat(usb_midi): demonstrate the use of macro to change the device name
SuGlider Aug 22, 2025
40fbd4f
fix(usb_midi): reduce changes to the code
SuGlider Aug 22, 2025
059a037
Merge branch 'master' into feature/device_name_usb_midi
SuGlider Aug 22, 2025
fb36be8
fix(usb_midi): Add macro guards
SuGlider Aug 22, 2025
2f4667a
Merge branch 'master' into feature/device_name_usb_midi
SuGlider Sep 8, 2025
b7b61c3
feat(midi): add midi dev name from macro
SuGlider Sep 9, 2025
1b4aea2
feat(midi): Enhance USBMIDI device name handling
SuGlider Sep 9, 2025
60e4a0b
feat(midi): USBMIDI class for device name
SuGlider Sep 9, 2025
219f838
fix(midi): comment typo
SuGlider Sep 9, 2025
10e1948
fix(rmt): fixes bad commentary formating
SuGlider Sep 9, 2025
62659c9
feat(rmt): more commentaries
SuGlider Sep 9, 2025
0d4dce2
fix(midi): move commentaries
SuGlider Sep 9, 2025
f3bd145
fix(midi): move commentaries
SuGlider Sep 9, 2025
c5cb5f0
feat(midi): add more commentaries
SuGlider Sep 9, 2025
fa7d041
fix(midi): fixes constructor commentary
SuGlider Sep 9, 2025
ce55c99
fix(midi): safeguard for memory leak
SuGlider Sep 9, 2025
c14e769
fix(midi): removes debug logging
SuGlider Sep 9, 2025
861abb9
feat(midi): explicit safer test and return
SuGlider Sep 9, 2025
3f59ac6
feat(midi): avoids possible strlen(NULL)
SuGlider Sep 9, 2025
d9f89b8
Merge branch 'master' into feature/device_name_usb_midi
SuGlider Sep 10, 2025
4bca706
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] Sep 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cores/esp32/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) { \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ void loop() {}

#include "USB.h"
#include "USBMIDI.h"
USBMIDI MIDI;
// Creates the MIDI device with specific descriptor
USBMIDI MIDI("ESP MIDI Device");

#define MIDI_NOTE_C4 60

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ 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"
USBMIDI MIDI;
// Creates the MIDI device with specific descriptor
USBMIDI MIDI();


#define MIDI_RX 39
#define MIDI_TX 40
Expand Down
18 changes: 17 additions & 1 deletion libraries/USB/src/USBMIDI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#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

Expand All @@ -18,7 +22,7 @@ extern "C" uint16_t tusb_midi_load_descriptor(uint8_t *dst, uint8_t *itf) {
}
tinyusb_midi_descriptor_loaded = true;

uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB MIDI");
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();
Expand All @@ -41,6 +45,18 @@ USBMIDI::USBMIDI() {
}
}

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;
tinyusb_enable_interface(USB_INTERFACE_MIDI, TUD_MIDI_DESC_LEN, tusb_midi_load_descriptor);
} else {
log_e("USBMIDI: Multiple instances of USBMIDI not supported!");
}
}

void USBMIDI::begin() {}
void USBMIDI::end() {}

Expand Down
43 changes: 43 additions & 0 deletions libraries/USB/src/USBMIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/**
* @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 */

Expand Down
Loading