ESPHome
2026.1.0-dev
Loading...
Searching...
No Matches
esphome
components
aqi
aqi_calculator.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <climits>
4
#include "
abstract_aqi_calculator.h
"
5
6
// https://document.airnow.gov/technical-assistance-document-for-the-reporting-of-daily-air-quailty.pdf
7
8
namespace
esphome::aqi
{
9
10
class
AQICalculator
:
public
AbstractAQICalculator
{
11
public
:
12
uint16_t
get_aqi
(uint16_t pm2_5_value, uint16_t pm10_0_value)
override
{
13
int
pm2_5_index =
calculate_index_
(pm2_5_value,
pm2_5_calculation_grid_
);
14
int
pm10_0_index =
calculate_index_
(pm10_0_value,
pm10_0_calculation_grid_
);
15
16
return
(pm2_5_index < pm10_0_index) ? pm10_0_index : pm2_5_index;
17
}
18
19
protected
:
20
static
const
int
AMOUNT_OF_LEVELS
= 6;
21
22
int
index_grid_
[
AMOUNT_OF_LEVELS
][2] = {{0, 50}, {51, 100}, {101, 150}, {151, 200}, {201, 300}, {301, 500}};
23
24
int
pm2_5_calculation_grid_
[
AMOUNT_OF_LEVELS
][2] = {{0, 9}, {10, 35}, {36, 55},
25
{56, 125}, {126, 225}, {226, INT_MAX}};
26
27
int
pm10_0_calculation_grid_
[
AMOUNT_OF_LEVELS
][2] = {{0, 54}, {55, 154}, {155, 254},
28
{255, 354}, {355, 424}, {425, INT_MAX}};
29
30
int
calculate_index_
(uint16_t value,
int
array[
AMOUNT_OF_LEVELS
][2]) {
31
int
grid_index =
get_grid_index_
(value, array);
32
if
(grid_index == -1) {
33
return
-1;
34
}
35
int
aqi_lo =
index_grid_
[grid_index][0];
36
int
aqi_hi =
index_grid_
[grid_index][1];
37
int
conc_lo = array[grid_index][0];
38
int
conc_hi = array[grid_index][1];
39
40
return
(value - conc_lo) * (aqi_hi - aqi_lo) / (conc_hi - conc_lo) + aqi_lo;
41
}
42
43
int
get_grid_index_
(uint16_t value,
int
array[
AMOUNT_OF_LEVELS
][2]) {
44
for
(
int
i = 0; i <
AMOUNT_OF_LEVELS
; i++) {
45
if
(value >= array[i][0] && value <= array[i][1]) {
46
return
i;
47
}
48
}
49
return
-1;
50
}
51
};
52
53
}
// namespace esphome::aqi
abstract_aqi_calculator.h
esphome::aqi::AQICalculator
Definition
aqi_calculator.h:10
esphome::aqi::AQICalculator::pm2_5_calculation_grid_
int pm2_5_calculation_grid_[AMOUNT_OF_LEVELS][2]
Definition
aqi_calculator.h:24
esphome::aqi::AQICalculator::pm10_0_calculation_grid_
int pm10_0_calculation_grid_[AMOUNT_OF_LEVELS][2]
Definition
aqi_calculator.h:27
esphome::aqi::AQICalculator::get_grid_index_
int get_grid_index_(uint16_t value, int array[AMOUNT_OF_LEVELS][2])
Definition
aqi_calculator.h:43
esphome::aqi::AQICalculator::calculate_index_
int calculate_index_(uint16_t value, int array[AMOUNT_OF_LEVELS][2])
Definition
aqi_calculator.h:30
esphome::aqi::AQICalculator::get_aqi
uint16_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value) override
Definition
aqi_calculator.h:12
esphome::aqi::AQICalculator::index_grid_
int index_grid_[AMOUNT_OF_LEVELS][2]
Definition
aqi_calculator.h:22
esphome::aqi::AQICalculator::AMOUNT_OF_LEVELS
static const int AMOUNT_OF_LEVELS
Definition
aqi_calculator.h:20
esphome::aqi::AbstractAQICalculator
Definition
abstract_aqi_calculator.h:7
esphome::aqi
Definition
abstract_aqi_calculator.h:5
Generated by
1.12.0