Skip to content

Commit

Permalink
Update for embedded-hal 1.0.0-rc.1
Browse files Browse the repository at this point in the history
Also moves the linux-embedded-hal example out of the readme and into a
standalone file in examples/.
  • Loading branch information
kelnos committed Nov 5, 2023
1 parent dbf5b36 commit 86d1797
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 56 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sen0177"
description = "Read air quality data from the SEN0177 and PMSA003I sensors"
version = "0.4.0-alpha.1"
version = "0.5.0"
authors = ["Brian J. Tarricone <[email protected]>"]
homepage = "https://github.com/kelnos/sen0177-rs"
repository = "https://github.com/kelnos/sen0177-rs"
Expand All @@ -20,9 +20,10 @@ default = []
std = []

[dependencies]
embedded-hal = "=1.0.0-alpha.8"
embedded-hal = "=1.0.0-rc.1"
embedded-hal-nb = "=1.0.0-rc.1"

[dev-dependencies]
anyhow = "1"
linux-embedded-hal = "0.3"
linux-embedded-hal = { git = "https://github.com/rust-embedded/linux-embedded-hal", rev = "27daf71e8ee22ff2b33fca5972a47dd139de69e2" }
serial = "0.4"
56 changes: 6 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,67 +22,23 @@ Include the following in your `Cargo.toml` file:

```toml
[dependencies]
sen0177 = "0.4"
sen0177 = "0.5"
```

If you are in a `no_std` environment, you may depend on this crate like so:

```toml
[dependencies]
sen0177 = { version = "0.4", default-features = false }
sen0177 = { version = "0.5", default-features = false }
```

## Usage

This example shows how to use the sensor when connected to a Linux-
based serial device.
See the `examples/` directory.

Note that this example currently does not work becuase this crate is
tracking the current embedded-hal 1.0.0 alpha, but linux-embedded-hal
is a little behind at the time of writing. If you want to use my patched
version, add the following to your `Cargo.toml`:

```toml
[patch.crates-io]
linux-embedded-hal = { git = "https://github.com/kelnos/linux-embedded-hal.git", branch = "embedded-hal-1.0.0-alpha.8" }
```

```rust,no_run,ignore
use linux_embedded_hal::Serial;
use sen0177::{serial::Sen0177, Reading};
use serial::{core::prelude::*, BaudRate, CharSize, FlowControl, Parity, StopBits};
use std::{io, path::Path, time::Duration};
const SERIAL_PORT: &str = "/dev/ttyS0";
const BAUD_RATE: BaudRate = BaudRate::Baud9600;
const CHAR_SIZE: CharSize = CharSize::Bits8;
const PARITY: Parity = Parity::ParityNone;
const STOP_BITS: StopBits = StopBits::Stop1;
const FLOW_CONTROL: FlowControl = FlowControl::FlowNone;
pub fn main() -> anyhow::Result<()> {
let mut serial = Serial::open(&Path::new(SERIAL_PORT))?;
serial.0.set_timeout(Duration::from_millis(1500))?;
serial.0.reconfigure(&|settings| {
settings.set_char_size(CHAR_SIZE);
settings.set_parity(PARITY);
settings.set_stop_bits(STOP_BITS);
settings.set_flow_control(FLOW_CONTROL);
settings.set_baud_rate(BAUD_RATE)
})?;
let mut sensor = Sen0177::new(serial);
loop {
match sensor.read() {
Ok(reading) => {
println!("PM1: {}µg/m³, PM2.5: {}µg/m³, PM10: {}µg/m³",
reading.pm1(), reading.pm2_5(), reading.pm10());
},
Err(err) => eprintln!("Error: {:?}", err),
}
}
}
```
Note that `linux-embedded-hal` does not currently have a release
supporting the 1.0.0 release candidates of `embedded-hal`, so the
Linux example has to pull `linux-embedded-hal` from GitHub.

Note that the serial device occasionally returns bad data. If you
receive [`SensorError::BadMagic`] or [`SensorError::ChecksumMismatch`]
Expand Down
38 changes: 38 additions & 0 deletions examples/linux-serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use linux_embedded_hal::{
serialport::{self, DataBits, FlowControl, Parity, StopBits},
Serial,
};
use sen0177::{serial::Sen0177, AirQualitySensor};
use std::time::Duration;

const SERIAL_PORT: &str = "/dev/ttyS0";
const BAUD_RATE: u32 = 9600;
const DATA_BITS: DataBits = DataBits::Eight;
const PARITY: Parity = Parity::None;
const STOP_BITS: StopBits = StopBits::One;
const FLOW_CONTROL: FlowControl = FlowControl::None;

pub fn main() -> anyhow::Result<()> {
let builder = serialport::new(SERIAL_PORT, BAUD_RATE)
.data_bits(DATA_BITS)
.flow_control(FLOW_CONTROL)
.parity(PARITY)
.stop_bits(STOP_BITS)
.timeout(Duration::from_millis(1500));
let serial = Serial::open_from_builder(builder)?;
let mut sensor = Sen0177::new(serial);

loop {
match sensor.read() {
Ok(reading) => {
println!(
"PM1: {}µg/m³, PM2.5: {}µg/m³, PM10: {}µg/m³",
reading.pm1(),
reading.pm2_5(),
reading.pm10()
);
}
Err(err) => eprintln!("Error: {:?}", err),
}
}
}
2 changes: 1 addition & 1 deletion src/i2c.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{read::*, AirQualitySensor, Reading, SensorError};
use embedded_hal::i2c::{blocking::I2c, AddressMode, Error as I2cError};
use embedded_hal::i2c::{AddressMode, Error as I2cError, I2c};

/// A SEN0177 device connected via I2C
pub struct Sen0177<A, I2C, E>
Expand Down
4 changes: 2 additions & 2 deletions src/serial.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{read::*, AirQualitySensor, Reading, SensorError};
use embedded_hal::{
use embedded_hal_nb::{
nb::block,
serial::{nb::Read, Error as SerialError},
serial::{Error as SerialError, Read},
};

/// A SEN0177 device connected via serial UART
Expand Down

0 comments on commit 86d1797

Please sign in to comment.