8 static const char *
const TAG =
"tcs34725";
10 static const uint8_t TCS34725_ADDRESS = 0x29;
11 static const uint8_t TCS34725_COMMAND_BIT = 0x80;
12 static const uint8_t TCS34725_REGISTER_ID = TCS34725_COMMAND_BIT | 0x12;
13 static const uint8_t TCS34725_REGISTER_ATIME = TCS34725_COMMAND_BIT | 0x01;
14 static const uint8_t TCS34725_REGISTER_CONTROL = TCS34725_COMMAND_BIT | 0x0F;
15 static const uint8_t TCS34725_REGISTER_ENABLE = TCS34725_COMMAND_BIT | 0x00;
16 static const uint8_t TCS34725_REGISTER_CDATAL = TCS34725_COMMAND_BIT | 0x14;
17 static const uint8_t TCS34725_REGISTER_RDATAL = TCS34725_COMMAND_BIT | 0x16;
18 static const uint8_t TCS34725_REGISTER_GDATAL = TCS34725_COMMAND_BIT | 0x18;
19 static const uint8_t TCS34725_REGISTER_BDATAL = TCS34725_COMMAND_BIT | 0x1A;
22 ESP_LOGCONFIG(TAG,
"Setting up TCS34725...");
47 ESP_LOGCONFIG(TAG,
"TCS34725:");
50 ESP_LOGE(TAG,
"Communication with TCS34725 failed!");
52 LOG_UPDATE_INTERVAL(
this);
76 void TCS34725Component::calculate_temperature_and_lux_(uint16_t r, uint16_t g, uint16_t b, uint16_t c) {
85 static const float DF = 310.f;
86 static const float R_COEF = 0.136f;
87 static const float G_COEF = 1.f;
88 static const float B_COEF = -0.444f;
89 static const float CT_COEF = 3810.f;
90 static const float CT_OFFSET = 1391.f;
108 if ((256 - this->integration_reg_) > 63) {
113 sat = 1024.f * (256.f - this->integration_reg_);
140 ESP_LOGI(TAG,
"Saturation too high, sample discarded, autogain ongoing");
144 "Saturation too high, sample with saturation %.1f and clear %d treat values carefully or use grey filter",
151 ir = ((r + g + b) > c) ? (r + g + b - c) / 2 : 0;
163 "No light detected on red channel, switch to auto gain or adjust timing, values will be unreliable");
170 float g1 = R_COEF * r2 + G_COEF * g2 + B_COEF * b2;
204 ESP_LOGV(TAG,
"Raw values clear=%d red=%d green=%d blue=%d", raw_c, raw_r, raw_g, raw_b);
212 channel_c = channel_r = channel_g = channel_b = 0.0f;
216 channel_r = raw_r / sum * 100.0f;
217 channel_g = raw_g / sum * 100.0f;
218 channel_b = raw_b / sum * 100.0f;
219 channel_c = raw_c / max_count * 100.0f;
232 calculate_temperature_and_lux_(raw_r, raw_g, raw_b, raw_c);
249 "Got Red=%.1f%%,Green=%.1f%%,Blue=%.1f%%,Clear=%.1f%% Illuminance=%.1flx Color " 256 float integration_time_ideal;
257 integration_time_ideal = 60 / ((float) raw_c / 655.35) * this->
integration_time_;
259 uint8_t gain_reg_val_new = this->gain_reg_;
263 if (this->gain_reg_ < 3) {
265 gain_reg_val_new = this->gain_reg_ + 1;
267 integration_time_ideal = integration_time_ideal / 4;
272 if (this->gain_reg_ > 0) {
274 gain_reg_val_new = this->gain_reg_ - 1;
276 integration_time_ideal = integration_time_ideal * 4;
281 float integration_time_next = integration_time_ideal;
282 if (integration_time_ideal > 2.4f * 256) {
283 integration_time_next = 2.4f * 256;
285 if (integration_time_ideal < 154) {
286 integration_time_next = 154;
290 uint8_t regval_atime = (uint8_t) (256.f - integration_time_next / 2.4f);
291 ESP_LOGD(TAG,
"Integration time: %.1fms, ideal: %.1fms regval_new %d Gain: %.f Clear channel raw: %d gain reg: %d",
294 if (this->integration_reg_ != regval_atime || gain_reg_val_new != this->gain_reg_) {
295 this->integration_reg_ = regval_atime;
296 this->gain_reg_ = gain_reg_val_new;
301 ESP_LOGW(TAG,
"TCS34725I update timing failed!");
323 ESP_LOGI(TAG,
"TCS34725I Integration time set to: %.1fms", this->
integration_time_);
326 this->gain_reg_ =
gain;
void set_integration_time(TCS34725IntegrationTime integration_time)
const float DATA
For components that import data from directly connected sensors like DHT.
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
void status_set_warning(const char *message="unspecified")
i2c::ErrorCode write_config_register_(uint8_t a_register, uint8_t data)
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
sensor::Sensor * clear_sensor_
void set_glass_attenuation_factor(float ga)
sensor::Sensor * blue_sensor_
sensor::Sensor * green_sensor_
void set_gain(TCS34725Gain gain)
No error found during execution of method.
sensor::Sensor * color_temperature_sensor_
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
IntegrationTime501 integration_time
virtual void mark_failed()
Mark this component as failed.
bool integration_time_auto_
Implementation of SPI Controller mode.
float get_setup_priority() const override
sensor::Sensor * illuminance_sensor_
i2c::ErrorCode read_data_register_(uint8_t a_register, uint16_t &data)
sensor::Sensor * red_sensor_
void dump_config() override
void IRAM_ATTR HOT delay(uint32_t ms)