#11 Add optional temperature offset

This commit is contained in:
marcelbuesing 2018-12-14 21:22:21 +01:00
parent e9c553d7d7
commit faec5049e4
No known key found for this signature in database
GPG key ID: E51C4F8A2B7FF43E
3 changed files with 22 additions and 3 deletions

View file

@ -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<i32>) -> (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)
}

View file

@ -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

View file

@ -70,6 +70,8 @@ pub struct TphSett {
pub os_pres: Option<OversamplingSetting>,
/// Filter coefficient
pub filter: Option<IIRFilterSize>,
/// If set, the temperature t_fine will be increased by the given value in celsius.
pub temperature_offset: Option<i32>,
}
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)
}