embedded_sensors_hal_async/
temperature.rs

1//! Async Temperature Sensor API
2//!
3//! This API provides generic methods for interfacing with temperature sensors specifically.
4//!
5//! # For HAL authors
6//!
7//! Here is an example for the implementation of the TemperatureSensor and TemperatureThresholdWait traits for a temperature sensor.
8//!
9//! ```
10//! use embedded_sensors_hal_async::sensor;
11//! use embedded_sensors_hal_async::temperature::{
12//!     DegreesCelsius, TemperatureHysteresis, TemperatureSensor,
13//!     TemperatureThresholdSet, TemperatureThresholdWait,
14//! };
15//!
16//! // A struct representing a temperature sensor.
17//! pub struct MyTempSensor {
18//!     // ...
19//! }
20//!
21//! #[derive(Clone, Copy, Debug)]
22//! pub enum Error {
23//!     // ...
24//! }
25//!
26//! impl sensor::Error for Error {
27//!     fn kind(&self) -> sensor::ErrorKind {
28//!         match *self {
29//!             // ...
30//!         }
31//!     }
32//! }
33//!
34//! impl sensor::ErrorType for MyTempSensor {
35//!     type Error = Error;
36//! }
37//!
38//! impl TemperatureSensor for MyTempSensor {
39//!     async fn temperature(&mut self) -> Result<DegreesCelsius, Self::Error> {
40//!         // ...
41//!         Ok(42.0)
42//!     }
43//! }
44//!
45//! impl TemperatureThresholdSet for MyTempSensor {
46//!     async fn set_temperature_threshold_low(
47//!         &mut self,
48//!         threshold: DegreesCelsius
49//!     ) -> Result<(), Self::Error> {
50//!         // Write value to threshold low register of sensor...
51//!         Ok(())
52//!     }
53//!
54//!     async fn set_temperature_threshold_high(
55//!         &mut self,
56//!         threshold: DegreesCelsius
57//!     ) -> Result<(), Self::Error> {
58//!         // Write value to threshold high register of sensor...
59//!         Ok(())
60//!     }
61//! }
62//!
63//! impl TemperatureThresholdWait for MyTempSensor {
64//!     async fn wait_for_temperature_threshold(
65//!         &mut self
66//!     ) -> Result<DegreesCelsius, Self::Error> {
67//!         // Await threshold alert (e.g. await GPIO level change on ALERT pin)...
68//!         // Then return current temperature so caller can determine which threshold was crossed
69//!         self.temperature().await
70//!     }
71//! }
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//! }
82//! ```
83
84use crate::decl_threshold_traits;
85use crate::sensor::ErrorType;
86pub use embedded_sensors_hal::temperature::DegreesCelsius;
87
88/// Async Temperature Sensor methods.
89pub trait TemperatureSensor: ErrorType {
90    /// Returns a temperature sample in degrees Celsius.
91    async fn temperature(&mut self) -> Result<DegreesCelsius, Self::Error>;
92}
93
94impl<T: TemperatureSensor + ?Sized> TemperatureSensor for &mut T {
95    #[inline]
96    async fn temperature(&mut self) -> Result<DegreesCelsius, Self::Error> {
97        T::temperature(self).await
98    }
99}
100
101decl_threshold_traits!(
102    Temperature,
103    TemperatureSensor,
104    DegreesCelsius,
105    "degrees Celsius"
106);