embedded_sensors_hal_async/
sensor.rs

1//! Async Sensor API
2//!
3//! This module contains traits generic to all sensors.
4//!
5//! Please see specific sensor-type modules for addtional example usage
6//! (e.g. see temperature.rs for TemperatureSensor examples).
7
8pub use embedded_sensors_hal::sensor::{Error, ErrorKind, ErrorType};
9
10/// Generates threshold traits for the specified sensor type.
11#[macro_export]
12macro_rules! decl_threshold_traits {
13    ($SensorName:ident, $SensorTrait:ident, $SampleType:ty, $unit:expr) => {
14        paste::paste! {
15            #[doc = concat!(" Asynchronously set ", stringify!($SensorName), " thresholds.")]
16            pub trait [<$SensorName ThresholdSet>]: $SensorTrait {
17                #[doc = concat!(" Set lower ", stringify!($SensorName), " threshold (in ", $unit, ").")]
18                async fn [<set_ $SensorName:snake _threshold_low>](&mut self, threshold: $SampleType) -> Result<(), Self::Error>;
19
20                #[doc = concat!(" Set upper ", stringify!($SensorName), " threshold (in ", $unit, ").")]
21                async fn [<set_ $SensorName:snake _threshold_high>](&mut self, threshold: $SampleType) -> Result<(), Self::Error>;
22            }
23
24            #[doc = concat!(" Asynchronously wait for ", stringify!($SensorName), " measurements to exceed specified thresholds.")]
25            pub trait [<$SensorName ThresholdWait>]: [<$SensorName ThresholdSet>] {
26                #[doc = concat!(" Wait for ", stringify!($SensorName), " to be measured above or below the previously set high and low thresholds.")]
27                #[doc = concat!(" Returns the measured ", stringify!($SensorName), " at time threshold is exceeded (in ", $unit, ").")]
28                async fn [<wait_for_ $SensorName:snake _threshold>](&mut self) -> Result<$SampleType, Self::Error>;
29            }
30
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
37            impl<T: [<$SensorName ThresholdSet>] + ?Sized> [<$SensorName ThresholdSet>] for &mut T {
38                async fn [<set_ $SensorName:snake _threshold_low>](&mut self, threshold: $SampleType) -> Result<(), Self::Error> {
39                    T::[<set_ $SensorName:snake _threshold_low>](self, threshold).await
40                }
41
42                async fn [<set_ $SensorName:snake _threshold_high>](&mut self, threshold: $SampleType) -> Result<(), Self::Error> {
43                    T::[<set_ $SensorName:snake _threshold_high>](self, threshold).await
44                }
45            }
46
47            impl<T: [<$SensorName ThresholdWait>] + ?Sized> [<$SensorName ThresholdWait>] for &mut T {
48                async fn [<wait_for_ $SensorName:snake _threshold>](&mut self) -> Result<$SampleType, Self::Error> {
49                    T::[<wait_for_ $SensorName:snake _threshold>](self).await
50                }
51            }
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            }
58        }
59    };
60}