Fix temperature offset to be of type f32, closes #11

This commit is contained in:
marcelbuesing 2018-12-20 00:06:30 +01:00
parent 2578140bda
commit 38bfe30acc
No known key found for this signature in database
GPG key ID: E51C4F8A2B7FF43E
5 changed files with 16 additions and 9 deletions

View file

@ -5,7 +5,7 @@ rust:
- nightly
script:
- cargo build --verbose
- cargo test --verbose --no-run
- cargo test --verbose
matrix:
allow_failures:
- rust: nightly

View file

@ -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();

View file

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

View file

@ -38,6 +38,8 @@
//! println!("Pressure {}hPa", data.pressure_hpa());
//! println!("Humidity {}%", data.humidity_percent());
//! println!("Gas Resistence {}Ω", data.gas_resistance_ohm());
//!
//! Ok(())
//! }
//! ```

View file

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