11static constexpr uint8_t REG_SYSTEM_CONTROL = 0x40;
12static constexpr uint8_t REG_MODE_CONTROL1 = 0x41;
13static constexpr uint8_t REG_MODE_CONTROL2 = 0x42;
14static constexpr uint8_t REG_MODE_CONTROL3 = 0x44;
15static constexpr uint8_t REG_RED_DATA_LSB = 0x50;
16static constexpr uint8_t REG_INTERRUPT = 0x60;
17static constexpr uint8_t REG_TH_LSB = 0x62;
18static constexpr uint8_t REG_MANUFACTURER_ID = 0x92;
20static constexpr uint8_t MANUFACTURER_ID = 0xE0;
21static constexpr uint8_t PART_ID = 0b001011;
22static constexpr uint8_t SYSTEM_CONTROL_PART_ID_MASK = 0x3F;
23static constexpr uint8_t SYSTEM_CONTROL_SW_RESET = 0x80;
24static constexpr uint8_t MODE_CONTROL2_RGBC_EN = 0x10;
25static constexpr uint8_t MODE_CONTROL2_VALID = 0x80;
26static constexpr uint8_t MODE_CONTROL3_VALUE = 0x02;
27static constexpr uint8_t INTERRUPT_ENABLE = 0x01;
29static constexpr uint8_t BH1745_CHANNELS = 4;
30static constexpr uint32_t RESET_DELAY_MS = 100;
31static constexpr uint32_t BASE_MEAS_TIME_MS = 160;
34 return BASE_MEAS_TIME_MS << static_cast<uint8_t>(time);
41static float calculate_lux(uint16_t red, uint16_t green,
bool low_clear_ratio,
float scale) {
44 float lx = low_clear_ratio ? 0.202f * red + 0.766f * green : 0.159f * red + 0.646f * green;
48static float calculate_cct(uint16_t red, uint16_t green, uint16_t blue,
bool low_clear_ratio) {
51 float all =
static_cast<float>(
static_cast<uint32_t>(red) + green + blue);
52 float r_ratio = red / all;
53 float b_ratio = blue / all;
55 if (low_clear_ratio) {
56 float b_eff = std::fmin(b_ratio * 3.13f, 1.0f);
57 ct = ((1.0f - b_eff) * 12746.0f * std::exp(-2.911f * r_ratio)) + (b_eff * 1637.0f * std::exp(4.865f * b_ratio));
59 float b_eff = std::fmin(b_ratio * 10.67f, 1.0f);
60 ct = ((1.0f - b_eff) * 16234.0f * std::exp(-2.781f * r_ratio)) + (b_eff * 1882.0f * std::exp(4.448f * b_ratio));
66 if (!this->
check_chip_id(
this, REG_MANUFACTURER_ID, MANUFACTURER_ID))
68 uint8_t system_control;
69 if (!this->
read_byte(REG_SYSTEM_CONTROL, &system_control) ||
70 (system_control & SYSTEM_CONTROL_PART_ID_MASK) != PART_ID) {
74 if (!this->
write_byte(REG_SYSTEM_CONTROL, SYSTEM_CONTROL_SW_RESET)) {
87 static constexpr uint8_t THRESHOLDS[4] = {0x00, 0x00, 0xFF, 0xFF};
88 if (!this->
write_bytes(REG_TH_LSB, THRESHOLDS,
sizeof(THRESHOLDS))) {
95 !this->
write_byte(REG_MODE_CONTROL2,
static_cast<uint8_t
>(this->
adc_gain_) | MODE_CONTROL2_RGBC_EN) ||
96 !this->
write_byte(REG_MODE_CONTROL3, MODE_CONTROL3_VALUE)) {
104void BH1745Component::dump_config() {
108 " Integration time: %" PRIu32
" ms\n"
109 " Glass attenuation factor: %.2f",
112 LOG_I2C_DEVICE(
this);
113 LOG_UPDATE_INTERVAL(
this);
115 LOG_SENSOR(
" ",
"Red Counts", this->red_counts_sensor_);
116 LOG_SENSOR(
" ",
"Green Counts", this->green_counts_sensor_);
117 LOG_SENSOR(
" ",
"Blue Counts", this->blue_counts_sensor_);
118 LOG_SENSOR(
" ",
"Clear Counts", this->clear_counts_sensor_);
119 LOG_SENSOR(
" ",
"Illuminance", this->illuminance_sensor_);
120 LOG_SENSOR(
" ",
"Color Temperature", this->color_temperature_sensor_);
123void BH1745Component::update() {
128 uint8_t mode_control2;
129 if (!this->
read_byte(REG_MODE_CONTROL2, &mode_control2)) {
133 if ((mode_control2 & MODE_CONTROL2_VALID) == 0)
136 uint16_t
buffer[BH1745_CHANNELS];
140 const uint16_t red =
buffer[0];
141 const uint16_t green =
buffer[1];
142 const uint16_t blue =
buffer[2];
143 const uint16_t clear =
buffer[3];
144 ESP_LOGV(TAG,
"Red: %u, Green: %u, Blue: %u, Clear: %u", red, green, blue, clear);
146 PUBLISH_STATE(this->red_counts_sensor_, red);
147 PUBLISH_STATE(this->green_counts_sensor_, green);
148 PUBLISH_STATE(this->blue_counts_sensor_, blue);
149 PUBLISH_STATE(this->clear_counts_sensor_, clear);
152 const bool low_clear_ratio =
static_cast<uint32_t>(clear) * 25 <
static_cast<uint32_t>(green) * 4;
153 PUBLISH_STATE(this->illuminance_sensor_, calculate_lux(red, green, low_clear_ratio, this->
lux_scale_));
154 PUBLISH_STATE(this->color_temperature_sensor_, calculate_cct(red, green, blue, low_clear_ratio));
157void BH1745Component::set_interrupt_state(
bool on) {
160 if (!this->
write_byte(REG_INTERRUPT, on ? INTERRUPT_ENABLE : 0x00)) {