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) {
18 while (idx == -1 && i < size) {
19 if (array[i] ==
val) {
25 if (idx == -1 || i + 1 >= size)
30template<
typename T,
size_t size> T
get_prev(
const T (&array)[size],
const T
val) {
33 while (idx == -1 && i > 0) {
34 if (array[i] ==
val) {
40 if (idx == -1 || 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");
139 static uint8_t tries{0};
141 switch (this->state_) {
143 err = this->
write(
nullptr, 0);
145 ESP_LOGV(TAG,
"i2c connection failed");
169 ESP_LOGV(TAG,
"Reading sensor data having gain = %.0fx, time = %d ms", get_gain_coeff(this->
als_readings_.
gain),
174 }
else if (tries >= MAX_TRIES) {
175 ESP_LOGW(TAG,
"Can't get data after several tries.");
190 ESP_LOGD(TAG,
"Reconfiguring sensitivity: gain = %.0fx, time = %d ms", get_gain_coeff(this->
als_readings_.
gain),
223 static uint32_t last_high_trigger_time{0};
224 static uint32_t last_low_trigger_time{0};
232 last_high_trigger_time = now;
233 ESP_LOGV(TAG,
"Proximity high threshold triggered. Value = %d, Trigger level = %d", ps_data,
237 last_low_trigger_time = now;
238 ESP_LOGV(TAG,
"Proximity low threshold triggered. Value = %d, Trigger level = %d", ps_data,
247 if (manuf_id != 0x05) {
248 ESP_LOGW(TAG,
"Unknown manufacturer ID: 0x%02X", manuf_id);
266 if (part_id.part_number_id != 0x0a && part_id.part_number_id != 0x09) {
267 ESP_LOGW(TAG,
"Unknown part number ID: 0x%02X. It might not work properly.", part_id.part_number_id);
275 ESP_LOGV(TAG,
"Resetting");
282 uint8_t tries = MAX_TRIES;
284 ESP_LOGV(TAG,
"Waiting for chip to reset");
287 }
while (als_ctrl.sw_reset && tries--);
289 if (als_ctrl.sw_reset) {
290 ESP_LOGW(TAG,
"Reset timed out");
298 als_ctrl.active_mode =
true;
299 als_ctrl.gain = this->
gain_;
301 ESP_LOGV(TAG,
"Setting active mode and gain reg 0x%02X", als_ctrl.raw);
305 uint8_t tries = MAX_TRIES;
307 ESP_LOGV(TAG,
"Waiting for device to become active");
310 }
while (!als_ctrl.active_mode && tries--);
312 if (!als_ctrl.active_mode) {
313 ESP_LOGW(TAG,
"Failed to activate device");
324 ps_ctrl.ps_mode_xxx =
true;
331 if (!als_status.ps_new_data || als_status.data_invalid) {
349 als_ctrl.gain =
gain;
355 if (read_als_ctrl.gain !=
gain) {
356 ESP_LOGW(TAG,
"Failed to set gain. We will try one more time.");
365 meas.integration_time = time;
371 if (read_meas.integration_time != time) {
372 ESP_LOGW(TAG,
"Failed to set integration time. We will try one more time.");
382 if (!als_status.als_new_data)
385 if (als_status.data_invalid) {
386 ESP_LOGW(TAG,
"Data available but not valid");
389 ESP_LOGV(TAG,
"Data ready, reported gain is %.0f", get_gain_coeff(als_status.gain));
390 if (data.
gain != als_status.gain) {
391 ESP_LOGW(TAG,
"Actual gain differs from requested (%.0f)", get_gain_coeff(data.
gain));
407 ESP_LOGV(TAG,
"Got sensor data: CH1 = %d, CH0 = %d", data.
ch1, data.
ch0);
416 ESP_LOGW(TAG,
"Too many sensitivity adjustments done. Apparently, sensor reconfiguration fails. Stopping.");
422 static const uint16_t LOW_INTENSITY_THRESHOLD = 1000;
423 static const uint16_t HIGH_INTENSITY_THRESHOLD = 30000;
429 if (data.
ch0 <= LOW_INTENSITY_THRESHOLD) {
431 if (next_gain != data.
gain) {
432 data.
gain = next_gain;
433 ESP_LOGV(TAG,
"Low illuminance. Increasing gain.");
439 ESP_LOGV(TAG,
"Low illuminance. Increasing integration time.");
442 }
else if (data.
ch0 >= HIGH_INTENSITY_THRESHOLD) {
444 if (prev_gain != data.
gain) {
445 data.
gain = prev_gain;
446 ESP_LOGV(TAG,
"High illuminance. Decreasing gain.");
452 ESP_LOGV(TAG,
"High illuminance. Decreasing integration time.");
456 ESP_LOGD(TAG,
"Illuminance is sufficient.");
459 ESP_LOGD(TAG,
"Can't adjust sensitivity anymore.");
464 if ((data.
ch0 == 0xFFFF) || (data.
ch1 == 0xFFFF)) {
465 ESP_LOGW(TAG,
"Sensors got saturated");
470 if ((data.
ch0 == 0x0000) && (data.
ch1 == 0x0000)) {
471 ESP_LOGW(TAG,
"Sensors blacked out");
476 float ch0 = data.
ch0;
477 float ch1 = data.
ch1;
478 float ratio = ch1 / (ch0 + ch1);
479 float als_gain = get_gain_coeff(data.
gain);
485 lux = (1.7743 * ch0 + 1.1059 * ch1);
486 }
else if (ratio < 0.64 && ratio >= 0.45) {
487 lux = (4.2785 * ch0 - 1.9548 * ch1);
488 }
else if (ratio < 0.85 && ratio >= 0.64) {
489 lux = (0.5926 * ch0 + 0.1185 * ch1);
491 ESP_LOGW(TAG,
"Impossible ch1/(ch0 + ch1) ratio");
494 lux = inv_pfactor * lux / als_gain / als_time;
497 ESP_LOGV(TAG,
"Lux calculation: ratio %.3f, gain %.0fx, int time %.1f, inv_pfactor %.3f, lux %.3f", ratio, als_gain,
498 als_time, inv_pfactor, lux);
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
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 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_
uint16_t ps_threshold_low_
CallbackManager< void()> on_ps_low_trigger_callback_
DataAvail is_als_data_ready_(AlsReadings &data)
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
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_
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)
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()
IntegrationTime integration_time
uint8_t number_of_adjustments
MeasurementRepeatRate measurement_repeat_rate
PsMeasurementRate ps_measurement_rate