Skip to content

Commit

Permalink
Update to embedded-hal 1.0 (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Timo <[email protected]>
  • Loading branch information
t-moe and Timo authored Feb 12, 2024
1 parent 701cab6 commit d3cc34f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ categories = ["embedded"]
thiserror = ["dep:thiserror"]

[dependencies]
embedded-hal = "0.2.7"
embedded-hal = "1.0.0"

crc = "3.0.0"
thiserror = {version= "1.0.38", optional = true}
thiserror = { version = "1.0.38", optional = true }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ the current thread until the sensor does a reading.
use sht31::prelude::*;

fn main() -> Result<()> {
// Requires an i2c connection + delay (both from embedded_hal::blocking)
// Requires an i2c connection + delay (both from embedded_hal 1.x.x)
let sht = SHT31::new(i2c, delay);

loop {
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod mode;

use crate::mode::SimpleSingleShot;
use crc::{Algorithm, Crc};
use embedded_hal::blocking::{delay::DelayMs, i2c};
use embedded_hal::{delay::DelayNs, i2c::I2c};

pub use crate::error::{Result, SHTError};
pub mod prelude {
Expand Down Expand Up @@ -168,8 +168,8 @@ impl<Mode, I2C> SHT31<Mode, I2C> {
#[allow(dead_code)]
impl<I2C, D> SHT31<SimpleSingleShot<D>, I2C>
where
I2C: i2c::WriteRead + i2c::Write,
D: DelayMs<u32>,
I2C: I2c,
D: DelayNs,
{
/// Create a new sensor
/// I2C clock frequency must must be between 0 and 1000 kHz
Expand All @@ -188,7 +188,7 @@ where
#[allow(dead_code)]
impl<Mode, I2C> SHT31<Mode, I2C>
where
I2C: i2c::WriteRead + i2c::Write,
I2C: I2c,
{
/// Changes the SHT31 mode
pub fn with_mode<NewMode>(self, mode: NewMode) -> SHT31<NewMode, I2C> {
Expand Down
6 changes: 3 additions & 3 deletions src/mode/periodic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
mode::{Sht31Measure, Sht31Reader},
Accuracy, Reading, SHT31,
};
use embedded_hal::blocking::i2c;
use embedded_hal::i2c::I2c;

/// Periodic reading where reading returns the last available data
#[derive(Default, Copy, Clone, Debug)]
Expand Down Expand Up @@ -55,7 +55,7 @@ impl Periodic {

impl<I2C> Sht31Reader for SHT31<Periodic, I2C>
where
I2C: i2c::WriteRead + i2c::Write,
I2C: I2c,
{
fn read(&mut self) -> Result<Reading> {
let mut buffer = [0; 6];
Expand All @@ -67,7 +67,7 @@ where

impl<I2C> Sht31Measure for SHT31<Periodic, I2C>
where
I2C: i2c::WriteRead + i2c::Write,
I2C: I2c,
{
/// Initialized the periodic measuring mode,
/// a break command must be run in order to change
Expand Down
10 changes: 5 additions & 5 deletions src/mode/simple_single_shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use crate::{
mode::{single_shot::single_shot_read, Sht31Reader},
Accuracy, Reading, SHT31,
};
use embedded_hal::blocking::{delay::DelayMs, i2c};
use embedded_hal::{delay::DelayNs, i2c::I2c};

/// A simple reading that blocks until the measurement is obtained
#[derive(Copy, Clone, Debug)]
pub struct SimpleSingleShot<D: DelayMs<u32>> {
pub struct SimpleSingleShot<D: DelayNs> {
max_retries: u8,
ms_delay: u32,
delay: D,
}

impl<D> SimpleSingleShot<D>
where
D: DelayMs<u32>,
D: DelayNs,
{
#[allow(dead_code)]
pub fn new(delay: D) -> Self {
Expand Down Expand Up @@ -47,8 +47,8 @@ where

impl<I2C, D> Sht31Reader for SHT31<SimpleSingleShot<D>, I2C>
where
I2C: i2c::WriteRead + i2c::Write,
D: DelayMs<u32>,
I2C: I2c,
D: DelayNs,
{
/// It will initiate a read and wont stop until its either exhausted its retries or a reading is found
fn read(&mut self) -> Result<Reading> {
Expand Down
10 changes: 4 additions & 6 deletions src/mode/single_shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
mode::{Sht31Measure, Sht31Reader},
Accuracy, Reading, SHT31,
};
use embedded_hal::blocking::i2c;
use embedded_hal::i2c::I2c;

/// Complex read that may require multiple attempts to read output until its ready
#[derive(Default, Copy, Clone, Debug)]
Expand All @@ -16,9 +16,7 @@ impl SingleShot {
}
}

pub(crate) fn single_shot_read<Mode, I2C: i2c::WriteRead + i2c::Write>(
sensor: &mut SHT31<Mode, I2C>,
) -> Result<Reading> {
pub(crate) fn single_shot_read<Mode, I2C: I2c>(sensor: &mut SHT31<Mode, I2C>) -> Result<Reading> {
// TODO: If error is a NACK then return another unique error to identify
let mut buffer = [0; 6];

Expand All @@ -28,7 +26,7 @@ pub(crate) fn single_shot_read<Mode, I2C: i2c::WriteRead + i2c::Write>(

impl<I2C> Sht31Reader for SHT31<SingleShot, I2C>
where
I2C: i2c::WriteRead + i2c::Write,
I2C: I2c,
{
/// Try reading, if the reading is not available yet then it will return an error
fn read(&mut self) -> Result<Reading> {
Expand All @@ -38,7 +36,7 @@ where

impl<I2C> Sht31Measure for SHT31<SingleShot, I2C>
where
I2C: i2c::WriteRead + i2c::Write,
I2C: I2c,
{
/// Commence measuring
#[allow(dead_code)]
Expand Down

0 comments on commit d3cc34f

Please sign in to comment.