9#define APDS9960_ERROR_CHECK(func) \
11 this->mark_failed(LOG_STR("Setting up APDS9960 registers failed")); \
30static constexpr i2c::I2CRegValue APDS9960_INTERRUPT_GESTURE[] PROGMEM = {
45static constexpr i2c::I2CRegValue APDS9960_GESTURE_OFFSETS[] PROGMEM = {
68 if (
id != 0xAB &&
id != 0x9C &&
id != 0xA8 &&
id != 0x9E) {
69 this->
mark_failed(LOG_STR(
"APDS9960 has invalid id"));
81 APDS9960_ERROR_CHECK(this->
write_registers(APDS9960_INTERRUPT_GESTURE));
101 APDS9960_ERROR_CHECK(this->
write_byte(0x80, val));
105 return this->red_sensor_ !=
nullptr || this->green_sensor_ !=
nullptr || this->blue_sensor_ !=
nullptr ||
106 this->clear_sensor_ !=
nullptr;
112void APDS9960::dump_config() {
113 ESP_LOGCONFIG(TAG,
"APDS9960:");
114 LOG_I2C_DEVICE(
this);
116 LOG_UPDATE_INTERVAL(
this);
119 LOG_SENSOR(
" ",
"Red channel", this->red_sensor_);
120 LOG_SENSOR(
" ",
"Green channel", this->green_sensor_);
121 LOG_SENSOR(
" ",
"Blue channel", this->blue_sensor_);
122 LOG_SENSOR(
" ",
"Clear channel", this->clear_sensor_);
123 LOG_SENSOR(
" ",
"Proximity", this->proximity_sensor_);
127#define APDS9960_WARNING_CHECK(func, warning) \
129 ESP_LOGW(TAG, warning); \
130 this->status_set_warning(); \
134void APDS9960::update() {
136 APDS9960_WARNING_CHECK(this->
read_byte(0x93, &status),
"Reading status bit failed.");
149 if ((status & 0x01) == 0x00) {
155 APDS9960_WARNING_CHECK(this->
read_bytes_16_le(0x94, raw, 4),
"Reading color values failed.");
157 uint16_t uint_clear =
raw[0];
158 uint16_t uint_red =
raw[1];
159 uint16_t uint_green =
raw[2];
160 uint16_t uint_blue =
raw[3];
162 float clear_perc = (uint_clear / float(UINT16_MAX)) * 100.0f;
163 float red_perc = (uint_red / float(UINT16_MAX)) * 100.0f;
164 float green_perc = (uint_green / float(UINT16_MAX)) * 100.0f;
165 float blue_perc = (uint_blue / float(UINT16_MAX)) * 100.0f;
167 ESP_LOGD(TAG,
"Got clear=%.1f%% red=%.1f%% green=%.1f%% blue=%.1f%%", clear_perc, red_perc, green_perc, blue_perc);
169 PUBLISH_STATE(this->clear_sensor_, clear_perc);
170 PUBLISH_STATE(this->red_sensor_, red_perc);
171 PUBLISH_STATE(this->green_sensor_, green_perc);
172 PUBLISH_STATE(this->blue_sensor_, blue_perc);
179 if (this->proximity_sensor_ ==
nullptr)
182 if ((
status & 0b10) == 0x00) {
188 APDS9960_WARNING_CHECK(this->
read_byte(0x9C, &prox),
"Reading proximity values failed.");
190 float prox_perc = (prox / float(UINT8_MAX)) * 100.0f;
191 ESP_LOGD(TAG,
"Got proximity=%.1f%%", prox_perc);
192 this->proximity_sensor_->publish_state(prox_perc);
200 APDS9960_WARNING_CHECK(this->
read_byte(0xAF, &status),
"Reading gesture status failed.");
202 if ((
status & 0b01) == 0) {
207 if ((
status & 0b10) == 0b10) {
208 ESP_LOGV(TAG,
"FIFO buffer has filled to capacity!");
212 APDS9960_WARNING_CHECK(this->
read_byte(0xAE, &fifo_level),
"Reading FIFO level failed.");
213 if (fifo_level == 0) {
218 APDS9960_WARNING_CHECK(fifo_level <= 32,
"FIFO level has invalid value.")
221 for (uint8_t
pos = 0;
pos < fifo_level * 4;
pos += 32) {
225 uint8_t
read = std::min(32, fifo_level * 4 -
pos);
226 APDS9960_WARNING_CHECK(this->
read_bytes(0xFC, buf +
pos,
read),
"Reading FIFO buffer failed.");
236 for (
uint32_t i = 0; i < fifo_level * 4; i += 4) {
237 const int up = buf[i + 0];
238 const int down = buf[i + 1];
239 const int left = buf[i + 2];
240 const int right = buf[i + 3];
245#ifdef USE_BINARY_SENSOR
249 bin = this->up_direction_binary_sensor_;
252 ESP_LOGD(TAG,
"Got gesture UP");
255 bin = this->down_direction_binary_sensor_;
258 ESP_LOGD(TAG,
"Got gesture DOWN");
261 bin = this->left_direction_binary_sensor_;
264 ESP_LOGD(TAG,
"Got gesture LEFT");
267 bin = this->right_direction_binary_sensor_;
270 ESP_LOGD(TAG,
"Got gesture RIGHT");
276 if (bin !=
nullptr) {
306 const int up_down_delta = up - down;
307 const int left_right_delta = left - right;
308 const bool up_down_significant = abs(up_down_delta) > 13;
309 const bool left_right_significant = abs(left_right_delta) > 13;
311 if (up_down_significant) {
312 if (up_down_delta < 0) {
333 if (left_right_significant) {
334 if (left_right_delta < 0) {
358 this->proximity_sensor_ !=
nullptr
365#ifdef USE_BINARY_SENSOR
366 return this->up_direction_binary_sensor_ !=
nullptr || this->left_direction_binary_sensor_ !=
nullptr ||
367 this->down_direction_binary_sensor_ !=
nullptr || this->right_direction_binary_sensor_ !=
nullptr;
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
void status_clear_warning()
bool gesture_right_started_
bool gesture_left_started_
void read_proximity_data_(uint8_t status)
void read_gesture_data_()
bool is_gesture_enabled_() const
void report_gesture_(int gesture)
void read_color_data_(uint8_t status)
bool gesture_down_started_
uint8_t gesture_wait_time_
bool is_color_enabled_() const
uint8_t gesture_led_drive_
void process_dataset_(int up, int down, int left, int right)
bool is_proximity_enabled_() const
Base class for all binary_sensor-type classes.
void publish_state(bool new_state)
Publish a new state to the front-end.
bool read_bytes_16_le(uint8_t a_register, uint16_t *data, uint8_t len)
Read len 16-bit values stored least significant byte first; no conversion is compiled on little-endia...
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
bool write_registers(const I2CRegValue *table, size_t count) const
Write each register and value of table in order, stopping at the first failure.
ESPHOME_LOG_TAG(TAG, "apds9960")
uint32_t IRAM_ATTR HOT millis()
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.