Skip to content

Commit 514d4eb

Browse files
committed
Added U14::from_split_u7 and U14::split_u7 functions
1 parent 13a8701 commit 514d4eb

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This release focuses on improving the internal message data types and their usag
2222

2323
### Changed
2424

25-
- Changed pitch wheel `Message` variant from `PitchWheelChange(Channel, U7, U7)` to `PitchWheelChange(Channel, U14)`.
25+
- 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.
2626

2727
## [0.4.0] - 2025-01-03
2828

src/message/data/u14.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! A primitive value with 14-bit length.
22
3-
use crate::message::data::{FromClamped, FromOverFlow};
3+
use crate::message::data::{u7::U7, FromClamped, FromOverFlow};
44

55
/// A primitive value that can be from 0-0x4000
66
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -50,6 +50,16 @@ impl U14 {
5050
pub const MAX: U14 = U14(0x3FFF);
5151
/// Minimum value for the type.
5252
pub const MIN: U14 = U14(0);
53+
54+
/// Creates a new U14 value from an (U7, U7) tuple containing the LSB and MSB.
55+
pub fn from_split_u7(value: (U7, U7)) -> Self {
56+
Self((value.0 .0 as u16) | ((value.1 .0 as u16) << 7))
57+
}
58+
59+
/// Returns the LSB and MSB of the value as (U7, U7) tuple.
60+
pub fn split_u7(&self) -> (U7, U7) {
61+
(U7((self.0 & 0x7F) as u8), U7((self.0 >> 7) as u8))
62+
}
5363
}
5464

5565
#[cfg(test)]
@@ -75,4 +85,14 @@ mod tests {
7585
fn from_clamped() {
7686
assert_eq!(U14::from_clamped(0x400F), U14(0x3FFF));
7787
}
88+
89+
#[test]
90+
fn from_split_u7() {
91+
assert_eq!(U14::from_split_u7((U7(0x7F), U7(0x6F))), U14(0x37FF));
92+
}
93+
94+
#[test]
95+
fn split_u7() {
96+
assert_eq!(U14(0x37FF).split_u7(), (U7(0x7F), U7(0x6F)));
97+
}
7898
}

0 commit comments

Comments
 (0)