7static const char *
const TAG =
"opt3001.sensor";
9static const uint8_t OPT3001_REG_RESULT = 0x00;
10static const uint8_t OPT3001_REG_CONFIGURATION = 0x01;
12static const uint16_t OPT3001_CONFIGURATION_RANGE_FULL = 0b1100000000000000;
13static const uint16_t OPT3001_CONFIGURATION_CONVERSION_TIME_800 = 0b100000000000;
14static const uint16_t OPT3001_CONFIGURATION_CONVERSION_MODE_MASK = 0b11000000000;
15static const uint16_t OPT3001_CONFIGURATION_CONVERSION_MODE_SINGLE_SHOT = 0b01000000000;
16static const uint16_t OPT3001_CONFIGURATION_CONVERSION_MODE_SHUTDOWN = 0b00000000000;
19static const uint16_t OPT3001_CONFIGURATION_FULL_RANGE_ONE_SHOT = OPT3001_CONFIGURATION_RANGE_FULL |
20 OPT3001_CONFIGURATION_CONVERSION_TIME_800 |
21 OPT3001_CONFIGURATION_CONVERSION_MODE_SINGLE_SHOT;
22static const uint16_t OPT3001_CONVERSION_TIME_800 = 825;
37 ESP_LOGW(TAG,
"Reading configuration register failed");
43 if ((raw_value & OPT3001_CONFIGURATION_CONVERSION_MODE_MASK) != OPT3001_CONFIGURATION_CONVERSION_MODE_SHUTDOWN) {
45 ESP_LOGW(TAG,
"Data not ready; waiting 10ms");
51 ESP_LOGW(TAG,
"Reading result register failed");
57 uint8_t exponent = raw_value >> 12;
58 uint16_t mantissa = raw_value & 0b111111111111;
60 double lx = 0.01 * pow(2.0,
double(exponent)) * double(mantissa);
66 uint16_t start_measurement =
i2c::htoi2cs(OPT3001_CONFIGURATION_FULL_RANGE_ONE_SHOT);
67 if (this->
write_register(OPT3001_REG_CONFIGURATION,
reinterpret_cast<uint8_t *
>(&start_measurement), 2) !=
69 ESP_LOGW(TAG,
"Triggering one shot measurement failed");
74 this->
set_timeout(
"read", OPT3001_CONVERSION_TIME_800, [
this, f]() {
76 ESP_LOGW(TAG,
"Starting configuration register read failed");
86 LOG_SENSOR(
"",
"OPT3001",
this);
89 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
92 LOG_UPDATE_INTERVAL(
this);
108 if (std::isnan(
val)) {
113 ESP_LOGD(TAG,
"'%s': Illuminance=%.1flx", this->
get_name().c_str(),
val);
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
const StringRef & get_name() const
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
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
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
void read_result_(const std::function< void(float)> &f)
void read_lx_(const std::function< void(float)> &f)
float get_setup_priority() const override
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)
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.