Add unit conversions

This commit is contained in:
marcelbuesing 2018-05-21 16:26:14 +02:00 committed by marcelbuesing
parent 9df8c0b2c3
commit fc57f277c4
No known key found for this signature in database
GPG key ID: EF3934305E975944
2 changed files with 21 additions and 10 deletions

View file

@ -48,14 +48,17 @@ fn main() -> result::Result<(), Bme680Error<<hal::I2cdev as i2c::Read>::Error ,
info!("Sensor settings: {:?}", sensor_settings); info!("Sensor settings: {:?}", sensor_settings);
loop { loop {
thread::sleep(Duration::from_millis(profile_dur as u64)); thread::sleep(Duration::from_millis(5000));
let power_mode = dev.get_sensor_mode(); let power_mode = dev.get_sensor_mode();
info!("Sensor power mode: {:?}", power_mode); info!("Sensor power mode: {:?}", power_mode);
info!("Setting forced power modes"); info!("Setting forced power modes");
dev.set_sensor_mode(PowerMode::ForcedMode)?; dev.set_sensor_mode(PowerMode::ForcedMode)?;
info!("Retrieving sensor data"); info!("Retrieving sensor data");
let data = dev.get_sensor_data()?; let (data, state) = dev.get_sensor_data()?;
info!("Sensor Data {:?}", data); info!("Sensor Data {:?}", data);
info!("Temperature {}°C", data.temperature_celsius());
info!("Pressure {}hPa", data.pressure_hpa());
info!("Humidity {}%", data.humidity_percent());
} }
Ok(()) Ok(())
} }

View file

@ -289,6 +289,20 @@ impl Clone for FieldData {
} }
} }
impl FieldData {
pub fn temperature_celsius(&self) -> f32 {
self.temperature as f32 / 100f32
}
pub fn pressure_hpa(&self) -> f32 {
self.pressure as f32 / 100f32
}
pub fn humidity_percent(&self) -> f32 {
self.humidity as f32 / 1000f32
}
}
/// TODO - replace naming of "State" with something better /// TODO - replace naming of "State" with something better
/// aka new_fields - BME680_NEW_DATA_MSK /// aka new_fields - BME680_NEW_DATA_MSK
/// ///
@ -471,7 +485,6 @@ where
// .map_err(|e| Bme680Error::I2CWrite(e)) // .map_err(|e| Bme680Error::I2CWrite(e))
} }
// TODO replace parameter desired_settings with safe flags
// TODO replace all the NullPtr mess and split this into separate methods // TODO replace all the NullPtr mess and split this into separate methods
pub fn set_sensor_settings( pub fn set_sensor_settings(
&mut self, &mut self,
@ -500,12 +513,8 @@ where
reg_addr = 0x75u8; reg_addr = 0x75u8;
let mut data = I2CUtil::read_byte(&mut self.i2c, self.dev_id, reg_addr)?; let mut data = I2CUtil::read_byte(&mut self.i2c, self.dev_id, reg_addr)?;
// TODO duplicate check of condition ? debug!("FILTER_SEL: true");
if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) { data = (data as (i32) & !0x1ci32 |tph_sett_filter as (i32) << 2i32 & 0x1ci32) as (u8);
debug!("FILTER_SEL: true");
data = (data as (i32) & !0x1ci32 |tph_sett_filter as (i32) << 2i32 & 0x1ci32)
as (u8);
}
reg.push((reg_addr, data)); reg.push((reg_addr, data));
} }
@ -832,7 +841,6 @@ where
data.status = data.status | buff[14] & BME680_HEAT_STAB_MSK; data.status = data.status | buff[14] & BME680_HEAT_STAB_MSK;
if data.status & BME680_NEW_DATA_MSK != 0 { if data.status & BME680_NEW_DATA_MSK != 0 {
let (temp, t_fine) = Calc::calc_temperature(&self.calib, adc_temp); let (temp, t_fine) = Calc::calc_temperature(&self.calib, adc_temp);
debug!("adc_temp: {} adc_pres: {} adc_hum: {} adc_gas_res: {}, t_fine: {}", adc_temp, adc_pres, adc_hum, adc_gas_res, t_fine); debug!("adc_temp: {} adc_pres: {} adc_hum: {} adc_gas_res: {}, t_fine: {}", adc_temp, adc_pres, adc_hum, adc_gas_res, t_fine);
data.temperature = temp; data.temperature = temp;