Pass the delayer around mutabily

This is as opposed to retaining ownership of it
This commit is contained in:
huntc 2021-04-29 15:05:45 +10:00 committed by Marcel
parent c97894f84c
commit 500dbd9811
3 changed files with 28 additions and 22 deletions

View file

@ -22,7 +22,8 @@ const INFLUX_DATABASE: &str = "influxdb";
async fn main() -> Result<(), ()> {
// Init device
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))?;
let settings = SettingsBuilder::new()
@ -33,17 +34,17 @@ async fn main() -> Result<(), ()> {
.with_gas_measurement(Duration::from_millis(1500), 320, 25)
.with_run_gas(true)
.build();
dev.set_sensor_settings(settings)
dev.set_sensor_settings(&mut delayer, settings)
.map_err(|e| eprintln!("Setting sensor settings failed: {:?}", e))?;
let client = Client::new(Url::parse(INFLUX_ADDRESS).unwrap(), INFLUX_DATABASE)
.set_authentication(INFLUX_USER, INFLUX_PASSWORD);
loop {
dev.set_sensor_mode(PowerMode::ForcedMode)
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)
.map_err(|e| eprintln!("Setting sensor mode failed: {:?}", e))?;
let (data, state) = dev
.get_sensor_data()
.get_sensor_data(&mut delayer)
.map_err(|e| eprintln!("Retrieving sensor data failed: {:?}", e))?;
println!("State {:?}", state);

View file

@ -15,8 +15,9 @@ fn main(
env_logger::init();
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 settings = SettingsBuilder::new()
@ -32,9 +33,9 @@ fn main(
let profile_dur = dev.get_profile_dur(&settings.0)?;
info!("Profile duration {:?}", profile_dur);
info!("Setting sensor settings");
dev.set_sensor_settings(settings)?;
dev.set_sensor_settings(&mut delayer, settings)?;
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);
info!("Sensor settings: {:?}", sensor_settings);
@ -44,9 +45,9 @@ fn main(
let power_mode = dev.get_sensor_mode();
info!("Sensor power mode: {:?}", power_mode);
info!("Setting forced power modes");
dev.set_sensor_mode(PowerMode::ForcedMode)?;
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)?;
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!("Temperature {}°C", data.temperature_celsius());
info!("Pressure {}hPa", data.pressure_hpa());

View file

@ -52,8 +52,9 @@
//! fn main() -> result::Result<(), Error<<hal::I2cdev as i2c::Read>::Error, <hal::I2cdev as i2c::Write>::Error>>
//! {
//! // Initialize device
//! let i2c = I2cdev {}; // Your I2C device construction will look different, perhaps using I2cdev::new(..)
//! let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)?;
//! let i2c = I2cdev {}; // Your I2C device construction will look different, perhaps using I2cdev::new(..)
//! 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()
//! .with_humidity_oversampling(OversamplingSetting::OS2x)
//! .with_pressure_oversampling(OversamplingSetting::OS4x)
@ -62,11 +63,11 @@
//! .with_gas_measurement(Duration::from_millis(1500), 320, 25)
//! .with_run_gas(true)
//! .build();
//! dev.set_sensor_settings(settings)?;
//! dev.set_sensor_settings(&mut delayer, settings)?;
//!
//! // Read sensor data
//! dev.set_sensor_mode(PowerMode::ForcedMode)?;
//! let (data, _state) = dev.get_sensor_data()?;
//! dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)?;
//! let (data, _state) = dev.get_sensor_data(&mut delayer)?;
//!
//! println!("Temperature {}°C", data.temperature_celsius());
//! println!("Pressure {}hPa", data.pressure_hpa());
@ -92,8 +93,8 @@ use crate::calc::Calc;
use crate::hal::blocking::delay::DelayMs;
use crate::hal::blocking::i2c::{Read, Write};
use core::result;
use core::time::Duration;
use core::{marker::PhantomData, result};
use embedded_hal as hal;
use log::{debug, error, info};
@ -393,7 +394,7 @@ impl I2CUtil {
#[repr(C)]
pub struct Bme680<I2C, D> {
i2c: I2C,
delay: D,
delay: PhantomData<D>,
dev_id: I2CAddress,
calib: CalibData,
// TODO remove ? as it may not reflect the state of the device
@ -450,10 +451,10 @@ where
pub fn init(
mut i2c: I2C,
mut delay: D,
delay: &mut D,
dev_id: I2CAddress,
) -> 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");
/* Soft reset to restore it to default values*/
@ -466,7 +467,7 @@ where
debug!("Calib data {:?}", calib);
let dev = Bme680 {
i2c: i2c,
delay: delay,
delay: PhantomData,
dev_id: dev_id,
calib: calib,
power_mode: PowerMode::ForcedMode,
@ -506,6 +507,7 @@ where
/// Set the settings to be used during the sensor measurements
pub fn set_sensor_settings(
&mut self,
delay: &mut D,
settings: Settings,
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
let (sensor_settings, desired_settings) = settings;
@ -521,7 +523,7 @@ where
}
let power_mode = self.power_mode;
self.set_sensor_mode(power_mode)?;
self.set_sensor_mode(delay, power_mode)?;
let mut element_index = 0;
// Selecting the filter
@ -688,6 +690,7 @@ where
/// * `target_power_mode` - Desired target power mode
pub fn set_sensor_mode(
&mut self,
delay: &mut D,
target_power_mode: PowerMode,
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
let mut tmp_pow_mode: u8;
@ -708,7 +711,7 @@ where
tmp_pow_mode = tmp_pow_mode & !BME680_MODE_MSK;
debug!("Setting to sleep tmp_pow_mode: {}", 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 {
// TODO do while in Rust?
break;
@ -919,6 +922,7 @@ where
/// Retrieve the current sensor informations
pub fn get_sensor_data(
&mut self,
delay: &mut D,
) -> Result<(FieldData, FieldDataCondition), <I2C as Read>::Error, <I2C as Write>::Error> {
let mut buff: [u8; BME680_FIELD_LENGTH] = [0; BME680_FIELD_LENGTH];
@ -969,7 +973,7 @@ where
return Ok((data, FieldDataCondition::NewData));
}
self.delay.delay_ms(BME680_POLL_PERIOD_MS);
delay.delay_ms(BME680_POLL_PERIOD_MS);
}
Ok((data, FieldDataCondition::Unchanged))
}