Skip to content

Instantly share code, notes, and snippets.

@YuuichiAkagawa
Created September 28, 2025 09:56
Show Gist options
  • Select an option

  • Save YuuichiAkagawa/2bb632dbf11f19017a60b0ca8d93e296 to your computer and use it in GitHub Desktop.

Select an option

Save YuuichiAkagawa/2bb632dbf11f19017a60b0ca8d93e296 to your computer and use it in GitHub Desktop.
USBH_MIDI/UHS2-MIDI Example of same two MIDI device
/*
*******************************************************************************
* USBH_MIDI/UHS2-MIDI Example of same two MIDI device
*
* Copyright (C) 2025 Yuuichi Akagawa
*******************************************************************************
*/
#include <UHS2-MIDI.h>
#include <usbhub.h>
USB Usb;
USBHub Hub1(&Usb);
#define NUMBER_OF_DEVICES 2
UHS2MIDI_CREATE_INSTANCE(&Usb, 0, Midi1);
UHS2MIDI_CREATE_INSTANCE(&Usb, 0, Midi2);
MIDI_NAMESPACE::MidiInterface<UHS2MIDI_NAMESPACE::uhs2MidiTransport> *Midi[] {&Midi1, &Midi2};
//device index
uint8_t ixmidi[2] = {0xff, 0xff};
void setup() {
Serial.begin(115200);
Midi1.begin(MIDI_CHANNEL_OMNI);
Midi2.begin(MIDI_CHANNEL_OMNI);
//USB attach/detach handler
Midi1.getTransport()->attachOnInit(onInit1);
Midi2.getTransport()->attachOnInit(onInit2);
Midi1.getTransport()->attachOnRelease(onDetach1);
Midi2.getTransport()->attachOnRelease(onDetach2);
if (Usb.Init() == -1) {
while (1); //halt
}//if (Usb.Init() == -1...
delay( 200 );
}
void loop() {
Usb.Task();
//MIDI Controller 1
if ( ixmidi[0] != 0xff ) {
do {
Midi[0]->read();
} while ( Midi[0]->getTransport()->available() > 0);
}
//MIDI Controller 2
if ( ixmidi[1] != 0xff ) {
do {
Midi[1]->read();
} while ( Midi[1]->getTransport()->available() > 0);
}
}
void onInit1()
{
setupmidi(0);
}
void onInit2()
{
setupmidi(1);
}
void onDetach1()
{
detachMidi(0);
}
void onDetach2()
{
detachMidi(1);
}
void setupmidi(uint8_t idx)
{
uint16_t vid = Midi[idx]->getTransport()->idVendor();
uint16_t pid = Midi[idx]->getTransport()->idProduct();
ixmidi[idx] = idx;
//register MIDI message handler
if( idx == 0 ) {
Midi[idx]->setHandleNoteOn(handleNoteOn_0);
Midi[idx]->setHandleNoteOff(handleNoteOff_0);
} else if( idx == 1 ) {
Midi[idx]->setHandleNoteOn(handleNoteOn_1);
Midi[idx]->setHandleNoteOff(handleNoteOff_1);
}
}
void detachMidi(uint8_t idx)
{
ixmidi[idx] = idx;
//unregister MIDI message handler
Midi[idx]->setHandleNoteOn(nullptr);
Midi[idx]->setHandleNoteOff(nullptr);
}
char print_buf[32];
//MIDI message handler
void handleNoteOn_0(byte inChannel, byte inNumber, byte inVelocity)
{
sprintf(print_buf, "%02X>NoteOn: %02X %02X %02X", Midi[0]->getTransport()->GetAddress(), inChannel, inNumber, inVelocity);
Serial.println(print_buf);
}
void handleNoteOff_0(byte inChannel, byte inNumber, byte inVelocity)
{
sprintf(print_buf, "%02X>NoteOff: %02X %02X %02X", Midi[0]->getTransport()->GetAddress(), inChannel, inNumber, inVelocity);
Serial.println(print_buf);
}
void handleNoteOn_1(byte inChannel, byte inNumber, byte inVelocity)
{
sprintf(print_buf, "%02X>NoteOn: %02X %02X %02X", Midi[1]->getTransport()->GetAddress(), inChannel, inNumber, inVelocity);
Serial.println(print_buf);
}
void handleNoteOff_1(byte inChannel, byte inNumber, byte inVelocity)
{
sprintf(print_buf, "%02X>NoteOff: %02X %02X %02X", Midi[1]->getTransport()->GetAddress(), inChannel, inNumber, inVelocity);
Serial.println(print_buf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment