9static constexpr uint32_t REREAD_TIMEOUT_ID = 0;
13static const uint16_t CLIPPED_COUNTS = 64224;
16static const uint16_t MAX_SENSITIVITY_FACTOR = 128;
19static const uint8_t MAX_ADJUSTMENTS_PER_UPDATE = 2;
22 uint8_t device_id[] = {0, 0};
24 this->
mark_failed(LOG_STR(
"Unable to write configuration"));
29 }
else if (device_id[0] != DEVICE_ID) {
30 ESP_LOGE(TAG,
"Incorrect device ID - expected 0x%.2x, read 0x%.2x", DEVICE_ID, device_id[0]);
36 uint16_t data = 0x100;
40 data |= (uint16_t(this->
gain_) << CONFIG_REG_G_BIT);
42 ESP_LOGVV(TAG,
"Writing 0x%.4x to register 0x%.2x", data, CONFIG_REG);
48 ESP_LOGV(TAG,
"'%s': Previous measurement still in progress; skipping update", LOG_STR_ARG(this->
get_log_name()));
56 uint8_t als_regs[] = {0, 0};
65 if (this->
auto_gain_ && adjustments_left > 0) {
78 ESP_LOGVV(TAG,
"'%s': ALS counts = %u, sensitivity = %ux", LOG_STR_ARG(this->
get_log_name()), als_counts,
80 ESP_LOGV(TAG,
"'%s': Illuminance = %.4flx", LOG_STR_ARG(this->
get_log_name()), lux);
91 switch (this->
gain_) {
109 uint8_t it_exponent = 0;
114 factor >>= it_exponent;
119 }
else if (factor == 2) {
132 const bool clipped = counts >= CLIPPED_COUNTS;
135 if (!clipped && counts >= low && counts <= high) {
143 }
else if (counts == 0) {
144 new_factor = MAX_SENSITIVITY_FACTOR;
149 float desired = float(current_factor) * ((float(low) + float(high)) * 0.5f / float(counts));
150 desired = clamp(desired, 1.0f,
float(MAX_SENSITIVITY_FACTOR));
152 while (new_factor * 2 <= uint16_t(desired)) {
157 if (new_factor == current_factor) {
169 this->
gain_ = old_gain;
175 ESP_LOGV(TAG,
"'%s': Sensitivity adjusted from %ux to %ux (ALS counts = %u)", LOG_STR_ARG(this->
get_log_name()),
176 current_factor, new_factor, counts);
183 LOG_SENSOR(
"",
"VEML3235",
this);
184 LOG_I2C_DEVICE(
this);
185 LOG_UPDATE_INTERVAL(
this);
186 ESP_LOGCONFIG(TAG,
" Auto-gain enabled: %s", YESNO(this->
auto_gain_));
189 " Auto-gain thresholds:\n"
192 " Values below will be used as initial values only",
196 " Digital gain: %uX\n"
198 " Integration time: %ums",
void mark_failed()
Mark this component as failed.
void status_set_warning()
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
bool write_byte_16_le(uint8_t a_register, uint16_t data) const
Write a 16-bit value least significant byte first.
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
void publish_state(float state)
Publish a new state to the front-end.
bool refresh_config_reg()
VEML3235ComponentIntegrationTime integration_time_
float auto_gain_threshold_high_
float auto_gain_threshold_low_
float counts_to_lux_(uint16_t counts) const
void set_sensitivity_factor_(uint16_t factor)
uint16_t integration_time_ms_() const
bool measurement_in_progress_
bool adjust_sensitivity_(uint16_t counts)
void read_and_publish_(uint8_t adjustments_left)
VEML3235ComponentGain gain_
void dump_config() override
uint16_t sensitivity_factor_() const
VEML3235ComponentDigitalGain digital_gain()
uint8_t gain_factor_() const
VEML3235ComponentDigitalGain digital_gain_
@ ERROR_OK
No error found during execution of method.
VEML3235ComponentDigitalGain
@ VEML3235_DIGITAL_GAIN_2X
@ VEML3235_DIGITAL_GAIN_1X
ESPHOME_LOG_TAG(TAG, "veml3235.sensor")
VEML3235ComponentIntegrationTime
@ VEML3235_INTEGRATION_TIME_800MS
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...