diff --git a/src/ds323x/status.rs b/src/ds323x/status.rs index 031d764..941a07a 100644 --- a/src/ds323x/status.rs +++ b/src/ds323x/status.rs @@ -31,4 +31,20 @@ where } Ok(()) } + + /// Read the temperature. + pub fn get_temperature(&mut self) -> Result> { + let mut data = [Register::TEMP_MSB, 0, 0]; + self.iface.read_data(&mut data)?; + let is_negative = (data[1] & 0b1000_0000) != 0; + let temp = ((data[1] as u16) << 2) | (data[2] >> 6) as u16; + if is_negative { + let temp_sign_extended = temp | 0b1111_1100_0000_0000; + Ok(temp_sign_extended as i16 as f32 * 0.25) + } + else { + let temp = ((data[1] as u16) << 2) | (data[2] >> 6) as u16; + Ok(temp as f32 * 0.25) + } + } } diff --git a/src/lib.rs b/src/lib.rs index 8df20a2..940cd88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -287,6 +287,7 @@ impl Register { const CONTROL : u8 = 0x0E; const STATUS : u8 = 0x0F; const AGING_OFFSET : u8 = 0x10; + const TEMP_MSB : u8 = 0x11; } struct BitFlags; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index a0203a9..4f4a7e7 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -19,6 +19,7 @@ impl Register { pub const CONTROL : u8 = 0x0E; pub const STATUS : u8 = 0x0F; pub const AGING_OFFSET : u8 = 0x10; + pub const TEMP_MSB : u8 = 0x11; } pub struct BitFlags; diff --git a/tests/status.rs b/tests/status.rs index 16d86eb..879bf27 100644 --- a/tests/status.rs +++ b/tests/status.rs @@ -12,4 +12,8 @@ get_param_test!(is_busy, is_busy, STATUS, true, 0xFF); get_param_test!(is_not_busy, is_busy, STATUS, false, 0xFF & !BF::BUSY); get_param_test!(stopped, has_been_stopped, STATUS, true, 0xFF); -get_param_test!(not_stopped, has_been_stopped, STATUS, false, 0xFF & !BF::OSC_STOP); \ No newline at end of file +get_param_test!(not_stopped, has_been_stopped, STATUS, false, 0xFF & !BF::OSC_STOP); + +get_param_read_array_test!(temp_0, get_temperature, 0.0, TEMP_MSB, [0, 0], [0, 0]); +get_param_read_array_test!(temp_min, get_temperature, -128.0, TEMP_MSB, [0b1000_0000, 0], [0, 0]); +get_param_read_array_test!(temp_max, get_temperature, 127.75, TEMP_MSB, [0b0111_1111, 0b1100_0000], [0, 0]);