From 38bfe30accc7d5b604d8778ba9c5994f7e0e5e90 Mon Sep 17 00:00:00 2001 From: marcelbuesing Date: Thu, 20 Dec 2018 00:06:30 +0100 Subject: [PATCH] Fix temperature offset to be of type f32, closes #11 --- .travis.yml | 2 +- examples/reading_temperature.rs | 1 + src/calc.rs | 13 +++++++------ src/lib.rs | 2 ++ src/settings.rs | 7 +++++-- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 92dd10b..d180731 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ rust: - nightly script: - cargo build --verbose - - cargo test --verbose --no-run + - cargo test --verbose matrix: allow_failures: - rust: nightly diff --git a/examples/reading_temperature.rs b/examples/reading_temperature.rs index 9f4447b..c7f2ac8 100644 --- a/examples/reading_temperature.rs +++ b/examples/reading_temperature.rs @@ -30,6 +30,7 @@ fn main( .with_temperature_oversampling(OversamplingSetting::OS8x) .with_temperature_filter(IIRFilterSize::Size3) .with_gas_measurement(Duration::from_millis(1500), 320, 25) + .with_temperature_offset(-2.2) .with_run_gas(true) .build(); diff --git a/src/calc.rs b/src/calc.rs index 82a885a..83a7bd2 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -46,16 +46,17 @@ impl Calc { /// * `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) { + 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 mut temp_offset = temp_offset.unwrap_or(0); - if temp_offset < 0 { - temp_offset = -1 * ((((temp_offset.abs() * 100) << 8) - 128) / 5); - } + + let temp_offset = match temp_offset { + None => 0i32, + Some(offset) if offset == 0.0 => 0i32, + Some(offset) => offset.signum() as i32 * (((((offset.abs() * 100.0) as i32) << 8) - 128) / 5), + }; let t_fine: i32 = (var2 + var3) as i32 + temp_offset; let calc_temp: i16 = (((t_fine * 5) + 128) >> 8) as i16; diff --git a/src/lib.rs b/src/lib.rs index 97ace3f..e9604c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,8 @@ //! println!("Pressure {}hPa", data.pressure_hpa()); //! println!("Humidity {}%", data.humidity_percent()); //! println!("Gas Resistence {}Ω", data.gas_resistance_ohm()); +//! +//! Ok(()) //! } //! ``` diff --git a/src/settings.rs b/src/settings.rs index 2f14789..c47f55e 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -71,7 +71,7 @@ pub struct TphSett { /// Filter coefficient pub filter: Option, /// If set, the temperature t_fine will be increased by the given value in celsius. - pub temperature_offset: Option, + pub temperature_offset: Option, } impl Clone for TphSett { @@ -148,12 +148,15 @@ bitflags! { /// /// # Example /// ``` +/// use bme680::{IIRFilterSize, OversamplingSetting, SettingsBuilder}; +/// use std::time::Duration; /// let settings = SettingsBuilder::new() /// .with_humidity_oversampling(OversamplingSetting::OS2x) /// .with_pressure_oversampling(OversamplingSetting::OS4x) /// .with_temperature_oversampling(OversamplingSetting::OS8x) /// .with_temperature_filter(IIRFilterSize::Size3) /// .with_gas_measurement(Duration::from_millis(1500), 320, 25) +/// .with_temperature_offset(-4.25) /// .with_run_gas(true) /// .build(); /// ``` @@ -231,7 +234,7 @@ impl SettingsBuilder { } /// Temperature offset in Celsius, e.g. 4, -8, 1.25 - pub fn with_temperature_offset(mut self, offset: i32) -> SettingsBuilder { + pub fn with_temperature_offset(mut self, offset: f32) -> SettingsBuilder { self.sensor_settings.tph_sett.temperature_offset = Some(offset); self }