-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from rust-embedded-community/next
Version 0.5.0
- Loading branch information
Showing
11 changed files
with
320 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
[package] | ||
name = "usbd-midi" | ||
version = "0.4.0" | ||
version = "0.5.0" | ||
authors = [ | ||
"Beau Trepp <[email protected]>", | ||
"Florian Jung <[email protected]>", | ||
"Oliver Rockstedt <[email protected]>", | ||
] | ||
edition = "2021" | ||
description = "A USB MIDI implementation for usb-device." | ||
rust-version = "1.78" | ||
description = "USB MIDI device class implementation for use with usb-device." | ||
homepage = "https://github.com/rust-embedded-community/usbd-midi" | ||
repository = "https://github.com/rust-embedded-community/usbd-midi" | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//! Primitives and structures used in USB MIDI data. | ||
pub mod u14; | ||
pub mod u4; | ||
pub mod u7; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
//! A primitive value with 14-bit length. | ||
use crate::message::data::{u7::U7, FromClamped, FromOverFlow}; | ||
|
||
/// A primitive value that can be from 0-0x4000 | ||
#[derive(Debug, Clone, Eq, PartialEq)] | ||
pub struct U14(pub(crate) u16); | ||
|
||
/// Error representing that this value is not a valid u14 | ||
#[derive(Debug, Clone, Eq, PartialEq)] | ||
pub struct InvalidU14(pub u16); | ||
|
||
impl TryFrom<u16> for U14 { | ||
type Error = InvalidU14; | ||
|
||
fn try_from(value: u16) -> Result<Self, Self::Error> { | ||
if value > 0x3FFF { | ||
Err(InvalidU14(value)) | ||
} else { | ||
Ok(U14(value)) | ||
} | ||
} | ||
} | ||
|
||
impl From<U14> for u16 { | ||
fn from(value: U14) -> u16 { | ||
value.0 | ||
} | ||
} | ||
|
||
impl FromOverFlow<u16> for U14 { | ||
fn from_overflow(value: u16) -> U14 { | ||
const MASK: u16 = 0b0011_1111_1111_1111; | ||
let value = MASK & value; | ||
U14(value) | ||
} | ||
} | ||
|
||
impl FromClamped<u16> for U14 { | ||
fn from_clamped(value: u16) -> U14 { | ||
match U14::try_from(value) { | ||
Ok(x) => x, | ||
_ => U14::MAX, | ||
} | ||
} | ||
} | ||
|
||
impl U14 { | ||
/// Maximum value for the type. | ||
pub const MAX: U14 = U14(0x3FFF); | ||
/// Minimum value for the type. | ||
pub const MIN: U14 = U14(0); | ||
|
||
/// Creates a new U14 value from an (U7, U7) tuple containing the LSB and MSB. | ||
pub fn from_split_u7(value: (U7, U7)) -> Self { | ||
Self((value.0 .0 as u16) | ((value.1 .0 as u16) << 7)) | ||
} | ||
|
||
/// Returns the LSB and MSB of the value as (U7, U7) tuple. | ||
pub fn split_u7(&self) -> (U7, U7) { | ||
(U7((self.0 & 0x7F) as u8), U7((self.0 >> 7) as u8)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn try_from_valid() { | ||
assert_eq!(U14::try_from(0x1234), Ok(U14(0x1234))); | ||
} | ||
|
||
#[test] | ||
fn try_from_invalid() { | ||
assert_eq!(U14::try_from(0x4000), Err(InvalidU14(0x4000))); | ||
} | ||
|
||
#[test] | ||
fn from_overflow() { | ||
assert_eq!(U14::from_overflow(0x400F), U14(0x0F)); | ||
} | ||
|
||
#[test] | ||
fn from_clamped() { | ||
assert_eq!(U14::from_clamped(0x400F), U14(0x3FFF)); | ||
} | ||
|
||
#[test] | ||
fn from_split_u7() { | ||
assert_eq!(U14::from_split_u7((U7(0x7F), U7(0x6F))), U14(0x37FF)); | ||
} | ||
|
||
#[test] | ||
fn split_u7() { | ||
assert_eq!(U14(0x37FF).split_u7(), (U7(0x7F), U7(0x6F))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.