Skip to content

Commit

Permalink
Fix all clippy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kshxtij committed Oct 14, 2024
1 parent 2b149c4 commit b443d32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
14 changes: 13 additions & 1 deletion lib/io/src/i2c.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
/// I2C trait used to abstract the I2C peripheral

pub enum I2cError {
// Error codes nased https://docs.embassy.dev/embassy-stm32/git/stm32g031c8/i2c/enum.Error.html
Bus,
Arbitration,
Nack,
Timeout,
Crc,
Overrun,
ZeroLengthTransfer,
Unknown,
}
pub trait HypedI2c {
fn read_byte(&mut self, device_address: u8, register_address: u8) -> Option<u8>;
fn write_byte_to_register(
&mut self,
device_address: u8,
register_address: u8,
data: u8,
) -> Result<(), ()>;
) -> Result<(), I2cError>;
}
17 changes: 9 additions & 8 deletions lib/sensors/src/temperature.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use hyped_io::i2c::HypedI2c;
use hyped_io::i2c::{HypedI2c, I2cError};

/// Temperature implements the logic to read the temperature from the STTS22H temperature sensor
/// using the I2C peripheral provided by the HypedI2c trait. The temperature sensor is configured
/// to continuous mode with a sampling rate of 200Hz. The temperature is read from the sensor and
/// converted to a floating point value in degrees Celsius.
/// using the I2C peripheral provided by the HypedI2c trait.
///
/// The temperature sensor is configured to continuous mode with a sampling rate of 200Hz.
/// The temperature is read from the sensor and converted to a floating point value in degrees Celsius.
///
/// Data sheet: https://www.st.com/resource/en/datasheet/stts22h.pdf
pub struct Temperature<T: HypedI2c> {
Expand All @@ -13,15 +14,15 @@ pub struct Temperature<T: HypedI2c> {

impl<T: HypedI2c> Temperature<T> {
/// Create a new instance of the temperature sensor and attempt to configure it
pub fn new(mut i2c: T, device_address: TemperatureAddresses) -> Result<Self, ()> {
pub fn new(mut i2c: T, device_address: TemperatureAddresses) -> Result<Self, I2cError> {
let device_address = device_address as u8;
// Set up the temperature sensor by sending the configuration settings to the STTS22H_CTRL register
match i2c.write_byte_to_register(device_address, STTS22H_CTRL, STTS22H_CONFIG_SETTINGS) {
Ok(_) => Ok(Self {
i2c,
device_address,
}),
Err(_) => Err(()),
Err(_) => Err(I2cError::Unknown),
}
}

Expand Down Expand Up @@ -50,10 +51,10 @@ impl<T: HypedI2c> Temperature<T> {

/// Check the status of the temperature sensor
pub fn check_status(&mut self) -> Status {
return match self.i2c.read_byte(self.device_address, STTS22H_STATUS) {
match self.i2c.read_byte(self.device_address, STTS22H_STATUS) {
Some(byte) => Status::from_byte(byte),
None => Status::Unknown,
};
}
}
}

Expand Down

0 comments on commit b443d32

Please sign in to comment.