humidity_core/sensors/
mod.rs

1//! # Sensors and calibrations
2//!
3//! Defines the [`Sensor`] trait for all sensors to implement
4//!
5//! ## TODO
6//! - Introduce a mechanism to override calibrations, specially to support different
7//! calibrations based on different environments.
8//!
9//! ## Supported sensors
10//!
11//! - Hygrometers
12//!   - [`Hygrometer::YL69`]
13//!   - [`Hygrometer::HW390`]
14//!
15//! ## Examples
16//!
17//! ```rust
18//! use humidity_core::sensors::{Hygrometer, Sensor};
19//! let sensor = Hygrometer::HW390;
20//! println!("sensor reading: {}", sensor.percentage(1200));
21//! ```
22
23pub use hygrometer::Hygrometer;
24
25use crate::serde;
26
27mod hygrometer;
28
29/// Defines common behaviour for all sensors, such as getting the calibrated low
30/// and high values, and provides a function to compute where a value
31/// fits within the calibrated boundaries.
32pub trait Sensor: serde::Serializable + serde::Deserializable {
33    /// Returns the calibrated low reading.
34    fn low(&self) -> u16;
35    /// Returns the calibrated high reading.
36    fn high(&self) -> u16;
37    /// Given a value, returns the percentage it falls within the calibrated boundaries.
38    fn percentage(&self, value: u16) -> f32 {
39        (value - self.low()) as f32 / (self.high() - self.low()) as f32
40    }
41}