From 3869047bf1f147fd803e95bc8bdd5eda4535a9f9 Mon Sep 17 00:00:00 2001 From: Moritz Bitsch Date: Mon, 19 Feb 2024 21:12:04 +0100 Subject: [PATCH] Update for embedded-hal 1.0 --- Cargo.toml | 4 ++-- src/commands.rs | 2 +- src/error.rs | 10 +++------- src/sht4x.rs | 27 ++++++++++++--------------- 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fb84d20..a765f2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,9 +23,9 @@ exclude = [ [dependencies] defmt = { version = "0.3.2", optional = true } -embedded-hal = "0.2.7" +embedded-hal = "1.0.0" fixed = "1.20.0" -sensirion-i2c = "0.2" +sensirion-i2c = "0.3.0" [features] defmt = ["dep:defmt"] diff --git a/src/commands.rs b/src/commands.rs index 132e12f..3d78617 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -31,7 +31,7 @@ impl Command { } } - pub(crate) fn duration_ms(&self) -> u16 { + pub(crate) fn duration_ms(&self) -> u32 { // Values rounded up from the maximum durations given in the datasheet // table 4, 'System timing specifications'. match self { diff --git a/src/error.rs b/src/error.rs index c4af875..545b03e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use embedded_hal::blocking::i2c::{Read, Write}; +use embedded_hal::i2c::I2c; use sensirion_i2c::i2c; /// Error conditions from accessing SHT4x sensors. @@ -12,12 +12,8 @@ pub enum Error { Crc, } -impl From> for Error -where - W: Write, - R: Read, -{ - fn from(err: i2c::Error) -> Self { +impl From> for Error { + fn from(err: i2c::Error) -> Self { match err { i2c::Error::Crc => Error::Crc, i2c::Error::I2cRead(e) => Error::I2c(e), diff --git a/src/sht4x.rs b/src/sht4x.rs index a49bf7d..a2e7f67 100644 --- a/src/sht4x.rs +++ b/src/sht4x.rs @@ -4,10 +4,8 @@ use crate::{ types::{Address, HeatingDuration, HeatingPower, Measurement, Precision, SensorData}, }; use core::marker::PhantomData; -use embedded_hal::blocking::{ - delay::DelayMs, - i2c::{Read, Write, WriteRead}, -}; +use embedded_hal::{delay::DelayNs, i2c::I2c}; + use sensirion_i2c::i2c; const RESPONSE_LEN: usize = 6; @@ -46,10 +44,9 @@ impl From for Command { } } -impl Sht4x +impl Sht4x where - I: Read + Write + WriteRead, - D: DelayMs, + D: DelayNs, { /// Creates a new driver instance using the given I2C bus. It configures the default I2C /// address 0x44 used by most family members. @@ -89,7 +86,7 @@ where power: HeatingPower, duration: HeatingDuration, delay: &mut D, - ) -> Result> { + ) -> Result> { let raw = self.heat_and_measure_raw(power, duration, delay)?; Ok(Measurement::from(raw)) @@ -106,7 +103,7 @@ where power: HeatingPower, duration: HeatingDuration, delay: &mut D, - ) -> Result> { + ) -> Result> { let command = Command::from((power, duration)); self.write_command_and_delay_for_execution(command, delay)?; @@ -121,7 +118,7 @@ where &mut self, precision: Precision, delay: &mut D, - ) -> Result> { + ) -> Result> { let raw = self.measure_raw(precision, delay)?; Ok(Measurement::from(raw)) } @@ -131,7 +128,7 @@ where &mut self, precision: Precision, delay: &mut D, - ) -> Result> { + ) -> Result> { let command = Command::from(precision); self.write_command_and_delay_for_execution(command, delay)?; @@ -142,7 +139,7 @@ where } /// Reads the sensor's serial number. - pub fn serial_number(&mut self, delay: &mut D) -> Result> { + pub fn serial_number(&mut self, delay: &mut D) -> Result> { self.write_command_and_delay_for_execution(Command::SerialNumber, delay)?; let response = self.read_response()?; @@ -155,11 +152,11 @@ where } /// Performs a soft reset of the sensor. - pub fn soft_reset(&mut self, delay: &mut D) -> Result<(), Error> { + pub fn soft_reset(&mut self, delay: &mut D) -> Result<(), Error> { self.write_command_and_delay_for_execution(Command::SoftReset, delay) } - fn read_response(&mut self) -> Result<[u8; RESPONSE_LEN], Error> { + fn read_response(&mut self) -> Result<[u8; RESPONSE_LEN], Error> { let mut response = [0; RESPONSE_LEN]; i2c::read_words_with_crc(&mut self.i2c, self.address.into(), &mut response)?; @@ -178,7 +175,7 @@ where &mut self, command: Command, delay: &mut D, - ) -> Result<(), Error> { + ) -> Result<(), Error> { let code = command.code(); i2c::write_command_u8(&mut self.i2c, self.address.into(), code).map_err(Error::I2c)?;