Skip to content

Commit

Permalink
Moved code from event_packet.rs into packet module root
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcebox committed Dec 21, 2024
1 parent 7ea917e commit 8e9eaf1
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 112 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Renamed `midi_device` module to `class`.
- Renamed `usb_midi` module to `packet` and moved it into crate root.
- Renamed `midi_packet_reader` module to `reader`.
- Renamed `usb_midi_event_packet` module to `event_packet`.
- Moved `usb_midi_event_packet` code into new `packet` module.
- Moved `channel` and `notes` modules into `message` module.
- Moved `message` module to crate root.
- Moved `byte` submodules into `data` module.
Expand Down
3 changes: 2 additions & 1 deletion src/class.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Contains the class implementation.
use crate::packet::event_packet::{MidiPacketParsingError, UsbMidiEventPacket};
use usb_device::class_prelude::*;
use usb_device::Result;

use crate::packet::{MidiPacketParsingError, UsbMidiEventPacket};

// Constants for use in descriptors.
const USB_AUDIO_CLASS: u8 = 0x01;
const USB_AUDIOCONTROL_SUBCLASS: u8 = 0x01;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub mod message;

pub use crate::class::MidiClass;
pub use crate::packet::cable_number::CableNumber;
pub use crate::packet::event_packet::UsbMidiEventPacket;
pub use crate::packet::reader::MidiPacketBufferReader;
pub use crate::packet::UsbMidiEventPacket;

#[cfg(feature = "message-types")]
pub use crate::message::Message;
4 changes: 2 additions & 2 deletions src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::message::notes::Note;
use crate::message::raw::{Payload, Raw};
use crate::packet::cable_number::CableNumber;
use crate::packet::code_index_number::CodeIndexNumber;
use crate::packet::event_packet::{MidiPacketParsingError, UsbMidiEventPacket};
use crate::packet::{MidiPacketParsingError, UsbMidiEventPacket};

type Velocity = U7;

Expand Down Expand Up @@ -216,7 +216,7 @@ mod tests {
use crate::message::notes::Note;
use crate::message::Message;
use crate::packet::cable_number::CableNumber::{Cable0, Cable1};
use crate::packet::event_packet::UsbMidiEventPacket;
use crate::packet::UsbMidiEventPacket;

macro_rules! decode_message_test {
($($id:ident:$value:expr,)*) => {
Expand Down
2 changes: 1 addition & 1 deletion src/packet/cable_number.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Enum representing the cable number of a packet.
use crate::data::u4::U4;
use crate::packet::event_packet::MidiPacketParsingError;
use crate::packet::MidiPacketParsingError;

/// The Cable Number (CN) is a value ranging from 0x0 to 0xF
/// indicating the number assignment of the Embedded MIDI Jack associated
Expand Down
2 changes: 1 addition & 1 deletion src/packet/code_index_number.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Enum representing the code index number of a packet.
use crate::data::u4::U4;
use crate::packet::event_packet::MidiPacketParsingError;
use crate::packet::MidiPacketParsingError;

/// The Code Index Number(CIN) indicates the classification
/// of the bytes in the MIDI_x fields.
Expand Down
103 changes: 0 additions & 103 deletions src/packet/event_packet.rs

This file was deleted.

103 changes: 102 additions & 1 deletion src/packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,106 @@
pub mod cable_number;
pub mod code_index_number;
pub mod event_packet;
pub mod reader;

use crate::packet::cable_number::CableNumber;
use crate::packet::code_index_number::CodeIndexNumber;

/// A packet that communicates with the host.
///
/// Currently supported is sending the specified normal midi
/// message over the supplied cable number
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct UsbMidiEventPacket {
/// Raw packet data.
raw: [u8; 4],
}

impl From<UsbMidiEventPacket> for [u8; 4] {
fn from(value: UsbMidiEventPacket) -> [u8; 4] {
value.raw
}
}

/// Error variants for parsing the packet.
#[derive(Debug)]
pub enum MidiPacketParsingError {
/// Invalid packet.
InvalidPacket,
/// Invalid note.
InvalidNote(u8),
/// Invalid cable number.
InvalidCableNumber(u8),
/// Invalid code index number.
InvalidCodeIndexNumber(u8),
/// Invalid event type.
InvalidEventType(u8),
/// Missing data packet.
MissingDataPacket,
/// Empty event.
EmptyEvent,
/// Invalid event status.
InvalidEventStatus,
}

impl TryFrom<&[u8]> for UsbMidiEventPacket {
type Error = MidiPacketParsingError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let Ok(raw) = value.try_into() else {
return Err(MidiPacketParsingError::InvalidPacket);
};

Ok(Self { raw })
}
}

impl UsbMidiEventPacket {
/// Returns the cable number.
pub fn cable_number(&self) -> CableNumber {
let raw_cable_number = self.raw[0] >> 4;

// Unwrap can't fail because of limited `raw_cable_number` value range.
CableNumber::try_from(raw_cable_number).unwrap_or_default()
}

/// Returns a slice to the message bytes. The length is dependent on the message type.
pub fn as_message_bytes(&self) -> &[u8] {
let raw_cin = self.raw[0] & 0x0F;

match CodeIndexNumber::try_from(raw_cin) {
Ok(cin) => {
let size = cin.event_size();
&self.raw[1..1 + size]
}

// Can't happen because of limited `raw_cin` value range.
Err(_) => &[],
}
}

/// Returns a reference to the raw bytes.
pub fn as_raw_bytes(&self) -> &[u8] {
&self.raw
}

/// Returns the raw bytes as owned array.
pub fn to_raw_bytes(&self) -> [u8; 4] {
self.raw
}

/// Creates a packet from a slice of message bytes.
pub fn try_from_message_bytes(
cable: CableNumber,
bytes: &[u8],
) -> Result<Self, MidiPacketParsingError> {
let cin = CodeIndexNumber::try_from_event(bytes)?;
let event_size = cin.event_size();

let mut raw = [0; 4];
raw[0] = (cable as u8) << 4 | cin as u8;
raw[1..1 + event_size].copy_from_slice(&bytes[..event_size]);

Ok(Self { raw })
}
}
2 changes: 1 addition & 1 deletion src/packet/reader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Reader for received packets.
use crate::class::{MAX_PACKET_SIZE, MIDI_PACKET_SIZE};
use crate::packet::event_packet::{MidiPacketParsingError, UsbMidiEventPacket};
use crate::packet::{MidiPacketParsingError, UsbMidiEventPacket};

/// Packet reader with internal buffer for received message.
pub struct MidiPacketBufferReader<'a> {
Expand Down

0 comments on commit 8e9eaf1

Please sign in to comment.