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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Provides a [`BmeSensor`] implementation for the BME680 sensor.
//!
//! This module is only available if the **use-bme680** feature is enabled.
//!
//! The implementation is based on the
//! [bme680 crate](https://crates.io/crates/bme680).
//!
//! # Example
//!
//! ```ignore
//! use bme680::{Bme680, I2CAddress};
//! use bsec::bme::bme680::Bme680Sensor;
//! use linux_embedded_hal::{Delay, I2cdev};
//!
//! let i2c = I2cdev::new("/dev/i2c")?;
//! let bme680 = Bme680::init(i2c, Delay {}, I2CAddress::Primary)?;
//! let sensor = Bme680SensorBuilder::new(bme680)
//!     .with_initial_ambient_temp_celsius(25.)
//!     .with_temp_offset_celsius(6.3)
//!     .build();
//! ```

use std::fmt::Debug;
use std::time::{Duration, Instant};

use crate::bme::{BmeSensor, BmeSettingsHandle};
use crate::{Input, InputKind};
use bme680::{Bme680, OversamplingSetting, PowerMode, SettingsBuilder};
use embedded_hal::blocking::{delay::DelayMs, i2c};

/// Builder for [`Bme680Sensor`] instances.
pub struct Bme680SensorBuilder<I2C, D>
where
    I2C: i2c::Read + i2c::Write,
    D: DelayMs<u8>,
{
    bme680: Bme680<I2C, D>,
    delay: D,
    initial_ambient_temp_celsius: f32,
    temp_offset_celsius: f32,
    disable_baseline_tracker: Option<f32>,
}

impl<I2C, D> Bme680SensorBuilder<I2C, D>
where
    I2C: i2c::Read + i2c::Write,
    D: DelayMs<u8>,
{
    /// Create a new builder instance with `bme680`.
    ///
    /// Uses the following defaults:
    ///
    /// * `initial_ambient_temp_celsius = 20.0`
    /// * `temp_offset_celsius = 0.0`
    /// * `disable_baseline_tracker = false`
    pub fn new(bme680: Bme680<I2C, D>, delay: D) -> Self {
        Self {
            bme680,
            delay,
            initial_ambient_temp_celsius: 20.,
            temp_offset_celsius: 0.,
            disable_baseline_tracker: None,
        }
    }

    /// Sets the initial ambient temperature in Celsius to `value`.
    pub fn initial_ambient_temp_celsius(mut self, value: f32) -> Self {
        self.initial_ambient_temp_celsius = value;
        self
    }

    /// Sets the temperature offset in Celsius to `value`.
    pub fn temp_offset_celsius(mut self, value: f32) -> Self {
        self.temp_offset_celsius = value;
        self
    }

    /// Set the status of the BSEC baseline tracker to `status`.
    pub fn disable_baseline_tracker(mut self, status: Option<f32>) -> Self {
        self.disable_baseline_tracker = status;
        self
    }

    /// Consume current configuration and create [`Bme680Sensor`] instance.
    pub fn build(self) -> Bme680Sensor<I2C, D> {
        Bme680Sensor::new(
            self.bme680,
            self.delay,
            self.initial_ambient_temp_celsius,
            self.temp_offset_celsius,
            self.disable_baseline_tracker,
        )
    }
}

/// Implementation of the [`BmeSensor`] trait for the BME680 sensor.
///
/// For the gas resistance measurement, the last reading of the temperature
/// sensor will be used as ambient temperature.
///
/// Use [`Bme680SensorBuilder`] to create instances of this struct.
pub struct Bme680Sensor<I2C, D>
where
    I2C: i2c::Read + i2c::Write,
    D: DelayMs<u8>,
{
    bme680: Bme680<I2C, D>,
    delay: D,
    measurement_available_after: Option<Instant>,
    last_measured_temp_celsius: f32,
    temp_offset_celsius: f32,
    disable_baseline_tracker: Option<f32>,
}

