|
| 1 | +//! Example for ESP32-S3. |
| 2 | +
|
| 3 | +#![no_std] |
| 4 | +#![no_main] |
| 5 | + |
| 6 | +use core::ptr::addr_of_mut; |
| 7 | + |
| 8 | +use esp_backtrace as _; |
| 9 | +use esp_println::println; |
| 10 | +use usb_device::prelude::*; |
| 11 | +use usbd_midi::{ |
| 12 | + data::usb_midi::midi_packet_reader::MidiPacketBufferReader, midi_device::MidiClass, |
| 13 | +}; |
| 14 | + |
| 15 | +static mut EP_MEMORY: [u32; 1024] = [0; 1024]; |
| 16 | + |
| 17 | +#[esp_hal::xtensa_lx_rt::entry] |
| 18 | +fn main() -> ! { |
| 19 | + let mut config = esp_hal::Config::default(); |
| 20 | + config.cpu_clock = esp_hal::clock::CpuClock::Clock240MHz; |
| 21 | + let peripherals = esp_hal::init(config); |
| 22 | + |
| 23 | + let usb_bus_allocator = esp_hal::otg_fs::UsbBus::new( |
| 24 | + esp_hal::otg_fs::Usb::new(peripherals.USB0, peripherals.GPIO20, peripherals.GPIO19), |
| 25 | + unsafe { &mut *addr_of_mut!(EP_MEMORY) }, |
| 26 | + ); |
| 27 | + |
| 28 | + let mut midi_class = MidiClass::new(&usb_bus_allocator, 1, 1).unwrap(); |
| 29 | + |
| 30 | + let mut usb_dev = UsbDeviceBuilder::new(&usb_bus_allocator, UsbVidPid(0x16c0, 0x5e4)) |
| 31 | + .device_class(0) |
| 32 | + .device_sub_class(0) |
| 33 | + .strings(&[StringDescriptors::default() |
| 34 | + .manufacturer("Music Company") |
| 35 | + .product("MIDI Device") |
| 36 | + .serial_number("12345678")]) |
| 37 | + .unwrap() |
| 38 | + .build(); |
| 39 | + |
| 40 | + loop { |
| 41 | + if usb_dev.poll(&mut [&mut midi_class]) { |
| 42 | + let mut buffer = [0; 64]; |
| 43 | + if let Ok(size) = midi_class.read(&mut buffer) { |
| 44 | + let buffer_reader = MidiPacketBufferReader::new(&buffer, size); |
| 45 | + for packet in buffer_reader.into_iter() { |
| 46 | + if let Ok(packet) = packet { |
| 47 | + println!("{:?}", packet); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments