7static const char *
const TAG =
"bh1750.sensor";
9static const uint8_t BH1750_COMMAND_POWER_ON = 0b00000001;
10static const uint8_t BH1750_COMMAND_MT_REG_HI = 0b01000000;
11static const uint8_t BH1750_COMMAND_MT_REG_LO = 0b01100000;
12static const uint8_t BH1750_COMMAND_ONE_TIME_L = 0b00100011;
13static const uint8_t BH1750_COMMAND_ONE_TIME_H = 0b00100000;
14static const uint8_t BH1750_COMMAND_ONE_TIME_H2 = 0b00100001;
16static constexpr uint32_t MEASUREMENT_TIMEOUT_MS = 2000;
17static constexpr float HIGH_LIGHT_THRESHOLD_LX = 7000.0f;
20static constexpr uint16_t MTREG_DEFAULT = 69;
21static constexpr uint16_t MTREG_MIN = 31;
22static constexpr uint16_t MTREG_MAX = 254;
23static constexpr uint16_t MEAS_TIME_L_MS = 24;
24static constexpr uint16_t MEAS_TIME_H_MS = 180;
27static constexpr float RESOLUTION_DIVISOR = 1.2f;
28static constexpr float MODE_H2_DIVISOR = 2.0f;
31static constexpr int COUNTS_TARGET = 50000;
32static constexpr int COUNTS_NUMERATOR = 10;
33static constexpr int COUNTS_DENOMINATOR = 12;
36static constexpr uint8_t MTREG_HI_SHIFT = 5;
37static constexpr uint8_t MTREG_HI_MASK = 0b111;
38static constexpr uint8_t MTREG_LO_SHIFT = 0;
39static constexpr uint8_t MTREG_LO_MASK = 0b11111;
66 uint8_t turn_on = BH1750_COMMAND_POWER_ON;
75 LOG_SENSOR(
"",
"BH1750",
this);
78 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL_FOR, this->
get_name().c_str());
81 LOG_UPDATE_INTERVAL(
this);
85 const uint32_t now =
millis();
91 ESP_LOGW(TAG,
"Measurement timeout, resetting state");
94 ESP_LOGW(TAG,
"Previous measurement not complete, skipping update");
157 ESP_LOGD(TAG,
"'%s': Illuminance=%.1flx", this->
get_name().c_str(), lx);
168 uint8_t turn_on = BH1750_COMMAND_POWER_ON;
170 ESP_LOGW(TAG,
"Power on failed");
176 uint8_t mtreg_hi = BH1750_COMMAND_MT_REG_HI | ((mtreg >> MTREG_HI_SHIFT) & MTREG_HI_MASK);
177 uint8_t mtreg_lo = BH1750_COMMAND_MT_REG_LO | ((mtreg >> MTREG_LO_SHIFT) & MTREG_LO_MASK);
179 ESP_LOGW(TAG,
"Set measurement time failed");
191 cmd = BH1750_COMMAND_ONE_TIME_L;
192 meas_time = MEAS_TIME_L_MS * mtreg / MTREG_DEFAULT;
195 cmd = BH1750_COMMAND_ONE_TIME_H;
196 meas_time = MEAS_TIME_H_MS * mtreg / MTREG_DEFAULT;
199 cmd = BH1750_COMMAND_ONE_TIME_H2;
200 meas_time = MEAS_TIME_H_MS * mtreg / MTREG_DEFAULT;
207 ESP_LOGW(TAG,
"Start measurement failed");
223 ESP_LOGW(TAG,
"Read data failed");
228 float lx = float(raw_value) / RESOLUTION_DIVISOR;
231 lx /= MODE_H2_DIVISOR;
239 if (std::isnan(lx)) {
246 if (lx <= HIGH_LIGHT_THRESHOLD_LX) {
255 int ideal_mtreg = COUNTS_TARGET * COUNTS_NUMERATOR * MTREG_DEFAULT / (COUNTS_DENOMINATOR * (int) lx);
256 this->
fine_mtreg_ = std::min((
int) MTREG_MAX, std::max((
int) MTREG_MIN, ideal_mtreg));
259 ESP_LOGV(TAG,
"L result: %.1f -> Calculated mode=%d, mtreg=%d", lx, (
int) this->
fine_mode_, this->
fine_mtreg_);
BedjetMode mode
BedJet operating mode.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
void status_clear_warning()
const StringRef & get_name() const
bool start_measurement_(BH1750Mode mode, uint8_t mtreg, uint32_t now)
uint32_t measurement_start_time_
void process_coarse_result_(float lx)
void dump_config() override
uint32_t measurement_duration_
float get_setup_priority() const override
bool read_measurement_(float &lx_out)
@ WAITING_COARSE_MEASUREMENT
@ WAITING_FINE_MEASUREMENT
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
void publish_state(float state)
Publish a new state to the front-end.
uint16_t i2ctohs(uint16_t i2cshort)
@ ERROR_OK
No error found during execution of method.
const float DATA
For components that import data from directly connected sensors like DHT.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.