use bit_field::BitField;
use crate::i2c::I2cStatus;
#[derive(Debug, Clone, Copy)]
pub struct Status {
pub i2c: I2cStatus,
pub interrupt_detected: bool,
pub hardware_revision: Revision,
pub firmware_revision: Revision,
pub adc_values: RawAdcValues,
}
impl Status {
pub(crate) fn from_buffer(buf: &[u8; 64]) -> Self {
Self {
i2c: I2cStatus {
communication_state: buf[8].into(),
transfer_requested_length: u16::from_le_bytes([buf[9], buf[10]]),
transfer_completed_length: u16::from_le_bytes([buf[11], buf[12]]),
internal_data_buffer_counter: buf[13],
bus_speed: buf[14].into(),
timeout_value: buf[15],
target_address: u16::from_le_bytes([buf[16], buf[17]]),
target_acknowledged_address: !buf[20].get_bit(6),
scl_line_high: buf[22] == 0x01,
sda_line_high: buf[23] == 0x01,
read_pending_value: buf[25],
},
interrupt_detected: buf[24] == 0x01,
hardware_revision: Revision {
major: buf[46] as char,
minor: buf[47] as char,
},
firmware_revision: Revision {
major: buf[48] as char,
minor: buf[49] as char,
},
adc_values: RawAdcValues {
ch1: u16::from_le_bytes([buf[50], buf[51]]),
ch2: u16::from_le_bytes([buf[52], buf[53]]),
ch3: u16::from_le_bytes([buf[54], buf[55]]),
},
}
}
}
#[derive(Clone, Copy)]
pub struct Revision {
pub major: char,
pub minor: char,
}
impl std::fmt::Debug for Revision {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Revision({}.{})", self.major, self.minor)
}
}
impl std::fmt::Display for Revision {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}", self.major, self.minor)
}
}
#[derive(Debug, Clone, Copy)]
pub struct RawAdcValues {
pub ch1: u16,
pub ch2: u16,
pub ch3: u16,
}