Skip to content

Commit 2295571

Browse files
committed
Merge pull request #232 from YuuichiAkagawa/pr_usbh_midi_031
Update MIDI driver v0.3.1
2 parents eb3a258 + a69979a commit 2295571

File tree

7 files changed

+252
-93
lines changed

7 files changed

+252
-93
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ HID devices are also supported by the library. However these require you to writ
320320
The library support MIDI devices.
321321
You can convert USB MIDI keyboard to legacy serial MIDI.
322322
323-
* [USB_MIDI_converter.ino](USBH_MIDI/USB_MIDI_converter)
324-
* [USB_MIDI_converter_multi.ino](USBH_MIDI/USB_MIDI_converter_multi)
323+
* [USB_MIDI_converter.ino](examples/USBH_MIDI/USB_MIDI_converter/USB_MIDI_converter.ino)
324+
* [USB_MIDI_converter_multi.ino](examples/USBH_MIDI/USB_MIDI_converter_multi/USB_MIDI_converter_multi.ino)
325325
326326
For information see the following page: <http://yuuichiakagawa.github.io/USBH_MIDI/>.
327327

examples/USBH_MIDI/USB_MIDI_converter/USB_MIDI_converter.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void loop()
6464
// Poll USB MIDI Controler and send to serial MIDI
6565
void MIDI_poll()
6666
{
67-
byte outBuf[ 3 ];
67+
uint8_t outBuf[ 3 ];
6868
uint8_t size;
6969

7070
do {

examples/USBH_MIDI/USB_MIDI_converter_multi/USB_MIDI_converter_multi.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void loop()
6666
// Poll USB MIDI Controler and send to serial MIDI
6767
void MIDI_poll()
6868
{
69-
byte outBuf[ 3 ];
69+
uint8_t outBuf[ 3 ];
7070
uint8_t size;
7171

7272
do {

examples/USBH_MIDI/bidrectional_converter/bidrectional_converter.ino

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,28 @@ MIDI_CREATE_DEFAULT_INSTANCE();
4040
//////////////////////////
4141

4242
USB Usb;
43-
USBH_MIDI Midi(&Usb);
43+
USBH_MIDI Midi(&Usb);
4444

4545
void MIDI_poll();
4646
void doDelay(unsigned long t1, unsigned long t2, unsigned long delayTime);
4747

48+
//If you want handle System Exclusive message, enable this #define otherwise comment out it.
49+
#define USBH_MIDI_SYSEX_ENABLE
50+
51+
#ifdef USBH_MIDI_SYSEX_ENABLE
52+
MidiSysEx sysExData;
53+
//SysEx:
54+
void handle_sysex( byte* sysexmsg, unsigned sizeofsysex) {
55+
Midi.SendSysEx(sysexmsg, sizeofsysex);
56+
}
57+
#endif
58+
4859
void setup()
4960
{
5061
MIDI.begin(MIDI_CHANNEL_OMNI);
51-
62+
#ifdef USBH_MIDI_SYSEX_ENABLE
63+
MIDI.setHandleSystemExclusive(handle_sysex);
64+
#endif
5265
if (Usb.Init() == -1) {
5366
while (1); //halt
5467
}//if (Usb.Init() == -1...
@@ -67,13 +80,17 @@ void loop()
6780
MIDI_poll();
6881
if (MIDI.read()) {
6982
msg[0] = MIDI.getType();
70-
if ( msg[0] == 0xf0 ) { //SysEX
71-
//TODO
72-
//SysEx implementation is not yet.
73-
} else {
74-
msg[1] = MIDI.getData1();
75-
msg[2] = MIDI.getData2();
76-
Midi.SendData(msg, 0);
83+
switch (msg[0]) {
84+
case midi::ActiveSensing :
85+
break;
86+
case midi::SystemExclusive :
87+
//SysEx is handled by event.
88+
break;
89+
default :
90+
msg[1] = MIDI.getData1();
91+
msg[2] = MIDI.getData2();
92+
Midi.SendData(msg, 0);
93+
break;
7794
}
7895
}
7996
}
@@ -84,13 +101,53 @@ void loop()
84101
// Poll USB MIDI Controler and send to serial MIDI
85102
void MIDI_poll()
86103
{
87-
byte outBuf[ 3 ];
88104
uint8_t size;
105+
#ifdef USBH_MIDI_SYSEX_ENABLE
106+
uint8_t recvBuf[MIDI_EVENT_PACKET_SIZE];
107+
uint8_t rcode = 0; //return code
108+
uint16_t rcvd;
109+
uint8_t readPtr = 0;
110+
111+
rcode = Midi.RecvData( &rcvd, recvBuf);
89112

90-
if ( (size = Midi.RecvData(outBuf)) > 0 ) {
91-
//MIDI Output
92-
_MIDI_SERIAL_PORT.write(outBuf, size);
113+
//data check
114+
if (rcode != 0) return;
115+
if ( recvBuf[0] == 0 && recvBuf[1] == 0 && recvBuf[2] == 0 && recvBuf[3] == 0 ) {
116+
return ;
93117
}
118+
119+
uint8_t *p = recvBuf;
120+
while (readPtr < MIDI_EVENT_PACKET_SIZE) {
121+
if (*p == 0 && *(p + 1) == 0) break; //data end
122+
MidiSysEx::Status rc = sysExData.set(p);
123+
switch (rc) {
124+
case MidiSysEx::nonsysex : //No SysEx message send data to Serial MIDI
125+
p++;
126+
size = Midi.lookupMsgSize(*p);
127+
_MIDI_SERIAL_PORT.write(p, size);
128+
p += 3;
129+
break;
130+
case MidiSysEx::done : //SysEx end. send data to Serial MIDI
131+
_MIDI_SERIAL_PORT.write(sysExData.get(), sysExData.getSize());
132+
/* FALLTHROUGH */
133+
case MidiSysEx::overflow : //SysEx buffer over. ignore and flush buffer.
134+
sysExData.clear();
135+
/* FALLTHROUGH */
136+
default:
137+
p += 4;
138+
break;
139+
}
140+
readPtr += 4;
141+
}
142+
#else
143+
uint8_t outBuf[ 3 ];
144+
do {
145+
if ( (size = Midi.RecvData(outBuf)) > 0 ) {
146+
//MIDI Output
147+
_MIDI_SERIAL_PORT.write(outBuf, size);
148+
}
149+
} while (size > 0);
150+
#endif
94151
}
95152

96153
// Delay time (max 16383 us)

examples/USBH_MIDI/eVY1_sample/eVY1_sample.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void loop()
6161
// Poll USB MIDI Controler
6262
void MIDI_poll()
6363
{
64-
byte inBuf[ 3 ];
64+
uint8_t inBuf[ 3 ];
6565

6666
//first call?
6767
if (Midi.vid != vid || Midi.pid != pid) {

0 commit comments

Comments
 (0)