Skip to content

Commit

Permalink
chore: add test and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zeldan committed Oct 6, 2024
1 parent 9951e31 commit da911b8
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Build
run: cargo build --verbose

- name: Run tests
run: cargo test --verbose
135 changes: 134 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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]
Expand Down
64 changes: 64 additions & 0 deletions src/dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,67 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht<P, D> {
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();
}
}

0 comments on commit da911b8

Please sign in to comment.