rename calc to calculation

This commit is contained in:
Denis-Cosmin Nutiu 2024-02-25 11:46:35 +02:00
parent ccfcf8bd32
commit 9a6893f326
2 changed files with 53 additions and 52 deletions

View file

@ -1,25 +1,26 @@
use crate::CalibData; use crate::CalibrationData;
use core::time::Duration; use core::time::Duration;
pub struct Calc {} /// Calculates values needed or returned by the BME80 Sensor.
pub struct Calculation {}
impl Calc { impl Calculation {
pub fn calc_heater_res(calib: &CalibData, amb_temp: i8, temp: u16) -> u8 { pub fn heater_resistance(calibration_data: &CalibrationData, ambient_temperature: i8, temperature: u16) -> u8 {
// cap temperature // cap temperature
let temp = if temp <= 400 { temp } else { 400 }; let temp = if temperature <= 400 { temperature } else { 400 };
let var1 = amb_temp as i32 * calib.par_gh3 as i32 / 1000i32 * 256i32; let var1 = ambient_temperature as i32 * calibration_data.par_gh3 as i32 / 1000i32 * 256i32;
let var2 = (calib.par_gh1 as i32 + 784i32) let var2 = (calibration_data.par_gh1 as i32 + 784i32)
* (((calib.par_gh2 as i32 + 154009i32) * temp as i32 * 5i32 / 100i32 + 3276800i32) * (((calibration_data.par_gh2 as i32 + 154009i32) * temp as i32 * 5i32 / 100i32 + 3276800i32)
/ 10i32); / 10i32);
let var3 = var1 + var2 / 2i32; let var3 = var1 + var2 / 2i32;
let var4 = var3 / (calib.res_heat_range as i32 + 4i32); let var4 = var3 / (calibration_data.res_heat_range as i32 + 4i32);
let var5 = 131i32 * calib.res_heat_val as i32 + 65536i32; let var5 = 131i32 * calibration_data.res_heat_val as i32 + 65536i32;
let heatr_res_x100 = (var4 / var5 - 250i32) * 34i32; let heatr_res_x100 = (var4 / var5 - 250i32) * 34i32;
((heatr_res_x100 + 50i32) / 100i32) as u8 ((heatr_res_x100 + 50i32) / 100i32) as u8
} }
pub fn calc_heater_dur(duration: Duration) -> u8 { pub fn heater_duration(duration: Duration) -> u8 {
let mut factor: u8 = 0u8; let mut factor: u8 = 0u8;
// TODO replace once https://github.com/rust-lang/rust/pull/50167 has been merged // TODO replace once https://github.com/rust-lang/rust/pull/50167 has been merged
const MILLIS_PER_SEC: u64 = 1_000; const MILLIS_PER_SEC: u64 = 1_000;
@ -45,15 +46,15 @@ impl Calc {
/// * `temp_adc` /// * `temp_adc`
/// * `temp_offset` - If set, the temperature t_fine will be increased by given /// * `temp_offset` - If set, the temperature t_fine will be increased by given
/// value in celsius. Temperature offset in Celsius, e.g. 4, -8, 1.25 /// value in celsius. Temperature offset in Celsius, e.g. 4, -8, 1.25
pub fn calc_temperature( pub fn temperature(
calib: &CalibData, calibration_data: &CalibrationData,
temp_adc: u32, temp_adc: u32,
temp_offset: Option<f32>, temp_offset: Option<f32>,
) -> (i16, i32) { ) -> (i16, i32) {
let var1: i64 = (temp_adc as i64 >> 3) - ((calib.par_t1 as i64) << 1); let var1: i64 = (temp_adc as i64 >> 3) - ((calibration_data.par_t1 as i64) << 1);
let var2: i64 = (var1 * (calib.par_t2 as i64)) >> 11; let var2: i64 = (var1 * (calibration_data.par_t2 as i64)) >> 11;
let var3: i64 = ((var1 >> 1) * (var1 >> 1)) >> 12; let var3: i64 = ((var1 >> 1) * (var1 >> 1)) >> 12;
let var3: i64 = (var3 * ((calib.par_t3 as i64) << 4)) >> 14; let var3: i64 = (var3 * ((calibration_data.par_t3 as i64) << 4)) >> 14;
let temp_offset = match temp_offset { let temp_offset = match temp_offset {
None => 0i32, None => 0i32,
@ -69,16 +70,16 @@ impl Calc {
(calc_temp, t_fine) (calc_temp, t_fine)
} }
pub fn calc_pressure(calib: &CalibData, t_fine: i32, pres_adc: u32) -> u32 { pub fn pressure(calibration_data: &CalibrationData, t_fine: i32, pres_adc: u32) -> u32 {
let mut var1: i32 = (t_fine >> 1) - 64000; let mut var1: i32 = (t_fine >> 1) - 64000;
let mut var2: i32 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * calib.par_p6 as i32) >> 2; let mut var2: i32 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * calibration_data.par_p6 as i32) >> 2;
var2 += (var1 * (calib.par_p5 as i32)) << 1; var2 += (var1 * (calibration_data.par_p5 as i32)) << 1;
var2 = (var2 >> 2i32) + ((calib.par_p4 as i32) << 16i32); var2 = (var2 >> 2i32) + ((calibration_data.par_p4 as i32) << 16i32);
var1 = (((((var1 >> 2i32) * (var1 >> 2i32)) >> 13i32) * ((calib.par_p3 as i32) << 5i32)) var1 = (((((var1 >> 2i32) * (var1 >> 2i32)) >> 13i32) * ((calibration_data.par_p3 as i32) << 5i32))
>> 3i32) >> 3i32)
+ ((calib.par_p2 as i32 * var1) >> 1i32); + ((calibration_data.par_p2 as i32 * var1) >> 1i32);
var1 >>= 18i32; var1 >>= 18i32;
var1 = ((32768i32 + var1) * calib.par_p1 as i32) >> 15i32; var1 = ((32768i32 + var1) * calibration_data.par_p1 as i32) >> 15i32;
let mut pressure_comp: i32 = 1048576u32.wrapping_sub(pres_adc) as i32; let mut pressure_comp: i32 = 1048576u32.wrapping_sub(pres_adc) as i32;
pressure_comp = ((pressure_comp - (var2 >> 12i32)) as u32).wrapping_mul(3125u32) as i32; pressure_comp = ((pressure_comp - (var2 >> 12i32)) as u32).wrapping_mul(3125u32) as i32;
if pressure_comp >= 0x40000000i32 { if pressure_comp >= 0x40000000i32 {
@ -86,32 +87,32 @@ impl Calc {
} else { } else {
pressure_comp = ((pressure_comp << 1i32) as u32).wrapping_div(var1 as u32) as i32; pressure_comp = ((pressure_comp << 1i32) as u32).wrapping_div(var1 as u32) as i32;
} }
var1 = (calib.par_p9 as i32 var1 = (calibration_data.par_p9 as i32
* (((pressure_comp >> 3i32) * (pressure_comp >> 3i32)) >> 13i32)) * (((pressure_comp >> 3i32) * (pressure_comp >> 3i32)) >> 13i32))
>> 12i32; >> 12i32;
var2 = ((pressure_comp >> 2i32) * calib.par_p8 as i32) >> 13i32; var2 = ((pressure_comp >> 2i32) * calibration_data.par_p8 as i32) >> 13i32;
let var3: i32 = ((pressure_comp >> 8i32) let var3: i32 = ((pressure_comp >> 8i32)
* (pressure_comp >> 8i32) * (pressure_comp >> 8i32)
* (pressure_comp >> 8i32) * (pressure_comp >> 8i32)
* calib.par_p10 as i32) * calibration_data.par_p10 as i32)
>> 17i32; >> 17i32;
pressure_comp += (var1 + var2 + var3 + ((calib.par_p7 as i32) << 7i32)) >> 4i32; pressure_comp += (var1 + var2 + var3 + ((calibration_data.par_p7 as i32) << 7i32)) >> 4i32;
pressure_comp as u32 pressure_comp as u32
} }
pub fn calc_humidity(calib: &CalibData, t_fine: i32, hum_adc: u16) -> u32 { pub fn humidity(calibration_data: &CalibrationData, t_fine: i32, hum_adc: u16) -> u32 {
let temp_scaled: i32 = (t_fine * 5i32 + 128i32) >> 8i32; let temp_scaled: i32 = (t_fine * 5i32 + 128i32) >> 8i32;
let var1: i32 = hum_adc as i32 let var1: i32 = hum_adc as i32
- calib.par_h1 as i32 * 16i32 - calibration_data.par_h1 as i32 * 16i32
- ((temp_scaled * calib.par_h3 as i32 / 100i32) >> 1i32); - ((temp_scaled * calibration_data.par_h3 as i32 / 100i32) >> 1i32);
let var2: i32 = (calib.par_h2 as i32 let var2: i32 = (calibration_data.par_h2 as i32
* (temp_scaled * calib.par_h4 as i32 / 100i32 * (temp_scaled * calibration_data.par_h4 as i32 / 100i32
+ ((temp_scaled * (temp_scaled * calib.par_h5 as i32 / 100i32)) >> 6i32) / 100i32 + ((temp_scaled * (temp_scaled * calibration_data.par_h5 as i32 / 100i32)) >> 6i32) / 100i32
+ (1i32 << 14i32))) + (1i32 << 14i32)))
>> 10i32; >> 10i32;
let var3: i32 = var1 * var2; let var3: i32 = var1 * var2;
let var4: i32 = (calib.par_h6 as i32) << 7i32; let var4: i32 = (calibration_data.par_h6 as i32) << 7i32;
let var4: i32 = (var4 + temp_scaled * calib.par_h7 as i32 / 100i32) >> 4i32; let var4: i32 = (var4 + temp_scaled * calibration_data.par_h7 as i32 / 100i32) >> 4i32;
let var5: i32 = ((var3 >> 14i32) * (var3 >> 14i32)) >> 10i32; let var5: i32 = ((var3 >> 14i32) * (var3 >> 14i32)) >> 10i32;
let var6: i32 = (var4 * var5) >> 1i32; let var6: i32 = (var4 * var5) >> 1i32;
let mut calc_hum: i32 = (((var3 + var6) >> 10i32) * 1000i32) >> 12i32; let mut calc_hum: i32 = (((var3 + var6) >> 10i32) * 1000i32) >> 12i32;
@ -123,7 +124,7 @@ impl Calc {
calc_hum as u32 calc_hum as u32
} }
pub fn calc_gas_resistance(calib: &CalibData, gas_res_adc: u16, gas_range: u8) -> u32 { pub fn gas_resistance(calibration_data: &CalibrationData, gas_res_adc: u16, gas_range: u8) -> u32 {
let lookup_table1: [u32; 16] = [ let lookup_table1: [u32; 16] = [
2147483647u32, 2147483647u32,
2147483647u32, 2147483647u32,
@ -160,7 +161,7 @@ impl Calc {
250000u32, 250000u32,
125000u32, 125000u32,
]; ];
let var1: i64 = ((1340 + 5 * calib.range_sw_err as i64) let var1: i64 = ((1340 + 5 * calibration_data.range_sw_err as i64)
* lookup_table1[gas_range as usize] as i64) * lookup_table1[gas_range as usize] as i64)
>> 16; >> 16;
let var2: u64 = (((gas_res_adc as i64) << 15) - 16777216 + var1) as u64; let var2: u64 = (((gas_res_adc as i64) << 15) - 16777216 + var1) as u64;

View file

@ -89,10 +89,10 @@ pub use self::settings::{
SettingsBuilder, TphSett, SettingsBuilder, TphSett,
}; };
mod calc; mod calculation;
mod settings; mod settings;
use crate::calc::Calc; use crate::calculation::Calculation;
use crate::hal::delay::DelayNs; use crate::hal::delay::DelayNs;
use crate::hal::i2c::I2c; use crate::hal::i2c::I2c;
@ -266,7 +266,7 @@ impl Default for I2CAddress {
/// Calibration data used during initalization /// Calibration data used during initalization
#[derive(Debug, Default, Copy)] #[derive(Debug, Default, Copy)]
#[repr(C)] #[repr(C)]
pub struct CalibData { pub struct CalibrationData {
pub par_h1: u16, pub par_h1: u16,
pub par_h2: u16, pub par_h2: u16,
pub par_h3: i8, pub par_h3: i8,
@ -295,7 +295,7 @@ pub struct CalibData {
pub range_sw_err: u8, pub range_sw_err: u8,
} }
impl Clone for CalibData { impl Clone for CalibrationData {
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self
} }
@ -416,7 +416,7 @@ pub struct Bme680<I2C, D> {
i2c: I2C, i2c: I2C,
delay: PhantomData<D>, delay: PhantomData<D>,
dev_id: I2CAddress, dev_id: I2CAddress,
calib: CalibData, calib: CalibrationData,
// TODO remove ? as it may not reflect the state of the device // TODO remove ? as it may not reflect the state of the device
tph_sett: TphSett, tph_sett: TphSett,
// TODO remove ? as it may not reflect the state of the device // TODO remove ? as it may not reflect the state of the device
@ -826,11 +826,11 @@ impl<I2C, D> Bme680<I2C, D>
fn get_calib_data<I2CX>( fn get_calib_data<I2CX>(
i2c: &mut I2CX, i2c: &mut I2CX,
dev_id: I2CAddress, dev_id: I2CAddress,
) -> Result<CalibData, <I2C as ErrorType>::Error> ) -> Result<CalibrationData, <I2C as ErrorType>::Error>
where where
I2CX: I2c, I2CX: I2c,
{ {
let mut calib: CalibData = Default::default(); let mut calib: CalibrationData = Default::default();
let mut coeff_array: [u8; BME680_COEFF_ADDR1_LEN + BME680_COEFF_ADDR2_LEN] = let mut coeff_array: [u8; BME680_COEFF_ADDR1_LEN + BME680_COEFF_ADDR2_LEN] =
[0; BME680_COEFF_ADDR1_LEN + BME680_COEFF_ADDR2_LEN]; [0; BME680_COEFF_ADDR1_LEN + BME680_COEFF_ADDR2_LEN];
@ -905,7 +905,7 @@ impl<I2C, D> Bme680<I2C, D>
let reg: [(u8, u8); 2] = [ let reg: [(u8, u8); 2] = [
( (
BME680_RES_HEAT0_ADDR, BME680_RES_HEAT0_ADDR,
Calc::calc_heater_res( Calculation::heater_resistance(
&self.calib, &self.calib,
gas_sett.ambient_temperature, gas_sett.ambient_temperature,
gas_sett.heatr_temp.unwrap_or(0), gas_sett.heatr_temp.unwrap_or(0),
@ -913,7 +913,7 @@ impl<I2C, D> Bme680<I2C, D>
), ),
( (
BME680_GAS_WAIT0_ADDR, BME680_GAS_WAIT0_ADDR,
Calc::calc_heater_dur(gas_sett.heatr_dur.unwrap_or_else(|| Duration::from_secs(0))), Calculation::heater_duration(gas_sett.heatr_dur.unwrap_or_else(|| Duration::from_secs(0))),
), ),
]; ];
@ -984,16 +984,16 @@ impl<I2C, D> Bme680<I2C, D>
if data.status & BME680_NEW_DATA_MSK != 0 { if data.status & BME680_NEW_DATA_MSK != 0 {
let (temp, t_fine) = let (temp, t_fine) =
Calc::calc_temperature(&self.calib, adc_temp, self.tph_sett.temperature_offset); Calculation::temperature(&self.calib, adc_temp, self.tph_sett.temperature_offset);
debug!( debug!(
"adc_temp: {} adc_pres: {} adc_hum: {} adc_gas_res: {}, t_fine: {}", "adc_temp: {} adc_pres: {} adc_hum: {} adc_gas_res: {}, t_fine: {}",
adc_temp, adc_pres, adc_hum, adc_gas_res, t_fine adc_temp, adc_pres, adc_hum, adc_gas_res, t_fine
); );
data.temperature = temp; data.temperature = temp;
data.pressure = Calc::calc_pressure(&self.calib, t_fine, adc_pres); data.pressure = Calculation::pressure(&self.calib, t_fine, adc_pres);
data.humidity = Calc::calc_humidity(&self.calib, t_fine, adc_hum); data.humidity = Calculation::humidity(&self.calib, t_fine, adc_hum);
data.gas_resistance = data.gas_resistance =
Calc::calc_gas_resistance(&self.calib, adc_gas_res, gas_range); Calculation::gas_resistance(&self.calib, adc_gas_res, gas_range);
return Ok((data, FieldDataCondition::NewData)); return Ok((data, FieldDataCondition::NewData));
} }