diff --git a/src/calculation.rs b/src/calculation.rs index 93b8d07..45bcce5 100644 --- a/src/calculation.rs +++ b/src/calculation.rs @@ -4,6 +4,44 @@ use core::time::Duration; /// Calculates values needed or returned by the BME80 Sensor. pub struct Calculation {} +static GAS_LOOKUP_TABLE_1: [u32; 16] = [ + 2147483647u32, + 2147483647u32, + 2147483647u32, + 2147483647u32, + 2147483647u32, + 2126008810u32, + 2147483647u32, + 2130303777u32, + 2147483647u32, + 2147483647u32, + 2143188679u32, + 2136746228u32, + 2147483647u32, + 2126008810u32, + 2147483647u32, + 2147483647u32, +]; + +static GAS_LOOKUP_TABLE_2: [u32; 16] = [ + 4096000000u32, + 2048000000u32, + 1024000000u32, + 512000000u32, + 255744255u32, + 127110228u32, + 64000000u32, + 32258064u32, + 16016016u32, + 8000000u32, + 4000000u32, + 2000000u32, + 1, + 500000u32, + 250000u32, + 125000u32, +]; + impl Calculation { /// Calculates and returns the sensor's heater resistance. /// * `calibration_data` - The calibration data of the sensor. @@ -154,47 +192,11 @@ impl Calculation { gas_resistance_adc: u16, gas_range: u8, ) -> u32 { - let lookup_table1: [u32; 16] = [ - 2147483647u32, - 2147483647u32, - 2147483647u32, - 2147483647u32, - 2147483647u32, - 2126008810u32, - 2147483647u32, - 2130303777u32, - 2147483647u32, - 2147483647u32, - 2143188679u32, - 2136746228u32, - 2147483647u32, - 2126008810u32, - 2147483647u32, - 2147483647u32, - ]; - let lookup_table2: [u32; 16] = [ - 4096000000u32, - 2048000000u32, - 1024000000u32, - 512000000u32, - 255744255u32, - 127110228u32, - 64000000u32, - 32258064u32, - 16016016u32, - 8000000u32, - 4000000u32, - 2000000u32, - 1, - 500000u32, - 250000u32, - 125000u32, - ]; let var1: i64 = ((1340 + 5 * calibration_data.range_sw_err as i64) - * lookup_table1[gas_range as usize] as i64) + * GAS_LOOKUP_TABLE_1[gas_range as usize] as i64) >> 16; let var2: u64 = (((gas_resistance_adc as i64) << 15) - 16777216 + var1) as u64; - let var3: i64 = (lookup_table2[gas_range as usize] as i64 * var1) >> 9; + let var3: i64 = (GAS_LOOKUP_TABLE_2[gas_range as usize] as i64 * var1) >> 9; let calc_gas_res: u32 = ((var3 + ((var2 as i64) >> 1i64)) / var2 as i64) as u32; calc_gas_res }