diff --git a/examples/reading_temperature.rs b/examples/reading_temperature.rs index a4db46b..15460a0 100644 --- a/examples/reading_temperature.rs +++ b/examples/reading_temperature.rs @@ -20,31 +20,25 @@ fn main() -> result::Result< let i2c = I2cdev::new("/dev/i2c-1").unwrap(); - let mut dev = Bme680_dev::init(i2c, Delay {}, I2CAddress::Primary, 25)?; + let mut dev = Bme680_dev::init(i2c, Delay {}, I2CAddress::Primary)?; - let mut sensor_settings: SensorSettings = Default::default(); + let settings = SettingsBuilder::new() + .with_humidity_oversampling(OversamplingSetting::OS2x) + .with_pressure_oversampling(OversamplingSetting::OS4x) + .with_temperature_oversampling(OversamplingSetting::OS8x) + .with_temperature_filter(2) + .with_gas_measurement(Duration::from_millis(1500), 320, 25) + .with_run_gas(true) + .build(); - sensor_settings.tph_sett.os_hum = Some(OversamplingSetting::OS2x); - sensor_settings.tph_sett.os_pres = Some(OversamplingSetting::OS4x); - sensor_settings.tph_sett.os_temp = Some(OversamplingSetting::OS8x); - sensor_settings.tph_sett.filter = Some(2); - - sensor_settings.gas_sett.run_gas_measurement = true; - sensor_settings.gas_sett.heatr_dur = Some(Duration::from_millis(1500)); - sensor_settings.gas_sett.heatr_temp = Some(320); - - let settings_sel = DesiredSensorSettings::OST_SEL | DesiredSensorSettings::OSP_SEL - | DesiredSensorSettings::OSH_SEL - | DesiredSensorSettings::GAS_SENSOR_SEL; - - let profile_dur = dev.get_profile_dur(&sensor_settings)?; + let profile_dur = dev.get_profile_dur(&settings.0)?; info!("Duration {:?}", profile_dur); info!("Setting sensor settings"); - dev.set_sensor_settings(settings_sel, &sensor_settings)?; + dev.set_sensor_settings(settings)?; info!("Setting forced power modes"); dev.set_sensor_mode(PowerMode::ForcedMode)?; - let sensor_settings = dev.get_sensor_settings(settings_sel); + let sensor_settings = dev.get_sensor_settings(settings.1); info!("Sensor settings: {:?}", sensor_settings); loop { diff --git a/src/lib.rs b/src/lib.rs index 785fb81..dfc1ef5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ extern crate embedded_hal as hal; extern crate log; pub use self::settings::{DesiredSensorSettings, GasSett, OversamplingSetting, SensorSettings, - TphSett}; + Settings, SettingsBuilder, TphSett}; mod calc; mod settings; @@ -366,7 +366,6 @@ pub struct Bme680_dev { i2c: I2C, delay: D, dev_id: I2CAddress, - amb_temp: i8, calib: CalibData, // TODO remove ? as it may not reflect the state of the device tph_sett: TphSett, @@ -424,7 +423,6 @@ where mut i2c: I2C, mut delay: D, dev_id: I2CAddress, - ambient_temperature: i8, ) -> Result, ::Error, ::Error> { Bme680_dev::soft_reset(&mut i2c, &mut delay, dev_id)?; @@ -442,7 +440,6 @@ where delay: delay, dev_id: dev_id, calib: calib, - amb_temp: ambient_temperature, power_mode: PowerMode::ForcedMode, tph_sett: Default::default(), gas_sett: Default::default(), @@ -479,9 +476,9 @@ where pub fn set_sensor_settings( &mut self, - desired_settings: DesiredSensorSettings, - sensor_settings: &SensorSettings, + settings: Settings, ) -> Result<(), ::Error, ::Error> { + let (sensor_settings, desired_settings) = settings; let tph_sett = sensor_settings.tph_sett; let gas_sett = sensor_settings.gas_sett; @@ -851,7 +848,11 @@ where let reg: [(u8, u8); 2] = [ ( BME680_RES_HEAT0_ADDR, - Calc::calc_heater_res(&self.calib, self.amb_temp, gas_sett.heatr_temp.unwrap_or(0)), + Calc::calc_heater_res( + &self.calib, + gas_sett.ambient_temperature, + gas_sett.heatr_temp.unwrap_or(0), + ), ), ( BME680_GAS_WAIT0_ADDR, diff --git a/src/settings.rs b/src/settings.rs index 2f1ff69..07243b2 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -53,6 +53,7 @@ pub struct GasSett { pub heatr_temp: Option, /// Profile duration pub heatr_dur: Option, + pub ambient_temperature: i8, } impl Clone for GasSett { @@ -76,6 +77,7 @@ impl Clone for SensorSettings { } bitflags! { + #[derive(Default)] pub struct DesiredSensorSettings: u16 { /// To set temperature oversampling const OST_SEL = 1; @@ -97,3 +99,80 @@ bitflags! { const GAS_SENSOR_SEL = Self::GAS_MEAS_SEL.bits | Self::RUN_GAS_SEL.bits | Self::NBCONV_SEL.bits; } } + +pub struct SettingsBuilder { + desired_settings: DesiredSensorSettings, + sensor_settings: SensorSettings, +} + +pub type Settings = (SensorSettings, DesiredSensorSettings); + +impl SettingsBuilder { + pub fn new() -> SettingsBuilder { + SettingsBuilder { + desired_settings: Default::default(), + sensor_settings: Default::default(), + } + } + pub fn with_temperature_filter(mut self, filter: u8) -> SettingsBuilder { + self.sensor_settings.tph_sett.filter = Some(filter); + self.desired_settings |= DesiredSensorSettings::FILTER_SEL; + self + } + + pub fn with_humidity_control(mut self, heatr_control: u8) -> SettingsBuilder { + self.sensor_settings.gas_sett.heatr_ctrl = Some(heatr_control); + self.desired_settings |= DesiredSensorSettings::HCNTRL_SEL; + self + } + + pub fn with_temperature_oversampling( + mut self, + os_temp: OversamplingSetting, + ) -> SettingsBuilder { + self.sensor_settings.tph_sett.os_temp = Some(os_temp); + self.desired_settings |= DesiredSensorSettings::OST_SEL; + self + } + + pub fn with_pressure_oversampling(mut self, os_pres: OversamplingSetting) -> SettingsBuilder { + self.sensor_settings.tph_sett.os_pres = Some(os_pres); + self.desired_settings |= DesiredSensorSettings::OSP_SEL; + self + } + + pub fn with_humidity_oversampling(mut self, os_hum: OversamplingSetting) -> SettingsBuilder { + self.sensor_settings.tph_sett.os_hum = Some(os_hum); + self.desired_settings |= DesiredSensorSettings::OSH_SEL; + self + } + + pub fn with_gas_measurement( + mut self, + heatr_dur: Duration, + heatr_temp: u16, + ambient_temperature: i8, + ) -> SettingsBuilder { + self.sensor_settings.gas_sett.heatr_dur = Some(heatr_dur); + self.sensor_settings.gas_sett.heatr_temp = Some(heatr_temp); + self.sensor_settings.gas_sett.ambient_temperature = ambient_temperature; + self.desired_settings |= DesiredSensorSettings::RUN_GAS_SEL; + self + } + + pub fn with_nb_conv(mut self, nb_conv: u8) -> SettingsBuilder { + self.sensor_settings.gas_sett.nb_conv = nb_conv; + self.desired_settings |= DesiredSensorSettings::NBCONV_SEL; + self + } + + pub fn with_run_gas(mut self, run_gas: bool) -> SettingsBuilder { + self.sensor_settings.gas_sett.run_gas_measurement = run_gas; + self.desired_settings |= DesiredSensorSettings::RUN_GAS_SEL; + self + } + + pub fn build(self) -> Settings { + (self.sensor_settings, self.desired_settings) + } +}