Skip to content

Commit d36bad4

Browse files
committed
usbd: Refactor MIDI to work with latest set of changes.
Drops the idea of having more than one TX or RX "cable", it's a nightmare to debug this all the way through the OS MIDI Stack. Can still be added in future if needed. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 760b4c9 commit d36bad4

File tree

3 files changed

+193
-153
lines changed

3 files changed

+193
-153
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# MicroPython USB MIDI example
2+
# MIT license; Copyright (c) 2023 Angus Gratton
3+
import usbd
4+
import time
5+
6+
7+
class MIDIExample(usbd.MIDIUSB):
8+
def on_note_on(self, channel, pitch, vel):
9+
print(f"RX Note On channel {channel} pitch {pitch} velocity {vel}")
10+
11+
def on_note_off(self, channel, pitch, vel):
12+
print(f"RX Note Off channel {channel} pitch {pitch} velocity {vel}")
13+
14+
def on_control_change(self, channel, controller, value):
15+
print(f"RX Control channel {channel} controller {controller} value {value}")
16+
17+
18+
ud = usbd.get_usbdevice()
19+
20+
m = MIDIExample()
21+
ud.add_interface(m)
22+
ud.reenumerate()
23+
24+
print("Waiting for interface...")
25+
26+
while not m.is_open():
27+
time.sleep_ms(100)
28+
29+
print("Starting MIDI loop...")
30+
31+
control_val = 0
32+
channel = 0
33+
34+
35+
while m.is_open():
36+
time.sleep(1)
37+
m.note_on(channel, 60)
38+
time.sleep(0.5)
39+
m.note_off(channel, 60)
40+
time.sleep(1)
41+
m.control_change(channel, 64, control_val)
42+
control_val += 1
43+
if control_val == 0x7F:
44+
control_val = 0
45+
time.sleep(1)

0 commit comments

Comments
 (0)