ESPHome 2025.12.0
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"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace ltr390 {
8
9static const char *const 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 const float GAINVALUES[5] = {1.0, 3.0, 6.0, 9.0, 18.0};
21static const float RESOLUTIONVALUE[6] = {4.0, 2.0, 1.0, 0.5, 0.25, 0.125};
22static const uint8_t RESOLUTION_BITS[6] = {20, 19, 18, 17, 16, 13};
23
24// Request fastest measurement rate - will be slowed by device if conversion rate is slower.
25static const float RESOLUTION_SETTING[6] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
26static const uint32_t MODEADDRESSES[2] = {0x0D, 0x10};
27
28static const float SENSITIVITY_MAX = 2300;
29static const float INTG_MAX = RESOLUTIONVALUE[0] * 100;
30static const int GAIN_MAX = GAINVALUES[4];
31
32uint32_t little_endian_bytes_to_int(const uint8_t *buffer, uint8_t num_bytes) {
33 uint32_t value = 0;
34
35 for (int i = 0; i < num_bytes; i++) {
36 value <<= 8;
37 value |= buffer[num_bytes - i - 1];
38 }
39
40 return value;
41}
42
44 const uint8_t num_bytes = 3;
45 uint8_t buffer[num_bytes];
46
47 // Wait until data available
48 const uint32_t now = millis();
49 while (true) {
50 std::bitset<8> status = this->reg(LTR390_MAIN_STATUS).get();
51 bool available = status[3];
52 if (available)
53 break;
54
55 if (millis() - now > 100) {
56 ESP_LOGW(TAG, "Sensor didn't return any data, aborting");
57 return {};
58 }
59 ESP_LOGD(TAG, "Waiting for data");
60 delay(2);
61 }
62
63 if (!this->read_bytes(MODEADDRESSES[mode], buffer, num_bytes)) {
64 ESP_LOGW(TAG, "Reading data from sensor failed!");
65 return {};
66 }
67
68 return little_endian_bytes_to_int(buffer, num_bytes);
69}
70
73 if (!val.has_value())
74 return;
75 uint32_t als = *val;
76
77 if (this->light_sensor_ != nullptr) {
78 float lux = ((0.6 * als) / (GAINVALUES[this->gain_als_] * RESOLUTIONVALUE[this->res_als_])) * this->wfac_;
79 this->light_sensor_->publish_state(lux);
80 }
81
82 if (this->als_sensor_ != nullptr) {
83 this->als_sensor_->publish_state(als);
84 }
85}
86
89 if (!val.has_value())
90 return;
91 uint32_t uv = *val;
92
93 if (this->uvi_sensor_ != nullptr) {
94 // Set sensitivity by linearly scaling against known value in the datasheet
95 float gain_scale_uv = GAINVALUES[this->gain_uv_] / GAIN_MAX;
96 float intg_scale_uv = (RESOLUTIONVALUE[this->res_uv_] * 100) / INTG_MAX;
97 float sensitivity_uv = SENSITIVITY_MAX * gain_scale_uv * intg_scale_uv;
98
99 this->uvi_sensor_->publish_state((uv / sensitivity_uv) * this->wfac_);
100 }
101
102 if (this->uv_sensor_ != nullptr) {
103 this->uv_sensor_->publish_state(uv);
104 }
105}
106
108 std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
109 ctrl[LTR390_CTRL_EN] = false;
110 this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
111 this->reading_ = false;
112}
113
115 // Set mode
116 std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
118 ctrl[LTR390_CTRL_EN] = true;
119 this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
120
121 uint32_t int_time{0};
122 // Set gain, resolution and measurement rate
123 switch (mode) {
124 case LTR390_MODE_ALS:
125 this->reg(LTR390_GAIN) = this->gain_als_;
126 this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_als_];
127 int_time = ((uint32_t) RESOLUTIONVALUE[this->res_als_]) * 100;
128 break;
129 case LTR390_MODE_UVS:
130 this->reg(LTR390_GAIN) = this->gain_uv_;
131 this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_uv_];
132 int_time = ((uint32_t) RESOLUTIONVALUE[this->res_uv_]) * 100;
133 break;
134 }
135
136 // After the sensor integration time do the following
137 this->set_timeout(int_time + LTR390_WAKEUP_TIME + LTR390_SETTLE_TIME, [this, mode]() {
138 // Read from the sensor and continue to next mode or standby
139 if (mode == LTR390_MODE_ALS) {
140 this->read_als_();
141 if (this->enabled_modes_ & ENABLED_MODE_UVS) {
143 return;
144 }
145 } else {
146 this->read_uvs_();
147 }
148 this->standby_();
149 });
150}
151
153 // reset
154 std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
155 ctrl[LTR390_CTRL_RST] = true;
156 this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
157 delay(10);
158
159 // Enable
160 ctrl = this->reg(LTR390_MAIN_CTRL).get();
161 ctrl[LTR390_CTRL_EN] = true;
162 this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
163
164 // check enabled
165 ctrl = this->reg(LTR390_MAIN_CTRL).get();
166 bool enabled = ctrl[LTR390_CTRL_EN];
167
168 if (!enabled) {
169 ESP_LOGW(TAG, "Sensor didn't respond with enabled state");
170 this->mark_failed();
171 return;
172 }
173
174 // Set sensor read state
175 this->reading_ = false;
176
177 // Determine which modes are enabled based on configured sensors
178 if (this->light_sensor_ != nullptr || this->als_sensor_ != nullptr) {
180 }
181 if (this->uvi_sensor_ != nullptr || this->uv_sensor_ != nullptr) {
183 }
184}
185
187 LOG_I2C_DEVICE(this);
188 ESP_LOGCONFIG(TAG,
189 " ALS Gain: X%.0f\n"
190 " ALS Resolution: %u-bit\n"
191 " UV Gain: X%.0f\n"
192 " UV Resolution: %u-bit",
193 GAINVALUES[this->gain_als_], RESOLUTION_BITS[this->res_als_], GAINVALUES[this->gain_uv_],
194 RESOLUTION_BITS[this->res_uv_]);
195}
196
198 if (this->reading_ || this->enabled_modes_ == 0)
199 return;
200
201 this->reading_ = true;
203}
204
205} // namespace ltr390
206} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:153
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:218
uint8_t get() const
returns the register value
Definition i2c.cpp:94
sensor::Sensor * als_sensor_
Definition ltr390.h:82
static constexpr uint8_t ENABLED_MODE_UVS
Definition ltr390.h:62
void read_mode_(LTR390MODE mode)
Definition ltr390.cpp:114
static constexpr uint8_t ENABLED_MODE_ALS
Definition ltr390.h:61
sensor::Sensor * light_sensor_
Definition ltr390.h:81
LTR390RESOLUTION res_uv_
Definition ltr390.h:78
optional< uint32_t > read_sensor_data_(LTR390MODE mode)
Definition ltr390.cpp:43
sensor::Sensor * uv_sensor_
Definition ltr390.h:85
sensor::Sensor * uvi_sensor_
Definition ltr390.h:84
LTR390RESOLUTION res_als_
Definition ltr390.h:77
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:77
mopeka_std_values val[4]
uint32_t little_endian_bytes_to_int(const uint8_t *buffer, uint8_t num_bytes)
Definition ltr390.cpp:32
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:31
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:30