diff --git a/src/calc.rs b/src/calc.rs index 04b735b..96546f2 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -26,19 +26,18 @@ impl Calc { const NANOS_PER_MILLI: u64 = 1_000_000; let mut dur = (duration.as_secs() as u64 * MILLIS_PER_SEC) + (duration.subsec_nanos() as u64 / NANOS_PER_MILLI); - let durval = if dur as i32 >= 0xfc0i32 { + if dur as i32 >= 0xfc0i32 { 0xffu8 // Max duration } else { loop { - if !(dur as i32 > 0x3fi32) { + if dur as i32 <= 0x3fi32 { break; } dur = (dur as i32 / 4i32) as u64; factor = (factor as i32 + 1i32) as u8; } (dur as i32 + factor as i32 * 64i32) as u8 - }; - durval + } } /// @@ -72,14 +71,14 @@ impl Calc { pub fn calc_pressure(calib: &CalibData, t_fine: i32, pres_adc: u32) -> u32 { let mut var1: i32 = (t_fine >> 1) - 64000; - let mut var2: i32 = ((var1 >> 2) * (var1 >> 2) >> 11) * calib.par_p6 as i32 >> 2; - var2 = var2 + (var1 * (calib.par_p5 as i32) << 1); + let mut var2: i32 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * calib.par_p6 as i32) >> 2; + var2 += (var1 * (calib.par_p5 as i32)) << 1; var2 = (var2 >> 2i32) + ((calib.par_p4 as i32) << 16i32); - var1 = (((var1 >> 2i32) * (var1 >> 2i32) >> 13i32) * ((calib.par_p3 as i32) << 5i32) + var1 = (((((var1 >> 2i32) * (var1 >> 2i32)) >> 13i32) * ((calib.par_p3 as i32) << 5i32)) >> 3i32) - + (calib.par_p2 as i32 * var1 >> 1i32); - var1 = var1 >> 18i32; - var1 = (32768i32 + var1) * calib.par_p1 as i32 >> 15i32; + + ((calib.par_p2 as i32 * var1) >> 1i32); + var1 >>= 18i32; + var1 = ((32768i32 + var1) * calib.par_p1 as i32) >> 15i32; let mut pressure_comp: i32 = 1048576u32.wrapping_sub(pres_adc) as i32; pressure_comp = ((pressure_comp - (var2 >> 12i32)) as u32).wrapping_mul(3125u32) as i32; if pressure_comp >= 0x40000000i32 { @@ -87,35 +86,35 @@ impl Calc { } else { pressure_comp = ((pressure_comp << 1i32) as u32).wrapping_div(var1 as u32) as i32; } - var1 = calib.par_p9 as i32 * ((pressure_comp >> 3i32) * (pressure_comp >> 3i32) >> 13i32) + var1 = (calib.par_p9 as i32 + * (((pressure_comp >> 3i32) * (pressure_comp >> 3i32)) >> 13i32)) >> 12i32; - var2 = (pressure_comp >> 2i32) * calib.par_p8 as i32 >> 13i32; - let var3: i32 = (pressure_comp >> 8i32) + var2 = ((pressure_comp >> 2i32) * calib.par_p8 as i32) >> 13i32; + let var3: i32 = ((pressure_comp >> 8i32) * (pressure_comp >> 8i32) * (pressure_comp >> 8i32) - * calib.par_p10 as i32 + * calib.par_p10 as i32) >> 17i32; - pressure_comp = - pressure_comp + (var1 + var2 + var3 + ((calib.par_p7 as i32) << 7i32) >> 4i32); + pressure_comp += (var1 + var2 + var3 + ((calib.par_p7 as i32) << 7i32)) >> 4i32; pressure_comp as u32 } pub fn calc_humidity(calib: &CalibData, t_fine: i32, hum_adc: u16) -> u32 { - let temp_scaled: i32 = t_fine * 5i32 + 128i32 >> 8i32; + let temp_scaled: i32 = (t_fine * 5i32 + 128i32) >> 8i32; let var1: i32 = hum_adc as i32 - calib.par_h1 as i32 * 16i32 - - (temp_scaled * calib.par_h3 as i32 / 100i32 >> 1i32); - let var2: i32 = calib.par_h2 as i32 + - ((temp_scaled * calib.par_h3 as i32 / 100i32) >> 1i32); + let var2: i32 = (calib.par_h2 as i32 * (temp_scaled * calib.par_h4 as i32 / 100i32 - + (temp_scaled * (temp_scaled * calib.par_h5 as i32 / 100i32) >> 6i32) / 100i32 - + (1i32 << 14i32)) + + ((temp_scaled * (temp_scaled * calib.par_h5 as i32 / 100i32)) >> 6i32) / 100i32 + + (1i32 << 14i32))) >> 10i32; let var3: i32 = var1 * var2; let var4: i32 = (calib.par_h6 as i32) << 7i32; - let var4: i32 = var4 + temp_scaled * calib.par_h7 as i32 / 100i32 >> 4i32; - let var5: i32 = (var3 >> 14i32) * (var3 >> 14i32) >> 10i32; - let var6: i32 = var4 * var5 >> 1i32; - let mut calc_hum: i32 = (var3 + var6 >> 10i32) * 1000i32 >> 12i32; + let var4: i32 = (var4 + temp_scaled * calib.par_h7 as i32 / 100i32) >> 4i32; + let var5: i32 = ((var3 >> 14i32) * (var3 >> 14i32)) >> 10i32; + let var6: i32 = (var4 * var5) >> 1i32; + let mut calc_hum: i32 = (((var3 + var6) >> 10i32) * 1000i32) >> 12i32; if calc_hum > 100000i32 { calc_hum = 100000i32; } else if calc_hum < 0i32 { @@ -161,10 +160,11 @@ impl Calc { 250000u32, 125000u32, ]; - let var1: i64 = - (1340 + 5 * calib.range_sw_err as i64) * lookup_table1[gas_range as usize] as i64 >> 16; + let var1: i64 = ((1340 + 5 * calib.range_sw_err as i64) + * lookup_table1[gas_range as usize] as i64) + >> 16; let var2: u64 = (((gas_res_adc as i64) << 15) - 16777216 + var1) as u64; - let var3: i64 = lookup_table2[gas_range as usize] as i64 * var1 >> 9; + let var3: i64 = (lookup_table2[gas_range as usize] as i64 * var1) >> 9; let calc_gas_res: u32 = ((var3 + ((var2 as i64) >> 1i64)) / var2 as i64) as u32; calc_gas_res } diff --git a/src/lib.rs b/src/lib.rs index a5caadc..e246a8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -247,7 +247,7 @@ impl I2CAddress { match &self { I2CAddress::Primary => 0x76u8, I2CAddress::Secondary => 0x77u8, - I2CAddress::Other(addr) => addr.clone(), + I2CAddress::Other(addr) => *addr, } } } @@ -378,8 +378,7 @@ impl I2CUtil { { let mut buf = [0; 1]; - i2c.write(dev_id, &mut [reg_addr]) - .map_err(|e| Error::I2CWrite(e))?; + i2c.write(dev_id, &[reg_addr]).map_err(Error::I2CWrite)?; match i2c.read(dev_id, &mut buf) { Ok(()) => Ok(buf[0]), @@ -396,8 +395,7 @@ impl I2CUtil { where I2C: Read + Write, { - i2c.write(dev_id, &mut [reg_addr]) - .map_err(|e| Error::I2CWrite(e))?; + i2c.write(dev_id, &[reg_addr]).map_err(Error::I2CWrite)?; match i2c.read(dev_id, buf) { Ok(()) => Ok(()), @@ -459,7 +457,7 @@ where let tmp_buff: [u8; 2] = [BME680_SOFT_RESET_ADDR, BME680_SOFT_RESET_CMD]; i2c.write(dev_id.addr(), &tmp_buff) - .map_err(|e| Error::I2CWrite(e))?; + .map_err(Error::I2CWrite)?; delay.delay_ms(BME680_RESET_PERIOD); Ok(()) @@ -482,10 +480,10 @@ where let calib = Bme680::::get_calib_data::(&mut i2c, dev_id)?; debug!("Calib data {:?}", calib); let dev = Bme680 { - i2c: i2c, + i2c, delay: PhantomData, - dev_id: dev_id, - calib: calib, + dev_id, + calib, power_mode: PowerMode::ForcedMode, tph_sett: Default::default(), gas_sett: Default::default(), @@ -507,14 +505,14 @@ where } for (reg_addr, reg_data) in reg { - let tmp_buff: [u8; 2] = [reg_addr.clone(), reg_data.clone()]; + let tmp_buff: [u8; 2] = [*reg_addr, *reg_data]; debug!( "Setting register reg: {:?} tmp_buf: {:?}", reg_addr, tmp_buff ); self.i2c .write(self.dev_id.addr(), &tmp_buff) - .map_err(|e| Error::I2CWrite(e))?; + .map_err(Error::I2CWrite)?; } Ok(()) @@ -724,7 +722,7 @@ where if current_power_mode != PowerMode::SleepMode { // Set to sleep - tmp_pow_mode = tmp_pow_mode & !BME680_MODE_MSK; + tmp_pow_mode &= !BME680_MODE_MSK; debug!("Setting to sleep tmp_pow_mode: {}", tmp_pow_mode); self.bme680_set_regs(&[(BME680_CONF_T_P_MODE_ADDR, tmp_pow_mode)])?; delay.delay_ms(BME680_POLL_PERIOD_MS); @@ -813,7 +811,7 @@ where 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 = duration + sensor_settings.gas_sett.heatr_dur.expect("Heatrdur"); + duration += sensor_settings.gas_sett.heatr_dur.expect("Heatrdur"); } Ok(duration) } @@ -908,7 +906,7 @@ where ), ( BME680_GAS_WAIT0_ADDR, - Calc::calc_heater_dur(gas_sett.heatr_dur.unwrap_or(Duration::from_secs(0))), + Calc::calc_heater_dur(gas_sett.heatr_dur.unwrap_or_else(|| Duration::from_secs(0))), ), ]; @@ -917,9 +915,7 @@ where } fn get_gas_config(&mut self) -> Result::Error, ::Error> { - let mut gas_sett: GasSett = Default::default(); - - gas_sett.heatr_temp = Some(I2CUtil::read_byte( + let heatr_temp = Some(I2CUtil::read_byte( &mut self.i2c, self.dev_id.addr(), BME680_ADDR_SENS_CONF_START, @@ -930,7 +926,12 @@ where self.dev_id.addr(), BME680_ADDR_GAS_CONF_START, )? as u64; - gas_sett.heatr_dur = Some(Duration::from_millis(heatr_dur_ms)); + + let gas_sett = GasSett { + heatr_temp, + heatr_dur: Some(Duration::from_millis(heatr_dur_ms)), + ..Default::default() + }; Ok(gas_sett) } @@ -971,8 +972,8 @@ where ((buff[13] as u32).wrapping_mul(4) | (buff[14] as u32).wrapping_div(64)) as u16; let gas_range = buff[14] & BME680_GAS_RANGE_MSK; - data.status = data.status | buff[14] & BME680_GASM_VALID_MSK; - data.status = data.status | buff[14] & BME680_HEAT_STAB_MSK; + data.status |= buff[14] & BME680_GASM_VALID_MSK; + data.status |= buff[14] & BME680_HEAT_STAB_MSK; if data.status & BME680_NEW_DATA_MSK != 0 { let (temp, t_fine) = diff --git a/src/settings.rs b/src/settings.rs index 26fbd65..f921c6a 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -151,7 +151,7 @@ bitflags! { /// ``` /// use bme680::{IIRFilterSize, OversamplingSetting, SettingsBuilder}; /// use std::time::Duration; -/// let settings = SettingsBuilder::new() +/// let settings = SettingsBuilder::default() /// .with_humidity_oversampling(OversamplingSetting::OS2x) /// .with_pressure_oversampling(OversamplingSetting::OS4x) /// .with_temperature_oversampling(OversamplingSetting::OS8x) @@ -161,6 +161,7 @@ bitflags! { /// .with_run_gas(true) /// .build(); /// ``` +#[derive(Default)] pub struct SettingsBuilder { desired_settings: DesiredSensorSettings, sensor_settings: SensorSettings, @@ -171,11 +172,9 @@ pub type Settings = (SensorSettings, DesiredSensorSettings); impl SettingsBuilder { pub fn new() -> SettingsBuilder { - SettingsBuilder { - desired_settings: Default::default(), - sensor_settings: Default::default(), - } + 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;