From b5d2f80e150344a9f21e38251e072df45fb8df7f Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Tue, 27 Feb 2024 20:41:00 +0200 Subject: [PATCH] refactor: fix settings variable names --- src/lib.rs | 76 +++++++++++++++++------------------ src/settings.rs | 104 ++++++++++++++++++++++++++---------------------- 2 files changed, 95 insertions(+), 85 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 04b02dd..c392778 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,8 +65,8 @@ #![forbid(unsafe_code)] pub use self::settings::{ - DesiredSensorSettings, GasSett, IIRFilterSize, OversamplingSetting, SensorSettings, Settings, - SettingsBuilder, TphSett, + DesiredSensorSettings, GasSettings, IIRFilterSize, OversamplingSetting, SensorSettings, Settings, + SettingsBuilder, TemperatureSettings, }; mod calculation; @@ -433,10 +433,10 @@ impl Bme680 settings: Settings, ) -> Result<(), Bme680Error> { let (sensor_settings, desired_settings) = settings; - let tph_sett = sensor_settings.tph_sett; - let gas_sett = sensor_settings.gas_sett; + let tph_sett = sensor_settings.temperature_settings; + let gas_sett = sensor_settings.gas_settings; - self.temperature_offset = sensor_settings.tph_sett.temperature_offset.unwrap_or(0f32); + self.temperature_offset = sensor_settings.temperature_settings.temperature_offset.unwrap_or(0f32); let mut reg: [(u8, u8); BME680_REG_BUFFER_LENGTH] = [(0, 0); BME680_REG_BUFFER_LENGTH]; let intended_power_mode = self.power_mode; @@ -451,22 +451,22 @@ impl Bme680 let mut element_index = 0; // Selecting the filter - if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) { + if desired_settings.contains(DesiredSensorSettings::FILTER_SIZE_SEL) { let mut data = I2CUtility::read_byte(&mut self.i2c_bus_handle, self.device_address.addr(), BME680_CONF_ODR_FILT_ADDR)?; debug!("FILTER_SEL: true"); data = (data as i32 & !0x1ci32 - | (tph_sett.filter.unwrap_or(IIRFilterSize::Size0) as i32) << 2i32 & 0x1ci32) + | (tph_sett.filter_size.unwrap_or(IIRFilterSize::Size0) as i32) << 2i32 & 0x1ci32) as u8; reg[element_index] = (BME680_CONF_ODR_FILT_ADDR, data); element_index += 1; } - if desired_settings.contains(DesiredSensorSettings::HCNTRL_SEL) { + if desired_settings.contains(DesiredSensorSettings::HUMIDITY_CONTROL_SEL) { debug!("HCNTRL_SEL: true"); let gas_sett_heatr_ctrl = - boundary_check_u8(gas_sett.heatr_ctrl, "GasSett.heatr_ctrl", 0x0u8, 0x8u8)?; + boundary_check_u8(gas_sett.heater_control, "GasSett.heatr_ctrl", 0x0u8, 0x8u8)?; let mut data = I2CUtility::read_byte( &mut self.i2c_bus_handle, self.device_address.addr(), @@ -487,7 +487,7 @@ impl Bme680 if desired_settings.contains(DesiredSensorSettings::OST_SEL) { debug!("OST_SEL: true"); let tph_sett_os_temp = boundary_check_u8( - tph_sett.os_temp.map(|x| x as u8), + tph_sett.temperature_oversampling.map(|x| x as u8), "TphSett.os_temp", 0, 5, @@ -497,7 +497,7 @@ impl Bme680 if desired_settings.contains(DesiredSensorSettings::OSP_SEL) { debug!("OSP_SEL: true"); - let tph_sett_os_pres = tph_sett.os_temp.unwrap_or(OversamplingSetting::OS1x); + let tph_sett_os_pres = tph_sett.temperature_oversampling.unwrap_or(OversamplingSetting::OS1x); data = (data as i32 & !0x1ci32 | (tph_sett_os_pres as i32) << 2i32 & 0x1ci32) as u8; } reg[element_index] = (BME680_CONF_T_P_MODE_ADDR, data); @@ -508,7 +508,7 @@ impl Bme680 if desired_settings.contains(DesiredSensorSettings::OSH_SEL) { debug!("OSH_SEL: true"); let tph_sett_os_hum = - boundary_check_u8(tph_sett.os_hum.map(|x| x as u8), "TphSett.os_hum", 0, 5)?; + boundary_check_u8(tph_sett.humidity_oversampling.map(|x| x as u8), "TphSett.os_hum", 0, 5)?; let mut data = I2CUtility::read_byte(&mut self.i2c_bus_handle, self.device_address.addr(), BME680_CONF_OS_H_ADDR)?; data = (data as i32 & !0x7i32 | tph_sett_os_hum as i32 & 0x7i32) as u8; @@ -529,7 +529,7 @@ impl Bme680 if desired_settings.contains(DesiredSensorSettings::RUN_GAS_SEL) { debug!("RUN_GAS_SEL: true"); data = (data as i32 & !0x10i32 - | (gas_sett.run_gas_measurement as i32) << 4i32 & 0x10i32) + | (gas_sett.enable_gas_measurement as i32) << 4i32 & 0x10i32) as u8; } @@ -567,11 +567,11 @@ impl Bme680 I2CUtility::read_bytes(&mut self.i2c_bus_handle, self.device_address.addr(), reg_addr, &mut data_array)?; if desired_settings.contains(DesiredSensorSettings::GAS_MEAS_SEL) { - sensor_settings.gas_sett = self.get_gas_config()?; + sensor_settings.gas_settings = self.get_gas_config()?; } - if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) { - sensor_settings.tph_sett.filter = Some(IIRFilterSize::from_u8( + if desired_settings.contains(DesiredSensorSettings::FILTER_SIZE_SEL) { + sensor_settings.temperature_settings.filter_size = Some(IIRFilterSize::from_u8( ((data_array[5usize] as i32 & 0x1ci32) >> 2i32) as u8, )); } @@ -581,24 +581,24 @@ impl Bme680 { let os_temp: u8 = ((data_array[4usize] as i32 & 0xe0i32) >> 5i32) as u8; let os_pres: u8 = ((data_array[4usize] as i32 & 0x1ci32) >> 2i32) as u8; - sensor_settings.tph_sett.os_temp = Some(OversamplingSetting::from_u8(os_temp)); - sensor_settings.tph_sett.os_pres = Some(OversamplingSetting::from_u8(os_pres)); + sensor_settings.temperature_settings.temperature_oversampling = Some(OversamplingSetting::from_u8(os_temp)); + sensor_settings.temperature_settings.pressure_oversampling = Some(OversamplingSetting::from_u8(os_pres)); } if desired_settings.contains(DesiredSensorSettings::OSH_SEL) { let os_hum: u8 = (data_array[2usize] as i32 & 0x7i32) as u8; - sensor_settings.tph_sett.os_hum = Some(OversamplingSetting::from_u8(os_hum)); + sensor_settings.temperature_settings.humidity_oversampling = Some(OversamplingSetting::from_u8(os_hum)); } - if desired_settings.contains(DesiredSensorSettings::HCNTRL_SEL) { - sensor_settings.gas_sett.heatr_ctrl = Some((data_array[0usize] as i32 & 0x8i32) as u8); + if desired_settings.contains(DesiredSensorSettings::HUMIDITY_CONTROL_SEL) { + sensor_settings.gas_settings.heater_control = Some((data_array[0usize] as i32 & 0x8i32) as u8); } if desired_settings .contains(DesiredSensorSettings::RUN_GAS_SEL | DesiredSensorSettings::NBCONV_SEL) { - sensor_settings.gas_sett.nb_conv = (data_array[1usize] as i32 & 0xfi32) as u8; - sensor_settings.gas_sett.run_gas_measurement = + sensor_settings.gas_settings.nb_conv = (data_array[1usize] as i32 & 0xfi32) as u8; + sensor_settings.gas_settings.enable_gas_measurement = ((data_array[1usize] as i32 & 0x10i32) >> 4i32) == 0; } @@ -665,20 +665,20 @@ impl Bme680 ) -> Result { let os_to_meas_cycles: [u8; 6] = [0u8, 1u8, 2u8, 4u8, 8u8, 16u8]; let mut meas_cycles = os_to_meas_cycles[sensor_settings - .tph_sett - .os_temp + .temperature_settings + .temperature_oversampling .unwrap_or(OversamplingSetting::OSNone) as usize] as u32; meas_cycles = meas_cycles.wrapping_add( os_to_meas_cycles[sensor_settings - .tph_sett - .os_pres + .temperature_settings + .pressure_oversampling .unwrap_or(OversamplingSetting::OSNone) as usize] as u32, ); meas_cycles = meas_cycles.wrapping_add( os_to_meas_cycles[sensor_settings - .tph_sett - .os_hum + .temperature_settings + .humidity_oversampling .unwrap_or(OversamplingSetting::OSNone) as usize] as u32, ); let mut tph_dur = meas_cycles.wrapping_mul(1963u32); @@ -688,8 +688,8 @@ impl Bme680 tph_dur = tph_dur.wrapping_div(1000u32); tph_dur = tph_dur.wrapping_add(1u32); let mut duration = Duration::from_millis(tph_dur as u64); - if sensor_settings.gas_sett.run_gas_measurement { - duration += sensor_settings.gas_sett.heatr_dur.unwrap_or(Duration::default()); + if sensor_settings.gas_settings.enable_gas_measurement { + duration += sensor_settings.gas_settings.heater_duration.unwrap_or(Duration::default()); } Ok(duration) } @@ -765,7 +765,7 @@ impl Bme680 fn set_gas_config( &mut self, - gas_sett: GasSett, + gas_sett: GasSettings, ) -> Result<(), Bme680Error> { if self.power_mode != PowerMode::ForcedMode { return Err(Bme680Error::DefinePwrMode); @@ -777,19 +777,19 @@ impl Bme680 Calculation::heater_resistance( &self.calibration_data, gas_sett.ambient_temperature, - gas_sett.heatr_temp.unwrap_or(0), + gas_sett.heater_temperature.unwrap_or(0), ), ), ( BME680_GAS_WAIT0_ADDR, - Calculation::heater_duration(gas_sett.heatr_dur.unwrap_or_else(|| Duration::from_secs(0))), + Calculation::heater_duration(gas_sett.heater_duration.unwrap_or_else(|| Duration::from_secs(0))), ), ]; self.bme680_set_regs(®) } - fn get_gas_config(&mut self) -> Result { + fn get_gas_config(&mut self) -> Result { let heatr_temp = Some(I2CUtility::read_byte( &mut self.i2c_bus_handle, self.device_address.addr(), @@ -802,9 +802,9 @@ impl Bme680 BME680_ADDR_GAS_CONF_START, )? as u64; - let gas_sett = GasSett { - heatr_temp, - heatr_dur: Some(Duration::from_millis(heatr_dur_ms)), + let gas_sett = GasSettings { + heater_temperature: heatr_temp, + heater_duration: Some(Duration::from_millis(heatr_dur_ms)), ..Default::default() }; diff --git a/src/settings.rs b/src/settings.rs index f921c6a..d45432e 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -14,16 +14,15 @@ pub enum OversamplingSetting { } impl OversamplingSetting { - // TODO replace with TryFrom once stabilized - pub fn from_u8(os: u8) -> OversamplingSetting { - match os { + pub fn from_u8(value: u8) -> OversamplingSetting { + match value { 0 => OversamplingSetting::OSNone, 1 => OversamplingSetting::OS1x, 2 => OversamplingSetting::OS2x, 3 => OversamplingSetting::OS4x, 4 => OversamplingSetting::OS8x, 5 => OversamplingSetting::OS16x, - _ => panic!("Unknown oversampling setting: {}", os), + _ => panic!("Unknown oversampling setting: {}", value), } } } @@ -43,9 +42,8 @@ pub enum IIRFilterSize { } impl IIRFilterSize { - // TODO replace with TryFrom once stabilized - pub fn from_u8(os: u8) -> IIRFilterSize { - match os { + pub fn from_u8(value: u8) -> IIRFilterSize { + match value { 0 => IIRFilterSize::Size0, 1 => IIRFilterSize::Size1, 2 => IIRFilterSize::Size3, @@ -54,7 +52,7 @@ impl IIRFilterSize { 5 => IIRFilterSize::Size31, 6 => IIRFilterSize::Size63, 7 => IIRFilterSize::Size127, - _ => panic!("Unknown IIRFilterSize: {}", os), + _ => panic!("Unknown IIRFilterSize: {}", value), } } } @@ -62,20 +60,20 @@ impl IIRFilterSize { /// Temperature settings #[derive(Debug, Default, Copy)] #[repr(C)] -pub struct TphSett { +pub struct TemperatureSettings { /// Humidity oversampling - pub os_hum: Option, + pub humidity_oversampling: Option, /// Temperature oversampling - pub os_temp: Option, + pub temperature_oversampling: Option, /// Pressure oversampling - pub os_pres: Option, + pub pressure_oversampling: Option, /// Filter coefficient - pub filter: Option, + pub filter_size: Option, /// If set, the temperature t_fine will be increased by the given value in celsius. pub temperature_offset: Option, } -impl Clone for TphSett { +impl Clone for TemperatureSettings { fn clone(&self) -> Self { *self } @@ -84,20 +82,22 @@ impl Clone for TphSett { /// Gas measurement settings #[derive(Debug, Default, Copy)] #[repr(C)] -pub struct GasSett { +pub struct GasSettings { + /// nb_conv is used to select heater set-points of the sensor. pub nb_conv: u8, /// Heater control - pub heatr_ctrl: Option, + pub heater_control: Option, /// Enable measurement of gas, disabled by default - pub run_gas_measurement: bool, - /// Heater temperature - pub heatr_temp: Option, - /// Profile duration - pub heatr_dur: Option, + pub enable_gas_measurement: bool, + /// The heater temperature + pub heater_temperature: Option, + /// The Heating duration + pub heater_duration: Option, + /// The ambient temperature. pub ambient_temperature: i8, } -impl Clone for GasSett { +impl Clone for GasSettings { fn clone(&self) -> Self { *self } @@ -107,9 +107,9 @@ impl Clone for GasSett { #[derive(Debug, Default, Copy)] pub struct SensorSettings { /// Gas settings - pub gas_sett: GasSett, + pub gas_settings: GasSettings, /// Temperature settings - pub tph_sett: TphSett, + pub temperature_settings: TemperatureSettings, } impl Clone for SensorSettings { @@ -132,9 +132,9 @@ bitflags! { /// To set gas measurement setting. const GAS_MEAS_SEL = 8; /// To set filter setting. - const FILTER_SEL = 16; + const FILTER_SIZE_SEL = 16; /// To set humidity control setting. - const HCNTRL_SEL = 32; + const HUMIDITY_CONTROL_SEL = 32; /// To set run gas setting. const RUN_GAS_SEL = 64; /// To set NB conversion setting. @@ -171,74 +171,84 @@ pub struct SettingsBuilder { pub type Settings = (SensorSettings, DesiredSensorSettings); impl SettingsBuilder { + /// Constructs a new instance of the SettingsBuilder. pub fn new() -> SettingsBuilder { SettingsBuilder::default() } - pub fn with_temperature_filter(mut self, filter: IIRFilterSize) -> SettingsBuilder { - self.sensor_settings.tph_sett.filter = Some(filter); - self.desired_settings |= DesiredSensorSettings::FILTER_SEL; + /// 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; 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; + /// 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; self } + /// With temperature oversampling pub fn with_temperature_oversampling( mut self, - os_temp: OversamplingSetting, + temperature_oversampling: OversamplingSetting, ) -> SettingsBuilder { - self.sensor_settings.tph_sett.os_temp = Some(os_temp); + self.sensor_settings.temperature_settings.temperature_oversampling = Some(temperature_oversampling); 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); + /// With pressure oversampling. + pub fn with_pressure_oversampling(mut self, pressure_oversampling: OversamplingSetting) -> SettingsBuilder { + self.sensor_settings.temperature_settings.pressure_oversampling = Some(pressure_oversampling); 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); + /// With humidity oversampling. + pub fn with_humidity_oversampling(mut self, humidity_oversampling: OversamplingSetting) -> SettingsBuilder { + self.sensor_settings.temperature_settings.humidity_oversampling = Some(humidity_oversampling); self.desired_settings |= DesiredSensorSettings::OSH_SEL; self } + /// With gas measurement. pub fn with_gas_measurement( mut self, - heatr_dur: Duration, - heatr_temp: u16, + heater_duration: Duration, + heater_temperature: 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.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; self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL; self } + /// With nb_conv. pub fn with_nb_conv(mut self, nb_conv: u8) -> SettingsBuilder { - self.sensor_settings.gas_sett.nb_conv = nb_conv; + self.sensor_settings.gas_settings.nb_conv = nb_conv; self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL; self } + /// With run gas. pub fn with_run_gas(mut self, run_gas: bool) -> SettingsBuilder { - self.sensor_settings.gas_sett.run_gas_measurement = run_gas; + self.sensor_settings.gas_settings.enable_gas_measurement = run_gas; self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL; self } - /// Temperature offset in Celsius, e.g. 4, -8, 1.25 + /// With temperature offset in Celsius, e.g. 4, -8, 1.25 pub fn with_temperature_offset(mut self, offset: f32) -> SettingsBuilder { - self.sensor_settings.tph_sett.temperature_offset = Some(offset); + self.sensor_settings.temperature_settings.temperature_offset = Some(offset); self } + /// Builds the settings object pub fn build(self) -> Settings { (self.sensor_settings, self.desired_settings) }