Skip to content

Commit

Permalink
Added U14::from_split_u7 and U14::split_u7 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcebox committed Jan 15, 2025
1 parent 13a8701 commit 514d4eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This release focuses on improving the internal message data types and their usag

### Changed

- Changed pitch wheel `Message` variant from `PitchWheelChange(Channel, U7, U7)` to `PitchWheelChange(Channel, U14)`.
- Changed pitch wheel `Message` variant from `PitchWheelChange(Channel, U7, U7)` to `PitchWheelChange(Channel, U14)`. Use `U14::from_split_u7` and `U14::split_u7` functions for conversions.

## [0.4.0] - 2025-01-03

Expand Down
22 changes: 21 additions & 1 deletion src/message/data/u14.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A primitive value with 14-bit length.
use crate::message::data::{FromClamped, FromOverFlow};
use crate::message::data::{u7::U7, FromClamped, FromOverFlow};

/// A primitive value that can be from 0-0x4000
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -50,6 +50,16 @@ impl U14 {
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)]
Expand All @@ -75,4 +85,14 @@ mod tests {
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)));
}
}

0 comments on commit 514d4eb

Please sign in to comment.