diff --git a/.gitignore b/.gitignore index 5f3ede7..3114301 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 522964e..fa3c0e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "stemma_soil_moisture_sensor" description = "A pure generic I2C crate for the Adafruit STEMMA soil moisture sensor " -version = "0.2.0" +version = "0.2.1" edition = "2021" license = "MIT" repository = "https://github.com/FloppyDisck/STEMMA_soil_moisture_sensor" @@ -14,9 +14,4 @@ categories = ["embedded"] [dependencies] embedded-hal = "1.0.0" -crc = "3.0.0" -thiserror-no-std = "2.0.2" - -[dev-dependencies] -embedded-hal-mock = "0.11.1" -rstest = "0.21.0" \ No newline at end of file +thiserror = { version = "2.0.3", default-features = false } \ No newline at end of file diff --git a/README.md b/README.md index b043144..abb966f 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,14 @@ use stemma_soil_moisture_sensor::prelude::*; fn main() -> Result<(), SoilMoistureSensorError> { // Setup your I2C and import relevant delay - let i2c = ...; - let moisture = SoilSensor::new(i2c, Delay).with_units(TemperatureUnit::Fahrenheit); - let temp = moisture.temperature()?; - let moist = moisture.moisture()?; + let sensor = SoilSensor::new(i2c, delay).with_units(TemperatureUnit::Fahrenheit); + // Full + let reading = sensor.read()?; + let temp = reading.temperature; + let moist = reading.moisture; + // Individual + let temp = sensor.temperature()?; + let moist = sensor.moisture()?; } ``` \ No newline at end of file diff --git a/examples/esp32/.cargo/config.toml b/examples/esp32/.cargo/config.toml new file mode 100644 index 0000000..b3c2e46 --- /dev/null +++ b/examples/esp32/.cargo/config.toml @@ -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"] diff --git a/examples/esp32/Cargo.toml b/examples/esp32/Cargo.toml new file mode 100644 index 0000000..1ed1344 --- /dev/null +++ b/examples/esp32/Cargo.toml @@ -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" } +stemma_soil_moisture_sensor = { 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 diff --git a/examples/esp32/build.rs b/examples/esp32/build.rs new file mode 100644 index 0000000..5efe9c9 --- /dev/null +++ b/examples/esp32/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rustc-link-arg-bins=-Tlinkall.x"); +} diff --git a/examples/esp32/rust-toolchain.toml b/examples/esp32/rust-toolchain.toml new file mode 100644 index 0000000..a2f5ab5 --- /dev/null +++ b/examples/esp32/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "esp" diff --git a/examples/esp32/src/bin/main.rs b/examples/esp32/src/bin/main.rs new file mode 100644 index 0000000..cb55421 --- /dev/null +++ b/examples/esp32/src/bin/main.rs @@ -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 log::info; +use stemma_soil_moisture_sensor::prelude::*; + +#[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 Moisture Sensor"); + let i2c = I2c::new( + peripherals.I2C0, + Config::default() + ); + let delay = Delay::new(); + let mut sensor = SoilSensor::new(i2c, delay).with_units(TemperatureUnit::Fahrenheit); + + loop { + let reading = sensor.read().unwrap(); + info!("Temperature: {}\nMoisture: {}", reading.temperature, reading.moisture); + } +} diff --git a/examples/esp32/src/lib.rs b/examples/esp32/src/lib.rs new file mode 100644 index 0000000..0c9ac1a --- /dev/null +++ b/examples/esp32/src/lib.rs @@ -0,0 +1 @@ +#![no_std] diff --git a/src/error.rs b/src/error.rs index da71ece..6c96aad 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use thiserror_no_std::Error; +use thiserror::Error; #[derive(Error, Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)] pub enum SoilMoistureSensorError { diff --git a/src/lib.rs b/src/lib.rs index c86f1f5..fff1503 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ pub mod prelude { } const TEMP_C_CONSTANT: f32 = 0.000015258789; -const TEMP_F_CONSTANT: f32 = 0.000027466; // TEMP_C_CONSTANT * 1.8 +const TEMP_F_CONSTANT: f32 = TEMP_C_CONSTANT * 1.8; const TEMP_F_CONSTANT_SUM: f32 = 32.0; /// Influences what the reading temperature numbers are @@ -80,6 +80,21 @@ where self.moisture_delay = moisture; self } + + pub fn with_temperature_delay(mut self, temp: u32) -> Self { + self.temp_delay = temp; + self + } + + pub fn with_moisture_delay(mut self, moisture: u32) -> Self { + self.moisture_delay = moisture; + self + } +} + +pub struct Reading { + pub temperature: f32, + pub moisture: u16, } impl SoilSensor @@ -103,6 +118,13 @@ where Ok(u16::from_be_bytes(buffer)) } + pub fn read(&mut self) -> Result { + Ok(Reading { + temperature: self.temperature()?, + moisture: self.moisture()?, + }) + } + fn i2c_read( &mut self, bytes: &[u8],