Skip to content

Commit da911b8

Browse files
committed
chore: add test and update docs
1 parent 9951e31 commit da911b8

File tree

5 files changed

+211
-4
lines changed

5 files changed

+211
-4
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v4
19+
1920
- name: Build
2021
run: cargo build --verbose
22+
2123
- name: Run tests
2224
run: cargo test --verbose

Cargo.lock

Lines changed: 134 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ keywords = ["embedded", "no-std", "dht11", "dht20", "dht22"]
1313
[dependencies]
1414
embedded-hal = "1.0.0"
1515

16+
[dev-dependencies]
17+
embedded-hal-mock = "0.11.1"
18+
1619
[lib]
1720
doctest = false
1821

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
![build workflow](https://github.com/rust-dd/embedded-dht-rs/actions/workflows/rust.yml/badge.svg)
22
[![Crates.io](https://img.shields.io/crates/v/embedded-dht-rs?style=flat-square)](https://crates.io/crates/embedded-dht-rs)
33
![Crates.io](https://img.shields.io/crates/l/embedded-dht-rs?style=flat-square)
4+
[![API](https://docs.rs/embedded-dht-rs/badge.svg)](https://docs.rs/embedded-dht-rs)
45

56
# embedded-dht-rs
67

7-
Welcome to `embedded-dht-rs`, a Rust library designed to make working with DHT sensors a breeze!
8+
`embedded-dht-rs` is a Rust library designed to simplify interfacing with DHT sensors in embedded systems.
89

9-
This library only depends on `embedded_hal`, making it versatile and compatible with virtually any microcontroller.
10+
This library is `#![no_std]` and depends only on `embedded_hal`, making it versatile and compatible with virtually any microcontroller.
1011

1112
**Support for DHT11, DHT20, and DHT22 Sensors**: All three sensors are fully implemented and ready for use.
1213

13-
We’ve tested it with the ESP32-WROOM, and you can find a detailed example below to help you get started.
14+
The library has been tested with the ESP32-WROOM, and a detailed example is provided below to help you get started.
1415

1516
## Getting Started
1617

@@ -25,6 +26,10 @@ Here are some general tutorials that provide brief introductions to embedded pro
2526

2627
### Example - ESP32
2728

29+
```rust
30+
cargo add cargo add embedded-dht-rs
31+
```
32+
2833
```rust
2934
#![no_std]
3035
#![no_main]

src/dht.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,67 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht<P, D> {
7373
Ok(())
7474
}
7575
}
76+
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use super::*;
81+
use embedded_hal_mock::eh1::digital::{Mock, State, Transaction as PinTransaction};
82+
use embedded_hal_mock::eh1::delay::NoopDelay as MockNoop;
83+
84+
#[test]
85+
fn test_read_byte() {
86+
// Set up the pin transactions to mock the behavior of the sensor during the reading of a byte.
87+
// Each bit read from the sensor starts with a High state that lasts long enough
88+
// to signify the bit, followed by reading whether it stays High (bit 1) or goes Low (bit 0).
89+
let expectations = [
90+
// Bit 1 - 0
91+
PinTransaction::get(State::High),
92+
PinTransaction::get(State::Low),
93+
94+
// Bit 2 - 1
95+
PinTransaction::get(State::High),
96+
PinTransaction::get(State::High),
97+
PinTransaction::get(State::Low),
98+
99+
// Bit 3 - 0
100+
PinTransaction::get(State::High),
101+
PinTransaction::get(State::Low),
102+
103+
// Bit 4 - 1
104+
PinTransaction::get(State::High),
105+
PinTransaction::get(State::High),
106+
PinTransaction::get(State::Low),
107+
108+
// Bit 5 - 0
109+
PinTransaction::get(State::High),
110+
PinTransaction::get(State::Low),
111+
112+
// Bit 6 - 1
113+
PinTransaction::get(State::High),
114+
PinTransaction::get(State::High),
115+
PinTransaction::get(State::Low),
116+
117+
// Bit 7 - 1
118+
PinTransaction::get(State::High),
119+
PinTransaction::get(State::High),
120+
PinTransaction::get(State::Low),
121+
122+
// Bit 8 - 1
123+
PinTransaction::get(State::High),
124+
PinTransaction::get(State::High),
125+
PinTransaction::get(State::Low),
126+
127+
];
128+
129+
let mock_pin = Mock::new(&expectations);
130+
let mock_delay = MockNoop::new();
131+
132+
let mut dht = Dht::new(mock_pin, mock_delay);
133+
134+
let result = dht.read_byte().unwrap();
135+
assert_eq!(result, 0b01010111);
136+
137+
dht.pin.done();
138+
}
139+
}

0 commit comments

Comments
 (0)