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];
66 ESP_LOGCONFIG(TAG,
"Running setup");
72 auto get_device_type = [](
LtrType typ) {
86 ESP_LOGCONFIG(TAG,
" Device type: %s", get_device_type(this->
ltr_type_));
89 " Automatic mode: %s\n"
91 " Integration time: %d ms\n"
92 " Measurement repeat rate: %d ms\n"
93 " Glass attenuation factor: %f",
104 " Proximity gain: %.0fx\n"
105 " Proximity cooldown time: %d s\n"
106 " Proximity high threshold: %d\n"
107 " Proximity low threshold: %d",
112 LOG_UPDATE_INTERVAL(
this);
115 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
120 ESP_LOGV(TAG,
"Updating");
122 ESP_LOGV(TAG,
"Initiating new data collection");
134 ESP_LOGV(TAG,
"Component not ready yet");
140 static uint8_t tries{0};
142 switch (this->state_) {
144 err = this->
write(
nullptr, 0);
146 ESP_LOGV(TAG,
"i2c connection failed");
170 ESP_LOGV(TAG,
"Reading sensor data having gain = %.0fx, time = %d ms", get_gain_coeff(this->
als_readings_.
gain),
175 }
else if (tries >= MAX_TRIES) {
176 ESP_LOGW(TAG,
"Can't get data after several tries.");
191 ESP_LOGD(TAG,
"Reconfiguring sensitivity: gain = %.0fx, time = %d ms", get_gain_coeff(this->
als_readings_.
gain),
224 static uint32_t last_high_trigger_time{0};
225 static uint32_t last_low_trigger_time{0};
233 last_high_trigger_time = now;
234 ESP_LOGV(TAG,
"Proximity high threshold triggered. Value = %d, Trigger level = %d", ps_data,
238 last_low_trigger_time = now;
239 ESP_LOGV(TAG,
"Proximity low threshold triggered. Value = %d, Trigger level = %d", ps_data,
248 if (manuf_id != 0x05) {
249 ESP_LOGW(TAG,
"Unknown manufacturer ID: 0x%02X", manuf_id);
267 if (part_id.part_number_id != 0x0a && part_id.part_number_id != 0x09) {
268 ESP_LOGW(TAG,
"Unknown part number ID: 0x%02X. It might not work properly.", part_id.part_number_id);
276 ESP_LOGV(TAG,
"Resetting");
283 uint8_t tries = MAX_TRIES;
285 ESP_LOGV(TAG,
"Waiting for chip to reset");
288 }
while (als_ctrl.sw_reset && tries--);
290 if (als_ctrl.sw_reset) {
291 ESP_LOGW(TAG,
"Reset timed out");
299 als_ctrl.active_mode =
true;
300 als_ctrl.gain = this->
gain_;
302 ESP_LOGV(TAG,
"Setting active mode and gain reg 0x%02X", als_ctrl.raw);
306 uint8_t tries = MAX_TRIES;
308 ESP_LOGV(TAG,
"Waiting for device to become active");
311 }
while (!als_ctrl.active_mode && tries--);
313 if (!als_ctrl.active_mode) {
314 ESP_LOGW(TAG,
"Failed to activate device");
325 ps_ctrl.ps_mode_xxx =
true;
332 if (!als_status.ps_new_data || als_status.data_invalid) {
350 als_ctrl.gain =
gain;
356 if (read_als_ctrl.gain !=
gain) {
357 ESP_LOGW(TAG,
"Failed to set gain. We will try one more time.");
366 meas.integration_time = time;
372 if (read_meas.integration_time != time) {
373 ESP_LOGW(TAG,
"Failed to set integration time. We will try one more time.");
383 if (!als_status.als_new_data)
386 if (als_status.data_invalid) {
387 ESP_LOGW(TAG,
"Data available but not valid");
390 ESP_LOGV(TAG,
"Data ready, reported gain is %.0f", get_gain_coeff(als_status.gain));
391 if (data.
gain != als_status.gain) {
392 ESP_LOGW(TAG,
"Actual gain differs from requested (%.0f)", get_gain_coeff(data.
gain));
408 ESP_LOGV(TAG,
"Got sensor data: CH1 = %d, CH0 = %d", data.
ch1, data.
ch0);
417 ESP_LOGW(TAG,
"Too many sensitivity adjustments done. Apparently, sensor reconfiguration fails. Stopping.");
423 static const uint16_t LOW_INTENSITY_THRESHOLD = 1000;
424 static const uint16_t HIGH_INTENSITY_THRESHOLD = 30000;
430 if (data.
ch0 <= LOW_INTENSITY_THRESHOLD) {
432 if (next_gain != data.
gain) {
433 data.
gain = next_gain;
434 ESP_LOGV(TAG,
"Low illuminance. Increasing gain.");
440 ESP_LOGV(TAG,
"Low illuminance. Increasing integration time.");
443 }
else if (data.
ch0 >= HIGH_INTENSITY_THRESHOLD) {
445 if (prev_gain != data.
gain) {
446 data.
gain = prev_gain;
447 ESP_LOGV(TAG,
"High illuminance. Decreasing gain.");
453 ESP_LOGV(TAG,
"High illuminance. Decreasing integration time.");
457 ESP_LOGD(TAG,
"Illuminance is sufficient.");
460 ESP_LOGD(TAG,
"Can't adjust sensitivity anymore.");
465 if ((data.
ch0 == 0xFFFF) || (data.
ch1 == 0xFFFF)) {
466 ESP_LOGW(TAG,
"Sensors got saturated");
471 if ((data.
ch0 == 0x0000) && (data.
ch1 == 0x0000)) {
472 ESP_LOGW(TAG,
"Sensors blacked out");
477 float ch0 = data.
ch0;
478 float ch1 = data.
ch1;
479 float ratio = ch1 / (ch0 + ch1);
480 float als_gain = get_gain_coeff(data.
gain);
486 lux = (1.7743 * ch0 + 1.1059 * ch1);
487 }
else if (ratio < 0.64 && ratio >= 0.45) {
488 lux = (4.2785 * ch0 - 1.9548 * ch1);
489 }
else if (ratio < 0.85 && ratio >= 0.64) {
490 lux = (0.5926 * ch0 + 0.1185 * ch1);
492 ESP_LOGW(TAG,
"Impossible ch1/(ch0 + ch1) ratio");
495 lux = inv_pfactor * lux / als_gain / als_time;
498 ESP_LOGV(TAG,
"Lux calculation: ratio %.3f, gain %.0fx, int time %.1f, inv_pfactor %.3f, lux %.3f", ratio, als_gain,
499 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 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