9 static const char *
const TAG =
"ltr390";
11 static const uint8_t LTR390_MAIN_CTRL = 0x00;
12 static const uint8_t LTR390_MEAS_RATE = 0x04;
13 static const uint8_t LTR390_GAIN = 0x05;
14 static const uint8_t LTR390_PART_ID = 0x06;
15 static const uint8_t LTR390_MAIN_STATUS = 0x07;
17 static const float GAINVALUES[5] = {1.0, 3.0, 6.0, 9.0, 18.0};
18 static const float RESOLUTIONVALUE[6] = {4.0, 2.0, 1.0, 0.5, 0.25, 0.125};
21 static const float RESOLUTION_SETTING[6] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
22 static const uint32_t MODEADDRESSES[2] = {0x0D, 0x10};
24 static const float SENSITIVITY_MAX = 2300;
25 static const float INTG_MAX = RESOLUTIONVALUE[0] * 100;
26 static const int GAIN_MAX = GAINVALUES[4];
31 for (
int i = 0; i < num_bytes; i++) {
33 value |= buffer[num_bytes - i - 1];
40 const uint8_t num_bytes = 3;
41 uint8_t buffer[num_bytes];
44 const uint32_t now =
millis();
46 std::bitset<8>
status = this->
reg(LTR390_MAIN_STATUS).
get();
47 bool available = status[3];
51 if (
millis() - now > 100) {
52 ESP_LOGW(TAG,
"Sensor didn't return any data, aborting");
55 ESP_LOGD(TAG,
"Waiting for data");
59 if (!this->
read_bytes(MODEADDRESSES[mode], buffer, num_bytes)) {
60 ESP_LOGW(TAG,
"Reading data from sensor failed!");
74 float lux = ((0.6 * als) / (GAINVALUES[this->
gain_] * RESOLUTIONVALUE[this->
res_])) * this->
wfac_;
102 std::bitset<8> ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
104 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
107 this->
set_timeout(((uint32_t) RESOLUTIONVALUE[this->
res_]) * 100, [
this, mode_index]() {
113 if (mode_index + 1 < (
int) this->
mode_funcs_.size()) {
122 ESP_LOGCONFIG(TAG,
"Setting up ltr390...");
125 std::bitset<8> ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
127 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
131 ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
133 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
136 ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
140 ESP_LOGW(TAG,
"Sensor didn't respond with enabled state");
149 this->
reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->
res_];
152 float gain_scale = GAINVALUES[this->
gain_] / GAIN_MAX;
153 float intg_scale = (RESOLUTIONVALUE[this->
res_] * 100) / INTG_MAX;
154 this->
sensitivity_ = SENSITIVITY_MAX * gain_scale * intg_scale;
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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
sensor::Sensor * uvi_sensor_
sensor::Sensor * als_sensor_
void IRAM_ATTR HOT delay(uint32_t ms)