mod irradiance; mod resistance; mod temperature; mod voltage; mod volume_rate; mod pressure; #[cfg(feature = "defmt")] pub use defmt_impl::*; pub use irradiance::*; pub use resistance::*; pub use temperature::*; pub use voltage::*; pub use volume_rate::*; pub use pressure::*; use core::fmt::Display; use core::marker::PhantomData; use core::ops::{Add, Sub}; use num_traits::{FromPrimitive, Num, NumCast}; const DECA: u8 = 10; const DECI: u8 = 10; const HECTO: u8 = 100; const CENTI: u8 = 100; const KILO: u16 = 1_000; const MILLI: u16 = 1_000; const MEGA: u32 = 1_000_000; const MICRO: u32 = 1_000_000; const GIGA: u32 = 1_000_000_000; const NANO: u32 = 1_000_000_000; pub trait Quantity: Copy + PartialEq + PartialOrd + Add + Sub { fn value(self) -> V; fn symbol() -> &'static str; /// Returns a wrapper that implements [Display] and [defmt::Format] for [Quantity]s with core number values. /// /// - `rounding` - Sets the number of decimal places to round to when formatting (currently ignored with defmt). #[inline(always)] fn fmt(self, rounding: Option) -> Fmt { Fmt::new(self, rounding) } } pub trait Value: Num + Copy + PartialOrd + FromPrimitive + NumCast + Display {} impl Value for V where V: Num + Copy + PartialOrd + FromPrimitive + NumCast + Display {} #[derive(Copy, Clone)] pub struct Fmt> { quantity: Q, rounding: Option, _phantom: PhantomData, } impl> Fmt { #[inline(always)] fn new(quantity: Q, rounding: Option) -> Self { Self { quantity, rounding, _phantom: PhantomData, } } } impl> Display for Fmt { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self.rounding { Some(places) => { write!(f, "{:.prec$} {}", self.quantity.value(), Q::symbol(), prec = places) }, None => write!(f, "{} {}", self.quantity.value(), Q::symbol()), } } } #[cfg(feature = "defmt")] mod defmt_impl { use super::*; use defmt::{write, Format, Formatter}; impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); } } }