Fix HM3301 AQI int8 overflow (#3361)

This commit is contained in:
calco88 2022-04-10 15:14:53 -07:00 committed by GitHub
parent 7663716ae8
commit a2d0c1bf18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 4 additions and 4 deletions

View file

@ -7,7 +7,7 @@ namespace hm3301 {
class AbstractAQICalculator {
public:
virtual uint8_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value) = 0;
virtual uint16_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value) = 0;
};
} // namespace hm3301

View file

@ -7,7 +7,7 @@ namespace hm3301 {
class AQICalculator : public AbstractAQICalculator {
public:
uint8_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value) override {
uint16_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value) override {
int pm2_5_index = calculate_index_(pm2_5_value, pm2_5_calculation_grid_);
int pm10_0_index = calculate_index_(pm10_0_value, pm10_0_calculation_grid_);

View file

@ -8,7 +8,7 @@ namespace hm3301 {
class CAQICalculator : public AbstractAQICalculator {
public:
uint8_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value) override {
uint16_t get_aqi(uint16_t pm2_5_value, uint16_t pm10_0_value) override {
int pm2_5_index = calculate_index_(pm2_5_value, pm2_5_calculation_grid_);
int pm10_0_index = calculate_index_(pm10_0_value, pm10_0_calculation_grid_);

View file

@ -62,7 +62,7 @@ void HM3301Component::update() {
pm_10_0_value = get_sensor_value_(data_buffer_, PM_10_0_VALUE_INDEX);
}
int8_t aqi_value = -1;
int16_t aqi_value = -1;
if (this->aqi_sensor_ != nullptr && pm_2_5_value != -1 && pm_10_0_value != -1) {
AbstractAQICalculator *calculator = this->aqi_calculator_factory_.get_calculator(this->aqi_calc_type_);
aqi_value = calculator->get_aqi(pm_2_5_value, pm_10_0_value);