fix code formatting
This commit is contained in:
parent
fe38f02a9c
commit
f77e251bfb
2 changed files with 108 additions and 45 deletions
31
src/i2c.rs
31
src/i2c.rs
|
@ -1,5 +1,5 @@
|
|||
use core::fmt::{Display, Formatter};
|
||||
use anyhow::anyhow;
|
||||
use core::fmt::{Display, Formatter};
|
||||
use embedded_hal::i2c::I2c;
|
||||
|
||||
///
|
||||
|
@ -46,11 +46,22 @@ impl I2CUtility {
|
|||
|
||||
i2c_handle
|
||||
.write(device_address, &[register_address])
|
||||
.map_err(|e| anyhow!("Failed to write a byte {} to device {}: {:?}", register_address, device_address, e))?;
|
||||
.map_err(|e| {
|
||||
anyhow!(
|
||||
"Failed to write a byte {} to device {}: {:?}",
|
||||
register_address,
|
||||
device_address,
|
||||
e
|
||||
)
|
||||
})?;
|
||||
|
||||
match i2c_handle.read(device_address, &mut buf) {
|
||||
Ok(()) => Ok(buf[0]),
|
||||
Err(_e) => Err(anyhow!("Failed to read byte {} from device {}", register_address, device_address)),
|
||||
Err(_e) => Err(anyhow!(
|
||||
"Failed to read byte {} from device {}",
|
||||
register_address,
|
||||
device_address
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,11 +74,21 @@ impl I2CUtility {
|
|||
) -> Result<(), anyhow::Error> {
|
||||
i2c_handle
|
||||
.write(device_address, &[register_address])
|
||||
.map_err(|_e| anyhow!("Failed to write a byte {} from device {}", register_address, device_address))?;
|
||||
.map_err(|_e| {
|
||||
anyhow!(
|
||||
"Failed to write a byte {} from device {}",
|
||||
register_address,
|
||||
device_address
|
||||
)
|
||||
})?;
|
||||
|
||||
match i2c_handle.read(device_address, buffer) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(_e) => Err(anyhow!("Failed to read bytes from register {} and device {}", register_address, device_address)),
|
||||
Err(_e) => Err(anyhow!(
|
||||
"Failed to read bytes from register {} and device {}",
|
||||
register_address,
|
||||
device_address
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
100
src/lib.rs
100
src/lib.rs
|
@ -77,10 +77,10 @@ use crate::calculation::Calculation;
|
|||
use crate::hal::delay::DelayNs;
|
||||
use crate::hal::i2c::I2c;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use core::marker::PhantomData;
|
||||
use core::ops::DerefMut;
|
||||
use core::time::Duration;
|
||||
use anyhow::anyhow;
|
||||
use embedded_hal as hal;
|
||||
use log::{debug, error, info};
|
||||
|
||||
|
@ -315,13 +315,21 @@ fn boundary_check_u8(
|
|||
if value < min {
|
||||
const MIN: &str = "Boundary check failure, value exceeds maximum";
|
||||
error!("{}, value name: {}", MIN, value_name);
|
||||
return Err(anyhow!("Failed MIN={} boundary check for {}", MIN, value_name));
|
||||
return Err(anyhow!(
|
||||
"Failed MIN={} boundary check for {}",
|
||||
MIN,
|
||||
value_name
|
||||
));
|
||||
}
|
||||
|
||||
if value > max {
|
||||
const MAX: &str = "Boundary check, value exceeds minimum";
|
||||
error!("{}, value name: {}", MAX, value_name);
|
||||
return Err(anyhow!("Failed MAX={} boundary check for {}", MAX, value_name));
|
||||
return Err(anyhow!(
|
||||
"Failed MAX={} boundary check for {}",
|
||||
MAX,
|
||||
value_name
|
||||
));
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
|
@ -706,7 +714,10 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
|||
Ok(duration)
|
||||
}
|
||||
|
||||
fn get_calibration_data<I2CX>(i2c: &mut I2CX, device_address: Address) -> Result<CalibrationData, anyhow::Error>
|
||||
fn get_calibration_data<I2CX>(
|
||||
i2c: &mut I2CX,
|
||||
device_address: Address,
|
||||
) -> Result<CalibrationData, anyhow::Error>
|
||||
where
|
||||
I2CX: I2c,
|
||||
{
|
||||
|
@ -721,7 +732,12 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
|||
BME680_COEFF_ADDR1,
|
||||
&mut coefficients_array[0..(BME680_COEFF_ADDR1_LEN - 1)],
|
||||
)
|
||||
.map_err(|_e| anyhow!("Failed to get calibration data from device: {}", device_address))?;
|
||||
.map_err(|_e| {
|
||||
anyhow!(
|
||||
"Failed to get calibration data from device: {}",
|
||||
device_address
|
||||
)
|
||||
})?;
|
||||
|
||||
I2CUtility::read_bytes::<I2CX>(
|
||||
i2c,
|
||||
|
@ -730,47 +746,70 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
|||
&mut coefficients_array
|
||||
[BME680_COEFF_ADDR1_LEN..(BME680_COEFF_ADDR1_LEN + BME680_COEFF_ADDR2_LEN - 1)],
|
||||
)
|
||||
.map_err(|_e| anyhow!("Failed to get calibration data from device: {}", device_address))?;
|
||||
.map_err(|_e| {
|
||||
anyhow!(
|
||||
"Failed to get calibration data from device: {}",
|
||||
device_address
|
||||
)
|
||||
})?;
|
||||
|
||||
calibration_data.par_t1 = ((coefficients_array[34usize] as i32) << 8i32 | coefficients_array[33usize] as i32) as u16;
|
||||
calibration_data.par_t2 = ((coefficients_array[2usize] as i32) << 8i32 | coefficients_array[1usize] as i32) as i16;
|
||||
calibration_data.par_t1 = ((coefficients_array[34usize] as i32) << 8i32
|
||||
| coefficients_array[33usize] as i32) as u16;
|
||||
calibration_data.par_t2 = ((coefficients_array[2usize] as i32) << 8i32
|
||||
| coefficients_array[1usize] as i32) as i16;
|
||||
calibration_data.par_t3 = coefficients_array[3usize] as i8;
|
||||
calibration_data.par_p1 = ((coefficients_array[6usize] as i32) << 8i32 | coefficients_array[5usize] as i32) as u16;
|
||||
calibration_data.par_p2 = ((coefficients_array[8usize] as i32) << 8i32 | coefficients_array[7usize] as i32) as i16;
|
||||
calibration_data.par_p1 = ((coefficients_array[6usize] as i32) << 8i32
|
||||
| coefficients_array[5usize] as i32) as u16;
|
||||
calibration_data.par_p2 = ((coefficients_array[8usize] as i32) << 8i32
|
||||
| coefficients_array[7usize] as i32) as i16;
|
||||
calibration_data.par_p3 = coefficients_array[9usize] as i8;
|
||||
calibration_data.par_p4 = ((coefficients_array[12usize] as i32) << 8i32 | coefficients_array[11usize] as i32) as i16;
|
||||
calibration_data.par_p5 = ((coefficients_array[14usize] as i32) << 8i32 | coefficients_array[13usize] as i32) as i16;
|
||||
calibration_data.par_p4 = ((coefficients_array[12usize] as i32) << 8i32
|
||||
| coefficients_array[11usize] as i32) as i16;
|
||||
calibration_data.par_p5 = ((coefficients_array[14usize] as i32) << 8i32
|
||||
| coefficients_array[13usize] as i32) as i16;
|
||||
calibration_data.par_p6 = coefficients_array[16usize] as i8;
|
||||
calibration_data.par_p7 = coefficients_array[15usize] as i8;
|
||||
calibration_data.par_p8 = ((coefficients_array[20usize] as i32) << 8i32 | coefficients_array[19usize] as i32) as i16;
|
||||
calibration_data.par_p9 = ((coefficients_array[22usize] as i32) << 8i32 | coefficients_array[21usize] as i32) as i16;
|
||||
calibration_data.par_p8 = ((coefficients_array[20usize] as i32) << 8i32
|
||||
| coefficients_array[19usize] as i32) as i16;
|
||||
calibration_data.par_p9 = ((coefficients_array[22usize] as i32) << 8i32
|
||||
| coefficients_array[21usize] as i32) as i16;
|
||||
calibration_data.par_p10 = coefficients_array[23usize];
|
||||
calibration_data.par_h1 =
|
||||
((coefficients_array[27usize] as i32) << 4i32 | coefficients_array[26usize] as i32 & 0xfi32) as u16;
|
||||
calibration_data.par_h2 =
|
||||
((coefficients_array[25usize] as i32) << 4i32 | coefficients_array[26usize] as i32 >> 4i32) as u16;
|
||||
calibration_data.par_h1 = ((coefficients_array[27usize] as i32) << 4i32
|
||||
| coefficients_array[26usize] as i32 & 0xfi32) as u16;
|
||||
calibration_data.par_h2 = ((coefficients_array[25usize] as i32) << 4i32
|
||||
| coefficients_array[26usize] as i32 >> 4i32) as u16;
|
||||
calibration_data.par_h3 = coefficients_array[28usize] as i8;
|
||||
calibration_data.par_h4 = coefficients_array[29usize] as i8;
|
||||
calibration_data.par_h5 = coefficients_array[30usize] as i8;
|
||||
calibration_data.par_h6 = coefficients_array[31usize];
|
||||
calibration_data.par_h7 = coefficients_array[32usize] as i8;
|
||||
calibration_data.par_gh1 = coefficients_array[37usize] as i8;
|
||||
calibration_data.par_gh2 =
|
||||
((coefficients_array[36usize] as i32) << 8i32 | coefficients_array[35usize] as i32) as i16;
|
||||
calibration_data.par_gh2 = ((coefficients_array[36usize] as i32) << 8i32
|
||||
| coefficients_array[35usize] as i32) as i16;
|
||||
calibration_data.par_gh3 = coefficients_array[38usize] as i8;
|
||||
|
||||
calibration_data.res_heat_range =
|
||||
(I2CUtility::read_byte::<I2CX>(i2c, device_address.addr(), BME680_ADDR_RES_HEAT_RANGE_ADDR)
|
||||
calibration_data.res_heat_range = (I2CUtility::read_byte::<I2CX>(
|
||||
i2c,
|
||||
device_address.addr(),
|
||||
BME680_ADDR_RES_HEAT_RANGE_ADDR,
|
||||
)
|
||||
.map_err(|_e| anyhow!("Failed to read from register BME680_ADDR_RES_HEAT_RANGE_ADDR"))?
|
||||
& 0x30)
|
||||
/ 16;
|
||||
|
||||
calibration_data.res_heat_val =
|
||||
I2CUtility::read_byte::<I2CX>(i2c, device_address.addr(), BME680_ADDR_RES_HEAT_VAL_ADDR)
|
||||
.map_err(|_e| anyhow!("Failed to read from register BME680_ADDR_RES_HEAT_VAL_ADDR"))? as i8;
|
||||
calibration_data.res_heat_val = I2CUtility::read_byte::<I2CX>(
|
||||
i2c,
|
||||
device_address.addr(),
|
||||
BME680_ADDR_RES_HEAT_VAL_ADDR,
|
||||
)
|
||||
.map_err(|_e| anyhow!("Failed to read from register BME680_ADDR_RES_HEAT_VAL_ADDR"))?
|
||||
as i8;
|
||||
|
||||
calibration_data.range_sw_err =
|
||||
(I2CUtility::read_byte::<I2CX>(i2c, device_address.addr(), BME680_ADDR_RANGE_SW_ERR_ADDR)
|
||||
calibration_data.range_sw_err = (I2CUtility::read_byte::<I2CX>(
|
||||
i2c,
|
||||
device_address.addr(),
|
||||
BME680_ADDR_RANGE_SW_ERR_ADDR,
|
||||
)
|
||||
.map_err(|_e| anyhow!("Failed to read from register BME680_ADDR_RANGE_SW_ERR_ADDR"))?
|
||||
& BME680_RSERROR_MSK)
|
||||
/ 16;
|
||||
|
@ -879,8 +918,11 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
|||
data.temperature = temp;
|
||||
data.pressure = Calculation::pressure(&self.calibration_data, t_fine, adc_pressure);
|
||||
data.humidity = Calculation::humidity(&self.calibration_data, t_fine, adc_humidity);
|
||||
data.gas_resistance =
|
||||
Calculation::gas_resistance(&self.calibration_data, adc_gas_resistance, gas_range);
|
||||
data.gas_resistance = Calculation::gas_resistance(
|
||||
&self.calibration_data,
|
||||
adc_gas_resistance,
|
||||
gas_range,
|
||||
);
|
||||
return Ok((data, FieldDataCondition::NewData));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue