13static constexpr uint8_t APDS9930_ENABLE = 0x00;
14static constexpr uint8_t APDS9930_ATIME = 0x01;
15static constexpr uint8_t APDS9930_PTIME = 0x02;
16static constexpr uint8_t APDS9930_PPULSE = 0x0E;
17static constexpr uint8_t APDS9930_CONTROL = 0x0F;
18static constexpr uint8_t APDS9930_ID = 0x12;
19static constexpr uint8_t APDS9930_STATUS = 0x13;
22static constexpr uint8_t APDS9930_CMD = 0x80;
23static constexpr uint8_t APDS9930_CMD_AUTO_INCREMENT = 0xA0;
25static constexpr uint8_t APDS9930_CHIP_ID = 0x39;
28static constexpr uint8_t APDS9930_PON = 0x01;
29static constexpr uint8_t APDS9930_AEN = 0x02;
30static constexpr uint8_t APDS9930_PEN = 0x04;
33static constexpr uint8_t APDS9930_AVALID = 0x01;
34static constexpr uint8_t APDS9930_PVALID = 0x02;
37static constexpr uint8_t CONTROL_PDRIVE_SHIFT = 6;
38static constexpr uint8_t CONTROL_PDIODE_SHIFT = 4;
39static constexpr uint8_t CONTROL_PGAIN_SHIFT = 2;
40static constexpr uint8_t CONTROL_AGAIN_SHIFT = 0;
41static constexpr uint8_t CONTROL_PDIODE_CH1 = 0b10;
43static constexpr uint8_t APDS9930_ATIME_VALUE = 0xED;
44static constexpr uint8_t APDS9930_PTIME_VALUE = 0xFF;
45static constexpr uint8_t APDS9930_PPULSE_VALUE = 0x08;
48static constexpr float APDS9930_DF = 52.0f;
49static constexpr float APDS9930_GA = 0.49f;
50static constexpr float APDS9930_ALS_B = 1.862f;
51static constexpr float APDS9930_ALS_C = 0.746f;
52static constexpr float APDS9930_ALS_D = 1.291f;
53static constexpr float APDS9930_ALSIT_MS = 2.73f * (256 - APDS9930_ATIME_VALUE);
55static constexpr uint8_t AMBIENT_GAINS[4] PROGMEM = {1, 8, 16, 120};
56static constexpr uint8_t PROXIMITY_GAINS[4] PROGMEM = {1, 2, 4, 8};
57static constexpr float LED_CURRENT_MAX_MA = 100.0f;
60static constexpr float LUX_PER_COUNT_1X = APDS9930_GA * APDS9930_DF / APDS9930_ALSIT_MS;
62static constexpr uint16_t ALS_SATURATION = 1024 * (256 - APDS9930_ATIME_VALUE);
68 if (!this->
read_byte(APDS9930_ID | APDS9930_CMD, &
id)) {
72 if (
id != APDS9930_CHIP_ID) {
73 ESP_LOGE(TAG,
"Wrong ID 0x%02X (expected 0x%02X)",
id, APDS9930_CHIP_ID);
78 const uint8_t control =
79 (this->
led_drive_ & 0b11) << CONTROL_PDRIVE_SHIFT | CONTROL_PDIODE_CH1 << CONTROL_PDIODE_SHIFT |
82 uint8_t enable = APDS9930_PON;
83 if (this->illuminance_sensor_ !=
nullptr)
84 enable |= APDS9930_AEN;
85 if (this->proximity_sensor_ !=
nullptr)
86 enable |= APDS9930_PEN;
88 if (!this->
write_reg_(APDS9930_ENABLE, 0x00) || !this->
write_reg_(APDS9930_ATIME, APDS9930_ATIME_VALUE) ||
89 !this->
write_reg_(APDS9930_PTIME, APDS9930_PTIME_VALUE) ||
90 !this->
write_reg_(APDS9930_PPULSE, APDS9930_PPULSE_VALUE) || !this->
write_reg_(APDS9930_CONTROL, control) ||
96void APDS9930Component::dump_config() {
99 " LED Drive: %.1f mA\n"
100 " Proximity Gain: %ux\n"
101 " Ambient Light Gain: %ux",
105 LOG_I2C_DEVICE(
this);
106 LOG_UPDATE_INTERVAL(
this);
107 LOG_SENSOR(
" ",
"Illuminance", this->illuminance_sensor_);
108 LOG_SENSOR(
" ",
"Proximity", this->proximity_sensor_);
111void APDS9930Component::update() {
114 if (!this->
read_bytes(APDS9930_STATUS | APDS9930_CMD_AUTO_INCREMENT,
raw,
sizeof(
raw))) {
121 if (this->illuminance_sensor_ !=
nullptr && (status & APDS9930_AVALID) != 0) {
124 if (ch0 >= ALS_SATURATION || ch1 >= ALS_SATURATION) {
126 ESP_LOGW(TAG,
"Light channel saturated, lower ambient_light_gain");
127 this->illuminance_sensor_->publish_state(NAN);
129 const float iac = std::max({ch0 - APDS9930_ALS_B * ch1, APDS9930_ALS_C * ch0 - APDS9930_ALS_D * ch1, 0.0f});
130 this->illuminance_sensor_->publish_state(iac * LUX_PER_COUNT_1X /
135 if (this->proximity_sensor_ !=
nullptr && (status & APDS9930_PVALID) != 0) {