refactor: fix settings variable names
This commit is contained in:
parent
43ca150615
commit
b5d2f80e15
2 changed files with 95 additions and 85 deletions
76
src/lib.rs
76
src/lib.rs
|
@ -65,8 +65,8 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
pub use self::settings::{
|
pub use self::settings::{
|
||||||
DesiredSensorSettings, GasSett, IIRFilterSize, OversamplingSetting, SensorSettings, Settings,
|
DesiredSensorSettings, GasSettings, IIRFilterSize, OversamplingSetting, SensorSettings, Settings,
|
||||||
SettingsBuilder, TphSett,
|
SettingsBuilder, TemperatureSettings,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod calculation;
|
mod calculation;
|
||||||
|
@ -433,10 +433,10 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
) -> Result<(), Bme680Error> {
|
) -> Result<(), Bme680Error> {
|
||||||
let (sensor_settings, desired_settings) = settings;
|
let (sensor_settings, desired_settings) = settings;
|
||||||
let tph_sett = sensor_settings.tph_sett;
|
let tph_sett = sensor_settings.temperature_settings;
|
||||||
let gas_sett = sensor_settings.gas_sett;
|
let gas_sett = sensor_settings.gas_settings;
|
||||||
|
|
||||||
self.temperature_offset = sensor_settings.tph_sett.temperature_offset.unwrap_or(0f32);
|
self.temperature_offset = sensor_settings.temperature_settings.temperature_offset.unwrap_or(0f32);
|
||||||
|
|
||||||
let mut reg: [(u8, u8); BME680_REG_BUFFER_LENGTH] = [(0, 0); BME680_REG_BUFFER_LENGTH];
|
let mut reg: [(u8, u8); BME680_REG_BUFFER_LENGTH] = [(0, 0); BME680_REG_BUFFER_LENGTH];
|
||||||
let intended_power_mode = self.power_mode;
|
let intended_power_mode = self.power_mode;
|
||||||
|
@ -451,22 +451,22 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
|
|
||||||
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_SIZE_SEL) {
|
||||||
let mut data =
|
let mut data =
|
||||||
I2CUtility::read_byte(&mut self.i2c_bus_handle, self.device_address.addr(), BME680_CONF_ODR_FILT_ADDR)?;
|
I2CUtility::read_byte(&mut self.i2c_bus_handle, self.device_address.addr(), BME680_CONF_ODR_FILT_ADDR)?;
|
||||||
|
|
||||||
debug!("FILTER_SEL: true");
|
debug!("FILTER_SEL: true");
|
||||||
data = (data as i32 & !0x1ci32
|
data = (data as i32 & !0x1ci32
|
||||||
| (tph_sett.filter.unwrap_or(IIRFilterSize::Size0) as i32) << 2i32 & 0x1ci32)
|
| (tph_sett.filter_size.unwrap_or(IIRFilterSize::Size0) as i32) << 2i32 & 0x1ci32)
|
||||||
as u8;
|
as u8;
|
||||||
reg[element_index] = (BME680_CONF_ODR_FILT_ADDR, data);
|
reg[element_index] = (BME680_CONF_ODR_FILT_ADDR, data);
|
||||||
element_index += 1;
|
element_index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if desired_settings.contains(DesiredSensorSettings::HCNTRL_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::HUMIDITY_CONTROL_SEL) {
|
||||||
debug!("HCNTRL_SEL: true");
|
debug!("HCNTRL_SEL: true");
|
||||||
let gas_sett_heatr_ctrl =
|
let gas_sett_heatr_ctrl =
|
||||||
boundary_check_u8(gas_sett.heatr_ctrl, "GasSett.heatr_ctrl", 0x0u8, 0x8u8)?;
|
boundary_check_u8(gas_sett.heater_control, "GasSett.heatr_ctrl", 0x0u8, 0x8u8)?;
|
||||||
let mut data = I2CUtility::read_byte(
|
let mut data = I2CUtility::read_byte(
|
||||||
&mut self.i2c_bus_handle,
|
&mut self.i2c_bus_handle,
|
||||||
self.device_address.addr(),
|
self.device_address.addr(),
|
||||||
|
@ -487,7 +487,7 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
if desired_settings.contains(DesiredSensorSettings::OST_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::OST_SEL) {
|
||||||
debug!("OST_SEL: true");
|
debug!("OST_SEL: true");
|
||||||
let tph_sett_os_temp = boundary_check_u8(
|
let tph_sett_os_temp = boundary_check_u8(
|
||||||
tph_sett.os_temp.map(|x| x as u8),
|
tph_sett.temperature_oversampling.map(|x| x as u8),
|
||||||
"TphSett.os_temp",
|
"TphSett.os_temp",
|
||||||
0,
|
0,
|
||||||
5,
|
5,
|
||||||
|
@ -497,7 +497,7 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
|
|
||||||
if desired_settings.contains(DesiredSensorSettings::OSP_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::OSP_SEL) {
|
||||||
debug!("OSP_SEL: true");
|
debug!("OSP_SEL: true");
|
||||||
let tph_sett_os_pres = tph_sett.os_temp.unwrap_or(OversamplingSetting::OS1x);
|
let tph_sett_os_pres = tph_sett.temperature_oversampling.unwrap_or(OversamplingSetting::OS1x);
|
||||||
data = (data as i32 & !0x1ci32 | (tph_sett_os_pres as i32) << 2i32 & 0x1ci32) as u8;
|
data = (data as i32 & !0x1ci32 | (tph_sett_os_pres as i32) << 2i32 & 0x1ci32) as u8;
|
||||||
}
|
}
|
||||||
reg[element_index] = (BME680_CONF_T_P_MODE_ADDR, data);
|
reg[element_index] = (BME680_CONF_T_P_MODE_ADDR, data);
|
||||||
|
@ -508,7 +508,7 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
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 =
|
||||||
boundary_check_u8(tph_sett.os_hum.map(|x| x as u8), "TphSett.os_hum", 0, 5)?;
|
boundary_check_u8(tph_sett.humidity_oversampling.map(|x| x as u8), "TphSett.os_hum", 0, 5)?;
|
||||||
let mut data =
|
let mut data =
|
||||||
I2CUtility::read_byte(&mut self.i2c_bus_handle, self.device_address.addr(), BME680_CONF_OS_H_ADDR)?;
|
I2CUtility::read_byte(&mut self.i2c_bus_handle, self.device_address.addr(), BME680_CONF_OS_H_ADDR)?;
|
||||||
data = (data as i32 & !0x7i32 | tph_sett_os_hum as i32 & 0x7i32) as u8;
|
data = (data as i32 & !0x7i32 | tph_sett_os_hum as i32 & 0x7i32) as u8;
|
||||||
|
@ -529,7 +529,7 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
if desired_settings.contains(DesiredSensorSettings::RUN_GAS_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::RUN_GAS_SEL) {
|
||||||
debug!("RUN_GAS_SEL: true");
|
debug!("RUN_GAS_SEL: true");
|
||||||
data = (data as i32 & !0x10i32
|
data = (data as i32 & !0x10i32
|
||||||
| (gas_sett.run_gas_measurement as i32) << 4i32 & 0x10i32)
|
| (gas_sett.enable_gas_measurement as i32) << 4i32 & 0x10i32)
|
||||||
as u8;
|
as u8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -567,11 +567,11 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
I2CUtility::read_bytes(&mut self.i2c_bus_handle, self.device_address.addr(), reg_addr, &mut data_array)?;
|
I2CUtility::read_bytes(&mut self.i2c_bus_handle, self.device_address.addr(), reg_addr, &mut data_array)?;
|
||||||
|
|
||||||
if desired_settings.contains(DesiredSensorSettings::GAS_MEAS_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::GAS_MEAS_SEL) {
|
||||||
sensor_settings.gas_sett = self.get_gas_config()?;
|
sensor_settings.gas_settings = self.get_gas_config()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if desired_settings.contains(DesiredSensorSettings::FILTER_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::FILTER_SIZE_SEL) {
|
||||||
sensor_settings.tph_sett.filter = Some(IIRFilterSize::from_u8(
|
sensor_settings.temperature_settings.filter_size = Some(IIRFilterSize::from_u8(
|
||||||
((data_array[5usize] as i32 & 0x1ci32) >> 2i32) as u8,
|
((data_array[5usize] as i32 & 0x1ci32) >> 2i32) as u8,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -581,24 +581,24 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
{
|
{
|
||||||
let os_temp: u8 = ((data_array[4usize] as i32 & 0xe0i32) >> 5i32) as u8;
|
let os_temp: u8 = ((data_array[4usize] as i32 & 0xe0i32) >> 5i32) as u8;
|
||||||
let os_pres: u8 = ((data_array[4usize] as i32 & 0x1ci32) >> 2i32) as u8;
|
let os_pres: u8 = ((data_array[4usize] as i32 & 0x1ci32) >> 2i32) as u8;
|
||||||
sensor_settings.tph_sett.os_temp = Some(OversamplingSetting::from_u8(os_temp));
|
sensor_settings.temperature_settings.temperature_oversampling = Some(OversamplingSetting::from_u8(os_temp));
|
||||||
sensor_settings.tph_sett.os_pres = Some(OversamplingSetting::from_u8(os_pres));
|
sensor_settings.temperature_settings.pressure_oversampling = Some(OversamplingSetting::from_u8(os_pres));
|
||||||
}
|
}
|
||||||
|
|
||||||
if desired_settings.contains(DesiredSensorSettings::OSH_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::OSH_SEL) {
|
||||||
let os_hum: u8 = (data_array[2usize] as i32 & 0x7i32) as u8;
|
let os_hum: u8 = (data_array[2usize] as i32 & 0x7i32) as u8;
|
||||||
sensor_settings.tph_sett.os_hum = Some(OversamplingSetting::from_u8(os_hum));
|
sensor_settings.temperature_settings.humidity_oversampling = Some(OversamplingSetting::from_u8(os_hum));
|
||||||
}
|
}
|
||||||
|
|
||||||
if desired_settings.contains(DesiredSensorSettings::HCNTRL_SEL) {
|
if desired_settings.contains(DesiredSensorSettings::HUMIDITY_CONTROL_SEL) {
|
||||||
sensor_settings.gas_sett.heatr_ctrl = Some((data_array[0usize] as i32 & 0x8i32) as u8);
|
sensor_settings.gas_settings.heater_control = Some((data_array[0usize] as i32 & 0x8i32) as u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
if desired_settings
|
if desired_settings
|
||||||
.contains(DesiredSensorSettings::RUN_GAS_SEL | DesiredSensorSettings::NBCONV_SEL)
|
.contains(DesiredSensorSettings::RUN_GAS_SEL | DesiredSensorSettings::NBCONV_SEL)
|
||||||
{
|
{
|
||||||
sensor_settings.gas_sett.nb_conv = (data_array[1usize] as i32 & 0xfi32) as u8;
|
sensor_settings.gas_settings.nb_conv = (data_array[1usize] as i32 & 0xfi32) as u8;
|
||||||
sensor_settings.gas_sett.run_gas_measurement =
|
sensor_settings.gas_settings.enable_gas_measurement =
|
||||||
((data_array[1usize] as i32 & 0x10i32) >> 4i32) == 0;
|
((data_array[1usize] as i32 & 0x10i32) >> 4i32) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -665,20 +665,20 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
) -> Result<Duration, Bme680Error> {
|
) -> Result<Duration, Bme680Error> {
|
||||||
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];
|
||||||
let mut meas_cycles = os_to_meas_cycles[sensor_settings
|
let mut meas_cycles = os_to_meas_cycles[sensor_settings
|
||||||
.tph_sett
|
.temperature_settings
|
||||||
.os_temp
|
.temperature_oversampling
|
||||||
.unwrap_or(OversamplingSetting::OSNone)
|
.unwrap_or(OversamplingSetting::OSNone)
|
||||||
as usize] as u32;
|
as usize] as u32;
|
||||||
meas_cycles = meas_cycles.wrapping_add(
|
meas_cycles = meas_cycles.wrapping_add(
|
||||||
os_to_meas_cycles[sensor_settings
|
os_to_meas_cycles[sensor_settings
|
||||||
.tph_sett
|
.temperature_settings
|
||||||
.os_pres
|
.pressure_oversampling
|
||||||
.unwrap_or(OversamplingSetting::OSNone) as usize] as u32,
|
.unwrap_or(OversamplingSetting::OSNone) as usize] as u32,
|
||||||
);
|
);
|
||||||
meas_cycles = meas_cycles.wrapping_add(
|
meas_cycles = meas_cycles.wrapping_add(
|
||||||
os_to_meas_cycles[sensor_settings
|
os_to_meas_cycles[sensor_settings
|
||||||
.tph_sett
|
.temperature_settings
|
||||||
.os_hum
|
.humidity_oversampling
|
||||||
.unwrap_or(OversamplingSetting::OSNone) as usize] as u32,
|
.unwrap_or(OversamplingSetting::OSNone) as usize] as u32,
|
||||||
);
|
);
|
||||||
let mut tph_dur = meas_cycles.wrapping_mul(1963u32);
|
let mut tph_dur = meas_cycles.wrapping_mul(1963u32);
|
||||||
|
@ -688,8 +688,8 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
tph_dur = tph_dur.wrapping_div(1000u32);
|
tph_dur = tph_dur.wrapping_div(1000u32);
|
||||||
tph_dur = tph_dur.wrapping_add(1u32);
|
tph_dur = tph_dur.wrapping_add(1u32);
|
||||||
let mut duration = Duration::from_millis(tph_dur as u64);
|
let mut duration = Duration::from_millis(tph_dur as u64);
|
||||||
if sensor_settings.gas_sett.run_gas_measurement {
|
if sensor_settings.gas_settings.enable_gas_measurement {
|
||||||
duration += sensor_settings.gas_sett.heatr_dur.unwrap_or(Duration::default());
|
duration += sensor_settings.gas_settings.heater_duration.unwrap_or(Duration::default());
|
||||||
}
|
}
|
||||||
Ok(duration)
|
Ok(duration)
|
||||||
}
|
}
|
||||||
|
@ -765,7 +765,7 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
|
|
||||||
fn set_gas_config(
|
fn set_gas_config(
|
||||||
&mut self,
|
&mut self,
|
||||||
gas_sett: GasSett,
|
gas_sett: GasSettings,
|
||||||
) -> Result<(), Bme680Error> {
|
) -> Result<(), Bme680Error> {
|
||||||
if self.power_mode != PowerMode::ForcedMode {
|
if self.power_mode != PowerMode::ForcedMode {
|
||||||
return Err(Bme680Error::DefinePwrMode);
|
return Err(Bme680Error::DefinePwrMode);
|
||||||
|
@ -777,19 +777,19 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
Calculation::heater_resistance(
|
Calculation::heater_resistance(
|
||||||
&self.calibration_data,
|
&self.calibration_data,
|
||||||
gas_sett.ambient_temperature,
|
gas_sett.ambient_temperature,
|
||||||
gas_sett.heatr_temp.unwrap_or(0),
|
gas_sett.heater_temperature.unwrap_or(0),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
BME680_GAS_WAIT0_ADDR,
|
BME680_GAS_WAIT0_ADDR,
|
||||||
Calculation::heater_duration(gas_sett.heatr_dur.unwrap_or_else(|| Duration::from_secs(0))),
|
Calculation::heater_duration(gas_sett.heater_duration.unwrap_or_else(|| Duration::from_secs(0))),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
self.bme680_set_regs(®)
|
self.bme680_set_regs(®)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_gas_config(&mut self) -> Result<GasSett, Bme680Error> {
|
fn get_gas_config(&mut self) -> Result<GasSettings, Bme680Error> {
|
||||||
let heatr_temp = Some(I2CUtility::read_byte(
|
let heatr_temp = Some(I2CUtility::read_byte(
|
||||||
&mut self.i2c_bus_handle,
|
&mut self.i2c_bus_handle,
|
||||||
self.device_address.addr(),
|
self.device_address.addr(),
|
||||||
|
@ -802,9 +802,9 @@ impl<I2C: I2c, D: DelayNs> Bme680<I2C, D>
|
||||||
BME680_ADDR_GAS_CONF_START,
|
BME680_ADDR_GAS_CONF_START,
|
||||||
)? as u64;
|
)? as u64;
|
||||||
|
|
||||||
let gas_sett = GasSett {
|
let gas_sett = GasSettings {
|
||||||
heatr_temp,
|
heater_temperature: heatr_temp,
|
||||||
heatr_dur: Some(Duration::from_millis(heatr_dur_ms)),
|
heater_duration: Some(Duration::from_millis(heatr_dur_ms)),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
104
src/settings.rs
104
src/settings.rs
|
@ -14,16 +14,15 @@ pub enum OversamplingSetting {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OversamplingSetting {
|
impl OversamplingSetting {
|
||||||
// TODO replace with TryFrom once stabilized
|
pub fn from_u8(value: u8) -> OversamplingSetting {
|
||||||
pub fn from_u8(os: u8) -> OversamplingSetting {
|
match value {
|
||||||
match os {
|
|
||||||
0 => OversamplingSetting::OSNone,
|
0 => OversamplingSetting::OSNone,
|
||||||
1 => OversamplingSetting::OS1x,
|
1 => OversamplingSetting::OS1x,
|
||||||
2 => OversamplingSetting::OS2x,
|
2 => OversamplingSetting::OS2x,
|
||||||
3 => OversamplingSetting::OS4x,
|
3 => OversamplingSetting::OS4x,
|
||||||
4 => OversamplingSetting::OS8x,
|
4 => OversamplingSetting::OS8x,
|
||||||
5 => OversamplingSetting::OS16x,
|
5 => OversamplingSetting::OS16x,
|
||||||
_ => panic!("Unknown oversampling setting: {}", os),
|
_ => panic!("Unknown oversampling setting: {}", value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,9 +42,8 @@ pub enum IIRFilterSize {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IIRFilterSize {
|
impl IIRFilterSize {
|
||||||
// TODO replace with TryFrom once stabilized
|
pub fn from_u8(value: u8) -> IIRFilterSize {
|
||||||
pub fn from_u8(os: u8) -> IIRFilterSize {
|
match value {
|
||||||
match os {
|
|
||||||
0 => IIRFilterSize::Size0,
|
0 => IIRFilterSize::Size0,
|
||||||
1 => IIRFilterSize::Size1,
|
1 => IIRFilterSize::Size1,
|
||||||
2 => IIRFilterSize::Size3,
|
2 => IIRFilterSize::Size3,
|
||||||
|
@ -54,7 +52,7 @@ impl IIRFilterSize {
|
||||||
5 => IIRFilterSize::Size31,
|
5 => IIRFilterSize::Size31,
|
||||||
6 => IIRFilterSize::Size63,
|
6 => IIRFilterSize::Size63,
|
||||||
7 => IIRFilterSize::Size127,
|
7 => IIRFilterSize::Size127,
|
||||||
_ => panic!("Unknown IIRFilterSize: {}", os),
|
_ => panic!("Unknown IIRFilterSize: {}", value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,20 +60,20 @@ impl IIRFilterSize {
|
||||||
/// Temperature settings
|
/// Temperature settings
|
||||||
#[derive(Debug, Default, Copy)]
|
#[derive(Debug, Default, Copy)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct TphSett {
|
pub struct TemperatureSettings {
|
||||||
/// Humidity oversampling
|
/// Humidity oversampling
|
||||||
pub os_hum: Option<OversamplingSetting>,
|
pub humidity_oversampling: Option<OversamplingSetting>,
|
||||||
/// Temperature oversampling
|
/// Temperature oversampling
|
||||||
pub os_temp: Option<OversamplingSetting>,
|
pub temperature_oversampling: Option<OversamplingSetting>,
|
||||||
/// Pressure oversampling
|
/// Pressure oversampling
|
||||||
pub os_pres: Option<OversamplingSetting>,
|
pub pressure_oversampling: Option<OversamplingSetting>,
|
||||||
/// Filter coefficient
|
/// Filter coefficient
|
||||||
pub filter: Option<IIRFilterSize>,
|
pub filter_size: Option<IIRFilterSize>,
|
||||||
/// If set, the temperature t_fine will be increased by the given value in celsius.
|
/// If set, the temperature t_fine will be increased by the given value in celsius.
|
||||||
pub temperature_offset: Option<f32>,
|
pub temperature_offset: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for TphSett {
|
impl Clone for TemperatureSettings {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
|
@ -84,20 +82,22 @@ impl Clone for TphSett {
|
||||||
/// Gas measurement settings
|
/// Gas measurement settings
|
||||||
#[derive(Debug, Default, Copy)]
|
#[derive(Debug, Default, Copy)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct GasSett {
|
pub struct GasSettings {
|
||||||
|
/// nb_conv is used to select heater set-points of the sensor.
|
||||||
pub nb_conv: u8,
|
pub nb_conv: u8,
|
||||||
/// Heater control
|
/// Heater control
|
||||||
pub heatr_ctrl: Option<u8>,
|
pub heater_control: Option<u8>,
|
||||||
/// Enable measurement of gas, disabled by default
|
/// Enable measurement of gas, disabled by default
|
||||||
pub run_gas_measurement: bool,
|
pub enable_gas_measurement: bool,
|
||||||
/// Heater temperature
|
/// The heater temperature
|
||||||
pub heatr_temp: Option<u16>,
|
pub heater_temperature: Option<u16>,
|
||||||
/// Profile duration
|
/// The Heating duration
|
||||||
pub heatr_dur: Option<Duration>,
|
pub heater_duration: Option<Duration>,
|
||||||
|
/// The ambient temperature.
|
||||||
pub ambient_temperature: i8,
|
pub ambient_temperature: i8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for GasSett {
|
impl Clone for GasSettings {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
|
@ -107,9 +107,9 @@ impl Clone for GasSett {
|
||||||
#[derive(Debug, Default, Copy)]
|
#[derive(Debug, Default, Copy)]
|
||||||
pub struct SensorSettings {
|
pub struct SensorSettings {
|
||||||
/// Gas settings
|
/// Gas settings
|
||||||
pub gas_sett: GasSett,
|
pub gas_settings: GasSettings,
|
||||||
/// Temperature settings
|
/// Temperature settings
|
||||||
pub tph_sett: TphSett,
|
pub temperature_settings: TemperatureSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for SensorSettings {
|
impl Clone for SensorSettings {
|
||||||
|
@ -132,9 +132,9 @@ bitflags! {
|
||||||
/// To set gas measurement setting.
|
/// To set gas measurement setting.
|
||||||
const GAS_MEAS_SEL = 8;
|
const GAS_MEAS_SEL = 8;
|
||||||
/// To set filter setting.
|
/// To set filter setting.
|
||||||
const FILTER_SEL = 16;
|
const FILTER_SIZE_SEL = 16;
|
||||||
/// To set humidity control setting.
|
/// To set humidity control setting.
|
||||||
const HCNTRL_SEL = 32;
|
const HUMIDITY_CONTROL_SEL = 32;
|
||||||
/// To set run gas setting.
|
/// To set run gas setting.
|
||||||
const RUN_GAS_SEL = 64;
|
const RUN_GAS_SEL = 64;
|
||||||
/// To set NB conversion setting.
|
/// To set NB conversion setting.
|
||||||
|
@ -171,74 +171,84 @@ pub struct SettingsBuilder {
|
||||||
pub type Settings = (SensorSettings, DesiredSensorSettings);
|
pub type Settings = (SensorSettings, DesiredSensorSettings);
|
||||||
|
|
||||||
impl SettingsBuilder {
|
impl SettingsBuilder {
|
||||||
|
/// Constructs a new instance of the SettingsBuilder.
|
||||||
pub fn new() -> SettingsBuilder {
|
pub fn new() -> SettingsBuilder {
|
||||||
SettingsBuilder::default()
|
SettingsBuilder::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_temperature_filter(mut self, filter: IIRFilterSize) -> SettingsBuilder {
|
/// With temperature filter.
|
||||||
self.sensor_settings.tph_sett.filter = Some(filter);
|
pub fn with_temperature_filter(mut self, filter_size: IIRFilterSize) -> SettingsBuilder {
|
||||||
self.desired_settings |= DesiredSensorSettings::FILTER_SEL;
|
self.sensor_settings.temperature_settings.filter_size = Some(filter_size);
|
||||||
|
self.desired_settings |= DesiredSensorSettings::FILTER_SIZE_SEL;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_humidity_control(mut self, heatr_control: u8) -> SettingsBuilder {
|
/// With gas heater control.
|
||||||
self.sensor_settings.gas_sett.heatr_ctrl = Some(heatr_control);
|
pub fn with_gas_heater_control(mut self, heater_control: u8) -> SettingsBuilder {
|
||||||
self.desired_settings |= DesiredSensorSettings::HCNTRL_SEL;
|
self.sensor_settings.gas_settings.heater_control = Some(heater_control);
|
||||||
|
self.desired_settings |= DesiredSensorSettings::HUMIDITY_CONTROL_SEL;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// With temperature oversampling
|
||||||
pub fn with_temperature_oversampling(
|
pub fn with_temperature_oversampling(
|
||||||
mut self,
|
mut self,
|
||||||
os_temp: OversamplingSetting,
|
temperature_oversampling: OversamplingSetting,
|
||||||
) -> SettingsBuilder {
|
) -> SettingsBuilder {
|
||||||
self.sensor_settings.tph_sett.os_temp = Some(os_temp);
|
self.sensor_settings.temperature_settings.temperature_oversampling = Some(temperature_oversampling);
|
||||||
self.desired_settings |= DesiredSensorSettings::OST_SEL;
|
self.desired_settings |= DesiredSensorSettings::OST_SEL;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_pressure_oversampling(mut self, os_pres: OversamplingSetting) -> SettingsBuilder {
|
/// With pressure oversampling.
|
||||||
self.sensor_settings.tph_sett.os_pres = Some(os_pres);
|
pub fn with_pressure_oversampling(mut self, pressure_oversampling: OversamplingSetting) -> SettingsBuilder {
|
||||||
|
self.sensor_settings.temperature_settings.pressure_oversampling = Some(pressure_oversampling);
|
||||||
self.desired_settings |= DesiredSensorSettings::OSP_SEL;
|
self.desired_settings |= DesiredSensorSettings::OSP_SEL;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_humidity_oversampling(mut self, os_hum: OversamplingSetting) -> SettingsBuilder {
|
/// With humidity oversampling.
|
||||||
self.sensor_settings.tph_sett.os_hum = Some(os_hum);
|
pub fn with_humidity_oversampling(mut self, humidity_oversampling: OversamplingSetting) -> SettingsBuilder {
|
||||||
|
self.sensor_settings.temperature_settings.humidity_oversampling = Some(humidity_oversampling);
|
||||||
self.desired_settings |= DesiredSensorSettings::OSH_SEL;
|
self.desired_settings |= DesiredSensorSettings::OSH_SEL;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// With gas measurement.
|
||||||
pub fn with_gas_measurement(
|
pub fn with_gas_measurement(
|
||||||
mut self,
|
mut self,
|
||||||
heatr_dur: Duration,
|
heater_duration: Duration,
|
||||||
heatr_temp: u16,
|
heater_temperature: u16,
|
||||||
ambient_temperature: i8,
|
ambient_temperature: i8,
|
||||||
) -> SettingsBuilder {
|
) -> SettingsBuilder {
|
||||||
self.sensor_settings.gas_sett.heatr_dur = Some(heatr_dur);
|
self.sensor_settings.gas_settings.heater_duration = Some(heater_duration);
|
||||||
self.sensor_settings.gas_sett.heatr_temp = Some(heatr_temp);
|
self.sensor_settings.gas_settings.heater_temperature = Some(heater_temperature);
|
||||||
self.sensor_settings.gas_sett.ambient_temperature = ambient_temperature;
|
self.sensor_settings.gas_settings.ambient_temperature = ambient_temperature;
|
||||||
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
|
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// With nb_conv.
|
||||||
pub fn with_nb_conv(mut self, nb_conv: u8) -> SettingsBuilder {
|
pub fn with_nb_conv(mut self, nb_conv: u8) -> SettingsBuilder {
|
||||||
self.sensor_settings.gas_sett.nb_conv = nb_conv;
|
self.sensor_settings.gas_settings.nb_conv = nb_conv;
|
||||||
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
|
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// With run gas.
|
||||||
pub fn with_run_gas(mut self, run_gas: bool) -> SettingsBuilder {
|
pub fn with_run_gas(mut self, run_gas: bool) -> SettingsBuilder {
|
||||||
self.sensor_settings.gas_sett.run_gas_measurement = run_gas;
|
self.sensor_settings.gas_settings.enable_gas_measurement = run_gas;
|
||||||
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
|
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Temperature offset in Celsius, e.g. 4, -8, 1.25
|
/// With temperature offset in Celsius, e.g. 4, -8, 1.25
|
||||||
pub fn with_temperature_offset(mut self, offset: f32) -> SettingsBuilder {
|
pub fn with_temperature_offset(mut self, offset: f32) -> SettingsBuilder {
|
||||||
self.sensor_settings.tph_sett.temperature_offset = Some(offset);
|
self.sensor_settings.temperature_settings.temperature_offset = Some(offset);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Builds the settings object
|
||||||
pub fn build(self) -> Settings {
|
pub fn build(self) -> Settings {
|
||||||
(self.sensor_settings, self.desired_settings)
|
(self.sensor_settings, self.desired_settings)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue