8static constexpr uint32_t RESULT_RETRY_TIMEOUT_ID = 0;
9static constexpr uint32_t READ_TIMEOUT_ID = 1;
11static const uint8_t OPT3001_REG_RESULT = 0x00;
12static const uint8_t OPT3001_REG_CONFIGURATION = 0x01;
14static const uint16_t OPT3001_CONFIGURATION_RANGE_FULL = 0b1100000000000000;
15static const uint16_t OPT3001_CONFIGURATION_CONVERSION_TIME_800 = 0b100000000000;
16static const uint16_t OPT3001_CONFIGURATION_CONVERSION_MODE_MASK = 0b11000000000;
17static const uint16_t OPT3001_CONFIGURATION_CONVERSION_MODE_SINGLE_SHOT = 0b01000000000;
18static const uint16_t OPT3001_CONFIGURATION_CONVERSION_MODE_SHUTDOWN = 0b00000000000;
21static const uint16_t OPT3001_CONFIGURATION_FULL_RANGE_ONE_SHOT = OPT3001_CONFIGURATION_RANGE_FULL |
22 OPT3001_CONFIGURATION_CONVERSION_TIME_800 |
23 OPT3001_CONFIGURATION_CONVERSION_MODE_SINGLE_SHOT;
24static const uint16_t OPT3001_CONVERSION_TIME_800 = 825;
39 ESP_LOGW(TAG,
"Reading configuration register failed");
45 if ((raw_value & OPT3001_CONFIGURATION_CONVERSION_MODE_MASK) != OPT3001_CONFIGURATION_CONVERSION_MODE_SHUTDOWN) {
47 ESP_LOGW(TAG,
"Data not ready; waiting 10ms");
53 ESP_LOGW(TAG,
"Reading result register failed");
59 uint8_t exponent = raw_value >> 12;
60 uint16_t mantissa = raw_value & 0b111111111111;
62 double lx = 0.01 * pow(2.0,
double(exponent)) * double(mantissa);
68 uint16_t start_measurement =
i2c::htoi2cs(OPT3001_CONFIGURATION_FULL_RANGE_ONE_SHOT);
69 if (this->
write_register(OPT3001_REG_CONFIGURATION,
reinterpret_cast<uint8_t *
>(&start_measurement), 2) !=
71 ESP_LOGW(TAG,
"Triggering one shot measurement failed");
76 this->
set_timeout(READ_TIMEOUT_ID, OPT3001_CONVERSION_TIME_800, [
this, f]() {
78 ESP_LOGW(TAG,
"Starting configuration register read failed");
88 LOG_SENSOR(
"",
"OPT3001",
this);
91 LOG_UPDATE_INTERVAL(
this);
111 ESP_LOGD(TAG,
"'%s': Illuminance=%.1flx", LOG_STR_ARG(this->
get_log_name()), val);
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
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
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
void read_result_(const std::function< void(float)> &f)
void read_lx_(const std::function< void(float)> &f)
void dump_config() override
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.
uint16_t htoi2cs(uint16_t hostshort)
ESPHOME_LOG_TAG(TAG, "opt3001.sensor")