ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
opt3001.cpp
Go to the documentation of this file.
1#include "opt3001.h"
2#include "esphome/core/log.h"
3
4namespace esphome::opt3001 {
5
6static const char *const TAG = "opt3001.sensor";
7
8static const uint8_t OPT3001_REG_RESULT = 0x00;
9static const uint8_t OPT3001_REG_CONFIGURATION = 0x01;
10// See datasheet for full description of each bit.
11static const uint16_t OPT3001_CONFIGURATION_RANGE_FULL = 0b1100000000000000;
12static const uint16_t OPT3001_CONFIGURATION_CONVERSION_TIME_800 = 0b100000000000;
13static const uint16_t OPT3001_CONFIGURATION_CONVERSION_MODE_MASK = 0b11000000000;
14static const uint16_t OPT3001_CONFIGURATION_CONVERSION_MODE_SINGLE_SHOT = 0b01000000000;
15static const uint16_t OPT3001_CONFIGURATION_CONVERSION_MODE_SHUTDOWN = 0b00000000000;
16// tl;dr: Configure an automatic-ranged, 800ms single shot reading,
17// with INT processing disabled
18static const uint16_t OPT3001_CONFIGURATION_FULL_RANGE_ONE_SHOT = OPT3001_CONFIGURATION_RANGE_FULL |
19 OPT3001_CONFIGURATION_CONVERSION_TIME_800 |
20 OPT3001_CONFIGURATION_CONVERSION_MODE_SINGLE_SHOT;
21static const uint16_t OPT3001_CONVERSION_TIME_800 = 825; // give it 25 extra ms; it seems to not be ready quite often
22
23/*
24opt3001 properties:
25
26- e (exponent) = high 4 bits of result register
27- m (mantissa) = low 12 bits of result register
28- formula: (0.01 * 2^e) * m lx
29
30*/
31
32void OPT3001Sensor::read_result_(const std::function<void(float)> &f) {
33 // ensure the single shot flag is clear, indicating it's done
34 uint16_t raw_value;
35 if (this->read(reinterpret_cast<uint8_t *>(&raw_value), 2) != i2c::ERROR_OK) {
36 ESP_LOGW(TAG, "Reading configuration register failed");
37 f(NAN);
38 return;
39 }
40 raw_value = i2c::i2ctohs(raw_value);
41
42 if ((raw_value & OPT3001_CONFIGURATION_CONVERSION_MODE_MASK) != OPT3001_CONFIGURATION_CONVERSION_MODE_SHUTDOWN) {
43 // not ready; wait 10ms and try again
44 ESP_LOGW(TAG, "Data not ready; waiting 10ms");
45 this->set_timeout("opt3001_wait", 10, [this, f]() { read_result_(f); });
46 return;
47 }
48
49 if (this->read_register(OPT3001_REG_RESULT, reinterpret_cast<uint8_t *>(&raw_value), 2) != i2c::ERROR_OK) {
50 ESP_LOGW(TAG, "Reading result register failed");
51 f(NAN);
52 return;
53 }
54 raw_value = i2c::i2ctohs(raw_value);
55
56 uint8_t exponent = raw_value >> 12;
57 uint16_t mantissa = raw_value & 0b111111111111;
58
59 double lx = 0.01 * pow(2.0, double(exponent)) * double(mantissa);
60 f(float(lx));
61}
62
63void OPT3001Sensor::read_lx_(const std::function<void(float)> &f) {
64 // turn on (after one-shot sensor automatically powers down)
65 uint16_t start_measurement = i2c::htoi2cs(OPT3001_CONFIGURATION_FULL_RANGE_ONE_SHOT);
66 if (this->write_register(OPT3001_REG_CONFIGURATION, reinterpret_cast<uint8_t *>(&start_measurement), 2) !=
68 ESP_LOGW(TAG, "Triggering one shot measurement failed");
69 f(NAN);
70 return;
71 }
72
73 this->set_timeout("read", OPT3001_CONVERSION_TIME_800, [this, f]() {
74 if (this->write(&OPT3001_REG_CONFIGURATION, 1) != i2c::ERROR_OK) {
75 ESP_LOGW(TAG, "Starting configuration register read failed");
76 f(NAN);
77 return;
78 }
79
80 this->read_result_(f);
81 });
82}
83
85 LOG_SENSOR("", "OPT3001", this);
86 LOG_I2C_DEVICE(this);
87 if (this->is_failed()) {
88 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
89 }
90
91 LOG_UPDATE_INTERVAL(this);
92}
93
95 // Set a flag and skip just in case the sensor isn't responding,
96 // and we just keep waiting for it in read_result_.
97 // This way we don't end up with potentially boundless "threads"
98 // using up memory and eventually crashing the device
99 if (this->updating_) {
100 return;
101 }
102 this->updating_ = true;
103
104 this->read_lx_([this](float val) {
105 this->updating_ = false;
106
107 if (std::isnan(val)) {
108 this->status_set_warning();
109 this->publish_state(NAN);
110 return;
111 }
112 ESP_LOGD(TAG, "'%s': Illuminance=%.1flx", this->get_name().c_str(), val);
113 this->status_clear_warning();
114 this->publish_state(val);
115 });
116}
117
118} // namespace esphome::opt3001
bool is_failed() const
Definition component.h:284
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:454
void status_clear_warning()
Definition component.h:306
const StringRef & get_name() const
Definition entity_base.h:71
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
Definition i2c.cpp:34
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
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
Definition i2c.cpp:25
void read_result_(const std::function< void(float)> &f)
Definition opt3001.cpp:32
void read_lx_(const std::function< void(float)> &f)
Definition opt3001.cpp:63
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
mopeka_std_values val[3]
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:127
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
uint16_t htoi2cs(uint16_t hostshort)
Definition i2c.h:128