7static const char *
const TAG =
"veml3235.sensor";
11static const uint16_t CLIPPED_COUNTS = 64224;
14static const uint16_t MAX_SENSITIVITY_FACTOR = 128;
17static const uint8_t MAX_ADJUSTMENTS_PER_UPDATE = 2;
20 uint8_t device_id[] = {0, 0};
22 ESP_LOGE(TAG,
"Unable to write configuration");
27 ESP_LOGE(TAG,
"Unable to read ID");
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]);
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", this->
get_name().c_str());
56 uint8_t als_regs[] = {0, 0};
66 uint16_t als_counts =
encode_uint16(als_regs[1], als_regs[0]);
68 if (this->
auto_gain_ && adjustments_left > 0) {
81 ESP_LOGVV(TAG,
"'%s': ALS counts = %u, sensitivity = %ux", this->
get_name().c_str(), als_counts,
83 ESP_LOGV(TAG,
"'%s': Illuminance = %.4flx", this->
get_name().c_str(), lux);
94 switch (this->
gain_) {
112 uint8_t it_exponent = 0;
117 factor >>= it_exponent;
122 }
else if (factor == 2) {
135 const bool clipped = counts >= CLIPPED_COUNTS;
138 if (!clipped && counts >= low && counts <= high) {
146 }
else if (counts == 0) {
147 new_factor = MAX_SENSITIVITY_FACTOR;
152 float desired = float(current_factor) * ((float(low) + float(high)) * 0.5f / float(counts));
153 desired = clamp(desired, 1.0f,
float(MAX_SENSITIVITY_FACTOR));
155 while (new_factor * 2 <= uint16_t(desired)) {
160 if (new_factor == current_factor) {
172 this->
gain_ = old_gain;
178 ESP_LOGV(TAG,
"'%s': Sensitivity adjusted from %ux to %ux (ALS counts = %u)", this->
get_name().c_str(),
179 current_factor, new_factor, counts);
186 LOG_SENSOR(
"",
"VEML3235",
this);
187 LOG_I2C_DEVICE(
this);
189 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
191 LOG_UPDATE_INTERVAL(
this);
192 ESP_LOGCONFIG(TAG,
" Auto-gain enabled: %s", YESNO(this->
auto_gain_));
195 " Auto-gain thresholds:\n"
198 " Values below will be used as initial values only",
202 " Digital gain: %uX\n"
204 " Integration time: %ums",
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()
const StringRef & get_name() const
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
bool write_byte_16(uint8_t a_register, uint16_t data) const
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
VEML3235ComponentIntegrationTime
@ VEML3235_INTEGRATION_TIME_800MS
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.