11static const uint8_t TCS34725_ADDRESS = 0x29;
12static const uint8_t TCS34725_COMMAND_BIT = 0x80;
13static const uint8_t TCS34725_REGISTER_ID = TCS34725_COMMAND_BIT | 0x12;
14static const uint8_t TCS34725_REGISTER_ATIME = TCS34725_COMMAND_BIT | 0x01;
15static const uint8_t TCS34725_REGISTER_CONTROL = TCS34725_COMMAND_BIT | 0x0F;
16static const uint8_t TCS34725_REGISTER_ENABLE = TCS34725_COMMAND_BIT | 0x00;
17static const uint8_t TCS34725_REGISTER_CRGBDATAL = TCS34725_COMMAND_BIT | 0x14;
19void TCS34725Component::setup() {
43void TCS34725Component::dump_config() {
44 ESP_LOGCONFIG(TAG,
"TCS34725:");
46 LOG_UPDATE_INTERVAL(
this);
48 LOG_SENSOR(
" ",
"Clear Channel", this->clear_sensor_);
49 LOG_SENSOR(
" ",
"Red Channel", this->red_sensor_);
50 LOG_SENSOR(
" ",
"Green Channel", this->green_sensor_);
51 LOG_SENSOR(
" ",
"Blue Channel", this->blue_sensor_);
52 LOG_SENSOR(
" ",
"Illuminance", this->illuminance_sensor_);
53 LOG_SENSOR(
" ",
"Color Temperature", this->color_temperature_sensor_);
69void TCS34725Component::calculate_temperature_and_lux_(uint16_t r, uint16_t g, uint16_t b, uint16_t c) {
76 static const float DF = 310.f;
77 static const float R_COEF = 0.136f;
78 static const float G_COEF = 1.f;
79 static const float B_COEF = -0.444f;
80 static const float CT_COEF = 3810.f;
81 static const float CT_OFFSET = 1391.f;
82 static const float MAX_ILLUMINANCE = 100000.0f;
83 static const float MAX_COLOR_TEMPERATURE = 15000.0f;
84 static const float MIN_COLOR_TEMPERATURE = 1000.0f;
102 if ((256 - this->integration_reg_) > 63) {
107 sat = 1024.f * (256.f - this->integration_reg_);
134 ESP_LOGI(TAG,
"Saturation too high, sample discarded, autogain ongoing");
138 "Saturation too high, sample with saturation %.1f and clear %d lux/color temperature cannot reliably "
139 "calculated, reduce integration/gain or use a grey filter.",
147 float g1 = R_COEF * (
float) r + G_COEF * (
float) g + B_COEF * (
float) b;
153 ESP_LOGW(TAG,
"Calculated illuminance greater than limit (%f), setting to NAN", this->
illuminance_);
159 ESP_LOGW(TAG,
"Red channel is zero, cannot compute color temperature");
171 ESP_LOGW(TAG,
"Calculated color temperature value too low (%f), setting to NAN", this->
color_temperature_);
174 ESP_LOGW(TAG,
"Calculated color temperature value too high (%f), setting to NAN", this->
color_temperature_);
179void TCS34725Component::update() {
185 ESP_LOGW(TAG,
"Error reading TCS34725 sensor data");
190 uint16_t raw_c =
data[0];
191 uint16_t raw_r =
data[1];
192 uint16_t raw_g =
data[2];
193 uint16_t raw_b =
data[3];
195 ESP_LOGV(TAG,
"Raw values clear=%d red=%d green=%d blue=%d", raw_c, raw_r, raw_g, raw_b);
203 channel_c = channel_r = channel_g = channel_b = 0.0f;
207 channel_r = raw_r / sum * 100.0f;
208 channel_g = raw_g / sum * 100.0f;
209 channel_b = raw_b / sum * 100.0f;
210 channel_c = raw_c / max_count * 100.0f;
213 PUBLISH_STATE(this->clear_sensor_, channel_c);
214 PUBLISH_STATE(this->red_sensor_, channel_r);
215 PUBLISH_STATE(this->green_sensor_, channel_g);
216 PUBLISH_STATE(this->blue_sensor_, channel_b);
218 if (this->illuminance_sensor_ || this->color_temperature_sensor_) {
219 calculate_temperature_and_lux_(raw_r, raw_g, raw_b, raw_c);
228 PUBLISH_STATE(this->illuminance_sensor_, this->
illuminance_);
234 "Got Red=%.1f%%,Green=%.1f%%,Blue=%.1f%%,Clear=%.1f%% Illuminance=%.1flx Color "
241 float integration_time_ideal;
243 integration_time_ideal = 60 / ((
float) std::max((uint16_t) 1, raw_c) / 655.35f) * this->
integration_time_;
245 uint8_t gain_reg_val_new = this->gain_reg_;
249 if (this->gain_reg_ < 3) {
251 gain_reg_val_new = this->gain_reg_ + 1;
253 integration_time_ideal = integration_time_ideal / 4;
258 if (this->gain_reg_ > 0) {
260 gain_reg_val_new = this->gain_reg_ - 1;
262 integration_time_ideal = integration_time_ideal * 4;
267 float integration_time_next = integration_time_ideal;
268 if (integration_time_ideal > 2.4f * 256) {
269 integration_time_next = 2.4f * 256;
271 if (integration_time_ideal < 154) {
272 integration_time_next = 154;
276 uint8_t regval_atime = (uint8_t) (256.f - integration_time_next / 2.4f);
277 ESP_LOGD(TAG,
"Integration time: %.1fms, ideal: %.1fms regval_new %d Gain: %.f Clear channel raw: %d gain reg: %d",
280 if (this->integration_reg_ != regval_atime || gain_reg_val_new != this->gain_reg_) {
281 this->integration_reg_ = regval_atime;
282 this->gain_reg_ = gain_reg_val_new;
287 ESP_LOGW(TAG,
"TCS34725I update timing failed!");
309 ESP_LOGI(TAG,
"TCS34725I Integration time set to: %.1fms", this->
integration_time_);
312 this->gain_reg_ =
gain;
332void TCS34725Component::set_glass_attenuation_factor(
float ga) {