use super::{Display, Frame, ParseError};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Debug, Display, Clone)]
pub struct Temperature(u16);
impl Temperature {
pub fn in_celsius(self) -> f32 {
let value = f32::from(self.0);
(value / 100.0) - 40.0
}
pub fn in_farenheit(self) -> f32 {
(self.in_celsius() * (9.0 / 5.0)) + 32.0
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Debug, Display, Clone)]
#[repr(align(8))]
pub enum HVAC {
Cabin(Temperature),
}
impl TryFrom<Frame> for HVAC {
type Error = ParseError;
fn try_from(frame: Frame) -> Result<Self, Self::Error> {
const ID: u32 = 0x33a;
const LEN: usize = 8;
if frame.id() != ID {
return Err(ParseError::Id { frame });
}
let data: [u8; LEN] = match frame.data().try_into() {
Ok(data) => data,
Err(_) => {
return Err(ParseError::Len {
frame,
expected: LEN,
})
}
};
Ok(HVAC::Cabin(Temperature(u16::from_be_bytes([
data[0], data[1],
]))))
}
}