update embedded-hal and linux-embedded-hal to latest stable versions
This commit is contained in:
parent
9a210bdbe3
commit
ccfcf8bd32
3 changed files with 51 additions and 57 deletions
|
@ -14,7 +14,7 @@ maintenance = { status = "passively-maintained" }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bitflags = "1.2"
|
bitflags = "1.2"
|
||||||
embedded-hal = "=1.0.0-alpha.5"
|
embedded-hal = "=1.0.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
serde = { version = "1.0", optional = true, default-features = false }
|
serde = { version = "1.0", optional = true, default-features = false }
|
||||||
|
|
||||||
|
@ -27,4 +27,4 @@ tokio = {version = "1.5", features = ["full"] }
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
|
|
||||||
[target.'cfg(target_os = "linux")'.dev-dependencies]
|
[target.'cfg(target_os = "linux")'.dev-dependencies]
|
||||||
linux-embedded-hal = "0.4.0-alpha.1"
|
linux-embedded-hal = "0.4.0"
|
||||||
|
|
|
@ -3,14 +3,12 @@
|
||||||
use bme680::*;
|
use bme680::*;
|
||||||
use core::result;
|
use core::result;
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
use embedded_hal::i2c::blocking::Read;
|
use embedded_hal::delay::DelayNs;
|
||||||
use embedded_hal::i2c::blocking::Write;
|
|
||||||
use embedded_hal::delay::blocking::DelayMs;
|
|
||||||
use linux_embedded_hal as hal;
|
use linux_embedded_hal as hal;
|
||||||
use linux_embedded_hal::Delay;
|
use linux_embedded_hal::{Delay, I2CError};
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
fn main() -> result::Result<(), Error<<hal::I2cdev as Read>::Error, <hal::I2cdev as Write>::Error>>
|
fn main() -> result::Result<(), Error<I2CError>>
|
||||||
{
|
{
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
|
|
90
src/lib.rs
90
src/lib.rs
|
@ -93,12 +93,13 @@ mod calc;
|
||||||
mod settings;
|
mod settings;
|
||||||
|
|
||||||
use crate::calc::Calc;
|
use crate::calc::Calc;
|
||||||
use crate::hal::delay::blocking::DelayMs;
|
use crate::hal::delay::DelayNs;
|
||||||
use crate::hal::i2c::blocking::{Read, Write};
|
use crate::hal::i2c::I2c;
|
||||||
|
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
use core::{marker::PhantomData, result};
|
use core::{marker::PhantomData, result};
|
||||||
use embedded_hal as hal;
|
use embedded_hal as hal;
|
||||||
|
use embedded_hal::i2c::ErrorType;
|
||||||
use log::{debug, error, info};
|
use log::{debug, error, info};
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
|
@ -169,12 +170,12 @@ const BME680_REG_BUFFER_LENGTH: usize = 6;
|
||||||
|
|
||||||
/// All possible errors in this crate
|
/// All possible errors in this crate
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error<R, W> {
|
pub enum Error<E> {
|
||||||
///
|
///
|
||||||
/// aka BME680_E_COM_FAIL
|
/// aka BME680_E_COM_FAIL
|
||||||
///
|
///
|
||||||
I2CWrite(W),
|
I2CWrite(E),
|
||||||
I2CRead(R),
|
I2CRead(E),
|
||||||
Delay,
|
Delay,
|
||||||
///
|
///
|
||||||
/// aka BME680_E_DEV_NOT_FOUND
|
/// aka BME680_E_DEV_NOT_FOUND
|
||||||
|
@ -199,7 +200,7 @@ pub enum Error<R, W> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Abbreviates `std::result::Result` type
|
/// Abbreviates `std::result::Result` type
|
||||||
pub type Result<T, R, W> = result::Result<T, Error<R, W>>;
|
pub type Result<T, E> = result::Result<T, Error<E>>;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Power mode settings
|
/// Power mode settings
|
||||||
|
@ -377,9 +378,9 @@ impl I2CUtil {
|
||||||
i2c: &mut I2C,
|
i2c: &mut I2C,
|
||||||
dev_id: u8,
|
dev_id: u8,
|
||||||
reg_addr: u8,
|
reg_addr: u8,
|
||||||
) -> Result<u8, <I2C as Read>::Error, <I2C as Write>::Error>
|
) -> Result<u8, <I2C as ErrorType>::Error>
|
||||||
where
|
where
|
||||||
I2C: Read + Write,
|
I2C: I2c
|
||||||
{
|
{
|
||||||
let mut buf = [0; 1];
|
let mut buf = [0; 1];
|
||||||
|
|
||||||
|
@ -396,9 +397,9 @@ impl I2CUtil {
|
||||||
dev_id: u8,
|
dev_id: u8,
|
||||||
reg_addr: u8,
|
reg_addr: u8,
|
||||||
buf: &mut [u8],
|
buf: &mut [u8],
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error>
|
) -> Result<(), <I2C as ErrorType>::Error>
|
||||||
where
|
where
|
||||||
I2C: Read + Write,
|
I2C: I2c,
|
||||||
{
|
{
|
||||||
i2c.write(dev_id, &[reg_addr]).map_err(Error::I2CWrite)?;
|
i2c.write(dev_id, &[reg_addr]).map_err(Error::I2CWrite)?;
|
||||||
|
|
||||||
|
@ -429,9 +430,9 @@ fn boundary_check<I2C>(
|
||||||
value_name: &'static str,
|
value_name: &'static str,
|
||||||
min: u8,
|
min: u8,
|
||||||
max: u8,
|
max: u8,
|
||||||
) -> Result<u8, <I2C as Read>::Error, <I2C as Write>::Error>
|
) -> Result<u8, <I2C as ErrorType>::Error>
|
||||||
where
|
where
|
||||||
I2C: Read + Write,
|
I2C: I2c,
|
||||||
{
|
{
|
||||||
let value = value.ok_or(Error::BoundaryCheckFailure(value_name))?;
|
let value = value.ok_or(Error::BoundaryCheckFailure(value_name))?;
|
||||||
|
|
||||||
|
@ -450,23 +451,21 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I2C, D> Bme680<I2C, D>
|
impl<I2C, D> Bme680<I2C, D>
|
||||||
where
|
where
|
||||||
D: DelayMs<u8>,
|
D: DelayNs,
|
||||||
I2C: Read + Write,
|
I2C: I2c,
|
||||||
{
|
{
|
||||||
pub fn soft_reset(
|
pub fn soft_reset(
|
||||||
i2c: &mut I2C,
|
i2c: &mut I2C,
|
||||||
delay: &mut D,
|
delay: &mut D,
|
||||||
dev_id: I2CAddress,
|
dev_id: I2CAddress,
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(), <I2C as ErrorType>::Error> {
|
||||||
let tmp_buff: [u8; 2] = [BME680_SOFT_RESET_ADDR, BME680_SOFT_RESET_CMD];
|
let tmp_buff: [u8; 2] = [BME680_SOFT_RESET_ADDR, BME680_SOFT_RESET_CMD];
|
||||||
|
|
||||||
i2c.write(dev_id.addr(), &tmp_buff)
|
i2c.write(dev_id.addr(), &tmp_buff)
|
||||||
.map_err(Error::I2CWrite)?;
|
.map_err(Error::I2CWrite)?;
|
||||||
|
|
||||||
delay
|
delay.delay_ms(BME680_RESET_PERIOD as u32);
|
||||||
.delay_ms(BME680_RESET_PERIOD)
|
|
||||||
.map_err(|_| Error::Delay)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -474,7 +473,7 @@ where
|
||||||
mut i2c: I2C,
|
mut i2c: I2C,
|
||||||
delay: &mut 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 ErrorType>::Error> {
|
||||||
Bme680::soft_reset(&mut i2c, delay, dev_id)?;
|
Bme680::soft_reset(&mut i2c, delay, dev_id)?;
|
||||||
|
|
||||||
debug!("Reading chip id");
|
debug!("Reading chip id");
|
||||||
|
@ -483,14 +482,14 @@ where
|
||||||
debug!("Chip id: {}", chip_id);
|
debug!("Chip id: {}", chip_id);
|
||||||
|
|
||||||
if chip_id == BME680_CHIP_ID {
|
if chip_id == BME680_CHIP_ID {
|
||||||
debug!("Reading calib data");
|
debug!("Reading calibration data");
|
||||||
let calib = Bme680::<I2C, D>::get_calib_data::<I2C>(&mut i2c, dev_id)?;
|
let calibration = Bme680::<I2C, D>::get_calib_data::<I2C>(&mut i2c, dev_id)?;
|
||||||
debug!("Calib data {:?}", calib);
|
debug!("Calibration data {:?}", calibration);
|
||||||
let dev = Bme680 {
|
let dev = Bme680 {
|
||||||
i2c,
|
i2c,
|
||||||
delay: PhantomData,
|
delay: PhantomData,
|
||||||
dev_id,
|
dev_id,
|
||||||
calib,
|
calib: calibration,
|
||||||
power_mode: PowerMode::ForcedMode,
|
power_mode: PowerMode::ForcedMode,
|
||||||
tph_sett: Default::default(),
|
tph_sett: Default::default(),
|
||||||
gas_sett: Default::default(),
|
gas_sett: Default::default(),
|
||||||
|
@ -506,7 +505,7 @@ where
|
||||||
fn bme680_set_regs(
|
fn bme680_set_regs(
|
||||||
&mut self,
|
&mut self,
|
||||||
reg: &[(u8, u8)],
|
reg: &[(u8, u8)],
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(), <I2C as ErrorType>::Error> {
|
||||||
if reg.is_empty() || reg.len() > (BME680_TMP_BUFFER_LENGTH / 2) as usize {
|
if reg.is_empty() || reg.len() > (BME680_TMP_BUFFER_LENGTH / 2) as usize {
|
||||||
return Err(Error::InvalidLength);
|
return Err(Error::InvalidLength);
|
||||||
}
|
}
|
||||||
|
@ -530,7 +529,7 @@ where
|
||||||
&mut self,
|
&mut self,
|
||||||
delay: &mut D,
|
delay: &mut D,
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(), <I2C as ErrorType>::Error> {
|
||||||
let (sensor_settings, desired_settings) = settings;
|
let (sensor_settings, desired_settings) = settings;
|
||||||
let tph_sett = sensor_settings.tph_sett;
|
let tph_sett = sensor_settings.tph_sett;
|
||||||
let gas_sett = sensor_settings.gas_sett;
|
let gas_sett = sensor_settings.gas_sett;
|
||||||
|
@ -657,7 +656,7 @@ where
|
||||||
pub fn get_sensor_settings(
|
pub fn get_sensor_settings(
|
||||||
&mut self,
|
&mut self,
|
||||||
desired_settings: DesiredSensorSettings,
|
desired_settings: DesiredSensorSettings,
|
||||||
) -> Result<SensorSettings, <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<SensorSettings, <I2C as ErrorType>::Error> {
|
||||||
let reg_addr: u8 = 0x70u8;
|
let reg_addr: u8 = 0x70u8;
|
||||||
let mut data_array: [u8; BME680_REG_BUFFER_LENGTH] = [0; BME680_REG_BUFFER_LENGTH];
|
let mut data_array: [u8; BME680_REG_BUFFER_LENGTH] = [0; BME680_REG_BUFFER_LENGTH];
|
||||||
let mut sensor_settings: SensorSettings = Default::default();
|
let mut sensor_settings: SensorSettings = Default::default();
|
||||||
|
@ -713,7 +712,7 @@ where
|
||||||
&mut self,
|
&mut self,
|
||||||
delay: &mut D,
|
delay: &mut D,
|
||||||
target_power_mode: PowerMode,
|
target_power_mode: PowerMode,
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(), <I2C as ErrorType>::Error> {
|
||||||
let mut tmp_pow_mode: u8;
|
let mut tmp_pow_mode: u8;
|
||||||
let mut current_power_mode: PowerMode;
|
let mut current_power_mode: PowerMode;
|
||||||
|
|
||||||
|
@ -733,8 +732,7 @@ where
|
||||||
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)])?;
|
||||||
delay
|
delay
|
||||||
.delay_ms(BME680_POLL_PERIOD_MS)
|
.delay_ms(BME680_POLL_PERIOD_MS as u32);
|
||||||
.map_err(|_| Error::Delay)?;
|
|
||||||
} else {
|
} else {
|
||||||
// TODO do while in Rust?
|
// TODO do while in Rust?
|
||||||
break;
|
break;
|
||||||
|
@ -753,7 +751,7 @@ where
|
||||||
/// Retrieve current sensor power mode via registers
|
/// Retrieve current sensor power mode via registers
|
||||||
pub fn get_sensor_mode(
|
pub fn get_sensor_mode(
|
||||||
&mut self,
|
&mut self,
|
||||||
) -> Result<PowerMode, <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<PowerMode, <I2C as ErrorType>::Error> {
|
||||||
let regs =
|
let regs =
|
||||||
I2CUtil::read_byte(&mut self.i2c, self.dev_id.addr(), BME680_CONF_T_P_MODE_ADDR)?;
|
I2CUtil::read_byte(&mut self.i2c, self.dev_id.addr(), BME680_CONF_T_P_MODE_ADDR)?;
|
||||||
let mode = regs & BME680_MODE_MSK;
|
let mode = regs & BME680_MODE_MSK;
|
||||||
|
@ -792,7 +790,7 @@ where
|
||||||
pub fn get_profile_dur(
|
pub fn get_profile_dur(
|
||||||
&self,
|
&self,
|
||||||
sensor_settings: &SensorSettings,
|
sensor_settings: &SensorSettings,
|
||||||
) -> Result<Duration, <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<Duration, <I2C as ErrorType>::Error> {
|
||||||
let os_to_meas_cycles: [u8; 6] = [0u8, 1u8, 2u8, 4u8, 8u8, 16u8];
|
let os_to_meas_cycles: [u8; 6] = [0u8, 1u8, 2u8, 4u8, 8u8, 16u8];
|
||||||
// TODO check if the following unwrap_ors do not change behaviour
|
// TODO check if the following unwrap_ors do not change behaviour
|
||||||
let mut meas_cycles = os_to_meas_cycles[sensor_settings
|
let mut meas_cycles = os_to_meas_cycles[sensor_settings
|
||||||
|
@ -828,9 +826,9 @@ where
|
||||||
fn get_calib_data<I2CX>(
|
fn get_calib_data<I2CX>(
|
||||||
i2c: &mut I2CX,
|
i2c: &mut I2CX,
|
||||||
dev_id: I2CAddress,
|
dev_id: I2CAddress,
|
||||||
) -> Result<CalibData, <I2CX as Read>::Error, <I2CX as Write>::Error>
|
) -> Result<CalibData, <I2C as ErrorType>::Error>
|
||||||
where
|
where
|
||||||
I2CX: Read + Write,
|
I2CX: I2c,
|
||||||
{
|
{
|
||||||
let mut calib: CalibData = Default::default();
|
let mut calib: CalibData = Default::default();
|
||||||
|
|
||||||
|
@ -842,7 +840,7 @@ where
|
||||||
dev_id.addr(),
|
dev_id.addr(),
|
||||||
BME680_COEFF_ADDR1,
|
BME680_COEFF_ADDR1,
|
||||||
&mut coeff_array[0..(BME680_COEFF_ADDR1_LEN - 1)],
|
&mut coeff_array[0..(BME680_COEFF_ADDR1_LEN - 1)],
|
||||||
)?;
|
).unwrap();
|
||||||
|
|
||||||
I2CUtil::read_bytes::<I2CX>(
|
I2CUtil::read_bytes::<I2CX>(
|
||||||
i2c,
|
i2c,
|
||||||
|
@ -850,7 +848,7 @@ where
|
||||||
BME680_COEFF_ADDR2,
|
BME680_COEFF_ADDR2,
|
||||||
&mut coeff_array
|
&mut coeff_array
|
||||||
[BME680_COEFF_ADDR1_LEN..(BME680_COEFF_ADDR1_LEN + BME680_COEFF_ADDR2_LEN - 1)],
|
[BME680_COEFF_ADDR1_LEN..(BME680_COEFF_ADDR1_LEN + BME680_COEFF_ADDR2_LEN - 1)],
|
||||||
)?;
|
).unwrap();
|
||||||
|
|
||||||
calib.par_t1 = ((coeff_array[34usize] as i32) << 8i32 | coeff_array[33usize] as i32) as u16;
|
calib.par_t1 = ((coeff_array[34usize] as i32) << 8i32 | coeff_array[33usize] as i32) as u16;
|
||||||
calib.par_t2 = ((coeff_array[2usize] as i32) << 8i32 | coeff_array[1usize] as i32) as i16;
|
calib.par_t2 = ((coeff_array[2usize] as i32) << 8i32 | coeff_array[1usize] as i32) as i16;
|
||||||
|
@ -880,15 +878,15 @@ where
|
||||||
calib.par_gh3 = coeff_array[38usize] as i8;
|
calib.par_gh3 = coeff_array[38usize] as i8;
|
||||||
|
|
||||||
calib.res_heat_range =
|
calib.res_heat_range =
|
||||||
(I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_RANGE_ADDR)?
|
(I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_RANGE_ADDR).unwrap()
|
||||||
& 0x30)
|
& 0x30)
|
||||||
/ 16;
|
/ 16;
|
||||||
|
|
||||||
calib.res_heat_val =
|
calib.res_heat_val =
|
||||||
I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_VAL_ADDR)? as i8;
|
I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RES_HEAT_VAL_ADDR).unwrap() as i8;
|
||||||
|
|
||||||
calib.range_sw_err =
|
calib.range_sw_err =
|
||||||
(I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RANGE_SW_ERR_ADDR)?
|
(I2CUtil::read_byte::<I2CX>(i2c, dev_id.addr(), BME680_ADDR_RANGE_SW_ERR_ADDR).unwrap()
|
||||||
& BME680_RSERROR_MSK)
|
& BME680_RSERROR_MSK)
|
||||||
/ 16;
|
/ 16;
|
||||||
|
|
||||||
|
@ -898,7 +896,7 @@ where
|
||||||
fn set_gas_config(
|
fn set_gas_config(
|
||||||
&mut self,
|
&mut self,
|
||||||
gas_sett: GasSett,
|
gas_sett: GasSett,
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(), <I2C as ErrorType>::Error> {
|
||||||
if self.power_mode != PowerMode::ForcedMode {
|
if self.power_mode != PowerMode::ForcedMode {
|
||||||
return Err(Error::DefinePwrMode);
|
return Err(Error::DefinePwrMode);
|
||||||
}
|
}
|
||||||
|
@ -923,7 +921,7 @@ where
|
||||||
self.bme680_set_regs(®)
|
self.bme680_set_regs(®)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_gas_config(&mut self) -> Result<GasSett, <I2C as Read>::Error, <I2C as Write>::Error> {
|
fn get_gas_config(&mut self) -> Result<GasSett, <I2C as ErrorType>::Error> {
|
||||||
let heatr_temp = Some(I2CUtil::read_byte(
|
let heatr_temp = Some(I2CUtil::read_byte(
|
||||||
&mut self.i2c,
|
&mut self.i2c,
|
||||||
self.dev_id.addr(),
|
self.dev_id.addr(),
|
||||||
|
@ -945,11 +943,11 @@ where
|
||||||
Ok(gas_sett)
|
Ok(gas_sett)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve the current sensor informations
|
/// Retrieve the current sensor information
|
||||||
pub fn get_sensor_data(
|
pub fn get_sensor_data(
|
||||||
&mut self,
|
&mut self,
|
||||||
delay: &mut D,
|
delay: &mut D,
|
||||||
) -> Result<(FieldData, FieldDataCondition), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(FieldData, FieldDataCondition), <I2C as ErrorType>::Error> {
|
||||||
let mut buff: [u8; BME680_FIELD_LENGTH] = [0; BME680_FIELD_LENGTH];
|
let mut buff: [u8; BME680_FIELD_LENGTH] = [0; BME680_FIELD_LENGTH];
|
||||||
|
|
||||||
debug!("Buf {:?}, len: {}", buff, buff.len());
|
debug!("Buf {:?}, len: {}", buff, buff.len());
|
||||||
|
@ -999,9 +997,7 @@ where
|
||||||
return Ok((data, FieldDataCondition::NewData));
|
return Ok((data, FieldDataCondition::NewData));
|
||||||
}
|
}
|
||||||
|
|
||||||
delay
|
delay.delay_ms(BME680_POLL_PERIOD_MS as u32);
|
||||||
.delay_ms(BME680_POLL_PERIOD_MS)
|
|
||||||
.map_err(|_| Error::Delay)?;
|
|
||||||
}
|
}
|
||||||
Ok((data, FieldDataCondition::Unchanged))
|
Ok((data, FieldDataCondition::Unchanged))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue