Skip to content

Commit

Permalink
expanded with more tests and slightly improved internal code (#9)
Browse files Browse the repository at this point in the history
* expanded with more tests and slightly improved internal code

* fixed tests

* fixed alloc issue

* added project example

* updated documentation

* fmt
  • Loading branch information
FloppyDisck authored Dec 1, 2024
1 parent 4687808 commit e5a4fff
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
/target/
/examples/**/target
/examples/**/Cargo.lock

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
name = "sht31"
description = "A library for the SHT31 temperature and humidity sensor"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
license = "MIT"
repository = "https://github.com/FloppyDisck/SHT31-rs"
readme = "README.md"
keywords = ["embedded", "sensor", "temperature", "humidity"]
categories = ["embedded"]

[features]
thiserror = ["dep:thiserror"]

[dependencies]
embedded-hal = "1.0.0"

crc = "3.0.0"
thiserror = { version = "1.0.38", optional = true }
thiserror = { version = "2.0.3", default-features = false }

[dev-dependencies]
embedded-hal-mock = "0.11.1"
rstest = "0.21.0"
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ fn main() -> Result<()> {

// Use single shot, with high accuracy, an alternate I2C address
// and return temp data in Fahrenheit
let sht = SHT31::new(i2c)
.with_mode(SingleShot::new())
let sht = SHT31::single_shot(i2c, SingleShot::new())
.with_accuracy(Accuracy::High)
.with_unit(TemperatureUnit::Fahrenheit)
.with_address(DeviceAddr::AD1);
Expand Down Expand Up @@ -73,8 +72,10 @@ fn main() -> Result<()> {

// In periodic mode, the sensor keeps updating the reading
// without needing to measure
let mut sht = SHT31::new(sht_i2c)
.with_mode(Periodic::new().with_mps(MPS::Normal))
let mut sht = SHT31::periodic(
i2c,
Periodic::new().with_mps(MPS::Normal)
)
.with_accuracy(Accuracy::High);

// Trigger the measure before running your loop to initialize the periodic mode
Expand All @@ -96,8 +97,7 @@ use sht31::prelude::*;
fn main() -> Result<()> {

// Makes the sensor acquire the data at 4 Hz
let mut sht = SHT31::new(sht_i2c)
.with_mode(Periodic::new().with_art());
let mut sht = SHT31::periodic(i2c, Periodic::new().with_art());
sht.measure()?;

loop {
Expand All @@ -117,8 +117,7 @@ use sht31::prelude::*;
fn main() -> Result<()> {
// i2c setup

let mut sht = SHT31::new(sht_i2c)
.with_mode(Periodic::new());
let mut sht = SHT31::periodic(i2c, Periodic::new());

// Trigger the measure before running your loop to initialize the periodic mode
sht.measure()?;
Expand Down
15 changes: 15 additions & 0 deletions examples/esp32/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[target.xtensa-esp32-none-elf]
runner = "espflash flash --monitor"

[env]
ESP_LOG="INFO"

[build]
rustflags = [
"-C", "link-arg=-nostartfiles",
]

target = "xtensa-esp32-none-elf"

[unstable]
build-std = ["core"]
33 changes: 33 additions & 0 deletions examples/esp32/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "esp32"
version = "0.1.0"
edition = "2021"

[dependencies]
esp-backtrace = { version = "0.14.1", features = [
"esp32",
"exception-handler",
"panic-handler",
"println",
]}

esp-hal = { version = "0.22.0", features = [
"esp32",
] }
esp-println = { version = "0.12.0", features = ["esp32", "log"] }
log = { version = "0.4.21" }
sht31 = { path = "../../." }

[profile.dev]
# Rust debug is too slow.
# For debug builds always builds with some optimization
opt-level = "s"

[profile.release]
codegen-units = 1 # LLVM can perform better optimizations using a single thread
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 's'
overflow-checks = false
3 changes: 3 additions & 0 deletions examples/esp32/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo:rustc-link-arg-bins=-Tlinkall.x");
}
2 changes: 2 additions & 0 deletions examples/esp32/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "esp"
32 changes: 32 additions & 0 deletions examples/esp32/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![no_std]
#![no_main]

use esp_backtrace as _;
use esp_hal::delay::Delay;
use esp_hal::i2c::master::{Config, I2c};
use esp_hal::prelude::*;
use sht31::prelude::*;
use log::info;

#[entry]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
let peripherals = esp_hal::init({
let mut config = esp_hal::Config::default();
config.cpu_clock = CpuClock::max();
config
});

info!("Initializing SHT31");
let i2c = I2c::new(
peripherals.I2C0,
Config::default()
);
let delay = Delay::new();
let mut sht = SHT31::new(i2c, delay);

loop {
let reading = sht.read().unwrap();
info!("Temperature: {}\nHumidity: {}", reading.temperature, reading.humidity);
}
}
1 change: 1 addition & 0 deletions examples/esp32/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#![no_std]
21 changes: 11 additions & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
use thiserror::Error;

pub type Result<T> = core::result::Result<T, SHTError>;
#[cfg_attr(feature = "thiserror", derive(thiserror::Error))]
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
#[derive(Error, Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum SHTError {
#[cfg_attr(feature = "thiserror", error("Read I2C Error"))]
#[error("Read I2C Error")]
ReadI2CError,
#[cfg_attr(feature = "thiserror", error("Write Read I2C Error"))]
#[error("Write Read I2C Error")]
WriteReadI2CError,
#[cfg_attr(feature = "thiserror", error("Write I2C Error"))]
#[error("Write I2C Error")]
WriteI2CError,
#[cfg_attr(feature = "thiserror", error("Humidity bytes [{bytes_start:#x}, {bytes_end:#x}] expected {expected_checksum:#x} but got the checksum {calculated_checksum:#x}"))]
#[error("Humidity bytes [{bytes_start:#x}, {bytes_end:#x}] expected {expected_checksum:#x} but got the checksum {calculated_checksum:#x}")]
InvalidHumidityChecksumError {
bytes_start: u8,
bytes_end: u8,
expected_checksum: u8,
calculated_checksum: u8,
},
#[cfg_attr(feature = "thiserror", error("Temperature bytes [{bytes_start:#x}, {bytes_end:#x}] expected {expected_checksum:#x} but got the checksum {calculated_checksum:#x}"))]
#[error("Temperature bytes [{bytes_start:#x}, {bytes_end:#x}] expected {expected_checksum:#x} but got the checksum {calculated_checksum:#x}")]
InvalidTemperatureChecksumError {
bytes_start: u8,
bytes_end: u8,
expected_checksum: u8,
calculated_checksum: u8,
},
#[cfg_attr(feature = "thiserror", error("Status bytes [{bytes_start:#x}, {bytes_end:#x}] expected {expected_checksum:#x} but got the checksum {calculated_checksum:#x}"))]
#[error("Status bytes [{bytes_start:#x}, {bytes_end:#x}] expected {expected_checksum:#x} but got the checksum {calculated_checksum:#x}")]
InvalidStatusChecksumError {
bytes_start: u8,
bytes_end: u8,
expected_checksum: u8,
calculated_checksum: u8,
},
#[cfg_attr(feature = "thiserror", error("Single shot reading timeout"))]
#[error("Single shot reading timeout")]
ReadingTimeoutError,
#[cfg_attr(feature = "thiserror", error("This error should not happen"))]
#[error("This error should not happen")]
PlaceholderError,
}
Loading

0 comments on commit e5a4fff

Please sign in to comment.