Add documentation to settings and lib
This commit is contained in:
parent
336305d30d
commit
3b84d4f6f3
2 changed files with 46 additions and 10 deletions
33
src/lib.rs
33
src/lib.rs
|
@ -452,7 +452,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub 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 Read>::Error, <I2C as Write>::Error> {
|
||||||
|
@ -474,6 +474,7 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the settings to be used during the sensor measurements
|
||||||
pub fn set_sensor_settings(
|
pub fn set_sensor_settings(
|
||||||
&mut self,
|
&mut self,
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
|
@ -494,7 +495,7 @@ where
|
||||||
self.set_sensor_mode(power_mode)?;
|
self.set_sensor_mode(power_mode)?;
|
||||||
|
|
||||||
let mut element_index = 0;
|
let mut element_index = 0;
|
||||||
/* Selecting the filter */
|
// Selecting the filter
|
||||||
if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) {
|
||||||
let mut data =
|
let mut data =
|
||||||
I2CUtil::read_byte(&mut self.i2c, self.dev_id.addr(), BME680_CONF_ODR_FILT_ADDR)?;
|
I2CUtil::read_byte(&mut self.i2c, self.dev_id.addr(), BME680_CONF_ODR_FILT_ADDR)?;
|
||||||
|
@ -521,7 +522,7 @@ where
|
||||||
element_index += 1;
|
element_index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Selecting heater T,P oversampling for the sensor */
|
// Selecting heater T,P oversampling for the sensor
|
||||||
if desired_settings
|
if desired_settings
|
||||||
.contains(DesiredSensorSettings::OST_SEL | DesiredSensorSettings::OSP_SEL)
|
.contains(DesiredSensorSettings::OST_SEL | DesiredSensorSettings::OSP_SEL)
|
||||||
{
|
{
|
||||||
|
@ -550,7 +551,7 @@ where
|
||||||
element_index += 1;
|
element_index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Selecting humidity oversampling for the sensor */
|
// Selecting humidity oversampling for the sensor
|
||||||
if desired_settings.contains(DesiredSensorSettings::OSH_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::OSH_SEL) {
|
||||||
debug!("OSH_SEL: true");
|
debug!("OSH_SEL: true");
|
||||||
let tph_sett_os_hum =
|
let tph_sett_os_hum =
|
||||||
|
@ -562,7 +563,7 @@ where
|
||||||
element_index += 1;
|
element_index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Selecting the runGas and NB conversion settings for the sensor */
|
// Selecting the runGas and NB conversion settings for the sensor
|
||||||
if desired_settings
|
if desired_settings
|
||||||
.contains(DesiredSensorSettings::RUN_GAS_SEL | DesiredSensorSettings::NBCONV_SEL)
|
.contains(DesiredSensorSettings::RUN_GAS_SEL | DesiredSensorSettings::NBCONV_SEL)
|
||||||
{
|
{
|
||||||
|
@ -592,11 +593,16 @@ where
|
||||||
|
|
||||||
self.bme680_set_regs(®[0..element_index])?;
|
self.bme680_set_regs(®[0..element_index])?;
|
||||||
|
|
||||||
/* Restore previous intended power mode */
|
// Restore previous intended power mode
|
||||||
self.power_mode = intended_power_mode;
|
self.power_mode = intended_power_mode;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Retrieve settings from sensor registers
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `desired_settings` - Settings to be retrieved. Setting values may stay `None` if not retrieved.
|
||||||
pub fn get_sensor_settings(
|
pub fn get_sensor_settings(
|
||||||
&mut self,
|
&mut self,
|
||||||
desired_settings: DesiredSensorSettings,
|
desired_settings: DesiredSensorSettings,
|
||||||
|
@ -647,6 +653,11 @@ where
|
||||||
Ok(sensor_settings)
|
Ok(sensor_settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the sensor into a certain power mode
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `target_power_mode` - Desired target power mode
|
||||||
pub fn set_sensor_mode(
|
pub fn set_sensor_mode(
|
||||||
&mut self,
|
&mut self,
|
||||||
target_power_mode: PowerMode,
|
target_power_mode: PowerMode,
|
||||||
|
@ -654,18 +665,18 @@ where
|
||||||
let mut tmp_pow_mode: u8;
|
let mut tmp_pow_mode: u8;
|
||||||
let mut current_power_mode: PowerMode;
|
let mut current_power_mode: PowerMode;
|
||||||
|
|
||||||
/* Call repeatedly until in sleep */
|
// Call repeatedly until in sleep
|
||||||
loop {
|
loop {
|
||||||
tmp_pow_mode =
|
tmp_pow_mode =
|
||||||
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)?;
|
||||||
|
|
||||||
/* Put to sleep before changing mode */
|
// Put to sleep before changing mode
|
||||||
current_power_mode = PowerMode::from(tmp_pow_mode & BME680_MODE_MSK);
|
current_power_mode = PowerMode::from(tmp_pow_mode & BME680_MODE_MSK);
|
||||||
|
|
||||||
debug!("Current power mode: {:?}", current_power_mode);
|
debug!("Current power mode: {:?}", current_power_mode);
|
||||||
|
|
||||||
if current_power_mode != PowerMode::SleepMode {
|
if current_power_mode != PowerMode::SleepMode {
|
||||||
/* Set to sleep*/
|
// Set to sleep
|
||||||
tmp_pow_mode = tmp_pow_mode & !BME680_MODE_MSK;
|
tmp_pow_mode = tmp_pow_mode & !BME680_MODE_MSK;
|
||||||
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)])?;
|
||||||
|
@ -676,7 +687,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Already in sleep */
|
// Already in sleep
|
||||||
if target_power_mode != PowerMode::SleepMode {
|
if target_power_mode != PowerMode::SleepMode {
|
||||||
tmp_pow_mode = tmp_pow_mode & !BME680_MODE_MSK | target_power_mode.value();
|
tmp_pow_mode = tmp_pow_mode & !BME680_MODE_MSK | target_power_mode.value();
|
||||||
debug!("Already in sleep Target power mode: {}", tmp_pow_mode);
|
debug!("Already in sleep Target power mode: {}", tmp_pow_mode);
|
||||||
|
@ -685,6 +696,7 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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 Read>::Error, <I2C as Write>::Error> {
|
||||||
|
@ -885,6 +897,7 @@ where
|
||||||
Ok(gas_sett)
|
Ok(gas_sett)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Retrieve the current sensor informations
|
||||||
pub fn get_sensor_data(
|
pub fn get_sensor_data(
|
||||||
&mut self,
|
&mut self,
|
||||||
) -> Result<(FieldData, FieldDataCondition), <I2C as Read>::Error, <I2C as Write>::Error> {
|
) -> Result<(FieldData, FieldDataCondition), <I2C as Read>::Error, <I2C as Write>::Error> {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
|
|
||||||
|
/// Over-sampling settings
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum OversamplingSetting {
|
pub enum OversamplingSetting {
|
||||||
|
@ -26,6 +27,7 @@ impl OversamplingSetting {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// IIR filter settings
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum IIRFilterSize {
|
pub enum IIRFilterSize {
|
||||||
|
@ -56,12 +58,17 @@ impl IIRFilterSize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Temperature settings
|
||||||
#[derive(Debug, Default, Copy)]
|
#[derive(Debug, Default, Copy)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct TphSett {
|
pub struct TphSett {
|
||||||
|
/// Humidity oversampling
|
||||||
pub os_hum: Option<OversamplingSetting>,
|
pub os_hum: Option<OversamplingSetting>,
|
||||||
|
/// Temperature oversampling
|
||||||
pub os_temp: Option<OversamplingSetting>,
|
pub os_temp: Option<OversamplingSetting>,
|
||||||
|
/// Pressure oversampling
|
||||||
pub os_pres: Option<OversamplingSetting>,
|
pub os_pres: Option<OversamplingSetting>,
|
||||||
|
/// Filter coefficient
|
||||||
pub filter: Option<IIRFilterSize>,
|
pub filter: Option<IIRFilterSize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +78,7 @@ impl Clone for TphSett {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gas measurement settings
|
||||||
#[derive(Debug, Default, Copy)]
|
#[derive(Debug, Default, Copy)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct GasSett {
|
pub struct GasSett {
|
||||||
|
@ -92,6 +100,7 @@ impl Clone for GasSett {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Stores gas and temperature settings
|
||||||
#[derive(Debug, Default, Copy)]
|
#[derive(Debug, Default, Copy)]
|
||||||
pub struct SensorSettings {
|
pub struct SensorSettings {
|
||||||
/// Gas settings
|
/// Gas settings
|
||||||
|
@ -130,6 +139,20 @@ bitflags! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Builder to construct the desired settings
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// let settings = SettingsBuilder::new()
|
||||||
|
/// .with_humidity_oversampling(OversamplingSetting::OS2x)
|
||||||
|
/// .with_pressure_oversampling(OversamplingSetting::OS4x)
|
||||||
|
/// .with_temperature_oversampling(OversamplingSetting::OS8x)
|
||||||
|
/// .with_temperature_filter(IIRFilterSize::Size3)
|
||||||
|
/// .with_gas_measurement(Duration::from_millis(1500), 320, 25)
|
||||||
|
/// .with_run_gas(true)
|
||||||
|
/// .build();
|
||||||
|
/// ```
|
||||||
pub struct SettingsBuilder {
|
pub struct SettingsBuilder {
|
||||||
desired_settings: DesiredSensorSettings,
|
desired_settings: DesiredSensorSettings,
|
||||||
sensor_settings: SensorSettings,
|
sensor_settings: SensorSettings,
|
||||||
|
|
Loading…
Reference in a new issue