use crate::protocol::opcode::RATE_CONFIDENT;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rate {
pub native_period_us: u16,
pub poll_period_us: u16,
pub confident: bool,
}
impl Rate {
pub fn native_hz(&self) -> Option<f32> {
if self.native_period_us == 0 {
None
} else {
Some(1_000_000.0 / f32::from(self.native_period_us))
}
}
pub(crate) fn from_payload(p: &[u8]) -> Option<Self> {
if p.len() < 6 {
return None;
}
Some(Rate {
native_period_us: u16::from_le_bytes([p[1], p[2]]),
poll_period_us: u16::from_le_bytes([p[3], p[4]]),
confident: p[5] & RATE_CONFIDENT != 0,
})
}
}