Pass the delayer around mutabily
This is as opposed to retaining ownership of it
This commit is contained in:
parent
c97894f84c
commit
500dbd9811
3 changed files with 28 additions and 22 deletions
|
@ -22,7 +22,8 @@ const INFLUX_DATABASE: &str = "influxdb";
|
||||||
async fn main() -> Result<(), ()> {
|
async fn main() -> Result<(), ()> {
|
||||||
// Init device
|
// Init device
|
||||||
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
|
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
|
||||||
let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)
|
let mut delayer = Delay {};
|
||||||
|
let mut dev = Bme680::init(i2c, &mut delayer, I2CAddress::Primary)
|
||||||
.map_err(|e| eprintln!("Init failed: {:?}", e))?;
|
.map_err(|e| eprintln!("Init failed: {:?}", e))?;
|
||||||
|
|
||||||
let settings = SettingsBuilder::new()
|
let settings = SettingsBuilder::new()
|
||||||
|
@ -33,17 +34,17 @@ async fn main() -> Result<(), ()> {
|
||||||
.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();
|
||||||
dev.set_sensor_settings(settings)
|
dev.set_sensor_settings(&mut delayer, settings)
|
||||||
.map_err(|e| eprintln!("Setting sensor settings failed: {:?}", e))?;
|
.map_err(|e| eprintln!("Setting sensor settings failed: {:?}", e))?;
|
||||||
|
|
||||||
let client = Client::new(Url::parse(INFLUX_ADDRESS).unwrap(), INFLUX_DATABASE)
|
let client = Client::new(Url::parse(INFLUX_ADDRESS).unwrap(), INFLUX_DATABASE)
|
||||||
.set_authentication(INFLUX_USER, INFLUX_PASSWORD);
|
.set_authentication(INFLUX_USER, INFLUX_PASSWORD);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
dev.set_sensor_mode(PowerMode::ForcedMode)
|
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)
|
||||||
.map_err(|e| eprintln!("Setting sensor mode failed: {:?}", e))?;
|
.map_err(|e| eprintln!("Setting sensor mode failed: {:?}", e))?;
|
||||||
let (data, state) = dev
|
let (data, state) = dev
|
||||||
.get_sensor_data()
|
.get_sensor_data(&mut delayer)
|
||||||
.map_err(|e| eprintln!("Retrieving sensor data failed: {:?}", e))?;
|
.map_err(|e| eprintln!("Retrieving sensor data failed: {:?}", e))?;
|
||||||
|
|
||||||
println!("State {:?}", state);
|
println!("State {:?}", state);
|
||||||
|
|
|
@ -15,8 +15,9 @@ fn main(
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let i2c = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
let i2c = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||||
|
let mut delayer = Delay {};
|
||||||
|
|
||||||
let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)?;
|
let mut dev = Bme680::init(i2c, &mut delayer, I2CAddress::Primary)?;
|
||||||
let mut delay = Delay {};
|
let mut delay = Delay {};
|
||||||
|
|
||||||
let settings = SettingsBuilder::new()
|
let settings = SettingsBuilder::new()
|
||||||
|
@ -32,9 +33,9 @@ fn main(
|
||||||
let profile_dur = dev.get_profile_dur(&settings.0)?;
|
let profile_dur = dev.get_profile_dur(&settings.0)?;
|
||||||
info!("Profile 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(&mut delayer, settings)?;
|
||||||
info!("Setting forced power modes");
|
info!("Setting forced power modes");
|
||||||
dev.set_sensor_mode(PowerMode::ForcedMode)?;
|
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)?;
|
||||||
|
|
||||||
let sensor_settings = dev.get_sensor_settings(settings.1);
|
let sensor_settings = dev.get_sensor_settings(settings.1);
|
||||||
info!("Sensor settings: {:?}", sensor_settings);
|
info!("Sensor settings: {:?}", sensor_settings);
|
||||||
|
@ -44,9 +45,9 @@ fn main(
|
||||||
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(&mut delayer, PowerMode::ForcedMode)?;
|
||||||
info!("Retrieving sensor data");
|
info!("Retrieving sensor data");
|
||||||
let (data, _state) = dev.get_sensor_data()?;
|
let (data, _state) = dev.get_sensor_data(&mut delayer)?;
|
||||||
info!("Sensor Data {:?}", data);
|
info!("Sensor Data {:?}", data);
|
||||||
info!("Temperature {}°C", data.temperature_celsius());
|
info!("Temperature {}°C", data.temperature_celsius());
|
||||||
info!("Pressure {}hPa", data.pressure_hpa());
|
info!("Pressure {}hPa", data.pressure_hpa());
|
||||||
|
|
28
src/lib.rs
28
src/lib.rs
|
@ -53,7 +53,8 @@
|
||||||
//! {
|
//! {
|
||||||
//! // Initialize device
|
//! // Initialize device
|
||||||
//! let i2c = I2cdev {}; // Your I2C device construction will look different, perhaps using I2cdev::new(..)
|
//! let i2c = I2cdev {}; // Your I2C device construction will look different, perhaps using I2cdev::new(..)
|
||||||
//! let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)?;
|
//! let mut delayer = Delay {}; // Your Delay construction will look different, perhaps using Delay::new(..)
|
||||||
|
//! let mut dev = Bme680::init(i2c, &mut delayer, I2CAddress::Primary)?;
|
||||||
//! let settings = SettingsBuilder::new()
|
//! let settings = SettingsBuilder::new()
|
||||||
//! .with_humidity_oversampling(OversamplingSetting::OS2x)
|
//! .with_humidity_oversampling(OversamplingSetting::OS2x)
|
||||||
//! .with_pressure_oversampling(OversamplingSetting::OS4x)
|
//! .with_pressure_oversampling(OversamplingSetting::OS4x)
|
||||||
|
@ -62,11 +63,11 @@
|
||||||
//! .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();
|
||||||
//! dev.set_sensor_settings(settings)?;
|
//! dev.set_sensor_settings(&mut delayer, settings)?;
|
||||||
//!
|
//!
|
||||||
//! // Read sensor data
|
//! // Read sensor data
|
||||||
//! dev.set_sensor_mode(PowerMode::ForcedMode)?;
|
//! dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)?;
|
||||||
//! let (data, _state) = dev.get_sensor_data()?;
|
//! let (data, _state) = dev.get_sensor_data(&mut delayer)?;
|
||||||
//!
|
//!
|
||||||
//! println!("Temperature {}°C", data.temperature_celsius());
|
//! println!("Temperature {}°C", data.temperature_celsius());
|
||||||
//! println!("Pressure {}hPa", data.pressure_hpa());
|
//! println!("Pressure {}hPa", data.pressure_hpa());
|
||||||
|
@ -92,8 +93,8 @@ use crate::calc::Calc;
|
||||||
use crate::hal::blocking::delay::DelayMs;
|
use crate::hal::blocking::delay::DelayMs;
|
||||||
use crate::hal::blocking::i2c::{Read, Write};
|
use crate::hal::blocking::i2c::{Read, Write};
|
||||||
|
|
||||||
use core::result;
|
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
|
use core::{marker::PhantomData, result};
|
||||||
use embedded_hal as hal;
|
use embedded_hal as hal;
|
||||||
use log::{debug, error, info};
|
use log::{debug, error, info};
|
||||||
|
|
||||||
|
@ -393,7 +394,7 @@ impl I2CUtil {
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct Bme680<I2C, D> {
|
pub struct Bme680<I2C, D> {
|
||||||
i2c: I2C,
|
i2c: I2C,
|
||||||
delay: D,
|
delay: PhantomData<D>,
|
||||||
dev_id: I2CAddress,
|
dev_id: I2CAddress,
|
||||||
calib: CalibData,
|
calib: CalibData,
|
||||||
// TODO remove ? as it may not reflect the state of the device
|
// TODO remove ? as it may not reflect the state of the device
|
||||||
|
@ -450,10 +451,10 @@ where
|
||||||
|
|
||||||
pub fn init(
|
pub fn init(
|
||||||
mut i2c: I2C,
|
mut i2c: I2C,
|
||||||
mut delay: D,
|
delay: &mut D,
|
||||||
dev_id: I2CAddress,
|
dev_id: I2CAddress,
|
||||||
) -> Result<Bme680<I2C, D>, <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<Bme680<I2C, D>, <I2C as Read>::Error, <I2C as Write>::Error> {
|
||||||
Bme680::soft_reset(&mut i2c, &mut delay, dev_id)?;
|
Bme680::soft_reset(&mut i2c, delay, dev_id)?;
|
||||||
|
|
||||||
debug!("Reading chip id");
|
debug!("Reading chip id");
|
||||||
/* Soft reset to restore it to default values*/
|
/* Soft reset to restore it to default values*/
|
||||||
|
@ -466,7 +467,7 @@ where
|
||||||
debug!("Calib data {:?}", calib);
|
debug!("Calib data {:?}", calib);
|
||||||
let dev = Bme680 {
|
let dev = Bme680 {
|
||||||
i2c: i2c,
|
i2c: i2c,
|
||||||
delay: delay,
|
delay: PhantomData,
|
||||||
dev_id: dev_id,
|
dev_id: dev_id,
|
||||||
calib: calib,
|
calib: calib,
|
||||||
power_mode: PowerMode::ForcedMode,
|
power_mode: PowerMode::ForcedMode,
|
||||||
|
@ -506,6 +507,7 @@ where
|
||||||
/// Set the settings to be used during the sensor measurements
|
/// Set the settings to be used during the sensor measurements
|
||||||
pub fn set_sensor_settings(
|
pub fn set_sensor_settings(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
delay: &mut D,
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
||||||
let (sensor_settings, desired_settings) = settings;
|
let (sensor_settings, desired_settings) = settings;
|
||||||
|
@ -521,7 +523,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
let power_mode = self.power_mode;
|
let power_mode = self.power_mode;
|
||||||
self.set_sensor_mode(power_mode)?;
|
self.set_sensor_mode(delay, power_mode)?;
|
||||||
|
|
||||||
let mut element_index = 0;
|
let mut element_index = 0;
|
||||||
// Selecting the filter
|
// Selecting the filter
|
||||||
|
@ -688,6 +690,7 @@ where
|
||||||
/// * `target_power_mode` - Desired target power mode
|
/// * `target_power_mode` - Desired target power mode
|
||||||
pub fn set_sensor_mode(
|
pub fn set_sensor_mode(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
delay: &mut D,
|
||||||
target_power_mode: PowerMode,
|
target_power_mode: PowerMode,
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
||||||
let mut tmp_pow_mode: u8;
|
let mut tmp_pow_mode: u8;
|
||||||
|
@ -708,7 +711,7 @@ where
|
||||||
tmp_pow_mode = tmp_pow_mode & !BME680_MODE_MSK;
|
tmp_pow_mode = tmp_pow_mode & !BME680_MODE_MSK;
|
||||||
debug!("Setting to sleep tmp_pow_mode: {}", tmp_pow_mode);
|
debug!("Setting to sleep tmp_pow_mode: {}", tmp_pow_mode);
|
||||||
self.bme680_set_regs(&[(BME680_CONF_T_P_MODE_ADDR, tmp_pow_mode)])?;
|
self.bme680_set_regs(&[(BME680_CONF_T_P_MODE_ADDR, tmp_pow_mode)])?;
|
||||||
self.delay.delay_ms(BME680_POLL_PERIOD_MS);
|
delay.delay_ms(BME680_POLL_PERIOD_MS);
|
||||||
} else {
|
} else {
|
||||||
// TODO do while in Rust?
|
// TODO do while in Rust?
|
||||||
break;
|
break;
|
||||||
|
@ -919,6 +922,7 @@ where
|
||||||
/// Retrieve the current sensor informations
|
/// Retrieve the current sensor informations
|
||||||
pub fn get_sensor_data(
|
pub fn get_sensor_data(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
delay: &mut D,
|
||||||
) -> Result<(FieldData, FieldDataCondition), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(FieldData, FieldDataCondition), <I2C as Read>::Error, <I2C as Write>::Error> {
|
||||||
let mut buff: [u8; BME680_FIELD_LENGTH] = [0; BME680_FIELD_LENGTH];
|
let mut buff: [u8; BME680_FIELD_LENGTH] = [0; BME680_FIELD_LENGTH];
|
||||||
|
|
||||||
|
@ -969,7 +973,7 @@ where
|
||||||
return Ok((data, FieldDataCondition::NewData));
|
return Ok((data, FieldDataCondition::NewData));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.delay.delay_ms(BME680_POLL_PERIOD_MS);
|
delay.delay_ms(BME680_POLL_PERIOD_MS);
|
||||||
}
|
}
|
||||||
Ok((data, FieldDataCondition::Unchanged))
|
Ok((data, FieldDataCondition::Unchanged))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue