Skip to content

Commit

Permalink
Merge pull request #9 from t-moe/main
Browse files Browse the repository at this point in the history
Update to embedded-hal 1.0
  • Loading branch information
hauju committed Feb 18, 2024
2 parents b06c693 + 41ba06c commit b100bef
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "scd4x"
version = "0.2.3"
version = "0.3.0"
authors = ["Hauke Jung <hauke.jung@outlook.de>"]
documentation = "https://docs.rs/scd4x"
repository = "https://github.com/hauju/scd4x-rs.git"
Expand All @@ -18,22 +18,22 @@ util = [ "structopt", "simplelog", "humantime", "linux-embedded-hal", "anyhow" ]
default = []

[dependencies]
sensirion-i2c = "0.1"
embedded-hal = "0.2"
sensirion-i2c = "0.3.0"
embedded-hal = "1.0.0"

log = { version = "0.4.16", default_features = false }
defmt = { version = "0.3.0", optional = true }
anyhow = { version = "1.0.56", optional=true, default_features = false }
anyhow = { version = "1.0.56", optional = true, default_features = false }

structopt = { version = "0.3.26", optional = true }
linux-embedded-hal = { version = "0.3.2", optional = true }
linux-embedded-hal = { version = "0.4.0", optional = true }
simplelog = { version = "0.12.0", optional = true }
humantime = { version = "2.1.0", optional = true }


[dev-dependencies]
linux-embedded-hal = "0.3"
embedded-hal-mock = "0.8"
linux-embedded-hal = "0.4.0"
embedded-hal-mock = "0.10.0"

[[bin]]
name = "scd4x-util"
Expand Down
43 changes: 23 additions & 20 deletions bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
use embedded_hal::blocking::delay::DelayMs;
use hal::{Delay, I2cdev, i2cdev::linux::LinuxI2CError};
use embedded_hal::delay::DelayNs;
use hal::{Delay, I2CError, I2cdev};
use linux_embedded_hal as hal;

use log::{debug, info, error};
use log::{debug, error, info};

use structopt::StructOpt;
use humantime::Duration as HumanDuration;
use simplelog::{TermLogger, LevelFilter};

use scd4x::{Scd4x, Error};
use simplelog::{LevelFilter, TermLogger};
use structopt::StructOpt;

use scd4x::{Error, Scd4x};

#[derive(StructOpt)]
#[structopt(name = "scd4x-util")]
/// A Command Line Interface (CLI) for interacting with a sensiron SCD4x environmental sensor via Linux I2C
pub struct Options {

/// Specify the i2c interface to use to connect to the scd30 device
#[structopt(short="d", long = "i2c", default_value = "/dev/i2c-1", env = "SCD4x_I2C")]
#[structopt(
short = "d",
long = "i2c",
default_value = "/dev/i2c-1",
env = "SCD4x_I2C"
)]
i2c: String,

/// Delay between sensor poll operations
#[structopt(long = "poll-delay", default_value="100ms")]
#[structopt(long = "poll-delay", default_value = "100ms")]
pub poll_delay: HumanDuration,

/// Number of allowed I2C errors (per measurement attempt) prior to exiting
#[structopt(long = "allowed-errors", default_value="3")]
#[structopt(long = "allowed-errors", default_value = "3")]
pub allowed_errors: usize,

/// Enable verbose logging
#[structopt(long = "log-level", default_value = "info")]
level: LevelFilter,
}


