9 static const char *
const TAG =
"ltr390";
11 static const float GAINVALUES[5] = {1.0, 3.0, 6.0, 9.0, 18.0};
12 static const float RESOLUTIONVALUE[6] = {4.0, 2.0, 1.0, 0.5, 0.25, 0.125};
13 static const uint32_t MODEADDRESSES[2] = {0x0D, 0x10};
18 for (
int i = 0; i < num_bytes; i++) {
20 value |= buffer[num_bytes - i - 1];
27 const uint8_t num_bytes = 3;
28 uint8_t buffer[num_bytes];
31 const uint32_t now =
millis();
33 std::bitset<8>
status = this->
reg(LTR390_MAIN_STATUS).
get();
34 bool available = status[3];
38 if (
millis() - now > 100) {
39 ESP_LOGW(TAG,
"Sensor didn't return any data, aborting");
42 ESP_LOGD(TAG,
"Waiting for data");
46 if (!this->
read_bytes(MODEADDRESSES[mode], buffer, num_bytes)) {
47 ESP_LOGW(TAG,
"Reading data from sensor failed!");
61 float lux = (0.6 * als) / (GAINVALUES[this->
gain_] * RESOLUTIONVALUE[this->
res_]) * this->
wfac_;
89 std::bitset<8> ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
91 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
94 this->
set_timeout(((uint32_t) RESOLUTIONVALUE[this->
res_]) * 100, [
this, mode_index]() {
100 if (mode_index + 1 < (
int) this->
mode_funcs_.size()) {
109 ESP_LOGCONFIG(TAG,
"Setting up ltr390...");
112 std::bitset<8> ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
114 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
118 ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
120 this->
reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
123 ctrl = this->
reg(LTR390_MAIN_CTRL).
get();
127 ESP_LOGW(TAG,
"Sensor didn't respond with enabled state");
136 uint8_t res = this->
reg(LTR390_MEAS_RATE).
get();
140 this->
reg(LTR390_MEAS_RATE) = res;
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)
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)
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
sensor::Sensor * uvi_sensor_
sensor::Sensor * als_sensor_
void IRAM_ATTR HOT delay(uint32_t ms)