Skip to content

Commit

Permalink
cleaned up dependencies
Browse files Browse the repository at this point in the history
added a better reading
added example
  • Loading branch information
FloppyDisck committed Dec 1, 2024
1 parent 2a7c672 commit 97c7d60
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 13 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
9 changes: 2 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
thiserror = { version = "2.0.3", default-features = false }
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
}
```
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" }
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
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 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);
}
}
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]
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use thiserror_no_std::Error;
use thiserror::Error;

#[derive(Error, Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum SoilMoistureSensorError {
Expand Down
24 changes: 23 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<I2C, D> SoilSensor<I2C, D>
Expand All @@ -103,6 +118,13 @@ where
Ok(u16::from_be_bytes(buffer))
}

pub fn read(&mut self) -> Result<Reading, SoilMoistureSensorError> {
Ok(Reading {
temperature: self.temperature()?,
moisture: self.moisture()?,
})
}

fn i2c_read(
&mut self,
bytes: &[u8],
Expand Down

0 comments on commit 97c7d60

Please sign in to comment.