From 0cfca20ee6529c3d79d998faf9897ede37b3a8e0 Mon Sep 17 00:00:00 2001 From: jweigele Date: Sat, 15 Jul 2023 17:07:09 -0700 Subject: [PATCH] now returns AQI band for later color indicators --- aqi_calc.c | 41 +++++++++++++++++++++++++++++++---------- include/aqi_calc.h | 1 + 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/aqi_calc.c b/aqi_calc.c index 97f3bdf..e83575d 100644 --- a/aqi_calc.c +++ b/aqi_calc.c @@ -84,10 +84,7 @@ float linear_calc(uint16_t value, aqi_lookup_t lookup_row, int lookup_type){ return retval; } -float aqi_calc_pm10(uint16_t pm10){ - // precision is integer, so div 100 - // ex. 2543 / 100 = 25 (real value: 25) - pm10 /= 100; +int get_lookup_int_pm10(uint16_t pm10){ int i = 0; while (i < 6){ if (pm10 < AQI_LOOKUP_TABLE[i].pm10_high ){ @@ -98,15 +95,20 @@ float aqi_calc_pm10(uint16_t pm10){ } if (pm10 > AQI_LOOKUP_TABLE[i].pm10_high){ printf("pm10 too high for aqi lookup, this should never happen %d > %d\n", pm10, AQI_LOOKUP_TABLE[i].pm10_high ); - return 0.0; + return -1; } + return i; +} + +float aqi_calc_pm10(uint16_t pm10){ + // precision is integer, so div 100 + // ex. 2543 / 100 = 25 (real value: 25) + pm10 /= 100; + int i = get_lookup_int_pm10(pm10); return linear_calc(pm10, AQI_LOOKUP_TABLE[i], LOOKUP_PM10); } -float aqi_calc_pm25(uint16_t pm25){ - // precision is one decimal, so div 10: - // ex. 2543 / 10 = 254 (real value: 25.4) - pm25 /= 10; +int get_lookup_int_pm25(uint16_t pm25){ printf("modified pm25: %d\n", pm25); int i = 0; while (i < 6){ @@ -118,7 +120,26 @@ float aqi_calc_pm25(uint16_t pm25){ } if (pm25 > AQI_LOOKUP_TABLE[i].pm25_high){ printf("pm25 too high for aqi lookup, this should never happen %d > %d\n", pm25, AQI_LOOKUP_TABLE[i].pm25_high ); - return 0.0; + return -1; } + return i; +} + +float aqi_calc_pm25(uint16_t pm25){ + // precision is one decimal, so div 10: + // ex. 2543 / 10 = 254 (real value: 25.4) + pm25 /= 10; + int i = get_lookup_int_pm25(pm25); return linear_calc(pm25, AQI_LOOKUP_TABLE[i], LOOKUP_PM25); } + +int get_lookup_highest(int pm10, int pm25){ + int pm10_lookup = get_lookup_int_pm10(pm10/100); + int pm25_lookup = get_lookup_int_pm25(pm25/10); + if (pm25_lookup > pm10_lookup){ + return pm25_lookup; + } else { + return pm10_lookup; + } +} + diff --git a/include/aqi_calc.h b/include/aqi_calc.h index 57a21c2..3593de0 100644 --- a/include/aqi_calc.h +++ b/include/aqi_calc.h @@ -5,4 +5,5 @@ float aqi_calc_pm10(uint16_t pm10); float aqi_calc_pm25(uint16_t pm25); +int get_lookup_highest(int pm10, int pm25); -- 2.30.2