Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(usb): allow the MIDI constructor to define a device name #11720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
me-no-dev merged 31 commits into master from feature/device_name_usb_midi
Sep 10, 2025
Merged
Show file tree
Hide file tree
Changes from 28 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
16 changes: 16 additions & 0 deletions cores/esp32/Arduino.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,22 @@ size_t getArduinoLoopTaskStackSize(void);
return sz; \
}

#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) { \
return ESP32_USB_MIDI_DEFAULT_NAME; \
} \
return name; \
}

bool shouldPrintChipDebugReport(void);
#define ENABLE_CHIP_DEBUG_REPORT \
bool shouldPrintChipDebugReport(void) { \
Expand Down
View file Open in desktop
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
4 changes: 4 additions & 0 deletions libraries/USB/examples/MIDI/MidiInterface/MidiInterface.ino
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ 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 name defined with the SET_USB_MIDI_DEVICE_NAME() macro
USBMIDI MIDI;

#define MIDI_RX 39
Expand Down
68 changes: 67 additions & 1 deletion libraries/USB/src/USBMIDI.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
#include "Arduino.h"
#include "esp32-hal-tinyusb.h"

// Initialize static members
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

Expand All @@ -18,7 +25,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,9 +48,68 @@ 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.");
}
} else {
log_w("USBMIDI: No device name provided, using default name [%s].", getUSBMIDIDefaultDeviceName());
}
}

/**
* @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"
* If device name is set as "", it will be ignored
*/
USBMIDI::USBMIDI(const char* name) {
if (!tinyusb_midi_interface_enabled) {
setDeviceName(name);
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() {
if (midiUserDeviceName) {
delete[] midiUserDeviceName;
midiUserDeviceName = nullptr;
}
}

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

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());
if (midiUserDeviceName && strlen(midiUserDeviceName)) {
return midiUserDeviceName;
} else {
return "TinyUSB MIDI";
}
}

// uint compatible version of constrain
#define uconstrain(amt, low, high) ((amt) <= (low) ? (low) : ((amt) > (high) ? (high) : (amt)))

Expand Down
30 changes: 30 additions & 0 deletions libraries/USB/src/USBMIDI.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,40 @@ typedef struct {
} midiEventPacket_t;

class USBMIDI {
private:
static char* midiUserDeviceName; // user device name
static void setDeviceName(const char* name); // set user device name limited to 32 characters

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 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);

/**
* @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);

/* User-level API */

Expand Down
Loading

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