Skip to content

Commit 39d13e8

Browse files
committed
feat: Add MIDI USB test sketches
1 parent 5835806 commit 39d13e8

File tree

2 files changed

+316
-0
lines changed

2 files changed

+316
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#include <MIDI.h>
2+
3+
#if defined(USBCON)
4+
#include <midi_UsbTransport.h>
5+
6+
static const unsigned sUsbTransportBufferSize = 16;
7+
typedef midi::UsbTransport<sUsbTransportBufferSize> UsbTransport;
8+
9+
UsbTransport sUsbTransport;
10+
11+
MIDI_CREATE_INSTANCE(UsbTransport, sUsbTransport, MIDI);
12+
13+
#else // No USB available, fallback to Serial
14+
MIDI_CREATE_DEFAULT_INSTANCE();
15+
#endif
16+
17+
// --
18+
19+
void setupRxBenchmark()
20+
{
21+
MIDI.setHandleNoteOff(handleNoteOff);
22+
MIDI.setHandleNoteOn(handleNoteOn);
23+
MIDI.setHandleControlChange(handleControlChange);
24+
MIDI.setHandleProgramChange(handleProgramChange);
25+
MIDI.setHandleAfterTouchChannel(handleAfterTouchChannel);
26+
MIDI.setHandleAfterTouchPoly(handleAfterTouchPoly);
27+
MIDI.setHandlePitchBend(handlePitchBend);
28+
MIDI.setHandleStart(handleStart);
29+
MIDI.setHandleStop(handleStop);
30+
MIDI.setHandleContinue(handleContinue);
31+
MIDI.setHandleClock(handleClock);
32+
MIDI.setHandleSystemExclusive(handleSysEx);
33+
}
34+
35+
void startTxBenchmark()
36+
{
37+
for (int i = 0; i < 128; ++i)
38+
{
39+
MIDI.sendNoteOn(i, 127, 1);
40+
MIDI.sendNoteOff(i, 127, 1);
41+
}
42+
for (int i = 0; i < 128; ++i)
43+
{
44+
MIDI.sendControlChange(i, 127, 1);
45+
MIDI.sendControlChange(i, 0, 1);
46+
}
47+
for (int i = 0; i < 128; ++i)
48+
{
49+
MIDI.sendProgramChange(i, 1);
50+
}
51+
MIDI.sendRealTime(midi::Start);
52+
MIDI.sendRealTime(midi::Stop);
53+
MIDI.sendRealTime(midi::Continue);
54+
MIDI.sendRealTime(midi::Clock);
55+
56+
const byte length = 128;
57+
const byte data[128] = {
58+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
59+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
60+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
61+
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
62+
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
63+
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
64+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
65+
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
66+
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
67+
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
68+
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
69+
0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
70+
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
71+
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
72+
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
73+
0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
74+
};
75+
MIDI.sendSysEx(length, data, false);
76+
}
77+
78+
// --
79+
80+
void printChannel(byte inChannel)
81+
{
82+
Serial.print("[");
83+
Serial.print(inChannel);
84+
Serial.print("] ");
85+
}
86+
87+
void handleNoteOff(byte inChannel, byte inNote, byte inVelocity)
88+
{
89+
printChannel(inChannel);
90+
Serial.print("Note Off\t");
91+
Serial.print(inNote);
92+
Serial.print(' ');
93+
Serial.println(inVelocity);
94+
}
95+
void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
96+
{
97+
printChannel(inChannel);
98+
Serial.print("Note On\t");
99+
Serial.print(inNote);
100+
Serial.print(' ');
101+
Serial.println(inVelocity);
102+
}
103+
void handleControlChange(byte inChannel, byte inControl, byte inValue)
104+
{
105+
printChannel(inChannel);
106+
Serial.print("Control Change\t");
107+
Serial.print(inControl);
108+
Serial.print(' ');
109+
Serial.println(inValue);
110+
}
111+
void handleProgramChange(byte inChannel, byte inProgram)
112+
{
113+
printChannel(inChannel);
114+
Serial.print("Program Change\t");
115+
Serial.println(inProgram);
116+
}
117+
void handleAfterTouchChannel(byte inChannel, byte inPressure)
118+
{
119+
printChannel(inChannel);
120+
Serial.print("AT Channel\t");
121+
Serial.println(inPressure);
122+
}
123+
void handleAfterTouchPoly(byte inChannel, byte inNote, byte inPressure)
124+
{
125+
printChannel(inChannel);
126+
Serial.print("AT Poly\t");
127+
Serial.print(inNote);
128+
Serial.print(' ');
129+
Serial.println(inPressure);
130+
}
131+
void handlePitchBend(byte inChannel, int inBend)
132+
{
133+
printChannel(inChannel);
134+
Serial.print("Pitch Bend\t");
135+
Serial.println(inBend);
136+
}
137+
void handleStart()
138+
{
139+
Serial.println("Start");
140+
}
141+
void handleStop()
142+
{
143+
Serial.println("Stop");
144+
}
145+
void handleContinue()
146+
{
147+
Serial.println("Continue");
148+
}
149+
void handleClock()
150+
{
151+
Serial.println("Clock");
152+
}
153+
void handleSysEx(byte* inArray, unsigned inSize)
154+
{
155+
Serial.print("SysEx\t[ ");
156+
for (unsigned i = 0; i < inSize; ++i)
157+
{
158+
Serial.print(inArray[i], HEX);
159+
Serial.print(' ');
160+
}
161+
Serial.println(']');
162+
}
163+
164+
// --
165+
166+
void setup()
167+
{
168+
Serial.begin(115200);
169+
while(!Serial); // Wait for the Serial Monitor to open
170+
171+
MIDI.begin();
172+
MIDI.turnThruOff();
173+
174+
setupRxBenchmark();
175+
startTxBenchmark();
176+
}
177+
178+
void loop()
179+
{
180+
MIDI.read();
181+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <MIDI.h>
2+
3+
#if defined(USBCON)
4+
#include <midi_UsbTransport.h>
5+
6+
static const unsigned sUsbTransportBufferSize = 16;
7+
typedef midi::UsbTransport<sUsbTransportBufferSize> UsbTransport;
8+
9+
UsbTransport sUsbTransport;
10+
11+
MIDI_CREATE_INSTANCE(UsbTransport, sUsbTransport, MIDI);
12+
13+
#else // No USB available, fallback to Serial
14+
MIDI_CREATE_DEFAULT_INSTANCE();
15+
#endif
16+
17+
// --
18+
19+
void attachCallbacks()
20+
{
21+
MIDI.setHandleNoteOff(handleNoteOff);
22+
MIDI.setHandleNoteOn(handleNoteOn);
23+
MIDI.setHandleControlChange(handleControlChange);
24+
MIDI.setHandleProgramChange(handleProgramChange);
25+
MIDI.setHandleAfterTouchChannel(handleAfterTouchChannel);
26+
MIDI.setHandleAfterTouchPoly(handleAfterTouchPoly);
27+
MIDI.setHandlePitchBend(handlePitchBend);
28+
MIDI.setHandleStart(handleStart);
29+
MIDI.setHandleStop(handleStop);
30+
MIDI.setHandleContinue(handleContinue);
31+
MIDI.setHandleClock(handleClock);
32+
MIDI.setHandleSystemExclusive(handleSysEx);
33+
}
34+
35+
// --
36+
37+
void printChannel(byte inChannel)
38+
{
39+
Serial.print("[");
40+
Serial.print(inChannel);
41+
Serial.print("] ");
42+
}
43+
44+
void handleNoteOff(byte inChannel, byte inNote, byte inVelocity)
45+
{
46+
printChannel(inChannel);
47+
Serial.print("Note Off\t");
48+
Serial.print(inNote);
49+
Serial.print(' ');
50+
Serial.println(inVelocity);
51+
}
52+
void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
53+
{
54+
printChannel(inChannel);
55+
Serial.print("Note On\t");
56+
Serial.print(inNote);
57+
Serial.print(' ');
58+
Serial.println(inVelocity);
59+
}
60+
void handleControlChange(byte inChannel, byte inControl, byte inValue)
61+
{
62+
printChannel(inChannel);
63+
Serial.print("Control Change\t");
64+
Serial.print(inControl);
65+
Serial.print(' ');
66+
Serial.println(inValue);
67+
}
68+
void handleProgramChange(byte inChannel, byte inProgram)
69+
{
70+
printChannel(inChannel);
71+
Serial.print("Program Change\t");
72+
Serial.println(inProgram);
73+
}
74+
void handleAfterTouchChannel(byte inChannel, byte inPressure)
75+
{
76+
printChannel(inChannel);
77+
Serial.print("AT Channel\t");
78+
Serial.println(inPressure);
79+
}
80+
void handleAfterTouchPoly(byte inChannel, byte inNote, byte inPressure)
81+
{
82+
printChannel(inChannel);
83+
Serial.print("AT Poly\t");
84+
Serial.print(inNote);
85+
Serial.print(' ');
86+
Serial.println(inPressure);
87+
}
88+
void handlePitchBend(byte inChannel, int inBend)
89+
{
90+
printChannel(inChannel);
91+
Serial.print("Pitch Bend\t");
92+
Serial.println(inBend);
93+
}
94+
void handleStart()
95+
{
96+
Serial.println("Start");
97+
}
98+
void handleStop()
99+
{
100+
Serial.println("Stop");
101+
}
102+
void handleContinue()
103+
{
104+
Serial.println("Continue");
105+
}
106+
void handleClock()
107+
{
108+
Serial.println("Clock");
109+
}
110+
void handleSysEx(byte* inArray, unsigned inSize)
111+
{
112+
Serial.print("SysEx\t[ ");
113+
for (unsigned i = 0; i < inSize; ++i)
114+
{
115+
Serial.print(inArray[i], HEX);
116+
Serial.print(' ');
117+
}
118+
Serial.println(']');
119+
}
120+
121+
// --
122+
123+
void setup()
124+
{
125+
MIDI.begin(MIDI_CHANNEL_OMNI);
126+
127+
// attachCallbacks();
128+
// Serial.begin(115200);
129+
// while(!Serial); // Wait for the Serial Monitor to open
130+
}
131+
132+
void loop()
133+
{
134+
MIDI.read();
135+
}

0 commit comments

Comments
 (0)