Rename Bme680Error to Error
This commit is contained in:
parent
301cd55df4
commit
ba7a1dd09f
2 changed files with 18 additions and 18 deletions
|
@ -12,10 +12,9 @@ use std::result;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
fn main() -> result::Result<
|
fn main(
|
||||||
(),
|
) -> result::Result<(), Error<<hal::I2cdev as i2c::Read>::Error, <hal::I2cdev as i2c::Write>::Error>>
|
||||||
Bme680Error<<hal::I2cdev as i2c::Read>::Error, <hal::I2cdev as i2c::Write>::Error>,
|
{
|
||||||
> {
|
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
|
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
|
||||||
|
|
29
src/lib.rs
29
src/lib.rs
|
@ -88,8 +88,9 @@ const BME680_HEAT_STAB_MSK: u8 = 0x10;
|
||||||
const BME680_TMP_BUFFER_LENGTH: usize = 40;
|
const BME680_TMP_BUFFER_LENGTH: usize = 40;
|
||||||
const BME680_REG_BUFFER_LENGTH: usize = 6;
|
const BME680_REG_BUFFER_LENGTH: usize = 6;
|
||||||
|
|
||||||
|
/// All possible errors in this crate
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Bme680Error<R, W> {
|
pub enum Error<R, W> {
|
||||||
///
|
///
|
||||||
/// aka BME680_E_COM_FAIL
|
/// aka BME680_E_COM_FAIL
|
||||||
///
|
///
|
||||||
|
@ -117,7 +118,7 @@ pub enum Bme680Error<R, W> {
|
||||||
BoundaryCheckFailure(&'static str),
|
BoundaryCheckFailure(&'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Result<T, R, W> = result::Result<T, Bme680Error<R, W>>;
|
pub type Result<T, R, W> = result::Result<T, Error<R, W>>;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Power mode settings
|
/// Power mode settings
|
||||||
|
@ -287,11 +288,11 @@ impl I2CUtil {
|
||||||
let mut buf = [0; 1];
|
let mut buf = [0; 1];
|
||||||
|
|
||||||
i2c.write(dev_id, &mut [reg_addr])
|
i2c.write(dev_id, &mut [reg_addr])
|
||||||
.map_err(|e| Bme680Error::I2CWrite(e))?;
|
.map_err(|e| Error::I2CWrite(e))?;
|
||||||
|
|
||||||
match i2c.read(dev_id, &mut buf) {
|
match i2c.read(dev_id, &mut buf) {
|
||||||
Ok(()) => Ok(buf[0]),
|
Ok(()) => Ok(buf[0]),
|
||||||
Err(e) => Err(Bme680Error::I2CRead(e)),
|
Err(e) => Err(Error::I2CRead(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -305,11 +306,11 @@ impl I2CUtil {
|
||||||
I2C: Read + Write,
|
I2C: Read + Write,
|
||||||
{
|
{
|
||||||
i2c.write(dev_id, &mut [reg_addr])
|
i2c.write(dev_id, &mut [reg_addr])
|
||||||
.map_err(|e| Bme680Error::I2CWrite(e))?;
|
.map_err(|e| Error::I2CWrite(e))?;
|
||||||
|
|
||||||
match i2c.read(dev_id, buf) {
|
match i2c.read(dev_id, buf) {
|
||||||
Ok(()) => Ok(()),
|
Ok(()) => Ok(()),
|
||||||
Err(e) => Err(Bme680Error::I2CRead(e)),
|
Err(e) => Err(Error::I2CRead(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -337,18 +338,18 @@ fn boundary_check<I2C>(
|
||||||
where
|
where
|
||||||
I2C: Read + Write,
|
I2C: Read + Write,
|
||||||
{
|
{
|
||||||
let value = value.ok_or(Bme680Error::BoundaryCheckFailure(value_name))?;
|
let value = value.ok_or(Error::BoundaryCheckFailure(value_name))?;
|
||||||
|
|
||||||
if value < min {
|
if value < min {
|
||||||
const MIN: &str = "Boundary check failure, value exceeds maximum";
|
const MIN: &str = "Boundary check failure, value exceeds maximum";
|
||||||
error!("{}, value name: {}", MIN, value_name);
|
error!("{}, value name: {}", MIN, value_name);
|
||||||
return Err(Bme680Error::BoundaryCheckFailure(MIN));
|
return Err(Error::BoundaryCheckFailure(MIN));
|
||||||
}
|
}
|
||||||
|
|
||||||
if value > max {
|
if value > max {
|
||||||
const MAX: &str = "Boundary check, value exceeds minimum";
|
const MAX: &str = "Boundary check, value exceeds minimum";
|
||||||
error!("{}, value name: {}", MAX, value_name);
|
error!("{}, value name: {}", MAX, value_name);
|
||||||
return Err(Bme680Error::BoundaryCheckFailure(MAX));
|
return Err(Error::BoundaryCheckFailure(MAX));
|
||||||
}
|
}
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
@ -366,7 +367,7 @@ where
|
||||||
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(|e| Bme680Error::I2CWrite(e))?;
|
.map_err(|e| Error::I2CWrite(e))?;
|
||||||
|
|
||||||
delay.delay_ms(BME680_RESET_PERIOD);
|
delay.delay_ms(BME680_RESET_PERIOD);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -401,7 +402,7 @@ where
|
||||||
Ok(dev)
|
Ok(dev)
|
||||||
} else {
|
} else {
|
||||||
error!("Device does not match chip id {}", BME680_CHIP_ID);
|
error!("Device does not match chip id {}", BME680_CHIP_ID);
|
||||||
Err(Bme680Error::DeviceNotFound)
|
Err(Error::DeviceNotFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -410,7 +411,7 @@ where
|
||||||
reg: &[(u8, u8)],
|
reg: &[(u8, u8)],
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::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(Bme680Error::InvalidLength);
|
return Err(Error::InvalidLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (reg_addr, reg_data) in reg {
|
for (reg_addr, reg_data) in reg {
|
||||||
|
@ -421,7 +422,7 @@ where
|
||||||
);
|
);
|
||||||
self.i2c
|
self.i2c
|
||||||
.write(self.dev_id.addr(), &tmp_buff)
|
.write(self.dev_id.addr(), &tmp_buff)
|
||||||
.map_err(|e| Bme680Error::I2CWrite(e))?;
|
.map_err(|e| Error::I2CWrite(e))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -808,7 +809,7 @@ where
|
||||||
gas_sett: GasSett,
|
gas_sett: GasSett,
|
||||||
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(), <I2C as Read>::Error, <I2C as Write>::Error> {
|
||||||
if self.power_mode != PowerMode::ForcedMode {
|
if self.power_mode != PowerMode::ForcedMode {
|
||||||
return Err(Bme680Error::DefinePwrMode);
|
return Err(Error::DefinePwrMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check whether unwrap_or changes behaviour
|
// TODO check whether unwrap_or changes behaviour
|
||||||
|
|
Loading…
Reference in a new issue