Skip to content

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

Open
mungewell wants to merge 1 commit into
micropython:masterfrom
mungewell:bug_1066
Open

Bug #1066: Add helper function to send SysEx/MTC via usb-device-midi#1068
mungewell wants to merge 1 commit into
micropython:masterfrom
mungewell:bug_1066

Conversation

@mungewell

Copy link
Copy Markdown

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

Signed-off-by: Simon Wood <simon@mungewell.org>
@dpgeorge

dpgeorge commented Dec 3, 2025

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Author

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.

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?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants