Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use midi-types and reorg #10

Closed
wants to merge 16 commits into from
Closed
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license = "MIT"
embedded-hal = "0.2.2"
nb = "1.0.0"
usb-device = "0.2.3"
midi-convert = "0.1.0"

[dependencies.num_enum]
version = "0.5.1"
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ usb-device traits.
## Example

### Receive MIDI
Turn on the integrated LED of a STM32 BluePill board as long as C2 is pressed
Turn on the integrated LED of a STM32 BluePill board as long a note is held
```rust
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
Expand All @@ -39,8 +39,7 @@ fn main() -> ! {

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

loop {
Expand All @@ -55,10 +54,10 @@ fn main() -> ! {
for packet in buffer_reader.into_iter() {
if let Ok(packet) = packet {
match packet.message {
Message::NoteOn(Channel1, Note::C2, ..) => {
MidiMessage::NoteOn(..) => {
led.set_low().unwrap();
},
Message::NoteOff(Channel1, Note::C2, ..) => {
MidiMessage::NoteOff(..) => {
led.set_high().unwrap();
},
_ => {}
Expand Down
67 changes: 67 additions & 0 deletions src/code_index_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! The Code Index Number(CIN) indicates the classification
//! of the bytes in the MIDI_x fields.
//!
//! # Note
//! These numbers are all pre-shifted and ready to be ORed with the cable number nibble.
use crate::midi_types::MidiMessage;

/// Miscellaneous function codes. Reserved for future extensions
pub const MISC_FUNCTION: u8 = 0x00;
/// Cable events. Reserved for future expansion.
pub const CABLE_EVENTS: u8 = 0x10;
/// Two-byte System Common messages like MTC, SongSelect, etc.
pub const SYSTEM_COMMON_LEN2: u8 = 0x20;
/// Three-byte System Common messages like SPP, etc.
pub const SYSTEM_COMMON_LEN3: u8 = 0x30;
/// SysEx starts or continues
pub const SYSEX_STARTS: u8 = 0x40;
pub const SYSEX_CONTINUES: u8 = SYSEX_STARTS;
/// Single-byte System Common Message or SysEx ends with following single byte.
pub const SYSTEM_COMMON_LEN1: u8 = 0x50;
/// SysEx ends with the following byte
pub const SYSEX_ENDS_NEXT1: u8 = SYSTEM_COMMON_LEN1;
/// SysEx ends with following two bytes
pub const SYSEX_ENDS_NEXT2: u8 = 0x60;
/// SysEx ends with following three bytes
pub const SYSEX_ENDS_NEXT3: u8 = 0x70;
/// Note - Off
pub const NOTE_OFF: u8 = 0x80;
/// Note - On
pub const NOTE_ON: u8 = 0x90;
/// Poly-KeyPress
pub const POLY_KEYPRESS: u8 = 0xA0;
/// Control Change
pub const CONTROL_CHANGE: u8 = 0xB0;
/// Program Change
pub const PROGRAM_CHANGE: u8 = 0xC0;
/// Channel Pressure
pub const CHANNEL_PRESSURE: u8 = 0xD0;
/// Pitch Bend Change
pub const PITCHBEND_CHANGE: u8 = 0xE0;
/// Single Byte
pub const SINGLE_BYTE: u8 = 0xF0;

/// Find the appropriate Code Index Number from a `MidiMessage`
pub fn find_from_message(value: &MidiMessage) -> u8 {
match value {
MidiMessage::NoteOn(_, _, _) => NOTE_ON,
MidiMessage::NoteOff(_, _, _) => NOTE_OFF,
MidiMessage::ChannelPressure(_, _) => CHANNEL_PRESSURE,
MidiMessage::PitchBendChange(_, _) => PITCHBEND_CHANGE,
MidiMessage::KeyPressure(_, _, _) => POLY_KEYPRESS,
MidiMessage::ProgramChange(_, _) => PROGRAM_CHANGE,
MidiMessage::ControlChange(_, _, _) => CONTROL_CHANGE,

MidiMessage::QuarterFrame(_) | MidiMessage::SongSelect(_) => SYSTEM_COMMON_LEN2,

MidiMessage::SongPositionPointer(_) => SYSTEM_COMMON_LEN3,

MidiMessage::TuneRequest
| MidiMessage::TimingClock
| MidiMessage::Start
| MidiMessage::Continue
| MidiMessage::Stop
| MidiMessage::ActiveSensing
| MidiMessage::Reset => SYSTEM_COMMON_LEN1,
}
}
19 changes: 19 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Shared constants
//This should be better organized in future

pub const USB_CLASS_NONE: u8 = 0x00;
pub const USB_AUDIO_CLASS: u8 = 0x01;
pub const USB_AUDIOCONTROL_SUBCLASS: u8 = 0x01;
pub const USB_MIDISTREAMING_SUBCLASS: u8 = 0x03;
pub const MIDI_IN_JACK_SUBTYPE: u8 = 0x02;
pub const MIDI_OUT_JACK_SUBTYPE: u8 = 0x03;
pub const EMBEDDED: u8 = 0x01;
pub const EXTERNAL: u8 = 0x02;
pub const CS_INTERFACE: u8 = 0x24;
pub const CS_ENDPOINT: u8 = 0x25;
pub const HEADER_SUBTYPE: u8 = 0x01;
pub const MS_HEADER_SUBTYPE: u8 = 0x01;
pub const MS_GENERAL: u8 = 0x01;

pub const MIDI_PACKET_SIZE: usize = 4;
pub const MAX_PACKET_SIZE: usize = 64;
12 changes: 0 additions & 12 deletions src/data/byte/from_traits.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/data/byte/mod.rs

This file was deleted.

46 changes: 0 additions & 46 deletions src/data/byte/u4.rs

This file was deleted.

50 changes: 0 additions & 50 deletions src/data/byte/u7.rs

This file was deleted.

83 changes: 0 additions & 83 deletions src/data/midi/channel.rs

This file was deleted.

Loading