bme680-rust/examples/reading_temperature.rs

62 lines
2 KiB
Rust
Raw Normal View History

2018-05-18 12:57:34 +00:00
extern crate embedded_hal;
2018-05-18 20:12:42 +00:00
extern crate env_logger;
2018-05-18 12:57:34 +00:00
extern crate linux_embedded_hal as hal;
#[macro_use]
extern crate log;
2018-05-18 12:57:34 +00:00
extern crate bme680_rs;
use bme680_rs::*;
use hal::*;
2018-05-18 15:37:11 +00:00
use embedded_hal::blocking::i2c as i2c;
2018-05-18 12:57:34 +00:00
use std::thread;
use std::result;
use std::time::Duration;
2018-05-18 15:37:11 +00:00
fn main() -> result::Result<(), Bme680Error<<hal::I2cdev as i2c::Read>::Error , <hal::I2cdev as i2c::Write>::Error>>{
2018-05-18 12:57:34 +00:00
2018-05-18 20:12:42 +00:00
env_logger::init();
2018-05-18 12:57:34 +00:00
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
let mut dev = Bme680_dev::init(i2c, Delay{}, 0x76, 25)?;
let mut sensor_settings: SensorSettings = Default::default();
2018-05-20 20:08:34 +00:00
sensor_settings.tph_sett.os_hum = Some(BME680_OS_2X);
sensor_settings.tph_sett.os_pres = Some(BME680_OS_4X);
sensor_settings.tph_sett.os_temp = Some(BME680_OS_8X);
sensor_settings.tph_sett.filter = Some(2);
2018-05-18 12:57:34 +00:00
sensor_settings.gas_sett.run_gas = Some(0x01);
2018-05-20 20:08:34 +00:00
sensor_settings.gas_sett.heatr_dur = Some(1500);
sensor_settings.gas_sett.heatr_temp = Some(320);
2018-05-18 12:57:34 +00:00
let settings_sel =
DesiredSensorSettings::OST_SEL |
DesiredSensorSettings::OSP_SEL |
DesiredSensorSettings::OSH_SEL |
DesiredSensorSettings::GAS_SENSOR_SEL;
let profile_dur = dev.get_profile_dur(&sensor_settings)?;
info!("Duration {}", profile_dur);
info!("Setting sensor settings");
2018-05-18 12:57:34 +00:00
dev.set_sensor_settings(settings_sel, &sensor_settings)?;
info!("Setting forced power modes");
2018-05-18 12:57:34 +00:00
dev.set_sensor_mode(PowerMode::ForcedMode)?;
2018-05-20 20:08:34 +00:00
let sensor_settings = dev.get_sensor_settings(settings_sel);
info!("Sensor settings: {:?}", sensor_settings);
loop {
thread::sleep(Duration::from_millis(profile_dur as u64));
2018-05-20 20:08:34 +00:00
let power_mode = dev.get_sensor_mode();
info!("Sensor power mode: {:?}", power_mode);
info!("Setting forced power modes");
dev.set_sensor_mode(PowerMode::ForcedMode)?;
info!("Retrieving sensor data");
let data = dev.get_sensor_data()?;
info!("Sensor Data {:?}", data);
}
2018-05-18 12:57:34 +00:00
Ok(())
}