#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]
pub mod i2c;
pub(crate) mod read;
pub mod serial;
use core::fmt;
pub trait AirQualitySensor<E: fmt::Debug> {
fn read(&mut self) -> Result<Reading, SensorError<E>>;
}
#[derive(Debug, Clone, Copy)]
pub struct Reading {
pm1: u16,
pm2_5: u16,
pm10: u16,
env_pm1: u16,
env_pm2_5: u16,
env_pm10: u16,
particles_0_3: u16,
particles_0_5: u16,
particles_1: u16,
particles_2_5: u16,
particles_5: u16,
particles_10: u16,
}
impl Reading {
pub fn pm1(&self) -> u16 {
self.pm1
}
pub fn pm2_5(&self) -> u16 {
self.pm2_5
}
pub fn pm10(&self) -> u16 {
self.pm10
}
pub fn env_pm1(&self) -> u16 {
self.env_pm1
}
pub fn env_pm2_5(&self) -> u16 {
self.env_pm2_5
}
pub fn env_pm10(&self) -> u16 {
self.env_pm10
}
pub fn particles_0_3(&self) -> u16 {
self.particles_0_3
}
pub fn particles_0_5(&self) -> u16 {
self.particles_0_5
}
pub fn particles_1(&self) -> u16 {
self.particles_1
}
pub fn particles_2_5(&self) -> u16 {
self.particles_2_5
}
pub fn particles_5(&self) -> u16 {
self.particles_5
}
pub fn particles_10(&self) -> u16 {
self.particles_10
}
}
#[derive(Debug)]
pub enum SensorError<E: fmt::Debug> {
BadMagic,
ChecksumMismatch,
ReadError(E),
}
impl<E: fmt::Debug> fmt::Display for SensorError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use SensorError::*;
match self {
BadMagic => f.write_str("Unable to find magic bytes at start of payload"),
ChecksumMismatch => f.write_str("Data read was corrupt"),
ReadError(error) => write!(f, "Read error: {:?}", error),
}
}
}
#[cfg(feature = "std")]
impl<E: fmt::Debug> std::error::Error for SensorError<E> {}
impl<E: fmt::Debug> From<E> for SensorError<E> {
fn from(error: E) -> Self {
SensorError::ReadError(error)
}
}