← index #1066PR #1068
Duplicate · high · value 7.658
QUERY · ISSUE

USB-device-midi is missing helper function to send SysEx

openby mungewellopened 2025-11-22updated 2025-11-22

I was using USB-device-midi to send MTC to the host, which is essentially SysEx. But was slowed by the missing helper function to send the packet of SysEx. Anyhow, I wrote one...

Full example script here:
https://github.com/orgs/micropython/discussions/18450#discussioncomment-15043069

Happy to share with the project

    def send_sysex(self, p):
        # start of SysEx packet
        if len(p) > 3:
            w = self._tx.pend_write()
            if len(w) < 4:
                return False  # TX buffer is full. TODO: block here?

            w[0] = 0x4  # _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] = 0x7  # _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] = 0x6  # _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] = 0x5  # _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
CANDIDATE · PULL REQUEST

Bug #1066: Add helper function to send SysEx/MTC via usb-device-midi

openby mungewellopened 2025-11-25updated 2025-12-04

I was using USB-device-midi to send MTC to the host, which is essentially SysEx. But was slowed by the missing helper function to send the packet of SysEx. Anyhow, I wrote one...

Full example script here:
https://github.com/orgs/micropython/discussions/18450#discussioncomment-15043069

2 comments
dpgeorge · 2025-12-03

Thanks for the contribution.

But I'm a bit confused, I thought system exclusive messages were a completely different format, namely:

  • they start with 0xF0
  • they have an number of data bytes with the most-sig-bit clear
  • they end with 0xF7

How does that differ to the sys ex added here?

mungewell · 2025-12-04

You are correct, SysEx generally starts with 0xF0 and ends with 0xF7.

The helper function takes an array of bytes (say "0xf07f7f010160000000f7") and fragments it to pass over USB-midi protocol, then the USB host reassembles it to pass to client application.

The example script (for Pico) generates both the long and short messages that MTC uses.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied