Skip to content

Commit

Permalink
tcs34725: Add check for Division by Zero (esphome#7485)
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenKelevra authored Sep 25, 2024
1 parent fa9df32 commit b61577b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion esphome/components/tcs34725/tcs34725.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "tcs34725.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include <algorithm>

namespace esphome {
namespace tcs34725 {
Expand Down Expand Up @@ -254,7 +255,8 @@ void TCS34725Component::update() {
// change integration time an gain to achieve maximum resolution an dynamic range
// calculate optimal integration time to achieve 70% satuaration
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_;
// increase gain if less than 20% of white channel used and high integration time
Expand Down

0 comments on commit b61577b

Please sign in to comment.