Skip to content

Commit

Permalink
Merge embedded-hal-1 support from 'madomo/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sirhcel committed Oct 12, 2024
2 parents 2255861 + 3869047 commit 864df53
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 3 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -12,12 +12,8 @@ pub enum Error<E> {
Crc,
}

impl<E, W, R> From<i2c::Error<W, R>> for Error<E>
where
W: Write<Error = E>,
R: Read<Error = E>,
{
fn from(err: i2c::Error<W, R>) -> Self {
impl<I: I2c> From<i2c::Error<I>> for Error<I::Error> {
fn from(err: i2c::Error<I>) -> Self {
match err {
i2c::Error::Crc => Error::Crc,
i2c::Error::I2cRead(e) => Error::I2c(e),
Expand Down
27 changes: 12 additions & 15 deletions src/sht4x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,10 +44,9 @@ impl From<Precision> for Command {
}
}

impl<I, D, E> Sht4x<I, D>
impl<I: I2c, D> Sht4x<I, D>
where
I: Read<Error = E> + Write<Error = E> + WriteRead<Error = E>,
D: DelayMs<u16>,
D: DelayNs,
{
/// Creates a new driver instance using the given I2C bus. It configures the default I2C
/// address 0x44 used by most family members.
Expand Down Expand Up @@ -89,7 +86,7 @@ where
power: HeatingPower,
duration: HeatingDuration,
delay: &mut D,
) -> Result<Measurement, Error<E>> {
) -> Result<Measurement, Error<I::Error>> {
let raw = self.heat_and_measure_raw(power, duration, delay)?;

Ok(Measurement::from(raw))
Expand All @@ -106,7 +103,7 @@ where
power: HeatingPower,
duration: HeatingDuration,
delay: &mut D,
) -> Result<SensorData, Error<E>> {
) -> Result<SensorData, Error<I::Error>> {
let command = Command::from((power, duration));

self.write_command_and_delay_for_execution(command, delay)?;
Expand All @@ -121,7 +118,7 @@ where
&mut self,
precision: Precision,
delay: &mut D,
) -> Result<Measurement, Error<E>> {
) -> Result<Measurement, Error<I::Error>> {
let raw = self.measure_raw(precision, delay)?;
Ok(Measurement::from(raw))
}
Expand All @@ -131,7 +128,7 @@ where
&mut self,
precision: Precision,
delay: &mut D,
) -> Result<SensorData, Error<E>> {
) -> Result<SensorData, Error<I::Error>> {
let command = Command::from(precision);

self.write_command_and_delay_for_execution(command, delay)?;
Expand All @@ -142,7 +139,7 @@ where
}

/// Reads the sensor's serial number.
pub fn serial_number(&mut self, delay: &mut D) -> Result<u32, Error<E>> {
pub fn serial_number(&mut self, delay: &mut D) -> Result<u32, Error<I::Error>> {
self.write_command_and_delay_for_execution(Command::SerialNumber, delay)?;
let response = self.read_response()?;

Expand All @@ -155,11 +152,11 @@ where
}

/// Performs a soft reset of the sensor.
pub fn soft_reset(&mut self, delay: &mut D) -> Result<(), Error<E>> {
pub fn soft_reset(&mut self, delay: &mut D) -> Result<(), Error<I::Error>> {
self.write_command_and_delay_for_execution(Command::SoftReset, delay)
}

fn read_response(&mut self) -> Result<[u8; RESPONSE_LEN], Error<E>> {
fn read_response(&mut self) -> Result<[u8; RESPONSE_LEN], Error<I::Error>> {
let mut response = [0; RESPONSE_LEN];

i2c::read_words_with_crc(&mut self.i2c, self.address.into(), &mut response)?;
Expand All @@ -178,7 +175,7 @@ where
&mut self,
command: Command,
delay: &mut D,
) -> Result<(), Error<E>> {
) -> Result<(), Error<I::Error>> {
let code = command.code();

i2c::write_command_u8(&mut self.i2c, self.address.into(), code).map_err(Error::I2c)?;
Expand Down

0 comments on commit 864df53

Please sign in to comment.