diff --git a/src/calc.rs b/src/calc.rs index 75955be..82a885a 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -41,13 +41,23 @@ impl Calc { durval } - pub fn calc_temperature(calib: &CalibData, temp_adc: u32) -> (i16, i32) { + /// + /// * `calib` - Calibration data used during initalization + /// * `temp_adc` + /// * `temp_offset` - If set, the temperature t_fine will be increased by given + /// value in celsius. Temperature offset in Celsius, e.g. 4, -8, 1.25 + pub fn calc_temperature(calib: &CalibData, temp_adc: u32, temp_offset: Option) -> (i16, i32) { let var1: i64 = (temp_adc as (i64) >> 3) - ((calib.par_t1 as (i64)) << 1); let var2: i64 = (var1 * (calib.par_t2 as i64)) >> 11; let var3: i64 = ((var1 >> 1) * (var1 >> 1)) >> 12; let var3: i64 = (var3 * ((calib.par_t3 as i64) << 4)) >> 14; // TODO really assign here ? - let t_fine: i32 = (var2 + var3) as (i32); + let mut temp_offset = temp_offset.unwrap_or(0); + if temp_offset < 0 { + temp_offset = -1 * ((((temp_offset.abs() * 100) << 8) - 128) / 5); + } + + let t_fine: i32 = (var2 + var3) as i32 + temp_offset; let calc_temp: i16 = (((t_fine * 5) + 128) >> 8) as i16; (calc_temp, t_fine) } diff --git a/src/lib.rs b/src/lib.rs index 1233b38..97ace3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -591,6 +591,7 @@ where // Restore previous intended power mode self.power_mode = intended_power_mode; + self.tph_sett = tph_sett; Ok(()) } @@ -931,7 +932,7 @@ where data.status = data.status | buff[14] & BME680_HEAT_STAB_MSK; if data.status & BME680_NEW_DATA_MSK != 0 { - let (temp, t_fine) = Calc::calc_temperature(&self.calib, adc_temp); + let (temp, t_fine) = Calc::calc_temperature(&self.calib, adc_temp, self.tph_sett.temperature_offset); debug!( "adc_temp: {} adc_pres: {} adc_hum: {} adc_gas_res: {}, t_fine: {}", adc_temp, adc_pres, adc_hum, adc_gas_res, t_fine diff --git a/src/settings.rs b/src/settings.rs index d1768b5..2f14789 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -70,6 +70,8 @@ pub struct TphSett { pub os_pres: Option, /// Filter coefficient pub filter: Option, + /// If set, the temperature t_fine will be increased by the given value in celsius. + pub temperature_offset: Option, } impl Clone for TphSett { @@ -228,6 +230,12 @@ impl SettingsBuilder { self } + /// Temperature offset in Celsius, e.g. 4, -8, 1.25 + pub fn with_temperature_offset(mut self, offset: i32) -> SettingsBuilder { + self.sensor_settings.tph_sett.temperature_offset = Some(offset); + self + } + pub fn build(self) -> Settings { (self.sensor_settings, self.desired_settings) }