11static const char *
const TAG =
"ltr_als_ps";
13static const uint8_t MAX_TRIES = 5;
15template<
typename T,
size_t size> T
get_next(
const T (&array)[
size],
const T
val) {
17 size_t idx = std::numeric_limits<size_t>::max();
18 while (idx == std::numeric_limits<size_t>::max() && i <
size) {
19 if (array[i] ==
val) {
25 if (idx == std::numeric_limits<size_t>::max() || i + 1 >=
size)
30template<
typename T,
size_t size> T
get_prev(
const T (&array)[
size],
const T
val) {
32 size_t idx = std::numeric_limits<size_t>::max();
33 while (idx == std::numeric_limits<size_t>::max() && i > 0) {
34 if (array[i] ==
val) {
40 if (idx == std::numeric_limits<size_t>::max() || i == 0)
46 static const uint16_t ALS_INT_TIME[8] = {100, 50, 200, 400, 150, 250, 300, 350};
47 return ALS_INT_TIME[time & 0b111];
51 static const uint16_t ALS_MEAS_RATE[8] = {50, 100, 200, 500, 1000, 2000, 2000, 2000};
52 return ALS_MEAS_RATE[rate & 0b111];
56 static const float ALS_GAIN[8] = {1, 2, 4, 8, 0, 0, 48, 96};
61 static const float PS_GAIN[4] = {16, 0, 32, 64};
62 return PS_GAIN[
gain & 0b11];
71 auto get_device_type = [](
LtrType typ) {
85 ESP_LOGCONFIG(TAG,
" Device type: %s", get_device_type(this->
ltr_type_));
88 " Automatic mode: %s\n"
90 " Integration time: %d ms\n"
91 " Measurement repeat rate: %d ms\n"
92 " Glass attenuation factor: %f",
103 " Proximity gain: %.0fx\n"
104 " Proximity cooldown time: %d s\n"
105 " Proximity high threshold: %d\n"
106 " Proximity low threshold: %d",
111 LOG_UPDATE_INTERVAL(
this);
114 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
119 ESP_LOGV(TAG,
"Updating");
121 ESP_LOGV(TAG,
"Initiating new data collection");
133 ESP_LOGV(TAG,
"Component not ready yet");
140 switch (this->state_) {
142 err = this->
write(
nullptr, 0);
144 ESP_LOGV(TAG,
"i2c connection failed");
168 ESP_LOGV(TAG,
"Reading sensor data having gain = %.0fx, time = %d ms", get_gain_coeff(this->
als_readings_.
gain),
174 ESP_LOGW(TAG,
"Can't get data after several tries.");
189 ESP_LOGD(TAG,
"Reconfiguring sensitivity: gain = %.0fx, time = %d ms", get_gain_coeff(this->
als_readings_.
gain),
231 ESP_LOGV(TAG,
"Proximity high threshold triggered. Value = %d, Trigger level = %d", ps_data,
237 ESP_LOGV(TAG,
"Proximity low threshold triggered. Value = %d, Trigger level = %d", ps_data,
246 if (manuf_id != 0x05) {
247 ESP_LOGW(TAG,
"Unknown manufacturer ID: 0x%02X", manuf_id);
265 if (part_id.part_number_id != 0x0a && part_id.part_number_id != 0x09) {
266 ESP_LOGW(TAG,
"Unknown part number ID: 0x%02X. It might not work properly.", part_id.part_number_id);
274 ESP_LOGV(TAG,
"Resetting");
281 uint8_t tries = MAX_TRIES;
283 ESP_LOGV(TAG,
"Waiting for chip to reset");
286 }
while (als_ctrl.sw_reset && tries--);
288 if (als_ctrl.sw_reset) {
289 ESP_LOGW(TAG,
"Reset timed out");
297 als_ctrl.active_mode =
true;
298 als_ctrl.gain = this->
gain_;
300 ESP_LOGV(TAG,
"Setting active mode and gain reg 0x%02X", als_ctrl.raw);
304 uint8_t tries = MAX_TRIES;
306 ESP_LOGV(TAG,
"Waiting for device to become active");
309 }
while (!als_ctrl.active_mode && tries--);
311 if (!als_ctrl.active_mode) {
312 ESP_LOGW(TAG,
"Failed to activate device");
323 ps_ctrl.ps_mode_xxx =
true;
330 if (!als_status.ps_new_data || als_status.data_invalid) {
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.");
381 if (!als_status.als_new_data)
384 if (als_status.data_invalid) {
385 ESP_LOGW(TAG,
"Data available but not valid");
388 ESP_LOGV(TAG,
"Data ready, reported gain is %.0f", get_gain_coeff(als_status.gain));
389 if (data.gain != als_status.gain) {
390 ESP_LOGW(TAG,
"Actual gain differs from requested (%.0f)", get_gain_coeff(data.gain));
406 ESP_LOGV(TAG,
"Got sensor data: CH1 = %d, CH0 = %d", data.ch1, data.ch0);
415 ESP_LOGW(TAG,
"Too many sensitivity adjustments done. Apparently, sensor reconfiguration fails. Stopping.");
418 data.number_of_adjustments++;
421 static const uint16_t LOW_INTENSITY_THRESHOLD = 1000;
422 static const uint16_t HIGH_INTENSITY_THRESHOLD = 30000;
428 if (data.ch0 <= LOW_INTENSITY_THRESHOLD) {
430 if (next_gain != data.gain) {
431 data.gain = next_gain;
432 ESP_LOGV(TAG,
"Low illuminance. Increasing gain.");
436 if (next_time != data.integration_time) {
437 data.integration_time = next_time;
438 ESP_LOGV(TAG,
"Low illuminance. Increasing integration time.");
441 }
else if (data.ch0 >= HIGH_INTENSITY_THRESHOLD) {
443 if (prev_gain != data.gain) {
444 data.gain = prev_gain;
445 ESP_LOGV(TAG,
"High illuminance. Decreasing gain.");
449 if (prev_time != data.integration_time) {
450 data.integration_time = prev_time;
451 ESP_LOGV(TAG,
"High illuminance. Decreasing integration time.");
455 ESP_LOGD(TAG,
"Illuminance is sufficient.");
458 ESP_LOGD(TAG,
"Can't adjust sensitivity anymore.");
463 if ((data.ch0 == 0xFFFF) || (data.ch1 == 0xFFFF)) {
464 ESP_LOGW(TAG,
"Sensors got saturated");
469 if ((data.ch0 == 0x0000) && (data.ch1 == 0x0000)) {
470 ESP_LOGW(TAG,
"Sensors blacked out");
475 float ch0 = data.ch0;
476 float ch1 = data.ch1;
477 float ratio = ch1 / (ch0 + ch1);
478 float als_gain = get_gain_coeff(data.gain);
479 float als_time = ((float) get_itime_ms(data.integration_time)) / 100.0f;
484 lux = (1.7743 * ch0 + 1.1059 * ch1);
485 }
else if (ratio < 0.64 && ratio >= 0.45) {
486 lux = (4.2785 * ch0 - 1.9548 * ch1);
487 }
else if (ratio < 0.85 && ratio >= 0.64) {
488 lux = (0.5926 * ch0 + 0.1185 * ch1);
490 ESP_LOGW(TAG,
"Impossible ch1/(ch0 + ch1) ratio");
493 lux = inv_pfactor * lux / als_gain / als_time;
496 ESP_LOGV(TAG,
"Lux calculation: ratio %.3f, gain %.0fx, int time %.1f, inv_pfactor %.3f, lux %.3f", ratio, als_gain,
497 als_time, inv_pfactor, lux);
void mark_failed()
Mark this component as failed.
void status_set_warning()
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void status_clear_warning()
ErrorCode write(const uint8_t *data, size_t len) const
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 publish_data_part_2_(AlsReadings &data)
void configure_integration_time_(IntegrationTime time)
IntegrationTime integration_time_
bool check_part_number_()
sensor::Sensor * full_spectrum_counts_sensor_
LtrDataAvail is_als_data_ready_(AlsReadings &data)
uint16_t ps_threshold_low_
CallbackManager< void()> on_ps_low_trigger_callback_
void read_sensor_data_(AlsReadings &data)
CallbackManager< void()> on_ps_high_trigger_callback_
float glass_attenuation_factor_
sensor::Sensor * proximity_counts_sensor_
MeasurementRepeatRate repeat_rate_
bool automatic_mode_enabled_
void dump_config() override
uint32_t last_ps_low_trigger_time_
sensor::Sensor * actual_gain_sensor_
void configure_gain_(AlsGain gain)
uint16_t ps_threshold_high_
uint16_t ps_cooldown_time_s_
void publish_data_part_1_(AlsReadings &data)
bool are_adjustments_required_(AlsReadings &data)
void check_and_trigger_ps_()
struct esphome::ltr_als_ps::LTRAlsPsComponent::AlsReadings als_readings_
sensor::Sensor * infrared_counts_sensor_
sensor::Sensor * ambient_light_sensor_
sensor::Sensor * actual_integration_time_sensor_
uint32_t last_ps_high_trigger_time_
void apply_lux_calculation_(AlsReadings &data)
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.
T get_prev(const T(&array)[size], const T val)
T get_next(const T(&array)[size], const T val)
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
IntegrationTime integration_time
uint8_t number_of_adjustments
MeasurementRepeatRate measurement_repeat_rate
PsMeasurementRate ps_measurement_rate