ESPHome  2025.2.2
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 
6 namespace esphome {
7 namespace ltr390 {
8 
9 static const char *const TAG = "ltr390";
10 
11 static const uint8_t LTR390_WAKEUP_TIME = 10;
12 static const uint8_t LTR390_SETTLE_TIME = 5;
13 
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;
19 
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};
23 
24 // Request fastest measurement rate - will be slowed by device if conversion rate is slower.
25 static const float RESOLUTION_SETTING[6] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
26 static const uint32_t MODEADDRESSES[2] = {0x0D, 0x10};
27 
28 static const float SENSITIVITY_MAX = 2300;
29 static const float INTG_MAX = RESOLUTIONVALUE[0] * 100;
30 static const int GAIN_MAX = GAINVALUES[4];
31 
32 uint32_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 
107 void LTR390Component::read_mode_(int mode_index) {
108  // Set mode
109  LTR390MODE mode = std::get<0>(this->mode_funcs_[mode_index]);
110 
111  std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
112  ctrl[LTR390_CTRL_MODE] = mode;
113  ctrl[LTR390_CTRL_EN] = true;
114  this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
115 
116  uint32_t int_time{0};
117  // Set gain, resolution and measurement rate
118  switch (mode) {
119  case LTR390_MODE_ALS:
120  this->reg(LTR390_GAIN) = this->gain_als_;
121  this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_als_];
122  int_time = ((uint32_t) RESOLUTIONVALUE[this->res_als_]) * 100;
123  break;
124  case LTR390_MODE_UVS:
125  this->reg(LTR390_GAIN) = this->gain_uv_;
126  this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_uv_];
127  int_time = ((uint32_t) RESOLUTIONVALUE[this->res_uv_]) * 100;
128  break;
129  }
130 
131  // After the sensor integration time do the following
132  this->set_timeout(int_time + LTR390_WAKEUP_TIME + LTR390_SETTLE_TIME, [this, mode_index]() {
133  // Read from the sensor
134  std::get<1>(this->mode_funcs_[mode_index])();
135 
136  // If there are more modes to read then begin the next
137  // otherwise stop
138  if (mode_index + 1 < (int) this->mode_funcs_.size()) {
139  this->read_mode_(mode_index + 1);
140  } else {
141  // put sensor in standby
142  std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
143  ctrl[LTR390_CTRL_EN] = false;
144  this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
145  this->reading_ = false;
146  }
147  });
148 }
149 
151  ESP_LOGCONFIG(TAG, "Setting up ltr390...");
152 
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  // If we need the light sensor then add to the list
178  if (this->light_sensor_ != nullptr || this->als_sensor_ != nullptr) {
179  this->mode_funcs_.emplace_back(LTR390_MODE_ALS, std::bind(&LTR390Component::read_als_, this));
180  }
181 
182  // If we need the UV sensor then add to the list
183  if (this->uvi_sensor_ != nullptr || this->uv_sensor_ != nullptr) {
184  this->mode_funcs_.emplace_back(LTR390_MODE_UVS, std::bind(&LTR390Component::read_uvs_, this));
185  }
186 }
187 
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_]);
194 }
195 
197  if (!this->reading_ && !mode_funcs_.empty()) {
198  this->reading_ = true;
199  this->read_mode_(0);
200  }
201 }
202 
203 } // namespace ltr390
204 } // namespace esphome
void read_mode_(int mode_index)
Definition: ltr390.cpp:107
uint32_t little_endian_bytes_to_int(const uint8_t *buffer, uint8_t num_bytes)
Definition: ltr390.cpp:32
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition: i2c.h:149
uint8_t get() const
returns the register value
Definition: i2c.cpp:75
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
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:212
mopeka_std_values val[4]
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
optional< uint32_t > read_sensor_data_(LTR390MODE mode)
Definition: ltr390.cpp:43
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
sensor::Sensor * uv_sensor_
Definition: ltr390.h:86
LTR390RESOLUTION res_uv_
Definition: ltr390.h:79
sensor::Sensor * light_sensor_
Definition: ltr390.h:82
uint8_t status
Definition: bl0942.h:74
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
std::vector< std::tuple< LTR390MODE, std::function< void()> > > mode_funcs_
Definition: ltr390.h:74
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
sensor::Sensor * uvi_sensor_
Definition: ltr390.h:85
sensor::Sensor * als_sensor_
Definition: ltr390.h:83
LTR390RESOLUTION res_als_
Definition: ltr390.h:78
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26