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