now returns AQI band for later color indicators master
authorjweigele <jweigele@local>
Sun, 16 Jul 2023 00:07:09 +0000 (17:07 -0700)
committerjweigele <jweigele@local>
Sun, 16 Jul 2023 00:07:09 +0000 (17:07 -0700)
aqi_calc.c
include/aqi_calc.h

index 97f3bdf34b5474aa960c3579ef982a912a0a4af5..e83575d06b4279921acf9ba621f2eaebac5f5ea8 100644 (file)
@@ -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;
+    }
+}
+
index 57a21c2869327010c6179dee2aba203691f0a7fb..3593de035a99fefc70342f48aabaacf422df7d0b 100644 (file)
@@ -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);