2019-11-27 10:07:15 +00:00
|
|
|
use bitflags::bitflags;
|
2018-05-25 19:29:39 +00:00
|
|
|
use core::time::Duration;
|
|
|
|
|
2018-05-30 19:27:29 +00:00
|
|
|
/// Over-sampling settings
|
2018-05-25 19:29:39 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum OversamplingSetting {
|
|
|
|
OSNone = 0,
|
|
|
|
OS1x = 1,
|
|
|
|
OS2x = 2,
|
|
|
|
OS4x = 3,
|
|
|
|
OS8x = 4,
|
|
|
|
OS16x = 5,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OversamplingSetting {
|
2024-02-27 18:41:00 +00:00
|
|
|
pub fn from_u8(value: u8) -> OversamplingSetting {
|
|
|
|
match value {
|
2018-05-25 19:29:39 +00:00
|
|
|
0 => OversamplingSetting::OSNone,
|
|
|
|
1 => OversamplingSetting::OS1x,
|
|
|
|
2 => OversamplingSetting::OS2x,
|
|
|
|
3 => OversamplingSetting::OS4x,
|
|
|
|
4 => OversamplingSetting::OS8x,
|
|
|
|
5 => OversamplingSetting::OS16x,
|
2024-02-27 18:41:00 +00:00
|
|
|
_ => panic!("Unknown oversampling setting: {}", value),
|
2018-05-25 19:29:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 19:27:29 +00:00
|
|
|
/// IIR filter settings
|
2018-05-30 18:53:18 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum IIRFilterSize {
|
|
|
|
Size0 = 0,
|
|
|
|
Size1 = 1,
|
|
|
|
Size3 = 2,
|
|
|
|
Size7 = 3,
|
|
|
|
Size15 = 4,
|
|
|
|
Size31 = 5,
|
|
|
|
Size63 = 6,
|
|
|
|
Size127 = 7,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IIRFilterSize {
|
2024-02-27 18:41:00 +00:00
|
|
|
pub fn from_u8(value: u8) -> IIRFilterSize {
|
|
|
|
match value {
|
2018-05-30 18:53:18 +00:00
|
|
|
0 => IIRFilterSize::Size0,
|
|
|
|
1 => IIRFilterSize::Size1,
|
|
|
|
2 => IIRFilterSize::Size3,
|
|
|
|
3 => IIRFilterSize::Size7,
|
|
|
|
4 => IIRFilterSize::Size15,
|
|
|
|
5 => IIRFilterSize::Size31,
|
|
|
|
6 => IIRFilterSize::Size63,
|
|
|
|
7 => IIRFilterSize::Size127,
|
2024-02-27 18:41:00 +00:00
|
|
|
_ => panic!("Unknown IIRFilterSize: {}", value),
|
2018-05-30 18:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 19:27:29 +00:00
|
|
|
/// Temperature settings
|
2018-05-25 19:29:39 +00:00
|
|
|
#[derive(Debug, Default, Copy)]
|
|
|
|
#[repr(C)]
|
2024-02-27 18:41:00 +00:00
|
|
|
pub struct TemperatureSettings {
|
2018-05-30 19:27:29 +00:00
|
|
|
/// Humidity oversampling
|
2024-02-27 18:41:00 +00:00
|
|
|
pub humidity_oversampling: Option<OversamplingSetting>,
|
2018-05-30 19:27:29 +00:00
|
|
|
/// Temperature oversampling
|
2024-02-27 18:41:00 +00:00
|
|
|
pub temperature_oversampling: Option<OversamplingSetting>,
|
2018-05-30 19:27:29 +00:00
|
|
|
/// Pressure oversampling
|
2024-02-27 18:41:00 +00:00
|
|
|
pub pressure_oversampling: Option<OversamplingSetting>,
|
2018-05-30 19:27:29 +00:00
|
|
|
/// Filter coefficient
|
2024-02-27 18:41:00 +00:00
|
|
|
pub filter_size: Option<IIRFilterSize>,
|
2018-12-14 20:22:21 +00:00
|
|
|
/// If set, the temperature t_fine will be increased by the given value in celsius.
|
2018-12-19 23:06:30 +00:00
|
|
|
pub temperature_offset: Option<f32>,
|
2018-05-25 19:29:39 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
impl Clone for TemperatureSettings {
|
2018-05-25 19:29:39 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 19:27:29 +00:00
|
|
|
/// Gas measurement settings
|
2018-05-25 19:29:39 +00:00
|
|
|
#[derive(Debug, Default, Copy)]
|
|
|
|
#[repr(C)]
|
2024-02-27 18:41:00 +00:00
|
|
|
pub struct GasSettings {
|
|
|
|
/// nb_conv is used to select heater set-points of the sensor.
|
2018-05-25 19:29:39 +00:00
|
|
|
pub nb_conv: u8,
|
2018-05-28 17:56:57 +00:00
|
|
|
/// Heater control
|
2024-02-27 18:41:00 +00:00
|
|
|
pub heater_control: Option<u8>,
|
2018-05-28 17:56:57 +00:00
|
|
|
/// Enable measurement of gas, disabled by default
|
2024-02-27 18:41:00 +00:00
|
|
|
pub enable_gas_measurement: bool,
|
|
|
|
/// The heater temperature
|
|
|
|
pub heater_temperature: Option<u16>,
|
|
|
|
/// The Heating duration
|
|
|
|
pub heater_duration: Option<Duration>,
|
|
|
|
/// The ambient temperature.
|
2018-05-30 06:28:48 +00:00
|
|
|
pub ambient_temperature: i8,
|
2018-05-25 19:29:39 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
impl Clone for GasSettings {
|
2018-05-25 19:29:39 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 19:27:29 +00:00
|
|
|
/// Stores gas and temperature settings
|
2018-05-25 19:29:39 +00:00
|
|
|
#[derive(Debug, Default, Copy)]
|
|
|
|
pub struct SensorSettings {
|
|
|
|
/// Gas settings
|
2024-02-27 18:41:00 +00:00
|
|
|
pub gas_settings: GasSettings,
|
2018-05-25 19:29:39 +00:00
|
|
|
/// Temperature settings
|
2024-02-27 18:41:00 +00:00
|
|
|
pub temperature_settings: TemperatureSettings,
|
2018-05-25 19:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for SensorSettings {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitflags! {
|
2018-05-31 10:10:34 +00:00
|
|
|
/// Flags that determine what settings are to be set and what settings are to be read.
|
|
|
|
/// Use the `SettingsBuilder` to initialize an instance when setting the settings.
|
2018-05-30 06:28:48 +00:00
|
|
|
#[derive(Default)]
|
2018-05-25 19:29:39 +00:00
|
|
|
pub struct DesiredSensorSettings: u16 {
|
|
|
|
/// To set temperature oversampling
|
|
|
|
const OST_SEL = 1;
|
|
|
|
/// To set pressure oversampling.
|
|
|
|
const OSP_SEL = 2;
|
|
|
|
/// To set humidity oversampling.
|
|
|
|
const OSH_SEL = 4;
|
|
|
|
/// To set gas measurement setting.
|
|
|
|
const GAS_MEAS_SEL = 8;
|
|
|
|
/// To set filter setting.
|
2024-02-27 18:41:00 +00:00
|
|
|
const FILTER_SIZE_SEL = 16;
|
2018-05-25 19:29:39 +00:00
|
|
|
/// To set humidity control setting.
|
2024-02-27 18:41:00 +00:00
|
|
|
const HUMIDITY_CONTROL_SEL = 32;
|
2018-05-25 19:29:39 +00:00
|
|
|
/// To set run gas setting.
|
|
|
|
const RUN_GAS_SEL = 64;
|
|
|
|
/// To set NB conversion setting.
|
|
|
|
const NBCONV_SEL = 128;
|
|
|
|
/// To set all gas sensor related settings
|
|
|
|
const GAS_SENSOR_SEL = Self::GAS_MEAS_SEL.bits | Self::RUN_GAS_SEL.bits | Self::NBCONV_SEL.bits;
|
|
|
|
}
|
|
|
|
}
|
2018-05-30 06:28:48 +00:00
|
|
|
|
2018-05-30 19:27:29 +00:00
|
|
|
///
|
|
|
|
/// Builder to construct the desired settings
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
2018-12-19 23:06:30 +00:00
|
|
|
/// use bme680::{IIRFilterSize, OversamplingSetting, SettingsBuilder};
|
|
|
|
/// use std::time::Duration;
|
2021-05-06 20:22:53 +00:00
|
|
|
/// let settings = SettingsBuilder::default()
|
2018-05-30 19:27:29 +00:00
|
|
|
/// .with_humidity_oversampling(OversamplingSetting::OS2x)
|
|
|
|
/// .with_pressure_oversampling(OversamplingSetting::OS4x)
|
|
|
|
/// .with_temperature_oversampling(OversamplingSetting::OS8x)
|
|
|
|
/// .with_temperature_filter(IIRFilterSize::Size3)
|
|
|
|
/// .with_gas_measurement(Duration::from_millis(1500), 320, 25)
|
2018-12-19 23:06:30 +00:00
|
|
|
/// .with_temperature_offset(-4.25)
|
2018-05-30 19:27:29 +00:00
|
|
|
/// .with_run_gas(true)
|
|
|
|
/// .build();
|
|
|
|
/// ```
|
2021-05-06 20:22:53 +00:00
|
|
|
#[derive(Default)]
|
2018-05-30 06:28:48 +00:00
|
|
|
pub struct SettingsBuilder {
|
|
|
|
desired_settings: DesiredSensorSettings,
|
|
|
|
sensor_settings: SensorSettings,
|
|
|
|
}
|
|
|
|
|
2018-05-31 10:10:34 +00:00
|
|
|
/// Tuple of desired sensor settings flags and sensor settings
|
2018-05-30 06:28:48 +00:00
|
|
|
pub type Settings = (SensorSettings, DesiredSensorSettings);
|
|
|
|
|
|
|
|
impl SettingsBuilder {
|
2024-02-27 18:41:00 +00:00
|
|
|
/// Constructs a new instance of the SettingsBuilder.
|
2018-05-30 06:28:48 +00:00
|
|
|
pub fn new() -> SettingsBuilder {
|
2021-05-06 20:22:53 +00:00
|
|
|
SettingsBuilder::default()
|
2018-05-30 06:28:48 +00:00
|
|
|
}
|
2021-05-06 20:22:53 +00:00
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// With temperature filter.
|
|
|
|
pub fn with_temperature_filter(mut self, filter_size: IIRFilterSize) -> SettingsBuilder {
|
|
|
|
self.sensor_settings.temperature_settings.filter_size = Some(filter_size);
|
|
|
|
self.desired_settings |= DesiredSensorSettings::FILTER_SIZE_SEL;
|
2018-05-30 06:28:48 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// With gas heater control.
|
|
|
|
pub fn with_gas_heater_control(mut self, heater_control: u8) -> SettingsBuilder {
|
|
|
|
self.sensor_settings.gas_settings.heater_control = Some(heater_control);
|
|
|
|
self.desired_settings |= DesiredSensorSettings::HUMIDITY_CONTROL_SEL;
|
2018-05-30 06:28:48 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// With temperature oversampling
|
2018-05-30 06:28:48 +00:00
|
|
|
pub fn with_temperature_oversampling(
|
|
|
|
mut self,
|
2024-02-27 18:41:00 +00:00
|
|
|
temperature_oversampling: OversamplingSetting,
|
2018-05-30 06:28:48 +00:00
|
|
|
) -> SettingsBuilder {
|
2024-02-27 20:09:16 +00:00
|
|
|
self.sensor_settings
|
|
|
|
.temperature_settings
|
|
|
|
.temperature_oversampling = Some(temperature_oversampling);
|
2018-05-30 06:28:48 +00:00
|
|
|
self.desired_settings |= DesiredSensorSettings::OST_SEL;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// With pressure oversampling.
|
2024-02-27 20:09:16 +00:00
|
|
|
pub fn with_pressure_oversampling(
|
|
|
|
mut self,
|
|
|
|
pressure_oversampling: OversamplingSetting,
|
|
|
|
) -> SettingsBuilder {
|
|
|
|
self.sensor_settings
|
|
|
|
.temperature_settings
|
|
|
|
.pressure_oversampling = Some(pressure_oversampling);
|
2018-05-30 06:28:48 +00:00
|
|
|
self.desired_settings |= DesiredSensorSettings::OSP_SEL;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// With humidity oversampling.
|
2024-02-27 20:09:16 +00:00
|
|
|
pub fn with_humidity_oversampling(
|
|
|
|
mut self,
|
|
|
|
humidity_oversampling: OversamplingSetting,
|
|
|
|
) -> SettingsBuilder {
|
|
|
|
self.sensor_settings
|
|
|
|
.temperature_settings
|
|
|
|
.humidity_oversampling = Some(humidity_oversampling);
|
2018-05-30 06:28:48 +00:00
|
|
|
self.desired_settings |= DesiredSensorSettings::OSH_SEL;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// With gas measurement.
|
2018-05-30 06:28:48 +00:00
|
|
|
pub fn with_gas_measurement(
|
|
|
|
mut self,
|
2024-02-27 18:41:00 +00:00
|
|
|
heater_duration: Duration,
|
|
|
|
heater_temperature: u16,
|
2018-05-30 06:28:48 +00:00
|
|
|
ambient_temperature: i8,
|
|
|
|
) -> SettingsBuilder {
|
2024-02-27 18:41:00 +00:00
|
|
|
self.sensor_settings.gas_settings.heater_duration = Some(heater_duration);
|
|
|
|
self.sensor_settings.gas_settings.heater_temperature = Some(heater_temperature);
|
|
|
|
self.sensor_settings.gas_settings.ambient_temperature = ambient_temperature;
|
2018-06-06 16:53:24 +00:00
|
|
|
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
|
2018-05-30 06:28:48 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// With nb_conv.
|
2018-05-30 06:28:48 +00:00
|
|
|
pub fn with_nb_conv(mut self, nb_conv: u8) -> SettingsBuilder {
|
2024-02-27 18:41:00 +00:00
|
|
|
self.sensor_settings.gas_settings.nb_conv = nb_conv;
|
2018-06-06 16:53:24 +00:00
|
|
|
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
|
2018-05-30 06:28:48 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// With run gas.
|
2018-05-30 06:28:48 +00:00
|
|
|
pub fn with_run_gas(mut self, run_gas: bool) -> SettingsBuilder {
|
2024-02-27 18:41:00 +00:00
|
|
|
self.sensor_settings.gas_settings.enable_gas_measurement = run_gas;
|
2018-06-06 16:53:24 +00:00
|
|
|
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
|
2018-05-30 06:28:48 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// With temperature offset in Celsius, e.g. 4, -8, 1.25
|
2018-12-19 23:06:30 +00:00
|
|
|
pub fn with_temperature_offset(mut self, offset: f32) -> SettingsBuilder {
|
2024-02-27 18:41:00 +00:00
|
|
|
self.sensor_settings.temperature_settings.temperature_offset = Some(offset);
|
2018-12-14 20:22:21 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:41:00 +00:00
|
|
|
/// Builds the settings object
|
2018-05-30 06:28:48 +00:00
|
|
|
pub fn build(self) -> Settings {
|
|
|
|
(self.sensor_settings, self.desired_settings)
|
|
|
|
}
|
|
|
|
}
|