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);
115 ESP_LOGV(TAG,
"Updating");
117 ESP_LOGV(TAG,
"Initiating new data collection");
129 ESP_LOGV(TAG,
"Component not ready yet");
136 switch (this->state_) {
138 err = this->
write(
nullptr, 0);
140 ESP_LOGV(TAG,
"i2c connection failed");
164 ESP_LOGV(TAG,
"Reading sensor data having gain = %.0fx, time = %d ms", get_gain_coeff(this->
als_readings_.
gain),
170 ESP_LOGW(TAG,
"Can't get data after several tries.");
185 ESP_LOGD(TAG,
"Reconfiguring sensitivity: gain = %.0fx, time = %d ms", get_gain_coeff(this->
als_readings_.
gain),
227 ESP_LOGV(TAG,
"Proximity high threshold triggered. Value = %d, Trigger level = %d", ps_data,
233 ESP_LOGV(TAG,
"Proximity low threshold triggered. Value = %d, Trigger level = %d", ps_data,
242 if (manuf_id != 0x05) {
243 ESP_LOGW(TAG,
"Unknown manufacturer ID: 0x%02X", manuf_id);
261 if (part_id.part_number_id != 0x0a && part_id.part_number_id != 0x09) {
262 ESP_LOGW(TAG,
"Unknown part number ID: 0x%02X. It might not work properly.", part_id.part_number_id);
270 ESP_LOGV(TAG,
"Resetting");
277 uint8_t tries = MAX_TRIES;
279 ESP_LOGV(TAG,
"Waiting for chip to reset");
282 }
while (als_ctrl.sw_reset && tries--);
284 if (als_ctrl.sw_reset) {
285 ESP_LOGW(TAG,
"Reset timed out");
293 als_ctrl.active_mode =
true;
294 als_ctrl.gain = this->
gain_;
296 ESP_LOGV(TAG,
"Setting active mode and gain reg 0x%02X", als_ctrl.raw);
300 uint8_t tries = MAX_TRIES;
302 ESP_LOGV(TAG,
"Waiting for device to become active");
305 }
while (!als_ctrl.active_mode && tries--);
307 if (!als_ctrl.active_mode) {
308 ESP_LOGW(TAG,
"Failed to activate device");
319 ps_ctrl.ps_mode_xxx =
true;
326 if (!als_status.ps_new_data || als_status.data_invalid) {
344 als_ctrl.gain =
gain;
350 if (read_als_ctrl.gain !=
gain) {
351 ESP_LOGW(TAG,
"Failed to set gain. We will try one more time.");
360 meas.integration_time = time;
366 if (read_meas.integration_time != time) {
367 ESP_LOGW(TAG,
"Failed to set integration time. We will try one more time.");
377 if (!als_status.als_new_data)
380 if (als_status.data_invalid) {
381 ESP_LOGW(TAG,
"Data available but not valid");
384 ESP_LOGV(TAG,
"Data ready, reported gain is %.0f", get_gain_coeff(als_status.gain));
385 if (data.gain != als_status.gain) {
386 ESP_LOGW(TAG,
"Actual gain differs from requested (%.0f)", get_gain_coeff(data.gain));
402 ESP_LOGV(TAG,
"Got sensor data: CH1 = %d, CH0 = %d", data.ch1, data.ch0);
411 ESP_LOGW(TAG,
"Too many sensitivity adjustments done. Apparently, sensor reconfiguration fails. Stopping.");
414 data.number_of_adjustments++;
417 static const uint16_t LOW_INTENSITY_THRESHOLD = 1000;
418 static const uint16_t HIGH_INTENSITY_THRESHOLD = 30000;
424 if (data.ch0 <= LOW_INTENSITY_THRESHOLD) {
426 if (next_gain != data.gain) {
427 data.gain = next_gain;
428 ESP_LOGV(TAG,
"Low illuminance. Increasing gain.");
432 if (next_time != data.integration_time) {
433 data.integration_time = next_time;
434 ESP_LOGV(TAG,
"Low illuminance. Increasing integration time.");
437 }
else if (data.ch0 >= HIGH_INTENSITY_THRESHOLD) {
439 if (prev_gain != data.gain) {
440 data.gain = prev_gain;
441 ESP_LOGV(TAG,
"High illuminance. Decreasing gain.");
445 if (prev_time != data.integration_time) {
446 data.integration_time = prev_time;
447 ESP_LOGV(TAG,
"High illuminance. Decreasing integration time.");
451 ESP_LOGD(TAG,
"Illuminance is sufficient.");
454 ESP_LOGD(TAG,
"Can't adjust sensitivity anymore.");
459 if ((data.ch0 == 0xFFFF) || (data.ch1 == 0xFFFF)) {
460 ESP_LOGW(TAG,
"Sensors got saturated");
465 if ((data.ch0 == 0x0000) && (data.ch1 == 0x0000)) {
466 ESP_LOGW(TAG,
"Sensors blacked out");
471 float ch0 = data.ch0;
472 float ch1 = data.ch1;
473 float ratio = ch1 / (ch0 + ch1);
474 float als_gain = get_gain_coeff(data.gain);
475 float als_time = ((float) get_itime_ms(data.integration_time)) / 100.0f;
480 lux = (1.7743f * ch0 + 1.1059f * ch1);
481 }
else if (ratio < 0.64f && ratio >= 0.45f) {
482 lux = (4.2785f * ch0 - 1.9548f * ch1);
483 }
else if (ratio < 0.85f && ratio >= 0.64f) {
484 lux = (0.5926f * ch0 + 0.1185f * ch1);
486 ESP_LOGW(TAG,
"Impossible ch1/(ch0 + ch1) ratio");
489 lux = inv_pfactor * lux / als_gain / als_time;
492 ESP_LOGV(TAG,
"Lux calculation: ratio %.3f, gain %.0fx, int time %.1f, inv_pfactor %.3f, lux %.3f", ratio, als_gain,
493 als_time, inv_pfactor, lux);
void mark_failed()
Mark this component as failed.
void status_set_warning()
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* 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)
ESPHOME_LOG_TAG(TAG, "ltr_als_ps")
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