14static constexpr uint8_t APDS9930_ENABLE = 0x00;
15static constexpr uint8_t APDS9930_ATIME = 0x01;
16static constexpr uint8_t APDS9930_PTIME = 0x02;
17static constexpr uint8_t APDS9930_PPULSE = 0x0E;
18static constexpr uint8_t APDS9930_CONTROL = 0x0F;
19static constexpr uint8_t APDS9930_ID = 0x12;
20static constexpr uint8_t APDS9930_STATUS = 0x13;
23static constexpr uint8_t APDS9930_CMD = 0x80;
24static constexpr uint8_t APDS9930_CMD_AUTO_INCREMENT = 0xA0;
26static constexpr uint8_t APDS9930_CHIP_ID = 0x39;
29static constexpr uint8_t APDS9930_PON = 0x01;
30static constexpr uint8_t APDS9930_AEN = 0x02;
31static constexpr uint8_t APDS9930_PEN = 0x04;
34static constexpr uint8_t APDS9930_AVALID = 0x01;
35static constexpr uint8_t APDS9930_PVALID = 0x02;
38static constexpr uint8_t CONTROL_PDRIVE_SHIFT = 6;
39static constexpr uint8_t CONTROL_PDIODE_SHIFT = 4;
40static constexpr uint8_t CONTROL_PGAIN_SHIFT = 2;
41static constexpr uint8_t CONTROL_AGAIN_SHIFT = 0;
42static constexpr uint8_t CONTROL_PDIODE_CH1 = 0b10;
44static constexpr uint8_t APDS9930_ATIME_VALUE = 0xED;
45static constexpr uint8_t APDS9930_PTIME_VALUE = 0xFF;
46static constexpr uint8_t APDS9930_PPULSE_VALUE = 0x08;
49static constexpr float APDS9930_DF = 52.0f;
50static constexpr float APDS9930_GA = 0.49f;
51static constexpr float APDS9930_ALS_B = 1.862f;
52static constexpr float APDS9930_ALS_C = 0.746f;
53static constexpr float APDS9930_ALS_D = 1.291f;
54static constexpr float APDS9930_ALSIT_MS = 2.73f * (256 - APDS9930_ATIME_VALUE);
56static constexpr uint8_t AMBIENT_GAINS[4] PROGMEM = {1, 8, 16, 120};
57static constexpr uint8_t PROXIMITY_GAINS[4] PROGMEM = {1, 2, 4, 8};
58static constexpr float LED_CURRENT_MAX_MA = 100.0f;
61static constexpr float LUX_PER_COUNT_1X = APDS9930_GA * APDS9930_DF / APDS9930_ALSIT_MS;
63static constexpr uint16_t ALS_SATURATION = 1024 * (256 - APDS9930_ATIME_VALUE);
68 if (!this->
check_chip_id(
this, APDS9930_ID | APDS9930_CMD, APDS9930_CHIP_ID))
71 const uint8_t control =
72 (this->
led_drive_ & 0b11) << CONTROL_PDRIVE_SHIFT | CONTROL_PDIODE_CH1 << CONTROL_PDIODE_SHIFT |
75 uint8_t enable = APDS9930_PON;
76 if (this->illuminance_sensor_ !=
nullptr)
77 enable |= APDS9930_AEN;
78 if (this->proximity_sensor_ !=
nullptr)
79 enable |= APDS9930_PEN;
81 if (!this->
write_reg_(APDS9930_ENABLE, 0x00) || !this->
write_reg_(APDS9930_ATIME, APDS9930_ATIME_VALUE) ||
82 !this->
write_reg_(APDS9930_PTIME, APDS9930_PTIME_VALUE) ||
83 !this->
write_reg_(APDS9930_PPULSE, APDS9930_PPULSE_VALUE) || !this->
write_reg_(APDS9930_CONTROL, control) ||
89void APDS9930Component::dump_config() {
92 " LED Drive: %.1f mA\n"
93 " Proximity Gain: %ux\n"
94 " Ambient Light Gain: %ux",
99 LOG_UPDATE_INTERVAL(
this);
100 LOG_SENSOR(
" ",
"Illuminance", this->illuminance_sensor_);
101 LOG_SENSOR(
" ",
"Proximity", this->proximity_sensor_);
104void APDS9930Component::update() {
108 LOG_STR(
"Reading data failed")))
112 if (this->illuminance_sensor_ !=
nullptr && (status & APDS9930_AVALID) != 0) {
115 if (ch0 >= ALS_SATURATION || ch1 >= ALS_SATURATION) {
117 ESP_LOGW(TAG,
"Light channel saturated, lower ambient_light_gain");
118 this->illuminance_sensor_->publish_state(NAN);
120 const float iac = std::max({ch0 - APDS9930_ALS_B * ch1, APDS9930_ALS_C * ch0 - APDS9930_ALS_D * ch1, 0.0f});
121 this->illuminance_sensor_->publish_state(iac * LUX_PER_COUNT_1X /
126 if (this->proximity_sensor_ !=
nullptr && (status & APDS9930_PVALID) != 0) {