From 336305d30d1f6cdb16b09c24e2db1b9e1b82c0d6 Mon Sep 17 00:00:00 2001 From: marcelbuesing Date: Wed, 30 May 2018 20:53:18 +0200 Subject: [PATCH] Replace u8 with IIRFilterSize enum --- examples/reading_temperature.rs | 4 ++-- src/lib.rs | 14 ++++++++------ src/settings.rs | 34 +++++++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/examples/reading_temperature.rs b/examples/reading_temperature.rs index 15460a0..ac4f58e 100644 --- a/examples/reading_temperature.rs +++ b/examples/reading_temperature.rs @@ -26,13 +26,13 @@ fn main() -> result::Result< .with_humidity_oversampling(OversamplingSetting::OS2x) .with_pressure_oversampling(OversamplingSetting::OS4x) .with_temperature_oversampling(OversamplingSetting::OS8x) - .with_temperature_filter(2) + .with_temperature_filter(IIRFilterSize::Size3) .with_gas_measurement(Duration::from_millis(1500), 320, 25) .with_run_gas(true) .build(); let profile_dur = dev.get_profile_dur(&settings.0)?; - info!("Duration {:?}", profile_dur); + info!("Profile duration {:?}", profile_dur); info!("Setting sensor settings"); dev.set_sensor_settings(settings)?; info!("Setting forced power modes"); diff --git a/src/lib.rs b/src/lib.rs index dfc1ef5..c0c45b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,8 +12,8 @@ extern crate embedded_hal as hal; #[macro_use] extern crate log; -pub use self::settings::{DesiredSensorSettings, GasSett, OversamplingSetting, SensorSettings, - Settings, SettingsBuilder, TphSett}; +pub use self::settings::{DesiredSensorSettings, GasSett, IIRFilterSize, OversamplingSetting, + SensorSettings, Settings, SettingsBuilder, TphSett}; mod calc; mod settings; @@ -496,12 +496,13 @@ where let mut element_index = 0; /* Selecting the filter */ if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) { - let tph_sett_filter = boundary_check::(tph_sett.filter, "TphSett.filter", 0, 7)?; let mut data = I2CUtil::read_byte(&mut self.i2c, self.dev_id.addr(), BME680_CONF_ODR_FILT_ADDR)?; debug!("FILTER_SEL: true"); - data = (data as (i32) & !0x1ci32 | tph_sett_filter as (i32) << 2i32 & 0x1ci32) as (u8); + data = (data as (i32) & !0x1ci32 + | tph_sett.filter.unwrap_or(IIRFilterSize::Size0) as (i32) << 2i32 & 0x1ci32) + as (u8); reg[element_index] = (BME680_CONF_ODR_FILT_ADDR, data); element_index += 1; } @@ -611,8 +612,9 @@ where } if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) { - sensor_settings.tph_sett.filter = - Some(((data_array[5usize] as (i32) & 0x1ci32) >> 2i32) as (u8)); + sensor_settings.tph_sett.filter = Some(IIRFilterSize::from_u8( + ((data_array[5usize] as (i32) & 0x1ci32) >> 2i32) as (u8), + )); } if desired_settings diff --git a/src/settings.rs b/src/settings.rs index 07243b2..7f1289d 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -26,13 +26,43 @@ impl OversamplingSetting { } } +#[derive(Copy, Clone, Debug)] +#[repr(u8)] +pub enum IIRFilterSize { + Size0 = 0, + Size1 = 1, + Size3 = 2, + Size7 = 3, + Size15 = 4, + Size31 = 5, + Size63 = 6, + Size127 = 7, +} + +impl IIRFilterSize { + // TODO replace with TryFrom once stabilized + pub fn from_u8(os: u8) -> IIRFilterSize { + match os { + 0 => IIRFilterSize::Size0, + 1 => IIRFilterSize::Size1, + 2 => IIRFilterSize::Size3, + 3 => IIRFilterSize::Size7, + 4 => IIRFilterSize::Size15, + 5 => IIRFilterSize::Size31, + 6 => IIRFilterSize::Size63, + 7 => IIRFilterSize::Size127, + _ => panic!("Unknown IIRFilterSize: {}", os), + } + } +} + #[derive(Debug, Default, Copy)] #[repr(C)] pub struct TphSett { pub os_hum: Option, pub os_temp: Option, pub os_pres: Option, - pub filter: Option, + pub filter: Option, } impl Clone for TphSett { @@ -114,7 +144,7 @@ impl SettingsBuilder { sensor_settings: Default::default(), } } - pub fn with_temperature_filter(mut self, filter: u8) -> SettingsBuilder { + pub fn with_temperature_filter(mut self, filter: IIRFilterSize) -> SettingsBuilder { self.sensor_settings.tph_sett.filter = Some(filter); self.desired_settings |= DesiredSensorSettings::FILTER_SEL; self