fn main() -> Result<(), Error<LinuxI2CError>> {

fn main() -> Result<(), Error<I2CError>> {
// Load options
let opts = Options::from_args();

// Setup logging
TermLogger::init(opts.level,
TermLogger::init(
opts.level,
simplelog::Config::default(),
simplelog::TerminalMode::Mixed,
simplelog::ColorChoice::Auto).unwrap();
simplelog::ColorChoice::Auto,
)
.unwrap();

debug!("Connecting to I2C device");
let i2c = match I2cdev::new(&opts.i2c) {
Expand All @@ -57,7 +61,6 @@ fn main() -> Result<(), Error<LinuxI2CError>> {
debug!("Connecting to sensor");
let mut sensor = Scd4x::new(i2c, Delay);


debug!("Initalising sensor");

#[cfg(feature = "scd41")]
Expand All @@ -74,7 +77,7 @@ fn main() -> Result<(), Error<LinuxI2CError>> {
debug!("Waiting for first measurement... (5 sec)");

loop {
hal::Delay.delay_ms(5000u16);
hal::Delay.delay_ms(5000u32);

debug!("Waiting for data ready");
loop {
Expand All @@ -84,7 +87,7 @@ fn main() -> Result<(), Error<LinuxI2CError>> {
Err(e) => {
error!("Failed to poll for data ready: {:?}", e);
std::process::exit(-2);
},
}
}
}

Expand All @@ -94,7 +97,7 @@ fn main() -> Result<(), Error<LinuxI2CError>> {
Err(e) => {
error!("Failed to read measurement: {:?}", e);
std::process::exit(-3);
},
}
};

println!(
Expand Down
4 changes: 2 additions & 2 deletions examples/linux.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::delay::DelayNs;
use hal::{Delay, I2cdev};
use linux_embedded_hal as hal;

Expand All @@ -19,7 +19,7 @@ fn main() {
sensor.start_periodic_measurement().unwrap();
println!("Waiting for first measurement... (5 sec)");
loop {
hal::Delay.delay_ms(5000u16);
hal::Delay.delay_ms(5000u32);

let data = sensor.measurement().unwrap();

Expand Down
11 changes: 5 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use embedded_hal as hal;
use hal::blocking::i2c::{Read, Write};
use hal::i2c::I2c;
use sensirion_i2c::i2c;

/// SCD4X errors
Expand All @@ -20,15 +20,14 @@ pub enum Error<E> {
NotAllowed,
#[cfg_attr(feature = "thiserror", error("Internal"))]
/// Internal fail
Internal
Internal,
}

impl<E, I2cWrite, I2cRead> From<i2c::Error<I2cWrite, I2cRead>> for Error<E>
impl<E, I2C> From<i2c::Error<I2C>> for Error<E>
where
I2cWrite: Write<Error = E>,
I2cRead: Read<Error = E>,
I2C: I2c<Error = E>,
{
fn from(err: i2c::Error<I2cWrite, I2cRead>) -> Self {
fn from(err: i2c::Error<I2C>) -> Self {
match err {
i2c::Error::Crc => Error::Crc,
i2c::Error::I2cWrite(e) => Error::I2c(e),
Expand Down
24 changes: 17 additions & 7 deletions src/scd4x.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use embedded_hal as hal;
use hal::blocking::delay::DelayMs;
use hal::blocking::i2c::{Read, Write, WriteRead};
use hal::delay::DelayNs;
use hal::i2c::I2c;

use crate::commands::Command;
use crate::error::Error;
Expand All @@ -19,8 +19,8 @@ pub struct Scd4x<I2C, D> {

impl<I2C, D, E> Scd4x<I2C, D>
where
I2C: Read<Error = E> + Write<Error = E> + WriteRead<Error = E>,
D: DelayMs<u32>,
I2C: I2c<Error = E>,
D: DelayNs,
{
pub fn new(i2c: I2C, delay: D) -> Self {
Scd4x {
Expand All @@ -30,6 +30,10 @@ where
}
}

pub fn destroy(self) -> I2C {
self.i2c
}

/// Start periodic measurement, signal update interval is 5 seconds.
/// This command is only available in idle mode.
pub fn start_periodic_measurement(&mut self) -> Result<(), Error<E>> {
Expand Down Expand Up @@ -264,7 +268,7 @@ where
if !allowed_if_running && self.is_running {
return Err(Error::NotAllowed);
}
i2c::write_command(&mut self.i2c, SCD4X_I2C_ADDRESS, command).map_err(Error::I2c)?;
i2c::write_command_u16(&mut self.i2c, SCD4X_I2C_ADDRESS, command).map_err(Error::I2c)?;
self.delay.delay_ms(delay);
Ok(())
}
Expand Down Expand Up @@ -293,9 +297,9 @@ where

#[cfg(test)]
mod tests {
use embedded_hal_mock as hal;
use embedded_hal_mock::eh1 as hal;

use self::hal::delay::MockNoop as DelayMock;
use self::hal::delay::NoopDelay as DelayMock;
use self::hal::i2c::{Mock as I2cMock, Transaction};
use super::*;

Expand All @@ -317,6 +321,9 @@ mod tests {
let serial = sensor.serial_number().unwrap();
// Assert
assert_eq!(serial, 0xbeefbeefbeef);

let mut mock = sensor.destroy();
mock.done();
}

/// Test the measurement function
Expand All @@ -339,5 +346,8 @@ mod tests {
assert_eq!(data.co2, 1000_u16);
assert_eq!(data.temperature, 22.000198_f32);
assert_eq!(data.humidity, 50_f32);

let mut mock = sensor.destroy();
mock.done();
}
}

0 comments on commit b100bef

Please sign in to comment.