ESPHome  2024.11.0
ltr390.cpp
Go to the documentation of this file.
1 #include "ltr390.h"
2 #include "esphome/core/hal.h"
3 #include "esphome/core/log.h"
4 #include <bitset>
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  this->uvi_sensor_->publish_state((uv / this->sensitivity_uv_) * this->wfac_);
95  }
96 
97  if (this->uv_sensor_ != nullptr) {
98  this->uv_sensor_->publish_state(uv);
99  }
100 }
101 
102 void LTR390Component::read_mode_(int mode_index) {
103  // Set mode
104  LTR390MODE mode = std::get<0>(this->mode_funcs_[mode_index]);
105 
106  std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
107  ctrl[LTR390_CTRL_MODE] = mode;
108  ctrl[LTR390_CTRL_EN] = true;
109  this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
110 
111  uint32_t int_time{0};
112  // Set gain, resolution and measurement rate
113  switch (mode) {
114  case LTR390_MODE_ALS:
115  this->reg(LTR390_GAIN) = this->gain_als_;
116  this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_als_];
117  int_time = ((uint32_t) RESOLUTIONVALUE[this->res_als_]) * 100;
118  break;
119  case LTR390_MODE_UVS:
120  this->reg(LTR390_GAIN) = this->gain_uv_;
121  this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_uv_];
122  int_time = ((uint32_t) RESOLUTIONVALUE[this->res_uv_]) * 100;
123  break;
124  }
125 
126  // After the sensor integration time do the following
127  this->set_timeout(int_time + LTR390_WAKEUP_TIME + LTR390_SETTLE_TIME, [this, mode_index]() {
128  // Read from the sensor
129  std::get<1>(this->mode_funcs_[mode_index])();
130 
131  // If there are more modes to read then begin the next
132  // otherwise stop
133  if (mode_index + 1 < (int) this->mode_funcs_.size()) {
134  this->read_mode_(mode_index + 1);
135  } else {
136  // put sensor in standby
137  std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
138  ctrl[LTR390_CTRL_EN] = false;
139  this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
140  this->reading_ = false;
141  }
142  });
143 }
144 
146  ESP_LOGCONFIG(TAG, "Setting up ltr390...");
147 
148  // reset
149  std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
150  ctrl[LTR390_CTRL_RST] = true;
151  this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
152  delay(10);
153 
154  // Enable
155  ctrl = this->reg(LTR390_MAIN_CTRL).get();
156  ctrl[LTR390_CTRL_EN] = true;
157  this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
158 
159  // check enabled
160  ctrl = this->reg(LTR390_MAIN_CTRL).get();
161  bool enabled = ctrl[LTR390_CTRL_EN];
162 
163  if (!enabled) {
164  ESP_LOGW(TAG, "Sensor didn't respond with enabled state");
165  this->mark_failed();
166  return;
167  }
168 
169  // Set sensitivity by linearly scaling against known value in the datasheet
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;
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:102
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:87
LTR390RESOLUTION res_uv_
Definition: ltr390.h:79
sensor::Sensor * light_sensor_
Definition: ltr390.h:83
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:86
sensor::Sensor * als_sensor_
Definition: ltr390.h:84
LTR390RESOLUTION res_als_
Definition: ltr390.h:78
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26