diff --git a/src/data/midi/channel.rs b/src/data/midi/channel.rs index 49ed33d..cbf7d1a 100644 --- a/src/data/midi/channel.rs +++ b/src/data/midi/channel.rs @@ -3,7 +3,7 @@ use core::convert::TryFrom; /// The Channel is a value ranging from 0x0 to 0xF /// This is a standard midi concept /// Note Channel1 = 0 on the wire -#[derive(Debug,Copy,Clone)] +#[derive(Debug,Copy,Clone, Eq, PartialEq)] #[repr(u8)] pub enum Channel { Channel1 = 0x0, Channel2 = 0x1, Channel3 = 0x2, Channel4 = 0x3, diff --git a/src/data/midi/message/message.rs b/src/data/midi/message/message.rs index 3125e8d..bd95061 100644 --- a/src/data/midi/message/message.rs +++ b/src/data/midi/message/message.rs @@ -14,7 +14,7 @@ type Velocity = U7; /// Note: not current exhaustive and SysEx messages end up /// being a confusing case. So are currently note implemented /// they are sort-of unbounded -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub enum Message { NoteOff(Channel,Note,Velocity), NoteOn(Channel,Note,Velocity), diff --git a/src/data/midi/notes.rs b/src/data/midi/notes.rs index 2273eea..cca582e 100644 --- a/src/data/midi/notes.rs +++ b/src/data/midi/notes.rs @@ -5,7 +5,7 @@ use num_enum::TryFromPrimitive; /// note the flat versions are associated constants /// but can be referenced like Note::Bb3 /// C1m is the C-1 -#[derive(Debug,Copy,Clone,TryFromPrimitive)] +#[derive(Debug,Copy,Clone,TryFromPrimitive,Eq,PartialEq)] #[repr(u8)] pub enum Note { C1m, Cs1m, D1m, Ds1m, E1m, F1m, Fs1m, G1m, Gs1m, A1m, As1m, B1m, diff --git a/src/data/usb_midi/cable_number.rs b/src/data/usb_midi/cable_number.rs index 1fff8a3..39500fa 100644 --- a/src/data/usb_midi/cable_number.rs +++ b/src/data/usb_midi/cable_number.rs @@ -4,7 +4,7 @@ use crate::data::byte::u4::U4; /// The Cable Number (CN) is a value ranging from 0x0 to 0xF /// indicating the number assignment of the Embedded MIDI Jack associated /// with the endpoint that is transferring the data -#[derive(Debug,Clone,Copy)] +#[derive(Debug,Clone,Copy,Eq, PartialEq)] #[repr(u8)] pub enum CableNumber { Cable0 = 0x0, Cable1 = 0x1, Cable2 = 0x2, Cable3 = 0x3, diff --git a/src/data/usb_midi/usb_midi_event_packet.rs b/src/data/usb_midi/usb_midi_event_packet.rs index a3cd807..5dcb14b 100644 --- a/src/data/usb_midi/usb_midi_event_packet.rs +++ b/src/data/usb_midi/usb_midi_event_packet.rs @@ -9,7 +9,7 @@ use core::convert::TryFrom; /// A packet that communicates with the host /// Currently supported is sending the specified normal midi /// message over the supplied cable number -#[derive(Debug)] +#[derive(Debug,Eq, PartialEq)] pub struct UsbMidiEventPacket { pub cable_number : CableNumber, pub message: Message @@ -84,4 +84,60 @@ impl UsbMidiEventPacket{ message : midi } } +} + +#[cfg(test)] +mod tests { + use core::convert::TryFrom; + use crate::data::usb_midi::usb_midi_event_packet::UsbMidiEventPacket; + use crate::data::midi::channel::Channel::{Channel1, Channel2}; + use crate::data::midi::notes::Note; + use crate::data::byte::u7::U7; + use crate::data::midi::message::Message; + use crate::data::usb_midi::cable_number::CableNumber::{Cable0,Cable1}; + use crate::data::midi::message::control_function::ControlFunction; + + macro_rules! decode_message_test { + ($($id:ident:$value:expr,)*) => { + $( + #[test] + fn $id() { + let (usb_midi_data_packet,expected) = $value; + let message = UsbMidiEventPacket::try_from(&usb_midi_data_packet[..]).unwrap(); + assert_eq!(expected, message); + } + )* + } + } + + decode_message_test! { + note_on: ([9, 144, 36, 127], UsbMidiEventPacket { + cable_number: Cable0, + message: Message::NoteOn(Channel1, Note::C2, U7(127)) + }), + note_off: ([8, 128, 36, 0], UsbMidiEventPacket { + cable_number: Cable0, + message: Message::NoteOff(Channel1, Note::C2, U7(0)) + }), + polyphonic_aftertouch: ([10, 160, 36, 64], UsbMidiEventPacket { + cable_number: Cable0, + message: Message::PolyphonicAftertouch(Channel1, Note::C2, U7(64)) + }), + program_change: ([28, 192, 127, 0], UsbMidiEventPacket { + cable_number: Cable1, + message: Message::ProgramChange(Channel1, U7(127)) + }), + channel_aftertouch: ([13, 208, 127, 0], UsbMidiEventPacket { + cable_number: Cable0, + message: Message::ChannelAftertouch(Channel1, U7(127)) + }), + pitch_wheel: ([14, 224, 64, 32], UsbMidiEventPacket { + cable_number: Cable0, + message: Message::PitchWheelChange(Channel1, U7(64), U7(32)) + }), + control_change: ([11, 177, 1, 32], UsbMidiEventPacket { + cable_number: Cable0, + message: Message::ControlChange(Channel2, ControlFunction::MOD_WHEEL_1, U7(32)) + }), + } } \ No newline at end of file