ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
veml3235.cpp
Go to the documentation of this file.
1#include "veml3235.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace veml3235 {
7
8static const char *const TAG = "veml3235.sensor";
9
11 uint8_t device_id[] = {0, 0};
12 if (!this->refresh_config_reg()) {
13 ESP_LOGE(TAG, "Unable to write configuration");
14 this->mark_failed();
15 return;
16 }
17 if ((this->write(&ID_REG, 1, false) != i2c::ERROR_OK) || !this->read_bytes_raw(device_id, 2)) {
18 ESP_LOGE(TAG, "Unable to read ID");
19 this->mark_failed();
20 return;
21 } else if (device_id[0] != DEVICE_ID) {
22 ESP_LOGE(TAG, "Incorrect device ID - expected 0x%.2x, read 0x%.2x", DEVICE_ID, device_id[0]);
23 this->mark_failed();
24 return;
25 }
26}
27
29 uint16_t data = this->power_on_ || force_on ? 0 : SHUTDOWN_BITS;
30
31 data |= (uint16_t(this->integration_time_ << CONFIG_REG_IT_BIT));
32 data |= (uint16_t(this->digital_gain_ << CONFIG_REG_DG_BIT));
33 data |= (uint16_t(this->gain_ << CONFIG_REG_G_BIT));
34 data |= 0x1; // mandatory 1 here per RM
35
36 ESP_LOGVV(TAG, "Writing 0x%.4x to register 0x%.2x", data, CONFIG_REG);
37 return this->write_byte_16(CONFIG_REG, data);
38}
39
41 if (!this->power_on_) { // if off, turn on
42 if (!this->refresh_config_reg(true)) {
43 ESP_LOGW(TAG, "Turning on failed");
44 this->status_set_warning();
45 return NAN;
46 }
47 delay(4); // from RM: a wait time of 4 ms should be observed before the first measurement is picked up, to allow
48 // for a correct start of the signal processor and oscillator
49 }
50
51 uint8_t als_regs[] = {0, 0};
52 if ((this->write(&ALS_REG, 1, false) != i2c::ERROR_OK) || !this->read_bytes_raw(als_regs, 2)) {
53 this->status_set_warning();
54 return NAN;
55 }
56
58
59 float als_raw_value_multiplier = LUX_MULTIPLIER_BASE;
60 uint16_t als_raw_value = encode_uint16(als_regs[1], als_regs[0]);
61 // determine multiplier value based on gains and integration time
63 als_raw_value_multiplier *= 2;
64 }
65 switch (this->gain_) {
67 als_raw_value_multiplier *= 4;
68 break;
70 als_raw_value_multiplier *= 2;
71 break;
72 default:
73 break;
74 }
75 switch (this->integration_time_) {
77 als_raw_value_multiplier *= 16;
78 break;
80 als_raw_value_multiplier *= 8;
81 break;
83 als_raw_value_multiplier *= 4;
84 break;
86 als_raw_value_multiplier *= 2;
87 break;
88 default:
89 break;
90 }
91 // finally, determine and return the actual lux value
92 float lx = float(als_raw_value) * als_raw_value_multiplier;
93 ESP_LOGVV(TAG, "'%s': ALS raw = %u, multiplier = %.5f", this->get_name().c_str(), als_raw_value,
94 als_raw_value_multiplier);
95 ESP_LOGD(TAG, "'%s': Illuminance = %.4flx", this->get_name().c_str(), lx);
96
97 if (!this->power_on_) { // turn off if required
98 if (!this->refresh_config_reg()) {
99 ESP_LOGW(TAG, "Turning off failed");
100 this->status_set_warning();
101 }
102 }
103
104 if (this->auto_gain_) {
105 this->adjust_gain_(als_raw_value);
106 }
107
108 return lx;
109}
110
111void VEML3235Sensor::adjust_gain_(const uint16_t als_raw_value) {
112 if ((als_raw_value > UINT16_MAX * this->auto_gain_threshold_low_) &&
113 (als_raw_value < UINT16_MAX * this->auto_gain_threshold_high_)) {
114 return;
115 }
116
117 if (als_raw_value >= UINT16_MAX * 0.9) { // over-saturated, reset all gains and start over
119 this->gain_ = VEML3235_GAIN_1X;
121 this->refresh_config_reg();
122 return;
123 }
124
125 if (this->gain_ != VEML3235_GAIN_4X) { // increase gain if possible
126 switch (this->gain_) {
127 case VEML3235_GAIN_1X:
128 this->gain_ = VEML3235_GAIN_2X;
129 break;
130 case VEML3235_GAIN_2X:
131 this->gain_ = VEML3235_GAIN_4X;
132 break;
133 default:
134 break;
135 }
136 this->refresh_config_reg();
137 return;
138 }
139 // gain is maxed out; reset it and try to increase digital gain
140 if (this->digital_gain_ != VEML3235_DIGITAL_GAIN_2X) { // increase digital gain if possible
142 this->gain_ = VEML3235_GAIN_1X;
143 this->refresh_config_reg();
144 return;
145 }
146 // digital gain is maxed out; reset it and try to increase integration time
147 if (this->integration_time_ != VEML3235_INTEGRATION_TIME_800MS) { // increase integration time if possible
148 switch (this->integration_time_) {
151 break;
154 break;
157 break;
160 break;
161 default:
162 break;
163 }
165 this->gain_ = VEML3235_GAIN_1X;
166 this->refresh_config_reg();
167 return;
168 }
169}
170
172 uint8_t digital_gain = 1;
173 uint8_t gain = 1;
174 uint16_t integration_time = 0;
175
177 digital_gain = 2;
178 }
179 switch (this->gain_) {
180 case VEML3235_GAIN_2X:
181 gain = 2;
182 break;
183 case VEML3235_GAIN_4X:
184 gain = 4;
185 break;
186 default:
187 break;
188 }
189 switch (this->integration_time_) {
191 integration_time = 50;
192 break;
194 integration_time = 100;
195 break;
197 integration_time = 200;
198 break;
200 integration_time = 400;
201 break;
203 integration_time = 800;
204 break;
205 default:
206 break;
207 }
208
209 LOG_SENSOR("", "VEML3235", this);
210 LOG_I2C_DEVICE(this);
211 if (this->is_failed()) {
212 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
213 }
214 LOG_UPDATE_INTERVAL(this);
215 ESP_LOGCONFIG(TAG, " Auto-gain enabled: %s", YESNO(this->auto_gain_));
216 if (this->auto_gain_) {
217 ESP_LOGCONFIG(TAG,
218 " Auto-gain upper threshold: %f%%\n"
219 " Auto-gain lower threshold: %f%%\n"
220 " Values below will be used as initial values only",
221 this->auto_gain_threshold_high_ * 100.0, this->auto_gain_threshold_low_ * 100.0);
222 }
223 ESP_LOGCONFIG(TAG,
224 " Digital gain: %uX\n"
225 " Gain: %uX\n"
226 " Integration time: %ums",
228}
229
230} // namespace veml3235
231} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
const StringRef & get_name() const
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition i2c.h:229
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition i2c.h:270
VEML3235ComponentIntegrationTime integration_time_
Definition veml3235.h:104
VEML3235ComponentGain gain()
Definition veml3235.h:88
void adjust_gain_(uint16_t als_raw_value)
Definition veml3235.cpp:111
VEML3235ComponentIntegrationTime integration_time()
Definition veml3235.h:89
VEML3235ComponentGain gain_
Definition veml3235.h:103
VEML3235ComponentDigitalGain digital_gain()
Definition veml3235.h:87
VEML3235ComponentDigitalGain digital_gain_
Definition veml3235.h:102
bool refresh_config_reg(bool force_on=false)
Definition veml3235.cpp:28
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
@ VEML3235_INTEGRATION_TIME_100MS
Definition veml3235.h:40
@ VEML3235_INTEGRATION_TIME_200MS
Definition veml3235.h:41
@ VEML3235_INTEGRATION_TIME_50MS
Definition veml3235.h:39
@ VEML3235_INTEGRATION_TIME_800MS
Definition veml3235.h:43
@ VEML3235_INTEGRATION_TIME_400MS
Definition veml3235.h:42
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:173
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29