Expand description
Async Temperature Sensor API
This API provides generic methods for interfacing with temperature sensors specifically.
§For HAL authors
Here is an example for the implementation of the TemperatureSensor and TemperatureThresholdWait traits for a temperature sensor.
use embedded_sensors_hal_async::sensor;
use embedded_sensors_hal_async::temperature::{
DegreesCelsius, TemperatureHysteresis, TemperatureSensor,
TemperatureThresholdSet, TemperatureThresholdWait,
};
// A struct representing a temperature sensor.
pub struct MyTempSensor {
// ...
}
#[derive(Clone, Copy, Debug)]
pub enum Error {
// ...
}
impl sensor::Error for Error {
fn kind(&self) -> sensor::ErrorKind {
match *self {
// ...
}
}
}
impl sensor::ErrorType for MyTempSensor {
type Error = Error;
}
impl TemperatureSensor for MyTempSensor {
async fn temperature(&mut self) -> Result<DegreesCelsius, Self::Error> {
// ...
Ok(42.0)
}
}
impl TemperatureThresholdSet for MyTempSensor {
async fn set_temperature_threshold_low(
&mut self,
threshold: DegreesCelsius
) -> Result<(), Self::Error> {
// Write value to threshold low register of sensor...
Ok(())
}
async fn set_temperature_threshold_high(
&mut self,
threshold: DegreesCelsius
) -> Result<(), Self::Error> {
// Write value to threshold high register of sensor...
Ok(())
}
}
impl TemperatureThresholdWait for MyTempSensor {
async fn wait_for_temperature_threshold(
&mut self
) -> Result<DegreesCelsius, Self::Error> {
// Await threshold alert (e.g. await GPIO level change on ALERT pin)...
// Then return current temperature so caller can determine which threshold was crossed
self.temperature().await
}
}
impl TemperatureHysteresis for MyTempSensor {
async fn set_temperature_threshold_hysteresis(
&mut self,
hysteresis: DegreesCelsius
) -> Result<(), Self::Error> {
// Write value to threshold hysteresis register of sensor...
Ok(())
}
}
Traits§
- Temperature
Hysteresis - Asynchronously set Temperature threshold hysteresis.
- Temperature
Sensor - Async Temperature Sensor methods.
- Temperature
Threshold Set - Asynchronously set Temperature thresholds.
- Temperature
Threshold Wait - Asynchronously wait for Temperature measurements to exceed specified thresholds.
Type Aliases§
- Degrees
Celsius - Associates the units temperature samples are measured in with the underlying data type.