ESPHome  2024.12.2
caqi_calculator.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/log.h"
5 
6 namespace esphome {
7 namespace hm3301 {
8 
10  public:
11  uint16_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value) override {
12  int pm2_5_index = calculate_index_(pm2_5_value, pm2_5_calculation_grid_);
13  int pm10_0_index = calculate_index_(pm10_0_value, pm10_0_calculation_grid_);
14 
15  return (pm2_5_index < pm10_0_index) ? pm10_0_index : pm2_5_index;
16  }
17 
18  protected:
19  static const int AMOUNT_OF_LEVELS = 5;
20 
21  int index_grid_[AMOUNT_OF_LEVELS][2] = {{0, 25}, {26, 50}, {51, 75}, {76, 100}, {101, 400}};
22 
23  int pm2_5_calculation_grid_[AMOUNT_OF_LEVELS][2] = {{0, 15}, {16, 30}, {31, 55}, {56, 110}, {111, 400}};
24 
25  int pm10_0_calculation_grid_[AMOUNT_OF_LEVELS][2] = {{0, 25}, {26, 50}, {51, 90}, {91, 180}, {181, 400}};
26 
27  int calculate_index_(uint16_t value, int array[AMOUNT_OF_LEVELS][2]) {
28  int grid_index = get_grid_index_(value, array);
29  if (grid_index == -1) {
30  return -1;
31  }
32 
33  int aqi_lo = index_grid_[grid_index][0];
34  int aqi_hi = index_grid_[grid_index][1];
35  int conc_lo = array[grid_index][0];
36  int conc_hi = array[grid_index][1];
37 
38  return (value - conc_lo) * (aqi_hi - aqi_lo) / (conc_hi - conc_lo) + aqi_lo;
39  }
40 
41  int get_grid_index_(uint16_t value, int array[AMOUNT_OF_LEVELS][2]) {
42  for (int i = 0; i < AMOUNT_OF_LEVELS; i++) {
43  if (value >= array[i][0] && value <= array[i][1]) {
44  return i;
45  }
46  }
47  return -1;
48  }
49 };
50 
51 } // namespace hm3301
52 } // namespace esphome
int get_grid_index_(uint16_t value, int array[AMOUNT_OF_LEVELS][2])
int pm2_5_calculation_grid_[AMOUNT_OF_LEVELS][2]
int pm10_0_calculation_grid_[AMOUNT_OF_LEVELS][2]
int calculate_index_(uint16_t value, int array[AMOUNT_OF_LEVELS][2])
uint16_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value) override
int index_grid_[AMOUNT_OF_LEVELS][2]
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7