mirror of https://github.com/eldruin/ds323x-rs
Add function to read the temperature
parent
03b7012ec3
commit
b01c258e60
|
@ -31,4 +31,20 @@ where
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Read the temperature.
|
||||
pub fn get_temperature(&mut self) -> Result<f32, Error<E>> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
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]);
|
||||
|
|
Loading…
Reference in New Issue