impl<I2C, D> Bme680Sensor<I2C, D>
where
    I2C: i2c::Read + i2c::Write,
    D: DelayMs<u8>,
{
    /// Create a new instance reading measurement from `bme680`.
    ///
    /// * `initial_ambient_temp_celsius` sets the ambient temperature for
    ///   the first gas resistance reading. All subsequent readings use the
    ///   respective last temperature reading.
    /// * `temp_offset_celsius` sets the temperature offset for the sensor to
    ///   its surroundings due to heat sources in proximity. The value will be
    ///   provided to the BSEC algorithm as [`crate::InputKind::HeatSource`].
    /// * 'disable_baseline_tracker` controls the BSEC baseline tracker if set.
    ///   The value will be provided to the BSEC algorithm as
    ///   [`crate::InputKind::DisableBaselineTracker`]. The the *Bosch BSEC*
    ///   documentation for details.
    fn new(
        bme680: Bme680<I2C, D>,
        delay: D,
        initial_ambient_temp_celsius: f32,
        temp_offset_celsius: f32,
        disable_baseline_tracker: Option<f32>,
    ) -> Self {
        Bme680Sensor {
            bme680,
            delay,
            measurement_available_after: None,
            last_measured_temp_celsius: initial_ambient_temp_celsius,
            temp_offset_celsius,
            disable_baseline_tracker,
        }
    }
}

impl<I2C, D> BmeSensor for Bme680Sensor<I2C, D>
where
    D: DelayMs<u8>,
    I2C: i2c::Read + i2c::Write,
    <I2C as i2c::Read>::Error: Debug,
    <I2C as i2c::Write>::Error: Debug,
{
    type Error = bme680::Error<<I2C as i2c::Read>::Error, <I2C as i2c::Write>::Error>;

    fn start_measurement(&mut self, settings: &BmeSettingsHandle) -> Result<Duration, Self::Error> {
        let settings = SettingsBuilder::new()
            .with_humidity_oversampling(OversamplingSetting::from_u8(
                settings.humidity_oversampling(),
            ))
            .with_temperature_oversampling(OversamplingSetting::from_u8(
                settings.temperature_oversampling(),
            ))
            .with_pressure_oversampling(OversamplingSetting::from_u8(
                settings.pressure_oversampling(),
            ))
            .with_run_gas(settings.run_gas())
            .with_gas_measurement(
                Duration::from_millis(settings.heating_duration().into()),
                settings.heater_temperature(),
                self.last_measured_temp_celsius.round() as i8,
            )
            .build();

        self.bme680.set_sensor_settings(&mut self.delay, settings)?;
        let profile_duration = self.bme680.get_profile_dur(&settings.0)?;
        self.bme680
            .set_sensor_mode(&mut self.delay, PowerMode::ForcedMode)?;
        self.measurement_available_after = Some(Instant::now() + profile_duration);
        Ok(profile_duration)
    }

    fn get_measurement(&mut self) -> nb::Result<Vec<Input>, Self::Error> {
        match self.measurement_available_after {
            None => panic!("must call start_measurement before get_measurement"),
            Some(instant) if instant > Instant::now() => Err(nb::Error::WouldBlock),
            _ => {
                let (data, _state) = self.bme680.get_sensor_data(&mut self.delay)?;
                self.last_measured_temp_celsius = data.temperature_celsius();
                let mut bsec_inputs = Vec::with_capacity(6);
                bsec_inputs.extend(&[
                    Input {
                        sensor: InputKind::Temperature,
                        signal: data.temperature_celsius(),
                    },
                    Input {
                        sensor: InputKind::Pressure,
                        signal: data.pressure_hpa(),
                    },
                    Input {
                        sensor: InputKind::Humidity,
                        signal: data.humidity_percent(),
                    },
                    Input {
                        sensor: InputKind::GasResistor,
                        signal: data.gas_resistance_ohm() as f32,
                    },
                    Input {
                        sensor: InputKind::HeatSource,
                        signal: self.temp_offset_celsius,
                    },
                ]);
                if let Some(status) = self.disable_baseline_tracker {
                    bsec_inputs.push(Input {
                        sensor: InputKind::DisableBaselineTracker,
                        signal: status,
                    });
                }
                Ok(bsec_inputs)
            }
        }
    }
}