Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions micropython/usb/usb-device-midi/usb/device/midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,65 @@ def note_off(self, channel, pitch, vel=0x40):
def control_change(self, channel, controller, value):
self.send_event(_CIN_CONTROL_CHANGE, _MIDI_CONTROL_CHANGE | channel, controller, value)

def send_sysex(self, p):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of duplicated code in this function. Do you think you can factor it to be simpler? For example:

def send_sysex(self, p):
    while p:
        w = self._tx.pend_write()
        if len(w) < 4:
           return False
        if len(p) > 3:
            w[0] = _CIN_SYSEX_START
            ...
        elif len(p) > 2:
            w[0] = _CIN_SYSEX_END_3BYTE
            ...
        elif len(p) > 1:
            ...
        else:
            w[0] = _CIN_SYSEX_END_1BYTE
        self._tx.finish_write(4)
        self._tx_xfer()

(And what's supposed to happen for when len(p) is greater than 7?)

if len(p) > 3:
w = self._tx.pend_write()
if len(w) < 4:
return False # TX buffer is full. TODO: block here?

w[0] = _CIN_SYSEX_START
w[1] = p[0]
w[2] = p[1]
w[3] = p[2]
self._tx.finish_write(4)
self._tx_xfer()

p = p[3:]

# play out til end
while p:
if len(p) > 2:
w = self._tx.pend_write()
if len(w) < 4:
return False # TX buffer is full. TODO: block here?

w[0] = _CIN_SYSEX_END_3BYTE
w[1] = p[0]
w[2] = p[1]
w[3] = p[2]
self._tx.finish_write(4)
self._tx_xfer()

p = p[3:]
elif len(p) > 1:
w = self._tx.pend_write()
if len(w) < 4:
return False # TX buffer is full. TODO: block here?

w[0] = _CIN_SYSEX_END_2BYTE
w[1] = p[0]
w[2] = p[1]
w[3] = 0
self._tx.finish_write(4)
self._tx_xfer()

p = p[2:]
else:
w = self._tx.pend_write()
if len(w) < 4:
return False # TX buffer is full. TODO: block here?

w[0] = _CIN_SYSEX_END_1BYTE
w[1] = p[0]
w[2] = 0
w[3] = 0
self._tx.finish_write(4)
self._tx_xfer()

p = p[1:]

return True

def send_event(self, cin, midi0, midi1=0, midi2=0):
# Queue a MIDI Event Packet to send to the host.
#
Expand Down
Loading