10 static const char *
const TAG =
"tcs34725";
12 static const uint8_t TCS34725_ADDRESS = 0x29;
13 static const uint8_t TCS34725_COMMAND_BIT = 0x80;
14 static const uint8_t TCS34725_REGISTER_ID = TCS34725_COMMAND_BIT | 0x12;
15 static const uint8_t TCS34725_REGISTER_ATIME = TCS34725_COMMAND_BIT | 0x01;
16 static const uint8_t TCS34725_REGISTER_CONTROL = TCS34725_COMMAND_BIT | 0x0F;
17 static const uint8_t TCS34725_REGISTER_ENABLE = TCS34725_COMMAND_BIT | 0x00;
18 static const uint8_t TCS34725_REGISTER_CRGBDATAL = TCS34725_COMMAND_BIT | 0x14;
21 ESP_LOGCONFIG(TAG,
"Setting up TCS34725...");
46 ESP_LOGCONFIG(TAG,
"TCS34725:");
49 ESP_LOGE(TAG,
"Communication with TCS34725 failed!");
51 LOG_UPDATE_INTERVAL(
this);
75 void TCS34725Component::calculate_temperature_and_lux_(uint16_t r, uint16_t g, uint16_t b, uint16_t c) {
82 static const float DF = 310.f;
83 static const float R_COEF = 0.136f;
84 static const float G_COEF = 1.f;
85 static const float B_COEF = -0.444f;
86 static const float CT_COEF = 3810.f;
87 static const float CT_OFFSET = 1391.f;
88 static const float MAX_ILLUMINANCE = 100000.0f;
89 static const float MAX_COLOR_TEMPERATURE = 15000.0f;
90 static const float MIN_COLOR_TEMPERATURE = 1000.0f;
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 lux/color temperature cannot reliably " 145 "calculated, reduce integration/gain or use a grey filter.",
153 float g1 = R_COEF * (float) r + G_COEF * (
float) g + B_COEF * (float) b;
159 ESP_LOGW(TAG,
"Calculated illuminance greater than limit (%f), setting to NAN", this->
illuminance_);
165 ESP_LOGW(TAG,
"Red channel is zero, cannot compute color temperature");
177 ESP_LOGW(TAG,
"Calculated color temperature value too low (%f), setting to NAN", this->
color_temperature_);
180 ESP_LOGW(TAG,
"Calculated color temperature value too high (%f), setting to NAN", this->
color_temperature_);
191 ESP_LOGW(TAG,
"Error reading TCS34725 sensor data");
201 ESP_LOGV(TAG,
"Raw values clear=%d red=%d green=%d blue=%d", raw_c, raw_r, raw_g, raw_b);
209 channel_c = channel_r = channel_g = channel_b = 0.0f;
213 channel_r = raw_r / sum * 100.0f;
214 channel_g = raw_g / sum * 100.0f;
215 channel_b = raw_b / sum * 100.0f;
216 channel_c = raw_c / max_count * 100.0f;
229 calculate_temperature_and_lux_(raw_r, raw_g, raw_b, raw_c);
246 "Got Red=%.1f%%,Green=%.1f%%,Blue=%.1f%%,Clear=%.1f%% Illuminance=%.1flx Color " 253 float integration_time_ideal;
255 integration_time_ideal = 60 / ((float) std::max((uint16_t) 1, raw_c) / 655.35f) * this->
integration_time_;
257 uint8_t gain_reg_val_new = this->gain_reg_;
261 if (this->gain_reg_ < 3) {
263 gain_reg_val_new = this->gain_reg_ + 1;
265 integration_time_ideal = integration_time_ideal / 4;
270 if (this->gain_reg_ > 0) {
272 gain_reg_val_new = this->gain_reg_ - 1;
274 integration_time_ideal = integration_time_ideal * 4;
279 float integration_time_next = integration_time_ideal;
280 if (integration_time_ideal > 2.4f * 256) {
281 integration_time_next = 2.4f * 256;
283 if (integration_time_ideal < 154) {
284 integration_time_next = 154;
288 uint8_t regval_atime = (uint8_t) (256.f - integration_time_next / 2.4f);
289 ESP_LOGD(TAG,
"Integration time: %.1fms, ideal: %.1fms regval_new %d Gain: %.f Clear channel raw: %d gain reg: %d",
292 if (this->integration_reg_ != regval_atime || gain_reg_val_new != this->gain_reg_) {
293 this->integration_reg_ = regval_atime;
294 this->gain_reg_ = gain_reg_val_new;
299 ESP_LOGW(TAG,
"TCS34725I update timing failed!");
321 ESP_LOGI(TAG,
"TCS34725I Integration time set to: %.1fms", this->
integration_time_);
324 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.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
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_
sensor::Sensor * red_sensor_
void dump_config() override
void IRAM_ATTR HOT delay(uint32_t ms)