Skip to content

Commit 20f5c3d

Browse files
committed
usb-device-midi: Add helper function to send SysEx/MTC via usb-device-midi
Signed-off-by: Simon Wood <[email protected]>
1 parent 852f2cc commit 20f5c3d

File tree

1 file changed

+59
-0
lines changed
  • micropython/usb/usb-device-midi/usb/device

1 file changed

+59
-0
lines changed

micropython/usb/usb-device-midi/usb/device/midi.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,65 @@ def note_off(self, channel, pitch, vel=0x40):
108108
def control_change(self, channel, controller, value):
109109
self.send_event(_CIN_CONTROL_CHANGE, _MIDI_CONTROL_CHANGE | channel, controller, value)
110110

111+
def send_sysex(self, p):
112+
if len(p) > 3:
113+
w = self._tx.pend_write()
114+
if len(w) < 4:
115+
return False # TX buffer is full. TODO: block here?
116+
117+
w[0] = _CIN_SYSEX_START
118+
w[1] = p[0]
119+
w[2] = p[1]
120+
w[3] = p[2]
121+
self._tx.finish_write(4)
122+
self._tx_xfer()
123+
124+
p = p[3:]
125+
126+
# play out til end
127+
while p:
128+
if len(p) > 2:
129+
w = self._tx.pend_write()
130+
if len(w) < 4:
131+
return False # TX buffer is full. TODO: block here?
132+
133+
w[0] = _CIN_SYSEX_END_3BYTE
134+
w[1] = p[0]
135+
w[2] = p[1]
136+
w[3] = p[2]
137+
self._tx.finish_write(4)
138+
self._tx_xfer()
139+
140+
p = p[3:]
141+
elif len(p) > 1:
142+
w = self._tx.pend_write()
143+
if len(w) < 4:
144+
return False # TX buffer is full. TODO: block here?
145+
146+
w[0] = _CIN_SYSEX_END_2BYTE
147+
w[1] = p[0]
148+
w[2] = p[1]
149+
w[3] = 0
150+
self._tx.finish_write(4)
151+
self._tx_xfer()
152+
153+
p = p[2:]
154+
else:
155+
w = self._tx.pend_write()
156+
if len(w) < 4:
157+
return False # TX buffer is full. TODO: block here?
158+
159+
w[0] = _CIN_SYSEX_END_1BYTE
160+
w[1] = p[0]
161+
w[2] = 0
162+
w[3] = 0
163+
self._tx.finish_write(4)
164+
self._tx_xfer()
165+
166+
p = p[1:]
167+
168+
return True
169+
111170
def send_event(self, cin, midi0, midi1=0, midi2=0):
112171
# Queue a MIDI Event Packet to send to the host.
113172
#

0 commit comments

Comments
 (0)