forked from felis/USB_Host_Shield_2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSB_MIDI_converter.ino
74 lines (62 loc) · 1.51 KB
/
USB_MIDI_converter.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
*******************************************************************************
* USB-MIDI to Legacy Serial MIDI converter
* Copyright (C) 2012-2021 Yuuichi Akagawa
*
* Idea from LPK25 USB-MIDI to Serial MIDI converter
* by Collin Cunningham - makezine.com, narbotic.com
*
* This is sample program. Do not expect perfect behavior.
*******************************************************************************
*/
#include <usbh_midi.h>
#include <usbhub.h>
#ifdef USBCON
#define _MIDI_SERIAL_PORT Serial1
#else
#define _MIDI_SERIAL_PORT Serial
#endif
// Set to 1 if you want to wait for the Serial MIDI transmission to complete.
// For more information, see https://github.com/felis/USB_Host_Shield_2.0/issues/570
#define ENABLE_MIDI_SERIAL_FLUSH 0
//////////////////////////
// MIDI Pin assign
// 2 : GND
// 4 : +5V(Vcc) with 220ohm
// 5 : TX
//////////////////////////
USB Usb;
USBH_MIDI Midi(&Usb);
void MIDI_poll();
void setup()
{
_MIDI_SERIAL_PORT.begin(31250);
if (Usb.Init() == -1) {
while (1); //halt
}//if (Usb.Init() == -1...
delay( 200 );
}
void loop()
{
Usb.Task();
if ( Midi ) {
MIDI_poll();
}
//delay(1ms) if you want
//delayMicroseconds(1000);
}
// Poll USB MIDI Controler and send to serial MIDI
void MIDI_poll()
{
uint8_t outBuf[ 3 ];
uint8_t size;
do {
if ( (size = Midi.RecvData(outBuf)) > 0 ) {
//MIDI Output
_MIDI_SERIAL_PORT.write(outBuf, size);
#if ENABLE_MIDI_SERIAL_FLUSH
_MIDI_SERIAL_PORT.flush();
#endif
}
} while (size > 0);
}