tcs34725: Add check for Division by Zero (#7485)
Some checks are pending
CI / CI Status (push) Blocked by required conditions
CI / Create common environment (push) Waiting to run
CI / Check black (push) Blocked by required conditions
CI / Check flake8 (push) Blocked by required conditions
CI / Check pylint (push) Blocked by required conditions
CI / Check pyupgrade (push) Blocked by required conditions
CI / Run script/ci-custom (push) Blocked by required conditions
CI / Run pytest (push) Blocked by required conditions
CI / Check clang-format (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 1/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 2/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 3/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 4/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 IDF (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP8266 (push) Blocked by required conditions
CI / list-components (push) Blocked by required conditions
CI / Component test (push) Blocked by required conditions
CI / Split components for testing into 20 groups maximum (push) Blocked by required conditions
CI / Test split components (push) Blocked by required conditions

This commit is contained in:
@RubenKelevra 2024-09-25 04:28:22 +02:00 committed by GitHub
parent fa9df32979
commit b61577b68b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
#include "tcs34725.h" #include "tcs34725.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include <algorithm>
namespace esphome { namespace esphome {
namespace tcs34725 { namespace tcs34725 {
@ -254,7 +255,8 @@ void TCS34725Component::update() {
// change integration time an gain to achieve maximum resolution an dynamic range // change integration time an gain to achieve maximum resolution an dynamic range
// calculate optimal integration time to achieve 70% satuaration // calculate optimal integration time to achieve 70% satuaration
float integration_time_ideal; float integration_time_ideal;
integration_time_ideal = 60 / ((float) raw_c / 655.35) * this->integration_time_;
integration_time_ideal = 60 / ((float) std::max((uint16_t) 1, raw_c) / 655.35f) * this->integration_time_;
uint8_t gain_reg_val_new = this->gain_reg_; uint8_t gain_reg_val_new = this->gain_reg_;
// increase gain if less than 20% of white channel used and high integration time // increase gain if less than 20% of white channel used and high integration time