Skip to content

Commit a7a3c8d

Browse files
authored
Merge pull request #29 from kurtjd/add-hysteresis-trait
Add set_hysteresis method to threshold trait
2 parents 86f6cad + 47fb6f9 commit a7a3c8d

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

embedded-sensors-async/src/humidity.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//! ```
1111
//! use embedded_sensors_hal_async::sensor;
1212
//! use embedded_sensors_hal_async::humidity::{
13-
//! Percentage, RelativeHumiditySensor, RelativeHumidityThresholdSet,
14-
//! RelativeHumidityThresholdWait,
13+
//! Percentage, RelativeHumidityHysteresis, RelativeHumiditySensor,
14+
//! RelativeHumidityThresholdSet, RelativeHumidityThresholdWait,
1515
//! };
1616
//!
1717
//! // A struct representing a humidity sensor.
@@ -46,16 +46,16 @@
4646
//! impl RelativeHumidityThresholdSet for MyHumiditySensor {
4747
//! async fn set_relative_humidity_threshold_low(
4848
//! &mut self,
49-
//! threshold: Percentage)
50-
//! -> Result<(), Self::Error> {
49+
//! threshold: Percentage
50+
//! ) -> Result<(), Self::Error> {
5151
//! // Write value to threshold low register of sensor...
5252
//! Ok(())
5353
//! }
5454
//!
5555
//! async fn set_relative_humidity_threshold_high(
5656
//! &mut self,
57-
//! threshold: Percentage)
58-
//! -> Result<(), Self::Error> {
57+
//! threshold: Percentage
58+
//! ) -> Result<(), Self::Error> {
5959
//! // Write value to threshold high register of sensor...
6060
//! Ok(())
6161
//! }
@@ -70,6 +70,16 @@
7070
//! self.relative_humidity().await
7171
//! }
7272
//! }
73+
//!
74+
//! impl RelativeHumidityHysteresis for MyHumiditySensor {
75+
//! async fn set_relative_humidity_threshold_hysteresis(
76+
//! &mut self,
77+
//! hysteresis: Percentage
78+
//! ) -> Result<(), Self::Error> {
79+
//! // Write value to threshold hysteresis register of sensor...
80+
//! Ok(())
81+
//! }
82+
//! }
7383
//! ```
7484
7585
use crate::decl_threshold_traits;

embedded-sensors-async/src/sensor.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ macro_rules! decl_threshold_traits {
2828
async fn [<wait_for_ $SensorName:snake _threshold>](&mut self) -> Result<$SampleType, Self::Error>;
2929
}
3030

31+
#[doc = concat!(" Asynchronously set ", stringify!($SensorName), " threshold hysteresis.")]
32+
pub trait [<$SensorName Hysteresis>]: [<$SensorName ThresholdSet>] {
33+
#[doc = concat!(" Set ", stringify!($SensorName), " threshold hysteresis (in ", $unit, ").")]
34+
async fn [<set_ $SensorName:snake _threshold_hysteresis>](&mut self, hysteresis: $SampleType) -> Result<(), Self::Error>;
35+
}
36+
3137
impl<T: [<$SensorName ThresholdSet>] + ?Sized> [<$SensorName ThresholdSet>] for &mut T {
3238
async fn [<set_ $SensorName:snake _threshold_low>](&mut self, threshold: $SampleType) -> Result<(), Self::Error> {
3339
T::[<set_ $SensorName:snake _threshold_low>](self, threshold).await
@@ -43,6 +49,12 @@ macro_rules! decl_threshold_traits {
4349
T::[<wait_for_ $SensorName:snake _threshold>](self).await
4450
}
4551
}
52+
53+
impl<T: [<$SensorName Hysteresis>] + ?Sized> [<$SensorName Hysteresis>] for &mut T {
54+
async fn [<set_ $SensorName:snake _threshold_hysteresis>](&mut self, hysteresis: $SampleType) -> Result<(), Self::Error> {
55+
T::[<set_ $SensorName:snake _threshold_hysteresis>](self, hysteresis).await
56+
}
57+
}
4658
}
4759
};
4860
}

embedded-sensors-async/src/temperature.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
//! ```
1010
//! use embedded_sensors_hal_async::sensor;
1111
//! use embedded_sensors_hal_async::temperature::{
12-
//! DegreesCelsius, TemperatureSensor, TemperatureThresholdSet,
13-
//! TemperatureThresholdWait,
12+
//! DegreesCelsius, TemperatureHysteresis, TemperatureSensor,
13+
//! TemperatureThresholdSet, TemperatureThresholdWait,
1414
//! };
1515
//!
1616
//! // A struct representing a temperature sensor.
@@ -43,25 +43,42 @@
4343
//! }
4444
//!
4545
//! impl TemperatureThresholdSet for MyTempSensor {
46-
//! async fn set_temperature_threshold_low(&mut self, threshold: DegreesCelsius) -> Result<(), Self::Error> {
46+
//! async fn set_temperature_threshold_low(
47+
//! &mut self,
48+
//! threshold: DegreesCelsius
49+
//! ) -> Result<(), Self::Error> {
4750
//! // Write value to threshold low register of sensor...
4851
//! Ok(())
4952
//! }
5053
//!
51-
//! async fn set_temperature_threshold_high(&mut self, threshold: DegreesCelsius) -> Result<(), Self::Error> {
54+
//! async fn set_temperature_threshold_high(
55+
//! &mut self,
56+
//! threshold: DegreesCelsius
57+
//! ) -> Result<(), Self::Error> {
5258
//! // Write value to threshold high register of sensor...
5359
//! Ok(())
5460
//! }
5561
//! }
5662
//!
5763
//! impl TemperatureThresholdWait for MyTempSensor {
58-
//!
59-
//! async fn wait_for_temperature_threshold(&mut self) -> Result<DegreesCelsius, Self::Error> {
64+
//! async fn wait_for_temperature_threshold(
65+
//! &mut self
66+
//! ) -> Result<DegreesCelsius, Self::Error> {
6067
//! // Await threshold alert (e.g. await GPIO level change on ALERT pin)...
6168
//! // Then return current temperature so caller can determine which threshold was crossed
6269
//! self.temperature().await
6370
//! }
6471
//! }
72+
//!
73+
//! impl TemperatureHysteresis for MyTempSensor {
74+
//! async fn set_temperature_threshold_hysteresis(
75+
//! &mut self,
76+
//! hysteresis: DegreesCelsius
77+
//! ) -> Result<(), Self::Error> {
78+
//! // Write value to threshold hysteresis register of sensor...
79+
//! Ok(())
80+
//! }
81+
//! }
6582
//! ```
6683
6784
use crate::decl_threshold_traits;

0 commit comments

Comments
 (0)