embedded_sensors_hal_async/
humidity.rs

1//! Async Humidity Sensor API
2//!
3//! This API provides generic methods for interfacing with humidity sensors specifically.
4//!
5//! # For HAL authors
6//!
7//! Here is an example for the implementation of the RelativeHumiditySensor
8//! and RelativityHumidityThresholdWait traits for a humidity sensor.
9//!
10//! ```
11//! use embedded_sensors_hal_async::sensor;
12//! use embedded_sensors_hal_async::humidity::{RelativeHumiditySensor, Percentage};
13//!
14//! // A struct representing a humidity sensor.
15//! pub struct MyHumiditySensor {
16//!     // ...
17//! }
18//!
19//! #[derive(Clone, Copy, Debug)]
20//! pub enum Error {
21//!     // ...
22//! }
23//!
24//! impl sensor::Error for Error {
25//!     fn kind(&self) -> sensor::ErrorKind {
26//!         match *self {
27//!             // ...
28//!         }
29//!     }
30//! }
31//!
32//! impl sensor::ErrorType for MyHumiditySensor {
33//!     type Error = Error;
34//! }
35//!
36//! impl RelativeHumiditySensor for MyHumiditySensor {
37//!     async fn relative_humidity(&mut self) -> Result<Percentage, Self::Error> {
38//!         // ...
39//!         Ok(42.0)
40//!     }
41//! }
42//!
43//! impl RelativeHumidityThresholdWait for MyHumiditySensor {
44//!     async fn wait_for_relative_humidity_low(
45//!         &mut self,
46//!         threshold: Percentage)
47//!     -> Result<(), Self::Error> {
48//!         // Enable lower threshold alerts for sensor...
49//!         // Await alert (e.g. GPIO level change)...
50//!         // Disable alerts for sensor...
51//!
52//!         Ok(())
53//!     }
54//!
55//!     async fn wait_for_relative_humidity_high(
56//!         &mut self,
57//!         threshold: Percentage)
58//!     -> Result<(), Self::Error> {
59//!         // Enable upper threshold alerts for sensor...
60//!         // Await alert (e.g. await GPIO level change)...
61//!         // Disable alerts for sensor...
62//!
63//!         Ok(())
64//!     }
65//!
66//!     async fn wait_for_relative_humidity_out_of_range(
67//!         &mut self,
68//!         threshold_low: Percentage,
69//!         threshold_high: Percentage
70//!     ) -> Result<(), Self::Error> {
71//!         // Enable lower and upper threshold alerts for sensor...
72//!         // Await alert (e.g. await GPIO level change)...
73//!         // Disable alerts for sensor...
74//!
75//!         Ok(())
76//!     }
77//! }
78//! ```
79
80use crate::decl_threshold_wait;
81use crate::sensor::ErrorType;
82pub use embedded_sensors_hal::humidity::Percentage;
83
84/// Async Relative Humidity Sensor methods.
85pub trait RelativeHumiditySensor: ErrorType {
86    /// Returns a relative humidity (RH) sample as a percentage.
87    async fn relative_humidity(&mut self) -> Result<Percentage, Self::Error>;
88}
89
90impl<T: RelativeHumiditySensor + ?Sized> RelativeHumiditySensor for &mut T {
91    #[inline]
92    async fn relative_humidity(&mut self) -> Result<Percentage, Self::Error> {
93        T::relative_humidity(self).await
94    }
95}
96
97decl_threshold_wait!(RelativeHumidity, Percentage, "percentage");