Skip to content

Commit

Permalink
Renamed term event to payload in several locations
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcebox committed Dec 21, 2024
1 parent 248f2fc commit 0398975
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added

- `UsbMidiEventPacket::cable_number` function.
- `UsbMidiEventPacket::as_message_bytes` function.
- `UsbMidiEventPacket::payload_bytes` function.
- `UsbMidiEventPacket::as_raw_bytes` function.
- `UsbMidiEventPacket::to_raw_bytes` function.
- `UsbMidiEventPacket::try_from_message_bytes` function.
- `UsbMidiEventPacket::try_from_payload_bytes` function.
- `TryFrom<&UsbMidiEventPacket>` implementation for `Message` type.
- `Message::into_packet` function.
- `Message::code_index_number` function.
- `CodeIndexNumber::try_from_event` function.
- `CodeIndexNumber::event_size` function.
- `CodeIndexNumber::try_from_payload` function.
- `CodeIndexNumber::payload_size` function.
- `CableNumber::Cable0` as default value.
- `FromOverFlow<u8> for U4` implementation.
- `FromClamped<u8> for U4` implementation.
Expand Down
4 changes: 2 additions & 2 deletions examples/example-esp32s3/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() -> ! {
let buffer_reader = MidiPacketBufferReader::new(&buffer, size);
for packet in buffer_reader.into_iter() {
if let Ok(packet) = packet {
let message = MidiMessage::try_parse_slice(packet.as_message_bytes());
let message = MidiMessage::try_parse_slice(packet.payload_bytes());
println!("Cable: {:?}, Message: {:?}", packet.cable_number(), message);
}
}
Expand All @@ -74,7 +74,7 @@ fn main() -> ! {
message.render_slice(&mut bytes);

let packet =
UsbMidiEventPacket::try_from_message_bytes(CableNumber::Cable0, &bytes).unwrap();
UsbMidiEventPacket::try_from_payload_bytes(CableNumber::Cable0, &bytes).unwrap();
let result = midi_class.send_packet(packet);

println!("Send result {:?}", result);
Expand Down
16 changes: 8 additions & 8 deletions src/packet/code_index_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ impl From<CodeIndexNumber> for U4 {
}

impl CodeIndexNumber {
/// Creates a new number from a MIDI event.
pub fn try_from_event(event: &[u8]) -> Result<Self, MidiPacketParsingError> {
let Some(status) = event.first() else {
return Err(MidiPacketParsingError::EmptyEvent);
/// Creates a new number from a MIDI event payload.
pub fn try_from_payload(payload: &[u8]) -> Result<Self, MidiPacketParsingError> {
let Some(status) = payload.first() else {
return Err(MidiPacketParsingError::EmptyPayload);
};

if *status < 0xF0 {
Expand All @@ -107,21 +107,21 @@ impl CodeIndexNumber {
0xC0 => Ok(Self::ProgramChange),
0xD0 => Ok(Self::ChannelPressure),
0xE0 => Ok(Self::PitchBendChange),
_ => Err(MidiPacketParsingError::InvalidEventStatus),
_ => Err(MidiPacketParsingError::InvalidPayloadStatus),
}
} else {
match status {
0xF1 | 0xF3 => Ok(Self::SystemCommon2Bytes),
0xF2 => Ok(Self::SystemCommon3Bytes),
0xF6 => Ok(Self::SystemCommon1Byte),
0xF8 | 0xFA | 0xFB | 0xFC | 0xFE | 0xFF => Ok(Self::SingleByte),
_ => Err(MidiPacketParsingError::InvalidEventStatus),
_ => Err(MidiPacketParsingError::InvalidPayloadStatus),
}
}
}

/// Returns the size of the MIDI_x event in bytes.
pub fn event_size(&self) -> usize {
/// Returns the size of the MIDI_x event payload in bytes.
pub fn payload_size(&self) -> usize {
match self {
Self::SystemCommon1Byte | Self::SingleByte => 1,
Self::SystemCommon2Bytes
Expand Down
32 changes: 16 additions & 16 deletions src/packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ pub enum MidiPacketParsingError {
InvalidEventType(u8),
/// Missing data packet.
MissingDataPacket,
/// Empty event.
EmptyEvent,
/// Invalid event status.
InvalidEventStatus,
/// Invalid event size.
InvalidEventSize,
/// Empty payload.
EmptyPayload,
/// Invalid payload status.
InvalidPayloadStatus,
/// Invalid payload size.
InvalidPayloadSize,
}

impl TryFrom<&[u8]> for UsbMidiEventPacket {
Expand All @@ -67,13 +67,13 @@ impl UsbMidiEventPacket {
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] {
/// Returns a slice to the event payload bytes. The length is dependent on the payload type.
pub fn payload_bytes(&self) -> &[u8] {
let raw_cin = self.raw[0] & 0x0F;

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

Expand All @@ -92,21 +92,21 @@ impl UsbMidiEventPacket {
self.raw
}

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

if bytes.len() < event_size {
return Err(MidiPacketParsingError::InvalidEventSize);
if bytes.len() < payload_size {
return Err(MidiPacketParsingError::InvalidPayloadSize);
}

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]);
raw[1..1 + payload_size].copy_from_slice(&bytes[..payload_size]);

Ok(Self { raw })
}
Expand Down

0 comments on commit 0398975

Please sign in to comment.