1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright Claudio Mattera 2022-2024.
//
// Distributed under the MIT License or the Apache 2.0 License at your option.
// See the accompanying files License-MIT.txt and License-Apache-2.0.txt, or
// online at
// https://opensource.org/licenses/MIT
// https://opensource.org/licenses/Apache-2.0

//! Data types and functions for BME280 sensor samples

#[cfg(feature = "uom")]
use uom::si::{
    f32::{
        Pressure as UomPressure, Ratio as UomHumidity, ThermodynamicTemperature as UomTemperature,
    },
    pressure::pascal,
    ratio::percent,
    thermodynamic_temperature::degree_celsius,
};

#[cfg(feature = "uom")]
/// Type for temperature measurements
pub type Temperature = UomTemperature;

#[cfg(feature = "uom")]
/// Type for pressure measurements
pub type Pressure = UomPressure;

#[cfg(feature = "uom")]
/// Type for humidity measurements
pub type Humidity = UomHumidity;

#[cfg(feature = "uom")]
/// Convert a raw value in Celsius to a temperature
pub(crate) fn temperature_from_celsius(raw: f32) -> Temperature {
    Temperature::new::<degree_celsius>(raw)
}

#[cfg(feature = "uom")]
/// Convert a raw value in Pascal to a pressure
pub(crate) fn pressure_from_pascal(raw: f32) -> Pressure {
    Pressure::new::<pascal>(raw)
}

#[cfg(feature = "uom")]
/// Convert a raw value to a humidity
pub(crate) fn humidity_from_number(raw: f32) -> Humidity {
    Humidity::new::<percent>(raw)
}

#[cfg(not(feature = "uom"))]
/// Type for pressure measurements
pub type Pressure = f32;

#[cfg(not(feature = "uom"))]
/// Type for temperature measurements
pub type Temperature = f32;

#[cfg(not(feature = "uom"))]
/// Type for temperature measurements
pub type Humidity = f32;

#[cfg(not(feature = "uom"))]
/// Convert a raw value in Celsius to a temperature
pub(crate) fn temperature_from_celsius(raw: f32) -> Temperature {
    raw
}

#[cfg(not(feature = "uom"))]
/// Convert a raw value in Pascal to a pressure
pub(crate) fn pressure_from_pascal(raw: f32) -> Pressure {
    raw
}

#[cfg(not(feature = "uom"))]
/// Convert a raw value to a humidity
pub(crate) fn humidity_from_number(raw: f32) -> Humidity {
    raw
}

/// A full sample: temperature, pressure and humidity
///
/// Disabled measures are `None`.
#[derive(Debug, Default)]
pub struct Sample {
    /// Temperature reading
    pub temperature: Option<Temperature>,

    /// Pressure reading
    pub pressure: Option<Pressure>,

    /// Humidity reading
    pub humidity: Option<Humidity>,
}

/// A full raw sample before compensation: temperature, pressure and humidity
///
/// Disabled measures are `None`.
#[derive(Debug, Default)]
#[allow(clippy::struct_field_names)]
pub(crate) struct RawSample {
    /// Temperature raw reading
    pub(crate) adc_t: Option<u32>,

    /// Pressure raw reading
    pub(crate) adc_p: Option<u32>,

    /// Humidity raw reading
    pub(crate) adc_h: Option<u16>,
}