11static const char *
const TAG =
"apds9306";
36#define APDS9306_ERROR_CHECK(func, error) \
38 ESP_LOGE(TAG, error); \
39 this->mark_failed(); \
42#define APDS9306_WARNING_CHECK(func, warning) \
44 ESP_LOGW(TAG, warning); \
45 this->status_set_warning(); \
48#define APDS9306_WRITE_BYTE(reg, value) \
49 ESP_LOGV(TAG, "Writing 0x%02x to 0x%02x", value, reg); \
50 if (!this->write_byte(reg, value)) { \
51 ESP_LOGE(TAG, "Failed writing 0x%02x to 0x%02x", value, reg); \
52 this->mark_failed(); \
57 ESP_LOGCONFIG(TAG,
"Setting up APDS9306...");
66 if (
id != 0xB1 &&
id != 0xB3) {
77 uint8_t als_gain = (this->
gain_ & 0x07);
90 ESP_LOGCONFIG(TAG,
"APDS9306 setup complete");
94 LOG_SENSOR(
"",
"APDS9306",
this);
98 switch (this->error_code_) {
100 ESP_LOGE(TAG,
"Communication with APDS9306 failed!");
103 ESP_LOGE(TAG,
"APDS9306 has invalid id!");
106 ESP_LOGE(TAG,
"Setting up APDS9306 registers failed!");
111 ESP_LOGCONFIG(TAG,
" Gain: %u", AMBIENT_LIGHT_GAIN_VALUES[this->
gain_]);
112 ESP_LOGCONFIG(TAG,
" Measurement rate: %u", MEASUREMENT_RATE_VALUES[this->
measurement_rate_]);
113 ESP_LOGCONFIG(TAG,
" Measurement Resolution/Bit width: %d", MEASUREMENT_BIT_WIDTH_VALUES[this->
bit_width_]);
115 LOG_UPDATE_INTERVAL(
this);
125 status &= 0b00001000;
142 uint32_t light_level = 0x00 |
encode_uint24(als_data[2], als_data[1], als_data[0]);
144 float lux = ((float) light_level / AMBIENT_LIGHT_GAIN_VALUES[this->
gain_]) *
147 ESP_LOGD(TAG,
"Got illuminance=%.1flx from", lux);
virtual void mark_failed()
Mark this component as failed.
void status_clear_warning()
MeasurementBitWidth bit_width_
void dump_config() override
MeasurementRate measurement_rate_
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
void publish_state(float state)
Publish a new state to the front-end.
@ APDS9306_ALS_THRES_LOW_1
@ APDS9306_INT_PERSISTENCE
@ APDS9306_ALS_THRES_UP_2
@ APDS9306_ALS_THRES_UP_1
@ APDS9306_ALS_THRES_UP_0
@ APDS9306_ALS_THRES_LOW_2
@ APDS9306_ALS_THRES_LOW_0
Providing packet encoding functions for exchanging data with a remote host.
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.