11static const char *
const TAG =
"ltr501";
13static const uint8_t MAX_TRIES = 5;
14static const uint8_t MAX_SENSITIVITY_ADJUSTMENTS = 10;
21bool operator==(
const GainTimePair &lhs,
const GainTimePair &rhs) {
22 return lhs.gain == rhs.gain && lhs.time == rhs.time;
25bool operator!=(
const GainTimePair &lhs,
const GainTimePair &rhs) {
26 return lhs.gain != rhs.gain || lhs.time != rhs.time;
29template<
typename T,
size_t size> T
get_next(
const T (&array)[size],
const T
val) {
32 while (idx == -1 && i < size) {
33 if (array[i] ==
val) {
39 if (idx == -1 || i + 1 >= size)
44template<
typename T,
size_t size> T
get_prev(
const T (&array)[size],
const T
val) {
47 while (idx == -1 && i > 0) {
48 if (array[i] ==
val) {
54 if (idx == -1 || i == 0)
60 static const uint16_t ALS_INT_TIME[4] = {100, 50, 200, 400};
61 return ALS_INT_TIME[time & 0b11];
65 static const uint16_t ALS_MEAS_RATE[8] = {50, 100, 200, 500, 1000, 2000, 2000, 2000};
66 return ALS_MEAS_RATE[rate & 0b111];
72 static const float PS_GAIN[4] = {1, 4, 8, 16};
73 return PS_GAIN[
gain & 0b11];
77 ESP_LOGCONFIG(TAG,
"Setting up LTR-501/301/558");
83 auto get_device_type = [](
LtrType typ) {
97 ESP_LOGCONFIG(TAG,
" Device type: %s", get_device_type(this->
ltr_type_));
99 ESP_LOGCONFIG(TAG,
" Gain: %.0fx", get_gain_coeff(this->
gain_));
100 ESP_LOGCONFIG(TAG,
" Integration time: %d ms", get_itime_ms(this->
integration_time_));
101 ESP_LOGCONFIG(TAG,
" Measurement repeat rate: %d ms", get_meas_time_ms(this->
repeat_rate_));
103 ESP_LOGCONFIG(TAG,
" Proximity gain: %.0fx", get_ps_gain_coeff(this->
ps_gain_));
108 LOG_UPDATE_INTERVAL(
this);
116 ESP_LOGE(TAG,
"Communication with I2C LTR-501/301/558 failed!");
122 ESP_LOGW(TAG,
"Update. ALS data not available. Change configuration to ALS or ALS_PS.");
126 ESP_LOGV(TAG,
"Update. Initiating new ALS data collection.");
138 ESP_LOGV(TAG,
"Update. Component not ready yet.");
144 static uint8_t tries{0};
146 switch (this->state_) {
148 err = this->
write(
nullptr, 0);
150 ESP_LOGW(TAG,
"i2c connection failed");
174 ESP_LOGV(TAG,
"Reading sensor data assuming gain = %.0fx, time = %d ms",
179 }
else if (tries >= MAX_TRIES) {
180 ESP_LOGW(TAG,
"Can't get data after several tries. Aborting.");
195 ESP_LOGD(TAG,
"Reconfiguring sensitivity: gain = %.0fx, time = %d ms", get_gain_coeff(this->
als_readings_.
gain),
228 static uint32_t last_high_trigger_time{0};
229 static uint32_t last_low_trigger_time{0};
237 last_high_trigger_time = now;
238 ESP_LOGD(TAG,
"Proximity high threshold triggered. Value = %d, Trigger level = %d", ps_data,
242 last_low_trigger_time = now;
243 ESP_LOGD(TAG,
"Proximity low threshold triggered. Value = %d, Trigger level = %d", ps_data,
252 if (manuf_id != 0x05) {
253 ESP_LOGW(TAG,
"Unknown manufacturer ID: 0x%02X", manuf_id);
268 if (part_id.part_number_id != 0x08) {
269 ESP_LOGW(TAG,
"Unknown part number ID: 0x%02X. LTR-501/301 shall have 0x08. It might not work properly.",
270 part_id.part_number_id);
278 ESP_LOGV(TAG,
"Resetting");
285 uint8_t tries = MAX_TRIES;
287 ESP_LOGV(TAG,
"Waiting chip to reset");
290 }
while (als_ctrl.sw_reset && tries--);
292 if (als_ctrl.sw_reset) {
293 ESP_LOGW(TAG,
"Reset failed");
300 als_ctrl.als_mode_active =
true;
301 als_ctrl.gain = this->
gain_;
303 ESP_LOGV(TAG,
"Setting active mode and gain reg 0x%02X", als_ctrl.raw);
307 uint8_t tries = MAX_TRIES;
309 ESP_LOGV(TAG,
"Waiting for ALS device to become active...");
312 }
while (!als_ctrl.als_mode_active && tries--);
314 if (!als_ctrl.als_mode_active) {
315 ESP_LOGW(TAG,
"Failed to activate ALS device");
326 ps_ctrl.ps_mode_xxx =
true;
333 if (!als_status.ps_new_data) {
348 als_ctrl.gain =
gain;
354 if (read_als_ctrl.gain !=
gain) {
355 ESP_LOGW(TAG,
"Failed to set gain. We will try one more time.");
364 meas.integration_time = time;
370 if (read_meas.integration_time != time) {
371 ESP_LOGW(TAG,
"Failed to set integration time. We will try one more time.");
380 if (!als_status.als_new_data)
382 ESP_LOGV(TAG,
"Data ready, reported gain is %.0fx", get_gain_coeff(als_status.gain));
383 if (data.
gain != als_status.gain) {
384 ESP_LOGW(TAG,
"Actual gain differs from requested (%.0f)", get_gain_coeff(data.
gain));
387 data.
gain = als_status.gain;
401 ESP_LOGD(TAG,
"Got sensor data: CH1 = %d, CH0 = %d", data.
ch1, data.
ch0);
410 ESP_LOGW(TAG,
"Too many sensitivity adjustments done. Something wrong with the sensor. Stopping.");
417 static const GainTimePair GAIN_TIME_PAIRS[] = {
432 if (data.
ch1 == 1 && data.
ch0 == 0) {
433 ESP_LOGV(TAG,
"Looks like sensor got saturated (?) CH1 = 1, CH0 = 0, Gain 150x");
437 }
else if (data.
ch1 == 65535 && data.
ch0 == 0) {
438 ESP_LOGV(TAG,
"Looks like sensor got saturated (?) CH1 = 65535, CH0 = 0, Gain 150x");
440 }
else if (data.
ch1 > 1000 && data.
ch0 == 0) {
441 ESP_LOGV(TAG,
"Looks like sensor got saturated (?) CH1 = %d, CH0 = 0, Gain 150x", data.
ch1);
446 static const uint16_t LOW_INTENSITY_THRESHOLD_1 = 100;
447 static const uint16_t LOW_INTENSITY_THRESHOLD_200 = 2000;
448 static const uint16_t HIGH_INTENSITY_THRESHOLD = 25000;
452 GainTimePair next_pair =
get_next(GAIN_TIME_PAIRS, current_pair);
453 if (next_pair != current_pair) {
454 data.
gain = next_pair.gain;
456 ESP_LOGV(TAG,
"Low illuminance. Increasing sensitivity.");
460 }
else if (data.
ch0 >= HIGH_INTENSITY_THRESHOLD || data.
ch1 >= HIGH_INTENSITY_THRESHOLD) {
461 GainTimePair prev_pair =
get_prev(GAIN_TIME_PAIRS, current_pair);
462 if (prev_pair != current_pair) {
463 data.
gain = prev_pair.gain;
465 ESP_LOGV(TAG,
"High illuminance. Decreasing sensitivity.");
469 ESP_LOGD(TAG,
"Illuminance is good enough.");
472 ESP_LOGD(TAG,
"Can't adjust sensitivity anymore.");
477 if ((data.
ch0 == 0xFFFF) || (data.
ch1 == 0xFFFF)) {
478 ESP_LOGW(TAG,
"Sensors got saturated");
483 if ((data.
ch0 == 0x0000) && (data.
ch1 == 0x0000)) {
484 ESP_LOGW(TAG,
"Sensors blacked out");
489 float ch0 = data.
ch0;
490 float ch1 = data.
ch1;
491 float ratio = ch1 / (ch0 + ch1);
492 float als_gain = get_gain_coeff(data.
gain);
501 lux = 1.7743 * ch0 + 1.1059 * ch1;
502 }
else if (ratio < 0.64) {
503 lux = 3.7725 * ch0 - 1.3363 * ch1;
504 }
else if (ratio < 0.85) {
505 lux = 1.6903 * ch0 - 0.1693 * ch1;
507 ESP_LOGW(TAG,
"Impossible ch1/(ch0 + ch1) ratio");
511 lux = inv_pfactor * lux / als_gain / als_time;
514 ESP_LOGD(TAG,
"Lux calculation: ratio %.3f, gain %.0fx, int time %.1f, inv_pfactor %.3f, lux %.3f", ratio, als_gain,
515 als_time, inv_pfactor, lux);
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
uint8_t get() const
returns the register value
void configure_gain_(AlsGain501 gain)
void publish_data_part_1_(AlsReadings &data)
void publish_data_part_2_(AlsReadings &data)
void check_and_trigger_ps_()
void configure_integration_time_(IntegrationTime501 time)
sensor::Sensor * actual_integration_time_sensor_
CallbackManager< void()> on_ps_high_trigger_callback_
float glass_attenuation_factor_
IntegrationTime501 integration_time_
struct esphome::ltr501::LTRAlsPs501Component::AlsReadings als_readings_
void read_sensor_data_(AlsReadings &data)
MeasurementRepeatRate repeat_rate_
bool automatic_mode_enabled_
CallbackManager< void()> on_ps_low_trigger_callback_
void dump_config() override
void apply_lux_calculation_(AlsReadings &data)
uint16_t ps_threshold_high_
sensor::Sensor * proximity_counts_sensor_
sensor::Sensor * infrared_counts_sensor_
bool are_adjustments_required_(AlsReadings &data)
uint16_t ps_threshold_low_
DataAvail is_als_data_ready_(AlsReadings &data)
sensor::Sensor * full_spectrum_counts_sensor_
sensor::Sensor * actual_gain_sensor_
sensor::Sensor * ambient_light_sensor_
uint16_t ps_cooldown_time_s_
bool check_part_number_()
void publish_state(float state)
Publish a new state to the front-end.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
bool operator!=(const GainTimePair &lhs, const GainTimePair &rhs)
T get_next(const T(&array)[size], const T val)
bool operator==(const GainTimePair &lhs, const GainTimePair &rhs)
T get_prev(const T(&array)[size], const T val)
Providing packet encoding functions for exchanging data with a remote host.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
uint8_t number_of_adjustments
IntegrationTime501 integration_time
MeasurementRepeatRate measurement_repeat_rate
PsMeasurementRate ps_measurement_rate