Skip to content

Commit

Permalink
add #[must_use] and new_with_bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbeechey committed Dec 18, 2024
1 parent a3624e9 commit 16985e4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
8 changes: 2 additions & 6 deletions boards/stm32f767zi/src/tasks/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
);

Expand Down
8 changes: 2 additions & 6 deletions boards/stm32l476rg/src/tasks/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
);

Expand Down
1 change: 1 addition & 0 deletions lib/sensors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod keyence;
pub mod temperature;

#[must_use]
#[derive(PartialEq, Debug)]
pub enum SensorValueRange<T: PartialEq> {
/// This is the normal range of values for the sensor.
Expand Down
42 changes: 24 additions & 18 deletions lib/sensors/src/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, TemperatureError> {
// 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<fn(f32) -> SensorValueRange<f32>>,
) -> Result<Self, TemperatureError> {
// Set up the temperature sensor by sending the configuration settings to the STTS22H_CTRL register
Expand Down Expand Up @@ -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))
Expand All @@ -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)));
}

Expand All @@ -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)));
}

Expand All @@ -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)));
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}

0 comments on commit 16985e4

Please sign in to comment.