-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathusbMidiHost.ino
336 lines (285 loc) · 8.53 KB
/
usbMidiHost.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright (c) 2023 Marcel Licence
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
* veröffentlichten Version, weiter verteilen und/oder modifizieren.
*
* Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
* OHNE JEDE GEWÄHR,; sogar ohne die implizite
* Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Einzelheiten.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
*/
/**
* @file usbMidiHost.ino
* @author Marcel Licence
* @date 18.05.2021
*
* @brief This file includes the implementation for MIDI in/out
* via the USB Host module using the "Revision 2.0 of MAX3421E-based USB Host Shield Library"
*
* You will require to use following library:
* https://github.com/felis/USB_Host_Shield_2.0
*
* Please check out USB-MIDI dump utility from Yuuichi Akagawa
*
* @see Mini USB host shield with ESP32 as MIDI interface (MAX3421E add-on for arduino synthesizer projects) - https://youtu.be/Mt3rT-SVZww
* @see https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/USBH_MIDI/USBH_MIDI_dump/USBH_MIDI_dump.ino
*/
#ifdef __CDT_PARSER__
#include <cdt.h>
#endif
/*
* Connections:
* CS: IO5
* INT: IO17 (not used)
* SCK: IO18
* MISO: IO19
* MOSI: IO23
*/
#include <usbh_midi.h> /* requires library USB_Host_Shield_2.0 from https://github.com/felis/USB_Host_Shield_2.0 */
#include <usbhub.h>
#include <SPI.h>
USB Usb;
USBH_MIDI Midi(&Usb);
static void UsbMidi_Poll();
uint16_t pid, vid;
struct usbMidiMappingEntry_s
{
void (*sendRaw)(uint8_t *buf);
void (*shortMsg)(uint8_t *buf);
void (*liveMsg)(uint8_t *buf);
void (*sysEx)(uint8_t *buf, uint8_t len);
uint8_t cableMask;
};
struct usbMidiMapping_s
{
void (*usbMidiRxIndication)(uint8_t cable);
void (*usbMidiTxIndication)(uint8_t cable);
struct usbMidiMappingEntry_s *usbMidiMappingEntries;
int usbMidiMappingEntriesCount;
};
extern struct usbMidiMapping_s usbMidiMapping; /* definition in z_config.ino */
void UsbMidi_Setup()
{
vid = pid = 0;
Serial.begin(115200);
Serial.println("Hello now we can start\n");
if (Usb.Init() == -1)
{
Serial.println("Usb init failed!\n");
while (1); //halt
}//if (Usb.Init() == -1...
delay(200);
Serial.println("Usb init done!\n");
}
uint8_t lastState = 0xFF;
/* this function comes from the USB Midi example of the library */
void UsbMidi_Loop()
{
Usb.Task();
if (lastState != Usb.getUsbTaskState())
{
lastState = Usb.getUsbTaskState();
Serial.printf("state: %d\n", Usb.getUsbTaskState());
switch (Usb.getUsbTaskState())
{
case USB_STATE_DETACHED:
Serial.printf(" USB_STATE_DETACHED\n");
vid = pid = 0;
break;
case USB_DETACHED_SUBSTATE_INITIALIZE:
Serial.printf(" USB_DETACHED_SUBSTATE_INITIALIZE\n");
vid = pid = 0;
break;
case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE:
Serial.printf(" USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE\n");
vid = pid = 0;
break;
case USB_DETACHED_SUBSTATE_ILLEGAL:
Serial.printf(" USB_DETACHED_SUBSTATE_ILLEGAL\n");
vid = pid = 0;
break;
case USB_ATTACHED_SUBSTATE_SETTLE:
Serial.printf(" USB_ATTACHED_SUBSTATE_SETTLE\n");
break;
case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
Serial.printf(" USB_ATTACHED_SUBSTATE_RESET_DEVICE\n");
break;
case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE:
Serial.printf(" USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE\n");
break;
case USB_ATTACHED_SUBSTATE_WAIT_SOF:
Serial.printf(" USB_ATTACHED_SUBSTATE_WAIT_SOF\n");
break;
case USB_ATTACHED_SUBSTATE_WAIT_RESET:
Serial.printf(" USB_ATTACHED_SUBSTATE_WAIT_RESET\n");
break;
case USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE:
Serial.printf(" USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE\n");
break;
case USB_STATE_CONFIGURING:
Serial.printf(" USB_STATE_CONFIGURING\n");
break;
case USB_STATE_RUNNING:
Serial.printf(" USB_STATE_RUNNING\n");
break;
case USB_STATE_ERROR:
Serial.printf(" USB_STATE_ERROR\n");
break;
}
}
if (Midi)
{
UsbMidi_Poll();
}
}
uint8_t msgQueue[128][3];
uint8_t msgQueueIn = 0;
uint8_t msgQueueOut = 0;
inline
void UsbMidi_HandleSysEx(uint8_t *buf, uint8_t len)
{
Serial.printf("not supported yet\n");
}
inline
void UsbMidi_HandleLiveMsg(uint8_t msg)
{
//Serial.printf("live msg\n");
}
inline
void UsbMidi_HandleShortMsg(uint8_t *data)
{
Serial.printf("short: %02x %02x %02x\n", data[0], data[1], data[2]);
/* forward data to mapped function */
for (int i = 0; i < usbMidiMapping.usbMidiMappingEntriesCount; i++)
{
if (((1 << 0) & usbMidiMapping.usbMidiMappingEntries[i].cableMask) > 0)
{
usbMidiMapping.usbMidiMappingEntries[i].shortMsg(data);
}
}
}
uint8_t MIDI_handleMsg(uint8_t *data, uint16_t len, uint8_t cable)
{
/* sometimes we got just zeros from some hardware */
int i = 0;
while (data[i] == 0)
{
i++;
}
if (i > 0)
{
return i;
}
if ((data[0] & 0xF0) == 0xF0)
{
/* handle status msg */
if (data[0] == 0xF0)
{
for (int i = 2; i < len; i++)
{
if (data[i] == 0xF7)
{
UsbMidi_HandleSysEx(data, i);
return i + 1;
}
}
}
else
{
UsbMidi_HandleLiveMsg(data[0]);
return 1;
}
}
else
{
memcpy(msgQueue[msgQueueIn], &data[0], 3);
msgQueueIn++;
if (msgQueueIn >= 128)
{
msgQueueIn = 0;
}
return 3;
}
Serial.printf("unhandled:\n");
for (int i = 0; i < len; i++)
{
Serial.printf("0x%02x ", data[i]);
}
Serial.printf("\n");
return 0;
}
/* process data now */
void UsbMidi_ProcessSync(void)
{
while (msgQueueIn != msgQueueOut)
{
UsbMidi_HandleShortMsg(msgQueue[msgQueueOut]);
msgQueueOut ++;
if (msgQueueOut >= 128)
{
msgQueueOut = 0;
}
}
}
static void UsbMidi_Poll()
{
uint8_t bufMidi[MIDI_EVENT_PACKET_SIZE];
uint16_t rcvd;
memset(bufMidi, 0xCC, sizeof(bufMidi));
if (Midi.idVendor() != vid || Midi.idProduct() != pid)
{
vid = Midi.idVendor();
pid = Midi.idProduct();
Serial.printf("VID: %04x, PID: %04x\n", vid, pid);
}
if (Midi.RecvData(&rcvd, bufMidi) == 0)
{
if (rcvd == 0)
{
/* Some devices sending a lot of empty messages */
return;
}
uint8_t *midiData = &bufMidi[0];
while (rcvd > 2)
{
midiData = &midiData[1];
int consumed = MIDI_handleMsg(midiData, rcvd, (bufMidi[0] >> 4) & 0xF);
midiData = &midiData[consumed];
if (consumed > rcvd)
{
Serial.printf("overrun!\n");
return;
}
rcvd -= consumed;
}
}
}
void UsbMidi_SendRaw(uint8_t *buf, uint8_t cable)
{
Midi.SendData(buf, cable);
}
inline
void UsbMidi_SendControlChange(uint8_t channel, uint8_t data1, uint8_t data2)
{
uint8_t shortBuf[3] = {(uint8_t)(0xB0U + (channel & 0x0FU)), data1, data2};
Serial2.write(shortBuf, 3);
}