Skip to content

Commit

Permalink
split i2c_read into read and write_read (#8)
Browse files Browse the repository at this point in the history
* add i2c_read

* bump version to 0.3.1
  • Loading branch information
teamplayer3 authored Jun 23, 2024
1 parent c107082 commit 4687808
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub type Result<T> = core::result::Result<T, SHTError>;
#[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"))]
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ where
pub fn status(&mut self) -> Result<Status> {
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::<u8>::new(&CRC_ALGORITHM), buffer[0], buffer[1]);
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/mode/periodic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
fn read(&mut self) -> Result<Reading> {
let mut buffer = [0; 6];

self.i2c_read(&[0xE0, 0x00], &mut buffer)?;
self.i2c_write_read(&[0xE0, 0x00], &mut buffer)?;
self.process_data(buffer)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mode/single_shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) fn single_shot_read<Mode, I2C: I2c>(sensor: &mut SHT31<Mode, I2C>) ->
// 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)
}

Expand Down

0 comments on commit 4687808

Please sign in to comment.