diff --git a/lib-bms-protocol/src/types.rs b/lib-bms-protocol/src/types.rs index 7b5996c..31ee68e 100644 --- a/lib-bms-protocol/src/types.rs +++ b/lib-bms-protocol/src/types.rs @@ -130,6 +130,14 @@ impl TryFrom for Stored_U26 { /// | 26 bits u26 value | 6 bits crc value | pub struct Transit_U26(u32); +impl Transit_U26 { + pub fn new(value: u32) -> Self { + assert!(value <= U26_MAX_VALUE); + let crc6 = calc_crc6(value); + Self((value << 6) | (crc6 & 0x3F) as u32) + } +} + impl From for u32 { fn from(value: Transit_U26) -> Self { value.0 & U26_VALUE_MASK >> 6 @@ -143,7 +151,7 @@ impl TryFrom for Transit_U26 { if value > U26_MAX_VALUE { Err(U26Error::Overflow) } else { - todo!() + Ok(Transit_U26::new(value)) } } } @@ -324,7 +332,6 @@ pub fn check_hemming(data: u32, hemming: u8) -> Result<(), HemmingCorrectionValu /// - All single-bit errors /// - All double-bit errors /// - Random errors with a probability of 2^-6 = 0.015625 of going undetected -#[cfg(test)] pub fn calc_crc6(data: u32) -> u8 { let mut working_data: u32 = data << 6; let mut polynom: u32 = 0x43 << 25;