Skip to content

Commit a145461

Browse files
committed
feat: add generic SensorReading
1 parent 2412cdf commit a145461

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "embedded-dht-rs"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = "A platform agnostic driver to interface with the DHT11/DHT22 (AM2302) temperature and humidity sensor"

src/dht11.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht11<P, D> {
1616
}
1717
}
1818

19-
pub fn read(&mut self) -> Result<SensorReading, SensorError> {
19+
pub fn read(&mut self) -> Result<SensorReading<u8>, SensorError> {
2020
// Start communication: pull pin low for 18ms, then release.
2121
let _ = self.dht.pin.set_low();
2222
self.dht.delay.delay_ms(18);
@@ -46,8 +46,8 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht11<P, D> {
4646
}
4747

4848
Ok(SensorReading {
49-
humidity: humidity_integer as f32,
50-
temperature: temperature_integer as f32,
49+
humidity: humidity_integer,
50+
temperature: temperature_integer,
5151
})
5252
}
5353
}

src/dht22.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht22<P, D> {
1616
}
1717
}
1818

19-
pub fn read(&mut self) -> Result<SensorReading, SensorError> {
19+
pub fn read(&mut self) -> Result<SensorReading<f32>, SensorError> {
2020
// Start communication: pull pin low for 18ms, then release.
2121
let _ = self.dht.pin.set_low();
2222
self.dht.delay.delay_ms(18);
@@ -52,8 +52,8 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht22<P, D> {
5252
let temperatue_percentage = temperature_value as f32 / 10.0;
5353

5454
Ok(SensorReading {
55-
humidity: humidity_percentage as f32,
56-
temperature: temperatue_percentage as f32,
55+
humidity: humidity_percentage,
56+
temperature: temperatue_percentage,
5757
})
5858
}
5959
}

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ pub mod dht11;
55
pub mod dht22;
66

77
/// Represents a reading from the sensor.
8-
pub struct SensorReading {
9-
pub humidity: f32,
10-
pub temperature: f32,
8+
pub struct SensorReading<T> {
9+
pub humidity: T,
10+
pub temperature: T,
1111
}
1212

1313
/// Possible errors when interacting with the sensor.

0 commit comments

Comments
 (0)