diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 9fd45e0..4ce73a2 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -16,7 +16,9 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Build run: cargo build --verbose + - name: Run tests run: cargo test --verbose diff --git a/Cargo.lock b/Cargo.lock index 80748e8..4711115 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,11 +2,28 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + [[package]] name = "embedded-dht-rs" version = "0.3.1" dependencies = [ - "embedded-hal", + "embedded-hal 1.0.0", + "embedded-hal-mock", +] + +[[package]] +name = "embedded-hal" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff" +dependencies = [ + "nb 0.1.3", + "void", ] [[package]] @@ -14,3 +31,119 @@ name = "embedded-hal" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" + +[[package]] +name = "embedded-hal-mock" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a0f04f8886106faf281c47b6a0e4054a369baedaf63591fdb8da9761f3f379" +dependencies = [ + "embedded-hal 0.2.7", + "embedded-hal 1.0.0", + "embedded-hal-nb", + "embedded-time", + "nb 1.1.0", + "void", +] + +[[package]] +name = "embedded-hal-nb" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fba4268c14288c828995299e59b12babdbe170f6c6d73731af1b4648142e8605" +dependencies = [ + "embedded-hal 1.0.0", + "nb 1.1.0", +] + +[[package]] +name = "embedded-time" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7a4b4d10ac48d08bfe3db7688c402baadb244721f30a77ce360bd24c3dffe58" +dependencies = [ + "num", +] + +[[package]] +name = "nb" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f" +dependencies = [ + "nb 1.1.0", +] + +[[package]] +name = "nb" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" + +[[package]] +name = "num" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b7a8e9be5e039e2ff869df49155f1c06bd01ade2117ec783e56ab0932b67a8f" +dependencies = [ + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "747d632c0c558b87dbabbe6a82f3b4ae03720d0646ac5b7b4dae89394be5f2c5" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" diff --git a/Cargo.toml b/Cargo.toml index f9b8b12..86b615a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,9 @@ keywords = ["embedded", "no-std", "dht11", "dht20", "dht22"] [dependencies] embedded-hal = "1.0.0" +[dev-dependencies] +embedded-hal-mock = "0.11.1" + [lib] doctest = false diff --git a/README.md b/README.md index efda2e1..3afc506 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,17 @@ ![build workflow](https://github.com/rust-dd/embedded-dht-rs/actions/workflows/rust.yml/badge.svg) [![Crates.io](https://img.shields.io/crates/v/embedded-dht-rs?style=flat-square)](https://crates.io/crates/embedded-dht-rs) ![Crates.io](https://img.shields.io/crates/l/embedded-dht-rs?style=flat-square) +[![API](https://docs.rs/embedded-dht-rs/badge.svg)](https://docs.rs/embedded-dht-rs) # embedded-dht-rs -Welcome to `embedded-dht-rs`, a Rust library designed to make working with DHT sensors a breeze! +`embedded-dht-rs` is a Rust library designed to simplify interfacing with DHT sensors in embedded systems. -This library only depends on `embedded_hal`, making it versatile and compatible with virtually any microcontroller. +This library is `#![no_std]` and depends only on `embedded_hal`, making it versatile and compatible with virtually any microcontroller. **Support for DHT11, DHT20, and DHT22 Sensors**: All three sensors are fully implemented and ready for use. -We’ve tested it with the ESP32-WROOM, and you can find a detailed example below to help you get started. +The library has been tested with the ESP32-WROOM, and a detailed example is provided below to help you get started. ## Getting Started @@ -25,6 +26,10 @@ Here are some general tutorials that provide brief introductions to embedded pro ### Example - ESP32 +```rust +cargo add cargo add embedded-dht-rs +``` + ```rust #![no_std] #![no_main] diff --git a/src/dht.rs b/src/dht.rs index c4782e1..2449331 100644 --- a/src/dht.rs +++ b/src/dht.rs @@ -73,3 +73,67 @@ impl Dht { Ok(()) } } + + +#[cfg(test)] +mod tests { + use super::*; + use embedded_hal_mock::eh1::digital::{Mock, State, Transaction as PinTransaction}; + use embedded_hal_mock::eh1::delay::NoopDelay as MockNoop; + + #[test] + fn test_read_byte() { + // Set up the pin transactions to mock the behavior of the sensor during the reading of a byte. + // Each bit read from the sensor starts with a High state that lasts long enough + // to signify the bit, followed by reading whether it stays High (bit 1) or goes Low (bit 0). + let expectations = [ + // Bit 1 - 0 + PinTransaction::get(State::High), + PinTransaction::get(State::Low), + + // Bit 2 - 1 + PinTransaction::get(State::High), + PinTransaction::get(State::High), + PinTransaction::get(State::Low), + + // Bit 3 - 0 + PinTransaction::get(State::High), + PinTransaction::get(State::Low), + + // Bit 4 - 1 + PinTransaction::get(State::High), + PinTransaction::get(State::High), + PinTransaction::get(State::Low), + + // Bit 5 - 0 + PinTransaction::get(State::High), + PinTransaction::get(State::Low), + + // Bit 6 - 1 + PinTransaction::get(State::High), + PinTransaction::get(State::High), + PinTransaction::get(State::Low), + + // Bit 7 - 1 + PinTransaction::get(State::High), + PinTransaction::get(State::High), + PinTransaction::get(State::Low), + + // Bit 8 - 1 + PinTransaction::get(State::High), + PinTransaction::get(State::High), + PinTransaction::get(State::Low), + + ]; + + let mock_pin = Mock::new(&expectations); + let mock_delay = MockNoop::new(); + + let mut dht = Dht::new(mock_pin, mock_delay); + + let result = dht.read_byte().unwrap(); + assert_eq!(result, 0b01010111); + + dht.pin.done(); + } +}