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