Add function to read the temperature

pull/4/head
Diego Barrios Romero 2018-10-29 18:36:15 +01:00
parent 03b7012ec3
commit b01c258e60
4 changed files with 23 additions and 1 deletions

View File

@ -31,4 +31,20 @@ where
} }
Ok(()) 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)
}
}
} }

View File

@ -287,6 +287,7 @@ impl Register {
const CONTROL : u8 = 0x0E; const CONTROL : u8 = 0x0E;
const STATUS : u8 = 0x0F; const STATUS : u8 = 0x0F;
const AGING_OFFSET : u8 = 0x10; const AGING_OFFSET : u8 = 0x10;
const TEMP_MSB : u8 = 0x11;
} }
struct BitFlags; struct BitFlags;

View File

@ -19,6 +19,7 @@ impl Register {
pub const CONTROL : u8 = 0x0E; pub const CONTROL : u8 = 0x0E;
pub const STATUS : u8 = 0x0F; pub const STATUS : u8 = 0x0F;
pub const AGING_OFFSET : u8 = 0x10; pub const AGING_OFFSET : u8 = 0x10;
pub const TEMP_MSB : u8 = 0x11;
} }
pub struct BitFlags; pub struct BitFlags;

View File

@ -13,3 +13,7 @@ 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!(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]);