usbd-midi

Crates.iousbd-midi
lib.rsusbd-midi
version0.3.0
sourcesrc
created_at2019-12-27 10:42:02.708499
updated_at2024-05-27 16:46:58.1507
descriptionA USB MIDI implementation for usb-device.
homepagehttps://github.com/rust-embedded-community/usbd-midi
repositoryhttps://github.com/rust-embedded-community/usbd-midi
max_upload_size
id192618
size65,874
all (github:rust-embedded-community:all)

documentation

README

usbd-midi

A simple USB MIDI device class for usb-device.

Currently this aims to be a very simple implementation, that allows the microcontroller to send or receive MIDI information to/from a host like a desktop computer.

This crate requires the use of a HAL that implements the usb-device traits.

Example

Receive MIDI

Turn on an LED as long as note C2 is pressed. The example only shows the hardware-independent parts.

// Prerequisites, must be setup according to the used board.
let mut led = todo!(); // Must implement `embedded_hal::digital::OutputPin`.
let usb_bus = todo!(); // Must be of type `usb_device::bus::UsbBusAllocator`.

// Create a MIDI class with 1 input and 1 output jack.
let mut midi = MidiClass::new(&usb_bus, 1, 1);

let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x5e4))
    .product("MIDI Test")
    .device_class(0)
    .device_sub_class(0)
    .build();

loop {
    if !usb_dev.poll(&mut [&mut midi]) {
        continue;
    }

    let mut buffer = [0; 64];

    if let Ok(size) = midi.read(&mut buffer) {
        let buffer_reader = MidiPacketBufferReader::new(&buffer, size);
        for packet in buffer_reader.into_iter() {
            if let Ok(packet) = packet {
                match packet.message {
                    Message::NoteOn(Channel1, Note::C2, ..) => {
                        led.set_low().unwrap();
                    },
                    Message::NoteOff(Channel1, Note::C2, ..) => {
                        led.set_high().unwrap();
                    },
                    _ => {}
                }
            }
        }
    }
}

Using more than one MIDI port

Calling MidiClass::new(&usb_bus, N, M); with N, M >= 1 to provide more than one input or output port requires the control-buffer-256 feature of the usb-device crate:

Cargo.toml:

usb-device = { version = ">=0.3.2", features = ["control-buffer-256"] }

Up to 5 in/out pairs can be used this way until we again run out of buffer space. Note that exceeding the available buffer space will silently fail to send the descriptors correctly, no obvious panic! will hint the actual problem.

Commit count: 61

cargo fmt