From 0f8d98b755fe27a658af765c9d74e0b046bb7616 Mon Sep 17 00:00:00 2001 From: teamplayer3 Date: Sat, 9 Mar 2024 10:47:05 +0100 Subject: [PATCH 1/2] add i2c_read --- src/error.rs | 2 ++ src/lib.rs | 11 +++++++++-- src/mode/periodic.rs | 2 +- src/mode/single_shot.rs | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index ef1814f..431911e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,6 +2,8 @@ pub type Result = core::result::Result; #[cfg_attr(feature = "thiserror", derive(thiserror::Error))] #[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)] pub enum SHTError { + #[cfg_attr(feature = "thiserror", error("Read I2C Error"))] + ReadI2CError, #[cfg_attr(feature = "thiserror", error("Write Read I2C Error"))] WriteReadI2CError, #[cfg_attr(feature = "thiserror", error("Write I2C Error"))] diff --git a/src/lib.rs b/src/lib.rs index 1c89dee..bf0b261 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -275,7 +275,7 @@ where pub fn status(&mut self) -> Result { let mut buffer = [0; 3]; - self.i2c_read(&[0xF3, 0x2D], &mut buffer)?; + self.i2c_write_read(&[0xF3, 0x2D], &mut buffer)?; // Verify data let calculated = calculate_checksum(&Crc::::new(&CRC_ALGORITHM), buffer[0], buffer[1]); @@ -308,7 +308,14 @@ where } } - fn i2c_read(&mut self, bytes: &[u8], buffer: &mut [u8]) -> Result<()> { + fn i2c_read(&mut self, buffer: &mut [u8]) -> Result<()> { + match self.i2c.read(self.address, buffer) { + Ok(res) => Ok(res), + Err(_) => Err(SHTError::ReadI2CError), + } + } + + fn i2c_write_read(&mut self, bytes: &[u8], buffer: &mut [u8]) -> Result<()> { match self.i2c.write_read(self.address, bytes, buffer) { Ok(res) => Ok(res), Err(_) => Err(SHTError::WriteReadI2CError), diff --git a/src/mode/periodic.rs b/src/mode/periodic.rs index 5ba7bb0..8efd742 100644 --- a/src/mode/periodic.rs +++ b/src/mode/periodic.rs @@ -60,7 +60,7 @@ where fn read(&mut self) -> Result { let mut buffer = [0; 6]; - self.i2c_read(&[0xE0, 0x00], &mut buffer)?; + self.i2c_write_read(&[0xE0, 0x00], &mut buffer)?; self.process_data(buffer) } } diff --git a/src/mode/single_shot.rs b/src/mode/single_shot.rs index 5984770..4738147 100644 --- a/src/mode/single_shot.rs +++ b/src/mode/single_shot.rs @@ -20,7 +20,7 @@ pub(crate) fn single_shot_read(sensor: &mut SHT31) -> // TODO: If error is a NACK then return another unique error to identify let mut buffer = [0; 6]; - sensor.i2c_read(&[], &mut buffer)?; + sensor.i2c_read(&mut buffer)?; sensor.process_data(buffer) } From d86dbc0ebd6a86a399681ad756a05863666b8995 Mon Sep 17 00:00:00 2001 From: teamplayer3 Date: Sat, 9 Mar 2024 10:52:18 +0100 Subject: [PATCH 2/2] bump version to 0.3.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 997c0a7..a84c031 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "sht31" description = "A library for the SHT31 temperature and humidity sensor" -version = "0.3.0" +version = "0.3.1" edition = "2021" license = "MIT" repository = "https://github.com/FloppyDisck/SHT31-rs"