ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ltr390.cpp
Go to the documentation of this file.
1#include "ltr390.h"
2#include <bitset>
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7namespace esphome::ltr390 {
8
9ESPHOME_LOG_TAG(TAG, "ltr390");
10
11static const uint8_t LTR390_WAKEUP_TIME = 10;
12static const uint8_t LTR390_SETTLE_TIME = 5;
13
14static const uint8_t LTR390_MAIN_CTRL = 0x00;
15static const uint8_t LTR390_MEAS_RATE = 0x04;
16static const uint8_t LTR390_GAIN = 0x05;
17static const uint8_t LTR390_PART_ID = 0x06;
18static const uint8_t LTR390_MAIN_STATUS = 0x07;
19
20static constexpr uint8_t LTR390_ALS_DATA = 0x0D;
21static constexpr uint8_t LTR390_UVS_DATA = 0x10;
22
23static constexpr uint8_t GAINVALUES[5] PROGMEM = {1, 3, 6, 9, 18};
24static uint8_t gain_value(LTR390GAIN gain) { return progmem_read_byte(&GAINVALUES[gain]); }
25static uint8_t resolution_bits(LTR390RESOLUTION res) { return res == LTR390_RESOLUTION_13BIT ? 13 : 20 - res; }
26
27// UV counts per UVI at 18x gain and 20-bit resolution
28static constexpr float SENSITIVITY_MAX = 2300;
29static constexpr float GAIN_MAX = 18;
30
32 const uint8_t num_bytes = 3;
33 uint8_t buffer[num_bytes];
34
35 // Wait until data available
36 constexpr uint32_t max_wait_ms = 25;
37 const uint32_t now = millis();
38 while (true) {
39 std::bitset<8> status = this->reg(LTR390_MAIN_STATUS).get();
40 bool available = status[3];
41 if (available)
42 break;
43
44 if (millis() - now > max_wait_ms) {
45 ESP_LOGW(TAG, "Sensor didn't return any data, aborting");
46 return {};
47 }
48 ESP_LOGV(TAG, "Waiting for data");
49 delay(1);
50 }
51
52 if (!this->read_bytes(mode == LTR390_MODE_UVS ? LTR390_UVS_DATA : LTR390_ALS_DATA, buffer, num_bytes)) {
53 ESP_LOGW(TAG, "Reading data from sensor failed!");
54 return {};
55 }
56
57 return encode_uint24(buffer[2], buffer[1], buffer[0]);
58}
59
62 if (!val.has_value())
63 return;
64 uint32_t als = *val;
65
66 if (this->light_sensor_ != nullptr) {
67 // The integration time halves per resolution step, from 400 ms at 20-bit
68 float lux = ((0.6f * als * (1 << this->res_als_)) / (4.0f * gain_value(this->gain_als_))) * this->wfac_;
69 this->light_sensor_->publish_state(lux);
70 }
71
72 PUBLISH_STATE(this->als_sensor_, als);
73}
74
77 if (!val.has_value())
78 return;
79 uint32_t uv = *val;
80
81 if (this->uvi_sensor_ != nullptr) {
82 // Set sensitivity by linearly scaling against known value in the datasheet
83 float sensitivity_uv = SENSITIVITY_MAX * gain_value(this->gain_uv_) / (GAIN_MAX * (1 << this->res_uv_));
84
85 this->uvi_sensor_->publish_state((uv / sensitivity_uv) * this->wfac_);
86 }
87
88 PUBLISH_STATE(this->uv_sensor_, uv);
89}
90
92 std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
93 ctrl[LTR390_CTRL_EN] = false;
94 this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
95 this->reading_ = false;
96}
97
99 // Set mode
100 std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
102 ctrl[LTR390_CTRL_EN] = true;
103 this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
104
105 const bool uv = mode == LTR390_MODE_UVS;
106 const uint8_t res = uv ? this->res_uv_ : this->res_als_;
107 this->reg(LTR390_GAIN) = uv ? this->gain_uv_ : this->gain_als_;
108 // Resolution in bits 6:4, rate 0 (fastest); the device slows it to the conversion time
109 this->reg(LTR390_MEAS_RATE) = res << 4;
110 // Conversion time: 400 ms at 20-bit, halved per step (12 ms at 13-bit)
111 const uint32_t int_time = 400u >> res;
112
113 // After the sensor integration time do the following
114 this->set_timeout(int_time + LTR390_WAKEUP_TIME + LTR390_SETTLE_TIME, [this, mode]() {
115 // Read from the sensor and continue to next mode or standby
116 if (mode == LTR390_MODE_ALS) {
117 this->read_als_();
118 if (this->enabled_modes_ & ENABLED_MODE_UVS) {
120 return;
121 }
122 } else {
123 this->read_uvs_();
124 }
125 this->standby_();
126 });
127}
128
130 // reset
131 std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
132 ctrl[LTR390_CTRL_RST] = true;
133 this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
134 delay(10);
135
136 // Enable
137 ctrl = this->reg(LTR390_MAIN_CTRL).get();
138 ctrl[LTR390_CTRL_EN] = true;
139 this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
140
141 // check enabled
142 ctrl = this->reg(LTR390_MAIN_CTRL).get();
143 bool enabled = ctrl[LTR390_CTRL_EN];
144
145 if (!enabled) {
146 ESP_LOGW(TAG, "Sensor didn't respond with enabled state");
147 this->mark_failed();
148 return;
149 }
150
151 // Set sensor read state
152 this->reading_ = false;
153
154 // Determine which modes are enabled based on configured sensors
155 if (this->light_sensor_ != nullptr || this->als_sensor_ != nullptr) {
157 }
158 if (this->uvi_sensor_ != nullptr || this->uv_sensor_ != nullptr) {
160 }
161}
162
163void LTR390Component::dump_config() {
164 LOG_I2C_DEVICE(this);
165 ESP_LOGCONFIG(TAG,
166 " ALS Gain: X%u\n"
167 " ALS Resolution: %u-bit\n"
168 " UV Gain: X%u\n"
169 " UV Resolution: %u-bit",
170 gain_value(this->gain_als_), resolution_bits(this->res_als_), gain_value(this->gain_uv_),
171 resolution_bits(this->res_uv_));
172}
173
174void LTR390Component::update() {
175 if (this->reading_ || this->enabled_modes_ == 0)
176 return;
177
178 this->reading_ = true;
180}
181
182} // namespace esphome::ltr390
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
uint8_t get() const
returns the register value
Definition i2c.cpp:143
static constexpr uint8_t ENABLED_MODE_UVS
Definition ltr390.h:61
void read_mode_(LTR390MODE mode)
Definition ltr390.cpp:98
static constexpr uint8_t ENABLED_MODE_ALS
Definition ltr390.h:60
LTR390RESOLUTION res_uv_
Definition ltr390.h:76
optional< uint32_t > read_sensor_data_(LTR390MODE mode)
Definition ltr390.cpp:31
LTR390RESOLUTION res_als_
Definition ltr390.h:75
uint32_t buffer
Definition gif.cpp:50
AlsGain501 gain
mopeka_std_values val[3]
@ LTR390_RESOLUTION_13BIT
Definition ltr390.h:39
ESPHOME_LOG_TAG(TAG, "ltr390")
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:1047
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t