9 static const char *
const TAG =
"ltr390";
11 static const uint8_t LTR390_WAKEUP_TIME = 10;
12 static const uint8_t LTR390_SETTLE_TIME = 5;
14 static const uint8_t LTR390_MAIN_CTRL = 0x00;
15 static const uint8_t LTR390_MEAS_RATE = 0x04;
16 static const uint8_t LTR390_GAIN = 0x05;
17 static const uint8_t LTR390_PART_ID = 0x06;
18 static const uint8_t LTR390_MAIN_STATUS = 0x07;
20 static const float GAINVALUES[5] = {1.0, 3.0, 6.0, 9.0, 18.0};
21 static const float RESOLUTIONVALUE[6] = {4.0, 2.0, 1.0, 0.5, 0.25, 0.125};
22 static const uint8_t RESOLUTION_BITS[6] = {20, 19, 18, 17, 16, 13};
25 static const float RESOLUTION_SETTING[6] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
26 static const uint32_t MODEADDRESSES[2] = {0x0D, 0x10};
28 static const float SENSITIVITY_MAX = 2300;
29 static const float INTG_MAX = RESOLUTIONVALUE[0] * 100;
30 static const int GAIN_MAX = GAINVALUES[4];
35 for (
int i = 0; i < num_bytes; i++) {
37 value |= buffer[num_bytes - i - 1];
44 const uint8_t num_bytes = 3;
45 uint8_t buffer[num_bytes];
48 const uint32_t now =
millis();
50 std::bitset<8>
status = this->
reg(LTR390_MAIN_STATUS).
get();
51 bool available = status[3];
55 if (
millis() - now > 100) {
56 ESP_LOGW(TAG,
"Sensor didn't return any data, aborting");
59 ESP_LOGD(TAG,
"Waiting for data");
63 if (!this->
read_bytes(MODEADDRESSES[mode], buffer, num_bytes)) {
64 ESP_LOGW(TAG,
"Reading data from sensor failed!");
106 std::bitset<8> ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
109 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
111 uint32_t int_time{0};
116 this->
reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->
res_als_];
117 int_time = ((uint32_t) RESOLUTIONVALUE[this->
res_als_]) * 100;
121 this->
reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->
res_uv_];
122 int_time = ((uint32_t) RESOLUTIONVALUE[this->
res_uv_]) * 100;
127 this->
set_timeout(int_time + LTR390_WAKEUP_TIME + LTR390_SETTLE_TIME, [
this, mode_index]() {
133 if (mode_index + 1 < (
int) this->
mode_funcs_.size()) {
137 std::bitset<8> ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
139 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
146 ESP_LOGCONFIG(TAG,
"Setting up ltr390...");
149 std::bitset<8> ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
151 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
155 ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
157 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
160 ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
164 ESP_LOGW(TAG,
"Sensor didn't respond with enabled state");
170 float gain_scale_uv = GAINVALUES[this->
gain_uv_] / GAIN_MAX;
171 float intg_scale_uv = (RESOLUTIONVALUE[this->
res_uv_] * 100) / INTG_MAX;
172 this->
sensitivity_uv_ = SENSITIVITY_MAX * gain_scale_uv * intg_scale_uv;
189 LOG_I2C_DEVICE(
this);
190 ESP_LOGCONFIG(TAG,
" ALS Gain: X%.0f", GAINVALUES[this->
gain_als_]);
191 ESP_LOGCONFIG(TAG,
" ALS Resolution: %u-bit", RESOLUTION_BITS[this->
res_als_]);
192 ESP_LOGCONFIG(TAG,
" UV Gain: X%.0f", GAINVALUES[this->
gain_uv_]);
193 ESP_LOGCONFIG(TAG,
" UV Resolution: %u-bit", RESOLUTION_BITS[this->
res_uv_]);
void read_mode_(int mode_index)
uint32_t little_endian_bytes_to_int(const uint8_t *buffer, uint8_t num_bytes)
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
uint8_t get() const
returns the register value
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
uint32_t IRAM_ATTR HOT millis()
optional< uint32_t > read_sensor_data_(LTR390MODE mode)
BedjetMode mode
BedJet operating mode.
void publish_state(float state)
Publish a new state to the front-end.
sensor::Sensor * uv_sensor_
sensor::Sensor * light_sensor_
virtual void mark_failed()
Mark this component as failed.
std::vector< std::tuple< LTR390MODE, std::function< void()> > > mode_funcs_
void dump_config() override
Implementation of SPI Controller mode.
sensor::Sensor * uvi_sensor_
sensor::Sensor * als_sensor_
LTR390RESOLUTION res_als_
void IRAM_ATTR HOT delay(uint32_t ms)