ESPHome  2024.4.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_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;
16 
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};
19 
20 // Request fastest measurement rate - will be slowed by device if conversion rate is slower.
21 static const float RESOLUTION_SETTING[6] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
22 static const uint32_t MODEADDRESSES[2] = {0x0D, 0x10};
23 
24 static const float SENSITIVITY_MAX = 2300;
25 static const float INTG_MAX = RESOLUTIONVALUE[0] * 100;
26 static const int GAIN_MAX = GAINVALUES[4];
27 
28 uint32_t little_endian_bytes_to_int(const uint8_t *buffer, uint8_t num_bytes) {
29  uint32_t value = 0;
30 
31  for (int i = 0; i < num_bytes; i++) {
32  value <<= 8;
33  value |= buffer[num_bytes - i - 1];
34  }
35 
36  return value;
37 }
38 
40  const uint8_t num_bytes = 3;
41  uint8_t buffer[num_bytes];
42 
43  // Wait until data available
44  const uint32_t now = millis();
45  while (true) {
46  std::bitset<8> status = this->reg(LTR390_MAIN_STATUS).get();
47  bool available = status[3];
48  if (available)
49  break;
50 
51  if (millis() - now > 100) {
52  ESP_LOGW(TAG, "Sensor didn't return any data, aborting");
53  return {};
54  }
55  ESP_LOGD(TAG, "Waiting for data");
56  delay(2);
57  }
58 
59  if (!this->read_bytes(MODEADDRESSES[mode], buffer, num_bytes)) {
60  ESP_LOGW(TAG, "Reading data from sensor failed!");
61  return {};
62  }
63 
64  return little_endian_bytes_to_int(buffer, num_bytes);
65 }
66 
69  if (!val.has_value())
70  return;
71  uint32_t als = *val;
72 
73  if (this->light_sensor_ != nullptr) {
74  float lux = ((0.6 * als) / (GAINVALUES[this->gain_] * RESOLUTIONVALUE[this->res_])) * this->wfac_;
75  this->light_sensor_->publish_state(lux);
76  }
77 
78  if (this->als_sensor_ != nullptr) {
79  this->als_sensor_->publish_state(als);
80  }
81 }
82 
85  if (!val.has_value())
86  return;
87  uint32_t uv = *val;
88 
89  if (this->uvi_sensor_ != nullptr) {
90  this->uvi_sensor_->publish_state((uv / this->sensitivity_) * this->wfac_);
91  }
92 
93  if (this->uv_sensor_ != nullptr) {
94  this->uv_sensor_->publish_state(uv);
95  }
96 }
97 
98 void LTR390Component::read_mode_(int mode_index) {
99  // Set mode
100  LTR390MODE mode = std::get<0>(this->mode_funcs_[mode_index]);
101 
102  std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
103  ctrl[LTR390_CTRL_MODE] = mode;
104  this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
105 
106  // After the sensor integration time do the following
107  this->set_timeout(((uint32_t) RESOLUTIONVALUE[this->res_]) * 100, [this, mode_index]() {
108  // Read from the sensor
109  std::get<1>(this->mode_funcs_[mode_index])();
110 
111  // If there are more modes to read then begin the next
112  // otherwise stop
113  if (mode_index + 1 < (int) this->mode_funcs_.size()) {
114  this->read_mode_(mode_index + 1);
115  } else {
116  this->reading_ = false;
117  }
118  });
119 }
120 
122  ESP_LOGCONFIG(TAG, "Setting up ltr390...");
123 
124  // reset
125  std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
126  ctrl[LTR390_CTRL_RST] = true;
127  this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
128  delay(10);
129 
130  // Enable
131  ctrl = this->reg(LTR390_MAIN_CTRL).get();
132  ctrl[LTR390_CTRL_EN] = true;
133  this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
134 
135  // check enabled
136  ctrl = this->reg(LTR390_MAIN_CTRL).get();
137  bool enabled = ctrl[LTR390_CTRL_EN];
138 
139  if (!enabled) {
140  ESP_LOGW(TAG, "Sensor didn't respond with enabled state");
141  this->mark_failed();
142  return;
143  }
144 
145  // Set gain
146  this->reg(LTR390_GAIN) = gain_;
147 
148  // Set resolution and measurement rate
149  this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_];
150 
151  // Set sensitivity by linearly scaling against known value in the datasheet
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;
155 
156  // Set sensor read state
157  this->reading_ = false;
158 
159  // If we need the light sensor then add to the list
160  if (this->light_sensor_ != nullptr || this->als_sensor_ != nullptr) {
161  this->mode_funcs_.emplace_back(LTR390_MODE_ALS, std::bind(&LTR390Component::read_als_, this));
162  }
163 
164  // If we need the UV sensor then add to the list
165  if (this->uvi_sensor_ != nullptr || this->uv_sensor_ != nullptr) {
166  this->mode_funcs_.emplace_back(LTR390_MODE_UVS, std::bind(&LTR390Component::read_uvs_, this));
167  }
168 }
169 
170 void LTR390Component::dump_config() { LOG_I2C_DEVICE(this); }
171 
173  if (!this->reading_ && !mode_funcs_.empty()) {
174  this->reading_ = true;
175  this->read_mode_(0);
176  }
177 }
178 
179 } // namespace ltr390
180 } // namespace esphome
void read_mode_(int mode_index)
Definition: ltr390.cpp:98
uint32_t little_endian_bytes_to_int(const uint8_t *buffer, uint8_t num_bytes)
Definition: ltr390.cpp:28
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:39
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
sensor::Sensor * uv_sensor_
Definition: ltr390.h:83
sensor::Sensor * light_sensor_
Definition: ltr390.h:79
uint8_t status
Definition: bl0942.h:23
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
LTR390RESOLUTION res_
Definition: ltr390.h:75
std::vector< std::tuple< LTR390MODE, std::function< void()> > > mode_funcs_
Definition: ltr390.h:72
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
sensor::Sensor * uvi_sensor_
Definition: ltr390.h:82
sensor::Sensor * als_sensor_
Definition: ltr390.h:80
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26