Skip to content

Commit

Permalink
Fix temperature offset to be of type f32, closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelbuesing committed Dec 22, 2018
1 parent 2578140 commit 38bfe30
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ rust:
- nightly
script:
- cargo build --verbose
- cargo test --verbose --no-run
- cargo test --verbose
matrix:
allow_failures:
- rust: nightly
1 change: 1 addition & 0 deletions examples/reading_temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
13 changes: 7 additions & 6 deletions src/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>) -> (i16, i32) {
pub fn calc_temperature(calib: &CalibData, temp_adc: u32, temp_offset: Option<f32>) -> (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;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
//! println!("Pressure {}hPa", data.pressure_hpa());
//! println!("Humidity {}%", data.humidity_percent());
//! println!("Gas Resistence {}Ω", data.gas_resistance_ohm());
//!
//! Ok(())
//! }
//! ```
Expand Down
7 changes: 5 additions & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub struct TphSett {
/// 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>,
pub temperature_offset: Option<f32>,
}

impl Clone for TphSett {
Expand Down Expand Up @@ -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();
/// ```
Expand Down Expand Up @@ -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
}
Expand Down

1 comment on commit 38bfe30

@nickbroon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.