ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
apds9930.cpp
Go to the documentation of this file.
1#include "apds9930.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6#include <algorithm>
7#include <cmath>
8
10
11ESPHOME_LOG_TAG(TAG, "apds9930");
12
13// Register addresses
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;
21
22// Command byte: repeated byte and auto-increment protocols
23static constexpr uint8_t APDS9930_CMD = 0x80;
24static constexpr uint8_t APDS9930_CMD_AUTO_INCREMENT = 0xA0;
25
26static constexpr uint8_t APDS9930_CHIP_ID = 0x39;
27
28// ENABLE register bits
29static constexpr uint8_t APDS9930_PON = 0x01;
30static constexpr uint8_t APDS9930_AEN = 0x02;
31static constexpr uint8_t APDS9930_PEN = 0x04;
32
33// STATUS register bits
34static constexpr uint8_t APDS9930_AVALID = 0x01;
35static constexpr uint8_t APDS9930_PVALID = 0x02;
36
37// CONTROL register fields
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; // The only supported proximity diode
43
44static constexpr uint8_t APDS9930_ATIME_VALUE = 0xED; // 19 cycles, about 52 ms
45static constexpr uint8_t APDS9930_PTIME_VALUE = 0xFF; // 2.73 ms
46static constexpr uint8_t APDS9930_PPULSE_VALUE = 0x08; // 8 pulses
47
48// Lux calculation coefficients
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);
55
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; // halved for each drive step
59
60// Lux per count at 1x gain and the fixed integration time
61static constexpr float LUX_PER_COUNT_1X = APDS9930_GA * APDS9930_DF / APDS9930_ALSIT_MS;
62// Each integration cycle adds up to 1024 counts, so this is the ceiling of both light channels
63static constexpr uint16_t ALS_SATURATION = 1024 * (256 - APDS9930_ATIME_VALUE);
64
65bool APDS9930Component::write_reg_(uint8_t reg, uint8_t value) { return this->write_byte(reg | APDS9930_CMD, value); }
66
68 if (!this->check_chip_id(this, APDS9930_ID | APDS9930_CMD, APDS9930_CHIP_ID))
69 return;
70
71 const uint8_t control =
72 (this->led_drive_ & 0b11) << CONTROL_PDRIVE_SHIFT | CONTROL_PDIODE_CH1 << CONTROL_PDIODE_SHIFT |
73 (this->proximity_gain_ & 0b11) << CONTROL_PGAIN_SHIFT | (this->ambient_gain_ & 0b11) << CONTROL_AGAIN_SHIFT;
74
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;
80
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) ||
84 !this->write_reg_(APDS9930_ENABLE, enable)) {
85 this->mark_failed(LOG_STR("Configuration failed"));
86 }
87}
88
89void APDS9930Component::dump_config() {
90 ESP_LOGCONFIG(TAG,
91 "APDS9930:\n"
92 " LED Drive: %.1f mA\n"
93 " Proximity Gain: %ux\n"
94 " Ambient Light Gain: %ux",
95 LED_CURRENT_MAX_MA / (1 << this->led_drive_),
96 progmem_read_byte(&PROXIMITY_GAINS[this->proximity_gain_]),
97 progmem_read_byte(&AMBIENT_GAINS[this->ambient_gain_]));
98 LOG_I2C_DEVICE(this);
99 LOG_UPDATE_INTERVAL(this);
100 LOG_SENSOR(" ", "Illuminance", this->illuminance_sensor_);
101 LOG_SENSOR(" ", "Proximity", this->proximity_sensor_);
102}
103
104void APDS9930Component::update() {
105 // STATUS, CH0DATAL/H, CH1DATAL/H, PDATAL/H
106 uint8_t raw[7];
107 if (!this->status_comm_result(this->read_bytes(APDS9930_STATUS | APDS9930_CMD_AUTO_INCREMENT, raw, sizeof(raw)),
108 LOG_STR("Reading data failed")))
109 return;
110 const uint8_t status = raw[0];
111
112 if (this->illuminance_sensor_ != nullptr && (status & APDS9930_AVALID) != 0) {
113 const uint16_t ch0 = encode_value_le<uint16_t>(&raw[1]);
114 const uint16_t ch1 = encode_value_le<uint16_t>(&raw[3]);
115 if (ch0 >= ALS_SATURATION || ch1 >= ALS_SATURATION) {
116 // A saturated channel would give a lux value that is too low, not too high
117 ESP_LOGW(TAG, "Light channel saturated, lower ambient_light_gain");
118 this->illuminance_sensor_->publish_state(NAN);
119 } else {
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 /
122 progmem_read_byte(&AMBIENT_GAINS[this->ambient_gain_]));
123 }
124 }
125
126 if (this->proximity_sensor_ != nullptr && (status & APDS9930_PVALID) != 0) {
127 this->proximity_sensor_->publish_state(encode_value_le<uint16_t>(&raw[5]));
128 }
129}
130
131} // namespace esphome::apds9930
uint8_t raw[35]
Definition bl0939.h:0
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool write_reg_(uint8_t reg, uint8_t value)
Definition apds9930.cpp:65
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
Definition i2c.h:232
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
ESPHOME_LOG_TAG(TAG, "apds9930")
uint32_t value
Definition hal.cpp:41
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51