From 16985e41cf382e3fca77c9437ef725c4f020f031 Mon Sep 17 00:00:00 2001 From: David Beechey Date: Wed, 18 Dec 2024 12:25:13 +0000 Subject: [PATCH] add `#[must_use]` and `new_with_bounds` --- boards/stm32f767zi/src/tasks/temperature.rs | 8 +--- boards/stm32l476rg/src/tasks/temperature.rs | 8 +--- lib/sensors/src/lib.rs | 1 + lib/sensors/src/temperature.rs | 42 ++++++++++++--------- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/boards/stm32f767zi/src/tasks/temperature.rs b/boards/stm32f767zi/src/tasks/temperature.rs index e362b30..1fdc64d 100644 --- a/boards/stm32f767zi/src/tasks/temperature.rs +++ b/boards/stm32f767zi/src/tasks/temperature.rs @@ -22,12 +22,8 @@ pub async fn read_temp() -> ! { ))); let mut hyped_i2c = Stm32f767ziI2c::new(i2c); - let mut temperature_sensor = Temperature::new( - &mut hyped_i2c, - TemperatureAddresses::Address3f, - None, - ) - .expect( + let mut temperature_sensor = Temperature::new(&mut hyped_i2c, TemperatureAddresses::Address3f) + .expect( "Failed to create temperature sensor. Check the wiring and the I2C address of the sensor.", ); diff --git a/boards/stm32l476rg/src/tasks/temperature.rs b/boards/stm32l476rg/src/tasks/temperature.rs index dda2002..f821264 100644 --- a/boards/stm32l476rg/src/tasks/temperature.rs +++ b/boards/stm32l476rg/src/tasks/temperature.rs @@ -22,12 +22,8 @@ pub async fn read_temp() -> ! { ))); let mut hyped_i2c = Stm32l476rgI2c::new(i2c); - let mut temperature_sensor = Temperature::new( - &mut hyped_i2c, - TemperatureAddresses::Address3f, - None, - ) - .expect( + let mut temperature_sensor = Temperature::new(&mut hyped_i2c, TemperatureAddresses::Address3f) + .expect( "Failed to create temperature sensor. Check the wiring and the I2C address of the sensor.", ); diff --git a/lib/sensors/src/lib.rs b/lib/sensors/src/lib.rs index 9de3e24..38b578a 100644 --- a/lib/sensors/src/lib.rs +++ b/lib/sensors/src/lib.rs @@ -3,6 +3,7 @@ pub mod keyence; pub mod temperature; +#[must_use] #[derive(PartialEq, Debug)] pub enum SensorValueRange { /// This is the normal range of values for the sensor. diff --git a/lib/sensors/src/temperature.rs b/lib/sensors/src/temperature.rs index 6839f63..6791f6e 100644 --- a/lib/sensors/src/temperature.rs +++ b/lib/sensors/src/temperature.rs @@ -17,14 +17,26 @@ pub struct Temperature<'a, T: HypedI2c> { impl<'a, T: HypedI2c> Temperature<'a, T> { /// Create a new instance of the temperature sensor and attempt to configure it - /// - /// # Arguments - /// * `i2c` - The I2C peripheral to communicate with the temperature sensor - /// * `device_address` - The I2C address of the temperature sensor - /// * `calculate_bounds` - An optional function to override the default bounds calculation pub fn new( i2c: &'a mut T, device_address: TemperatureAddresses, + ) -> Result { + // Set up the temperature sensor by sending the configuration settings to the STTS22H_CTRL register + let device_address = device_address as u8; + match i2c.write_byte_to_register(device_address, STTS22H_CTRL, STTS22H_CONFIG_SETTINGS) { + Ok(_) => Ok(Self { + i2c, + device_address, + calculate_bounds: default_calculate_bounds, + }), + Err(e) => Err(TemperatureError::I2cError(e)), + } + } + + /// Create a new instance of the temperature sensor with the specified bounds and attempt to configure it + pub fn new_with_bounds( + i2c: &'a mut T, + device_address: TemperatureAddresses, calculate_bounds: Option SensorValueRange>, ) -> Result { // Set up the temperature sensor by sending the configuration settings to the STTS22H_CTRL register @@ -169,7 +181,7 @@ mod tests { fn test_write_config() { let i2c_values = Mutex::new(RefCell::new(FnvIndexMap::new())); let mut i2c = MockI2c::new(&i2c_values); - let _ = Temperature::new(&mut i2c, TemperatureAddresses::Address3f, None).unwrap(); + let _ = Temperature::new(&mut i2c, TemperatureAddresses::Address3f).unwrap(); let i2c_value = i2c .get_writes() .get(&(TemperatureAddresses::Address3f as u8, STTS22H_CTRL)) @@ -190,8 +202,7 @@ mod tests { ); let i2c_values = Mutex::new(RefCell::new(i2c_values)); let mut i2c = MockI2c::new(&i2c_values); - let mut temperature = - Temperature::new(&mut i2c, TemperatureAddresses::Address3f, None).unwrap(); + let mut temperature = Temperature::new(&mut i2c, TemperatureAddresses::Address3f).unwrap(); assert_eq!(temperature.read(), Some(SensorValueRange::Critical(0.0))); } @@ -208,8 +219,7 @@ mod tests { ); let i2c_values = Mutex::new(RefCell::new(i2c_values)); let mut i2c = MockI2c::new(&i2c_values); - let mut temperature = - Temperature::new(&mut i2c, TemperatureAddresses::Address3f, None).unwrap(); + let mut temperature = Temperature::new(&mut i2c, TemperatureAddresses::Address3f).unwrap(); assert_eq!(temperature.read(), Some(SensorValueRange::Safe(25.0))); } @@ -226,8 +236,7 @@ mod tests { ); let i2c_values = Mutex::new(RefCell::new(i2c_values)); let mut i2c = MockI2c::new(&i2c_values); - let mut temperature = - Temperature::new(&mut i2c, TemperatureAddresses::Address3f, None).unwrap(); + let mut temperature = Temperature::new(&mut i2c, TemperatureAddresses::Address3f).unwrap(); assert_eq!(temperature.read(), Some(SensorValueRange::Critical(-10.0))); } @@ -240,8 +249,7 @@ mod tests { ); let i2c_values = Mutex::new(RefCell::new(i2c_values)); let mut i2c = MockI2c::new(&i2c_values); - let mut temperature = - Temperature::new(&mut i2c, TemperatureAddresses::Address3f, None).unwrap(); + let mut temperature = Temperature::new(&mut i2c, TemperatureAddresses::Address3f).unwrap(); assert_eq!(temperature.check_status(), Status::Busy); } @@ -254,8 +262,7 @@ mod tests { ); let i2c_values = Mutex::new(RefCell::new(i2c_values)); let mut i2c: MockI2c<'_> = MockI2c::new(&i2c_values); - let mut temperature = - Temperature::new(&mut i2c, TemperatureAddresses::Address3f, None).unwrap(); + let mut temperature = Temperature::new(&mut i2c, TemperatureAddresses::Address3f).unwrap(); assert_eq!(temperature.check_status(), Status::TempOverUpperLimit); } @@ -268,8 +275,7 @@ mod tests { ); let i2c_values = Mutex::new(RefCell::new(i2c_values)); let mut i2c: MockI2c<'_> = MockI2c::new(&i2c_values); - let mut temperature = - Temperature::new(&mut i2c, TemperatureAddresses::Address3f, None).unwrap(); + let mut temperature = Temperature::new(&mut i2c, TemperatureAddresses::Address3f).unwrap(); assert_eq!(temperature.check_status(), Status::TempUnderLowerLimit); } }