ariel_os_sensors/
measurement_unit.rs

1/// Represents a unit of measurement.
2///
3/// # For sensor driver implementors
4///
5/// Missing variants can be added when required.
6/// Please open an issue to discuss it.
7// Built upon https://doc.riot-os.org/phydat_8h_source.html
8// and https://bthome.io/format/#sensor-data
9// and https://www.iana.org/assignments/senml/senml.xhtml
10#[derive(Debug, Copy, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum MeasurementUnit {
13    /// [Acceleration *g*](https://en.wikipedia.org/wiki/G-force#Unit_and_measurement).
14    AccelG,
15    /// Ampere (A).
16    Ampere,
17    /// Becquerel (Bq).
18    Becquerel,
19    /// Logic boolean: `0` means `false` and `1` means `true`.
20    Bool,
21    /// Candela (cd).
22    Candela,
23    /// Degrees Celsius (°C).
24    Celsius,
25    /// Coulomb (C).
26    Coulomb,
27    /// Decibel (dB).
28    Decibel,
29    /// Decimal degrees (°).
30    DecimalDegree,
31    /// Degrees (°).
32    Degree,
33    /// Degrees per second (°/s).
34    DegreePerSecond,
35    /// Farad (F).
36    Farad,
37    // FIXME: Kilogram as well?
38    /// Gram (g).
39    Gram,
40    /// Gray (Gy).
41    Gray,
42    /// Henry (H).
43    Henry,
44    /// Hertz (Hz).
45    Hertz,
46    /// Joule (J).
47    Joule,
48    /// Katal (kat).
49    Katal,
50    /// Kelvin (K).
51    Kelvin,
52    /// Lumen (lm).
53    Lumen,
54    /// Lux (lx).
55    Lux,
56    /// Meter (m).
57    Meter,
58    /// Meter per second (m/s).
59    MeterPerSecond,
60    /// Mole (mol).
61    Mole,
62    /// Newton (N).
63    Newton,
64    /// Ohm (Ω).
65    Ohm,
66    /// Parts per million (ppm).
67    PartsPerMillion,
68    /// Pascal (Pa).
69    Pascal,
70    /// Percent (%).
71    Percent,
72    /// %RH.
73    PercentageRelativeHumidity,
74    /// Radian (rad).
75    Radian,
76    /// Second (s).
77    Second,
78    /// Siemens (S).
79    Siemens,
80    /// Sievert (Sv).
81    Sievert,
82    /// Steradian (sr).
83    Steradian,
84    /// Tesla (T).
85    Tesla,
86    /// Volt (V).
87    Volt,
88    /// Watt (W).
89    Watt,
90    /// Weber (Wb).
91    Weber,
92}
93
94macro_rules! provide_unit_fmt {
95    ($unit:expr, $f:expr) => {
96        match $unit {
97            Self::AccelG => write!($f, "g"),
98            Self::Ampere => write!($f, "A"),
99            Self::Becquerel => write!($f, "Bq"),
100            Self::Bool => write!($f, ""),
101            Self::Candela => write!($f, "cd"),
102            // As recommended by the Unicode Standard v16 (U+00B0 + U+0043)
103            Self::Celsius => write!($f, "°C"),
104            Self::Coulomb => write!($f, "C"),
105            Self::Decibel => write!($f, "dB"),
106            Self::DecimalDegree => write!($f, "°"),
107            Self::Degree => write!($f, "°"),
108            Self::DegreePerSecond => write!($f, "°/s"),
109            Self::Farad => write!($f, "F"),
110            Self::Gram => write!($f, "g"),
111            Self::Gray => write!($f, "Gy"),
112            Self::Henry => write!($f, "H"),
113            Self::Hertz => write!($f, "Hz"),
114            Self::Joule => write!($f, "J"),
115            Self::Katal => write!($f, "kat"),
116            Self::Kelvin => write!($f, "K"),
117            Self::Lumen => write!($f, "lm"),
118            Self::Lux => write!($f, "lx"),
119            Self::Meter => write!($f, "m"),
120            Self::MeterPerSecond => write!($f, "m/s"),
121            Self::Mole => write!($f, "mol"),
122            Self::Newton => write!($f, "N"),
123            Self::Ohm => write!($f, "Ω"),
124            Self::PartsPerMillion => write!($f, "ppm"),
125            Self::Pascal => write!($f, "Pa"),
126            Self::Percent => write!($f, "%"),
127            Self::PercentageRelativeHumidity => write!($f, "%RH"),
128            Self::Radian => write!($f, "rad"),
129            Self::Second => write!($f, "s"),
130            Self::Siemens => write!($f, "S"),
131            Self::Sievert => write!($f, "Sv"),
132            Self::Steradian => write!($f, "sr"),
133            Self::Tesla => write!($f, "T"),
134            Self::Volt => write!($f, "V"),
135            Self::Watt => write!($f, "W"),
136            Self::Weber => write!($f, "Wb"),
137        }
138    };
139}
140
141impl core::fmt::Display for MeasurementUnit {
142    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
143        provide_unit_fmt!(self, f)
144    }
145}
146
147#[cfg(feature = "defmt")]
148impl defmt::Format for MeasurementUnit {
149    fn format(&self, f: defmt::Formatter<'_>) {
150        use defmt::write;
151
152        provide_unit_fmt!(self, f);
153    }
154}