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::{TemperatureSensor, DegreesCelsius};
12//!
13//! // A struct representing a temperature sensor.
14//! pub struct MyTempSensor {
15//! // ...
16//! }
17//!
18//! #[derive(Clone, Copy, Debug)]
19//! pub enum Error {
20//! // ...
21//! }
22//!
23//! impl sensor::Error for Error {
24//! fn kind(&self) -> sensor::ErrorKind {
25//! match *self {
26//! // ...
27//! }
28//! }
29//! }
30//!
31//! impl sensor::ErrorType for MyTempSensor {
32//! type Error = Error;
33//! }
34//!
35//! impl TemperatureSensor for MyTempSensor {
36//! async fn temperature(&mut self) -> Result<DegreesCelsius, Self::Error> {
37//! // ...
38//! Ok(42.0)
39//! }
40//! }
41//!
42//! impl TemperatureThresholdWait for MyTempSensor {
43//! async fn wait_for_temperature_low(&mut self, threshold: DegreesCelsius) -> Result<(), Self::Error> {
44//! // Enable lower threshold alerts for sensor...
45//! // Await alert (e.g. GPIO level change)...
46//! // Disable alerts for sensor...
47//!
48//! Ok(())
49//! }
50//!
51//! async fn wait_for_temperature_high(&mut self, threshold: DegreesCelsius) -> Result<(), Self::Error> {
52//! // Enable upper threshold alerts for sensor...
53//! // Await alert (e.g. await GPIO level change)...
54//! // Disable alerts for sensor...
55//!
56//! Ok(())
57//! }
58//!
59//! async fn wait_for_temperature_out_of_range(
60//! &mut self,
61//! threshold_low: DegreesCelsius,
62//! threshold_high: DegreesCelsius
63//! ) -> Result<(), Self::Error> {
64//! // Enable lower and upper threshold alerts for sensor...
65//! // Await alert (e.g. await GPIO level change)...
66//! // Disable alerts for sensor...
67//!
68//! Ok(())
69//! }
70//! }
71//! ```
72
73use crate::decl_threshold_wait;
74use crate::sensor::ErrorType;
75pub use embedded_sensors_hal::temperature::DegreesCelsius;
76
77/// Async Temperature Sensor methods.
78pub trait TemperatureSensor: ErrorType {
79 /// Returns a temperature sample in degrees Celsius.
80 async fn temperature(&mut self) -> Result<DegreesCelsius, Self::Error>;
81}
82
83impl<T: TemperatureSensor + ?Sized> TemperatureSensor for &mut T {
84 #[inline]
85 async fn temperature(&mut self) -> Result<DegreesCelsius, Self::Error> {
86 T::temperature(self).await
87 }
88}
89
90decl_threshold_wait!(Temperature, DegreesCelsius, "degrees Celsius");