Last active
November 13, 2023 01:46
-
-
Save YuuichiAkagawa/0841a1f1fee67c9726a86e688851f56a to your computer and use it in GitHub Desktop.
USBH_MIDI: Example of transmitting keyboard input data to an instrument.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
******************************************************************************* | |
* USBH_MIDI Example of transmitting keyboard input data to an instrument. | |
* | |
* Copyright (C) 2023 Yuuichi Akagawa | |
******************************************************************************* | |
*/ | |
#include <usbh_midi.h> | |
#include <usbhub.h> | |
USB Usb; | |
USBHub Hub1(&Usb); | |
USBH_MIDI Midi1(&Usb); | |
USBH_MIDI Midi2(&Usb); | |
//Midi instance array | |
USBH_MIDI *Midi[] {&Midi1, &Midi2}; | |
//device index | |
uint8_t ixnanoKEY2 = 0xff; | |
uint8_t ixUmOne2 = 0xff; | |
//Functions for identifying USB devices | |
void setupmidi(uint8_t idx) | |
{ | |
uint16_t vid = Midi[idx]->idVendor(); | |
uint16_t pid = Midi[idx]->idProduct(); | |
//is KORG nanoKEY2? | |
if ( vid == 0x0944 && pid == 0x0115 ) { | |
ixnanoKEY2 = idx; | |
return; | |
} | |
//is Roland UM-ONE mk2 ? | |
if ( vid == 0x0582 && pid == 0x012a ) { | |
ixUmOne2 = idx; | |
return; | |
} | |
} | |
//Init handler for Midi1 | |
void onInit1() | |
{ | |
setupmidi(0); | |
} | |
//Init handler for Midi2 | |
void onInit2() | |
{ | |
setupmidi(1); | |
} | |
void setup() { | |
//attache onInit handler | |
Midi1.attachOnInit(onInit1); | |
Midi2.attachOnInit(onInit2); | |
if (Usb.Init() == -1) { | |
while (1); //halt | |
}//if (Usb.Init() == -1... | |
delay( 200 ); | |
} | |
void loop() { | |
Usb.Task(); | |
//UM-ONE mk2 | |
if ( ixUmOne2 == 0xff) { | |
//Subsequent processes are skipped until the device is online. | |
return; | |
} | |
//nanoKEY2 | |
if ( ixnanoKEY2 != 0xff) { | |
uint8_t midiMsg[ 3 ]; | |
uint8_t size; | |
//Read all MIDI messages | |
do { | |
if ( (size = Midi[ixnanoKEY2]->RecvData(midiMsg)) > 0 ) { | |
//Send to UME-ONE mk2 | |
Midi[ixUmOne2]->SendData(midiMsg); | |
} | |
} while (size > 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment