-
Notifications
You must be signed in to change notification settings - Fork 4
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
format and simplify oranization #1
base: main
Are you sure you want to change the base?
Changes from 15 commits
851904e
5a50195
8a5316d
cd120e1
07c61b4
536a49c
43301c9
d3fd142
9d5ea55
a03f778
019d23d
4256998
4f00f88
9dbf9be
5e10201
08f09a4
55a1df0
7d505e7
1117fd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
use crate::data::byte::u4::U4; | ||
use crate::data::usb_midi::cable_number::CableNumber; | ||
use crate::data::usb_midi::code_index_number::CodeIndexNumber; | ||
use crate:: midi_types::MidiMessage; | ||
use core::convert::TryFrom; | ||
use midi_convert::{ | ||
MidiParseError, | ||
MidiRenderSlice, | ||
MidiTryParseSlice, | ||
use crate::{ | ||
cable_number::CableNumber, | ||
code_index_number::CodeIndexNumber, | ||
midi_convert::{MidiParseError, MidiRenderSlice, MidiTryParseSlice}, | ||
midi_types::MidiMessage, | ||
}; | ||
|
||
use core::convert::TryFrom; | ||
|
||
/// A packet that communicates with the host | ||
/// Currently supported is sending the specified normal midi | ||
|
@@ -22,22 +18,29 @@ pub struct UsbMidiEventPacket { | |
impl From<UsbMidiEventPacket> for [u8; 4] { | ||
fn from(value: UsbMidiEventPacket) -> [u8; 4] { | ||
let message = value.message; | ||
let cable_number = U4::from(value.cable_number); | ||
let index_number = { | ||
let code_index = CodeIndexNumber::find_from_message(&message); | ||
U4::from(code_index) | ||
}; | ||
let header = U4::combine(cable_number, index_number); | ||
let cable_number: u8 = value.cable_number.into(); | ||
let index_number: u8 = CodeIndexNumber::find_from_message(&message).into(); | ||
let header = cable_number << 4 | index_number; | ||
let mut data: [u8; 4] = [header, 0, 0, 0]; | ||
message.render_slice(&mut data[1..]); | ||
|
||
data | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
impl From<(CableNumber, MidiMessage)> for UsbMidiEventPacket { | ||
fn from(value: (CableNumber, MidiMessage)) -> Self { | ||
let (cable_number, message) = value; | ||
Self { | ||
cable_number, | ||
message, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub enum MidiPacketParsingError { | ||
MissingCableNumber, | ||
MissingHeader, | ||
MissingDataPacket, | ||
ParseError(MidiParseError), | ||
} | ||
|
@@ -48,16 +51,18 @@ impl TryFrom<&[u8]> for UsbMidiEventPacket { | |
fn try_from(value: &[u8]) -> Result<Self, Self::Error> { | ||
let raw_cable_number = match value.get(0) { | ||
Some(byte) => *byte >> 4, | ||
None => return Err(MidiPacketParsingError::MissingCableNumber) | ||
None => return Err(MidiPacketParsingError::MissingHeader), | ||
}; | ||
|
||
let cable_number = CableNumber::try_from(u8::from(raw_cable_number)).expect("(u8 >> 4) < 16"); | ||
let cable_number = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should be handled as a ParseError/similar instead of just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0xFF >> 4 == 0xF which will never fail here, so maybe it should just be |
||
CableNumber::try_from(u8::from(raw_cable_number)).expect("(u8 >> 4) < 16"); | ||
let message_body = match value.get(1..) { | ||
Some(bytes) => bytes, | ||
None => return Err(MidiPacketParsingError::MissingDataPacket), | ||
}; | ||
|
||
let message = MidiMessage::try_parse_slice(message_body).map_err(|e| MidiPacketParsingError::ParseError(e))?; | ||
let message = MidiMessage::try_parse_slice(message_body) | ||
.map_err(|e| MidiPacketParsingError::ParseError(e))?; | ||
|
||
Ok(UsbMidiEventPacket { | ||
cable_number, | ||
|
@@ -66,28 +71,21 @@ impl TryFrom<&[u8]> for UsbMidiEventPacket { | |
} | ||
} | ||
|
||
impl UsbMidiEventPacket { | ||
pub fn from_midi(cable: CableNumber, midi: MidiMessage) -> UsbMidiEventPacket { | ||
UsbMidiEventPacket { | ||
cable_number: cable, | ||
message: midi, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::data::usb_midi::cable_number::CableNumber::{Cable0, Cable1}; | ||
use crate::data::usb_midi::usb_midi_event_packet::UsbMidiEventPacket; | ||
use crate::midi_types::{Channel, Control, MidiMessage, Note, Program, Value14, Value7}; | ||
use crate::{ | ||
cable_number::CableNumber::{Cable0, Cable1}, | ||
event_packet::UsbMidiEventPacket, | ||
midi_types::{Channel, Control, MidiMessage, Note, Program, Value14, Value7}, | ||
}; | ||
use core::convert::TryFrom; | ||
|
||
macro_rules! decode_message_test { | ||
($($id:ident:$value:expr,)*) => { | ||
$( | ||
#[test] | ||
fn $id() { | ||
let (usb_midi_data_packet,expected) = $value; | ||
let (usb_midi_data_packet, expected) = $value; | ||
let message = UsbMidiEventPacket::try_from(&usb_midi_data_packet[..]).unwrap(); | ||
assert_eq!(expected, message); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
#![no_std] | ||
|
||
pub mod data; | ||
pub mod midi_device; | ||
mod util; | ||
|
||
/// re-exports | ||
pub use midi_convert; | ||
pub use midi_types; | ||
|
||
pub mod constants; | ||
|
||
pub use { | ||
cable_number::{CableNumber, InvalidCableNumber}, | ||
event_packet::{MidiPacketParsingError, UsbMidiEventPacket}, | ||
midi_device::{MidiClass, MidiClassInvalidArgs}, | ||
packet_reader::MidiPacketBufferReader, | ||
}; | ||
|
||
mod cable_number; | ||
mod code_index_number; | ||
mod event_packet; | ||
mod midi_device; | ||
mod packet_reader; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
use crate::data::usb::constants::*; | ||
use crate::data::usb_midi::usb_midi_event_packet::{MidiPacketParsingError, UsbMidiEventPacket}; | ||
use usb_device::class_prelude::*; | ||
use usb_device::Result; | ||
use crate::{constants::*, event_packet::UsbMidiEventPacket}; | ||
use usb_device::{class_prelude::*, Result}; | ||
|
||
const MIDI_IN_SIZE: u8 = 0x06; | ||
const MIDI_OUT_SIZE: u8 = 0x09; | ||
Comment on lines
4
to
5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these be moved to constants? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they're not |
||
|
||
pub const MIDI_PACKET_SIZE: usize = 4; | ||
pub const MAX_PACKET_SIZE: usize = 64; | ||
|
||
///Note we are using MidiIn here to refer to the fact that | ||
///The Host sees it as a midi in device | ||
///This class allows you to send data in | ||
/// USB Midi Device | ||
pub struct MidiClass<'a, B: UsbBus> { | ||
standard_ac: InterfaceNumber, | ||
standard_mc: InterfaceNumber, | ||
|
@@ -21,13 +14,9 @@ pub struct MidiClass<'a, B: UsbBus> { | |
n_out_jacks: u8, | ||
} | ||
|
||
pub enum MidiReadError { | ||
ParsingFailed(MidiPacketParsingError), | ||
UsbError(UsbError), | ||
} | ||
|
||
/// Invalid construction error struct | ||
#[derive(Debug)] | ||
pub struct InvalidArguments; | ||
pub struct MidiClassInvalidArgs; | ||
|
||
impl<B: UsbBus> MidiClass<'_, B> { | ||
/// Creates a new MidiClass with the provided UsbBus and `n_in/out_jacks` embedded input/output jacks (or "cables", | ||
|
@@ -37,9 +26,9 @@ impl<B: UsbBus> MidiClass<'_, B> { | |
alloc: &UsbBusAllocator<B>, | ||
n_in_jacks: u8, | ||
n_out_jacks: u8, | ||
) -> core::result::Result<MidiClass<'_, B>, InvalidArguments> { | ||
) -> core::result::Result<MidiClass<'_, B>, MidiClassInvalidArgs> { | ||
if n_in_jacks > 16 || n_out_jacks > 16 { | ||
return Err(InvalidArguments); | ||
return Err(MidiClassInvalidArgs); | ||
} | ||
Ok(MidiClass { | ||
standard_ac: alloc.interface(), | ||
|
@@ -66,23 +55,23 @@ impl<B: UsbBus> MidiClass<'_, B> { | |
/// calculates the index'th external midi in jack id | ||
fn in_jack_id_ext(&self, index: u8) -> u8 { | ||
debug_assert!(index < self.n_in_jacks); | ||
return 2 * index + 1; | ||
2 * index + 1 | ||
} | ||
/// calculates the index'th embedded midi out jack id | ||
fn out_jack_id_emb(&self, index: u8) -> u8 { | ||
debug_assert!(index < self.n_in_jacks); | ||
return 2 * index + 2; | ||
2 * index + 2 | ||
} | ||
|
||
/// calculates the index'th external midi out jack id | ||
fn out_jack_id_ext(&self, index: u8) -> u8 { | ||
debug_assert!(index < self.n_out_jacks); | ||
return 2 * self.n_in_jacks + 2 * index + 1; | ||
2 * self.n_in_jacks + 2 * index + 1 | ||
} | ||
/// calculates the index'th embedded midi in jack id | ||
fn in_jack_id_emb(&self, index: u8) -> u8 { | ||
debug_assert!(index < self.n_out_jacks); | ||
return 2 * self.n_in_jacks + 2 * index + 2; | ||
2 * self.n_in_jacks + 2 * index + 2 | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe constants could be grouped into namespaces, eg.
constants::usb
, and misc. constants can just live in the top-level module, eg.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reduced the public constants to 6 total, the additional
usb
module seemed a bit excessive foruse
and navigating docs..