Replace u8 with IIRFilterSize enum

This commit is contained in:
marcelbuesing 2018-05-30 20:53:18 +02:00
parent 44fc891b3e
commit 336305d30d
No known key found for this signature in database
GPG key ID: E51C4F8A2B7FF43E
3 changed files with 42 additions and 10 deletions

View file

@ -26,13 +26,13 @@ fn main() -> result::Result<
.with_humidity_oversampling(OversamplingSetting::OS2x) .with_humidity_oversampling(OversamplingSetting::OS2x)
.with_pressure_oversampling(OversamplingSetting::OS4x) .with_pressure_oversampling(OversamplingSetting::OS4x)
.with_temperature_oversampling(OversamplingSetting::OS8x) .with_temperature_oversampling(OversamplingSetting::OS8x)
.with_temperature_filter(2) .with_temperature_filter(IIRFilterSize::Size3)
.with_gas_measurement(Duration::from_millis(1500), 320, 25) .with_gas_measurement(Duration::from_millis(1500), 320, 25)
.with_run_gas(true) .with_run_gas(true)
.build(); .build();
let profile_dur = dev.get_profile_dur(&settings.0)?; let profile_dur = dev.get_profile_dur(&settings.0)?;
info!("Duration {:?}", profile_dur); info!("Profile duration {:?}", profile_dur);
info!("Setting sensor settings"); info!("Setting sensor settings");
dev.set_sensor_settings(settings)?; dev.set_sensor_settings(settings)?;
info!("Setting forced power modes"); info!("Setting forced power modes");

View file

@ -12,8 +12,8 @@ extern crate embedded_hal as hal;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
pub use self::settings::{DesiredSensorSettings, GasSett, OversamplingSetting, SensorSettings, pub use self::settings::{DesiredSensorSettings, GasSett, IIRFilterSize, OversamplingSetting,
Settings, SettingsBuilder, TphSett}; SensorSettings, Settings, SettingsBuilder, TphSett};
mod calc; mod calc;
mod settings; mod settings;
@ -496,12 +496,13 @@ where
let mut element_index = 0; let mut element_index = 0;
/* Selecting the filter */ /* Selecting the filter */
if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) { if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) {
let tph_sett_filter = boundary_check::<I2C>(tph_sett.filter, "TphSett.filter", 0, 7)?;
let mut data = let mut data =
I2CUtil::read_byte(&mut self.i2c, self.dev_id.addr(), BME680_CONF_ODR_FILT_ADDR)?; I2CUtil::read_byte(&mut self.i2c, self.dev_id.addr(), BME680_CONF_ODR_FILT_ADDR)?;
debug!("FILTER_SEL: true"); 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); reg[element_index] = (BME680_CONF_ODR_FILT_ADDR, data);
element_index += 1; element_index += 1;
} }
@ -611,8 +612,9 @@ where
} }
if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) { if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) {
sensor_settings.tph_sett.filter = sensor_settings.tph_sett.filter = Some(IIRFilterSize::from_u8(
Some(((data_array[5usize] as (i32) & 0x1ci32) >> 2i32) as (u8)); ((data_array[5usize] as (i32) & 0x1ci32) >> 2i32) as (u8),
));
} }
if desired_settings if desired_settings

View file

@ -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)] #[derive(Debug, Default, Copy)]
#[repr(C)] #[repr(C)]
pub struct TphSett { pub struct TphSett {
pub os_hum: Option<OversamplingSetting>, pub os_hum: Option<OversamplingSetting>,
pub os_temp: Option<OversamplingSetting>, pub os_temp: Option<OversamplingSetting>,
pub os_pres: Option<OversamplingSetting>, pub os_pres: Option<OversamplingSetting>,
pub filter: Option<u8>, pub filter: Option<IIRFilterSize>,
} }
impl Clone for TphSett { impl Clone for TphSett {
@ -114,7 +144,7 @@ impl SettingsBuilder {
sensor_settings: Default::default(), 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.sensor_settings.tph_sett.filter = Some(filter);
self.desired_settings |= DesiredSensorSettings::FILTER_SEL; self.desired_settings |= DesiredSensorSettings::FILTER_SEL;
self self