From 514d4ebcd7a2e8472a0511f4952135ed87c1dbd5 Mon Sep 17 00:00:00 2001 From: Oliver Rockstedt Date: Wed, 15 Jan 2025 12:43:39 +0100 Subject: [PATCH] Added U14::from_split_u7 and U14::split_u7 functions --- CHANGELOG.md | 2 +- src/message/data/u14.rs | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07f65d3..ba626a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/message/data/u14.rs b/src/message/data/u14.rs index 74389f8..11742b8 100644 --- a/src/message/data/u14.rs +++ b/src/message/data/u14.rs @@ -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)] @@ -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)] @@ -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))); + } }