-
Notifications
You must be signed in to change notification settings - Fork 7
/
MIDIUARTUSB.ino
316 lines (290 loc) · 8.85 KB
/
MIDIUARTUSB.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
MIT License
Copyright (c) 2018,2021 [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
MIDIUARTUSB converter for 32u4 such as Leonardo, Pro Micro, Itsy Bitsy.
Works on Arduino Zero and Adafruit Trinket M0 that has more RAM so larger
Sys Ex is possible. Other SAMD21 boards might work.
*/
#include <MIDI.h>
#ifdef USE_TINYUSB
#include <Adafruit_TinyUSB.h>
Adafruit_USBD_MIDI usb_midi;
// Create a new instance of the Arduino MIDI Library,
// and attach usb_midi as the transport.
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDIUSB);
typedef struct
{
uint8_t header;
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
} midiEventPacket_t;
#define SEND_MIDI(evt) while (!usb_midi.writePacket((uint8_t *)&evt)) delay(1)
#define READ_MIDI(evt) usb_midi.readPacket((uint8_t *)&evt)
#define FLUSH_MIDI() usb_midi.flush()
#else
#include <MIDIUSB.h>
#define SEND_MIDI(evt) MidiUSB.sendMIDI(evt)
#define READ_MIDI(evt) evt = MidiUSB.read()
#define FLUSH_MIDI() MidiUSB.flush()
#endif
#define HAS_DOTSTAR_LED (defined(ADAFRUIT_TRINKET_M0) || defined(ADAFRUIT_ITSYBITSY_M0) || defined(ADAFRUIT_ITSYBITSY_M4_EXPRESS))
#if HAS_DOTSTAR_LED
#include <Adafruit_DotStar.h>
#if defined(ADAFRUIT_ITSYBITSY_M4_EXPRESS)
#define DATAPIN 8
#define CLOCKPIN 6
#elif defined(ADAFRUIT_ITSYBITSY_M0)
#define DATAPIN 41
#define CLOCKPIN 40
#elif defined(ADAFRUIT_TRINKET_M0)
#define DATAPIN 7
#define CLOCKPIN 8
#endif
Adafruit_DotStar strip = Adafruit_DotStar(1, DATAPIN, CLOCKPIN, DOTSTAR_BRG);
#endif
#if CFG_TUSB_MCU==OPT_MCU_RP2040
// 1 turns on debug, 0 off
#define DBGSERIAL if (0) Serial
#define MIDI_SERIAL_PORT Serial1
#else
// 1 turns on debug, 0 off
#define DBGSERIAL if (0) SERIAL_PORT_MONITOR
#ifdef USBCON
#define MIDI_SERIAL_PORT Serial1
#else
#define MIDI_SERIAL_PORT Serial
#endif
#endif
struct MySettings : public midi::DefaultSettings
{
static const bool Use1ByteParsing = false;
static const unsigned SysExMaxSize = 1026; // Accept SysEx messages up to 1024 bytes long.
static const long BaudRate = 31250;
};
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, MIDI_SERIAL_PORT, MIDIUART, MySettings);
void USBSystemExclusive(unsigned size, byte *data, bool markersIncluded) {
uint8_t bytesOut;
midiEventPacket_t evt;
while (size > 0) {
bytesOut = min(3, size);
evt.byte1 = *data++;
size -= bytesOut;
switch (bytesOut) {
case 1:
evt.header = 0x05;
evt.byte2 = evt.byte3 = 0;
break;
case 2:
evt.header = 0x06;
evt.byte2 = *data++;
evt.byte3 = 0;
break;
case 3:
evt.header = (size > 0) ? 0x04 : 0x07;
evt.byte2 = *data++;
evt.byte3 = *data++;
break;
default:
break;
}
SEND_MIDI(evt);
}
}
inline uint8_t writeUARTwait(uint8_t *p, uint16_t size)
{
// Apparently, not needed. write blocks, if needed
// while (MIDI_SERIAL_PORT.availableForWrite() < size) {
// delay(1);
// }
return MIDI_SERIAL_PORT.write(p, size);
}
uint16_t sysexSize = 0;
void sysex_end(uint8_t i)
{
sysexSize += i;
DBGSERIAL.print(F("USB sysexSize="));
DBGSERIAL.println(sysexSize);
sysexSize = 0;
}
const uint8_t MIDI_passthru_pin=2;
bool MIDI_passthru;
void setup() {
// Turn off built-in RED LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
#if HAS_DOTSTAR_LED
// Turn off built-in Dotstar RGB LED
strip.begin();
strip.clear();
strip.show();
#endif
DBGSERIAL.begin(115200);
// Pin 2 LOW selects MIDI pass through on
pinMode(MIDI_passthru_pin, INPUT_PULLUP);
MIDI_passthru = (digitalRead(MIDI_passthru_pin) == LOW);
MIDIUART.begin(MIDI_CHANNEL_OMNI);
if (MIDI_passthru) {
DBGSERIAL.println(F("MIDI UART thru on"));
}
else {
DBGSERIAL.println(F("MIDI UART thru off"));
MIDIUART.turnThruOff();
}
#if USE_TINYUSB
MIDIUSB.begin(MIDI_CHANNEL_OMNI);
if (MIDI_passthru) {
DBGSERIAL.println(F("MIDI USB thru on"));
}
else {
DBGSERIAL.println(F("MIDI USB thru off"));
MIDIUSB.turnThruOff();
}
#endif
}
void loop()
{
/* MIDI UART -> MIDI USB */
if (MIDIUART.read()) {
midi::MidiType msgType = MIDIUART.getType();
DBGSERIAL.print(F("UART "));
DBGSERIAL.print(msgType, HEX);
DBGSERIAL.print(' ');
DBGSERIAL.print(MIDIUART.getData1(), HEX);
DBGSERIAL.print(' ');
DBGSERIAL.println(MIDIUART.getData2(), HEX);
switch (msgType) {
case midi::InvalidType:
break;
case midi::NoteOff:
case midi::NoteOn:
case midi::AfterTouchPoly:
case midi::ControlChange:
case midi::ProgramChange:
case midi::AfterTouchChannel:
case midi::PitchBend:
{
midiEventPacket_t tx = {
(byte)(msgType >> 4),
(byte)((msgType & 0xF0) | ((MIDIUART.getChannel() - 1) & 0x0F)), /* getChannel() returns values from 1 to 16 */
MIDIUART.getData1(),
MIDIUART.getData2()
};
SEND_MIDI(tx);
FLUSH_MIDI();
break;
}
case midi::SystemExclusive:
USBSystemExclusive(MIDIUART.getSysExArrayLength(),
(byte *)MIDIUART.getSysExArray(), true);
DBGSERIAL.print(F("UART sysex size="));
DBGSERIAL.println(MIDIUART.getSysExArrayLength());
FLUSH_MIDI();
break;
case midi::TuneRequest:
case midi::Clock:
case midi::Start:
case midi::Continue:
case midi::Stop:
case midi::ActiveSensing:
case midi::SystemReset:
{
midiEventPacket_t tx = { 0x0F, (byte)(msgType), 0, 0 };
SEND_MIDI(tx);
FLUSH_MIDI();
break;
}
case midi::TimeCodeQuarterFrame:
case midi::SongSelect:
{
midiEventPacket_t tx = { 0x02, (byte)(msgType), MIDIUART.getData1(), 0 };
SEND_MIDI(tx);
FLUSH_MIDI();
break;
}
case midi::SongPosition:
{
midiEventPacket_t tx = { 0x03, (byte)(msgType), MIDIUART.getData1(), MIDIUART.getData2() };
SEND_MIDI(tx);
FLUSH_MIDI();
break;
}
default:
break;
}
}
/* MIDI USB -> MIDI UART */
midiEventPacket_t rx;
#ifdef USE_TINYUSB
if (READ_MIDI(rx)) {
#else
READ_MIDI(rx);
if (rx.header != 0) {
#endif
DBGSERIAL.print(F("USB "));
DBGSERIAL.print(rx.header, HEX);
DBGSERIAL.print(' ');
DBGSERIAL.print(rx.byte1, HEX);
DBGSERIAL.print(' ');
DBGSERIAL.print(rx.byte2, HEX);
DBGSERIAL.print(' ');
DBGSERIAL.println(rx.byte3, HEX);
switch (rx.header & 0x0F) {
case 0x00: // Misc. Reserved for future extensions.
break;
case 0x01: // Cable events. Reserved for future expansion.
break;
case 0x02: // Two-byte System Common messages
case 0x0C: // Program Change
case 0x0D: // Channel Pressure
writeUARTwait(&rx.byte1, 2);
break;
case 0x03: // Three-byte System Common messages
case 0x08: // Note-off
case 0x09: // Note-on
case 0x0A: // Poly-KeyPress
case 0x0B: // Control Change
case 0x0E: // PitchBend Change
writeUARTwait(&rx.byte1, 3);
break;
case 0x04: // SysEx starts or continues
sysexSize += 3;
DBGSERIAL.print(F("USB sysexSize="));
DBGSERIAL.println(sysexSize);
writeUARTwait(&rx.byte1, 3);
break;
case 0x05: // Single-byte System Common Message or SysEx ends with the following single byte
sysex_end(1);
writeUARTwait(&rx.byte1, 1);
break;
case 0x06: // SysEx ends with the following two bytes
sysex_end(2);
writeUARTwait(&rx.byte1, 2);
break;
case 0x07: // SysEx ends with the following three bytes
sysex_end(3);
writeUARTwait(&rx.byte1, 3);
break;
case 0x0F: // Single Byte, TuneRequest, Clock, Start, Continue, Stop, etc.
writeUARTwait(&rx.byte1, 1);
break;
}
}
}