Skip to content

Commit 0e8efdb

Browse files
authored
Merge pull request #896 from adafruit/copilot/add-adafruit-apds9999-sensor
[Claude_Opus4.6] Add APDS9999 I2C driver
2 parents 8381007 + 79a2229 commit 0e8efdb

File tree

6 files changed

+170
-4
lines changed

6 files changed

+170
-4
lines changed

.agents/skills/add-sensor-component-v1/SKILL.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ call the Celsius method and convert. Only implement the Celsius version. Never i
137137
**Read-and-cache requirement:** The calling order of `getEvent*()` methods is not guaranteed (°F
138138
may be called before °C). Every `getEvent*()` must go through a shared `_readSensor()` with a
139139
millis-based time guard so only the first call per cycle does the I2C read; subsequent calls
140-
return cached data. See the driver template in Step 1 and `WipperSnapper_I2C_Driver_SCD30.h` and SGP30
140+
return cached data. **Cache `millis()` once** at the top of the function (e.g.
141+
`unsigned long now = millis();`) and use the cached value for both the time guard check and
142+
setting `_lastRead` — never call `millis()` twice.
143+
See the driver template in Step 1 and `WipperSnapper_I2C_Driver_SCD30.h` and SGP30
141144
for the canonical patterns.
142145

143146
This is especially important for multi-reading sensors but also applies to temperature sensors
@@ -534,11 +537,12 @@ protected:
534537
unsigned long _lastRead = 0;
535538

536539
bool _readSensor() {
537-
if (_lastRead != 0 && millis() - _lastRead < 1000)
540+
unsigned long now = millis();
541+
if (_lastRead != 0 && now - _lastRead < 1000)
538542
return true; // recently read, use cached value
539543
if (!_tmp119->getEvent(&_cachedTemp))
540544
return false;
541-
_lastRead = millis();
545+
_lastRead = now;
542546
return true;
543547
}
544548

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPA06_003, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, Adafruit INA237 and INA238 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit AS5600 Library, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit BMP5xx Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Adafruit SGP41, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit QMC5883P Library, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306, Adafruit EPD, Adafruit ST7735 and ST7789 Library
10+
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPA06_003, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, Adafruit INA237 and INA238 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit APDS9999, Adafruit AS5600 Library, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit BMP5xx Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Adafruit SGP41, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit QMC5883P Library, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306, Adafruit EPD, Adafruit ST7735 and ST7789 Library

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ lib_deps =
2323
adafruit/ENS160 - Adafruit Fork
2424
adafruit/Adafruit SleepyDog Library
2525
adafruit/Adafruit AHTX0
26+
adafruit/Adafruit APDS9999
2627
adafruit/Adafruit AS5600 Library
2728
adafruit/Adafruit BME280 Library
2829
adafruit/Adafruit BMP280 Library

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
239239
}
240240
_ahtx0->configureDriver(msgDeviceInitReq);
241241
drivers.push_back(_ahtx0);
242+
} else if (strcmp("apds9999", msgDeviceInitReq->i2c_device_name) == 0) {
243+
_apds9999 = new WipperSnapper_I2C_Driver_APDS9999(this->_i2c, i2cAddress);
244+
if (!_apds9999->begin()) {
245+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize APDS9999!");
246+
_busStatusResponse =
247+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
248+
return false;
249+
}
250+
_apds9999->configureDriver(msgDeviceInitReq);
251+
drivers.push_back(_apds9999);
252+
WS_DEBUG_PRINTLN("APDS9999 Initialized Successfully!");
242253
} else if (strcmp("as5600", msgDeviceInitReq->i2c_device_name) == 0) {
243254
_as5600 = new WipperSnapper_I2C_Driver_AS5600(this->_i2c, i2cAddress);
244255
if (!_as5600->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "drivers/WipperSnapper_I2C_Driver.h"
2323
#include "drivers/WipperSnapper_I2C_Driver_ADT7410.h"
2424
#include "drivers/WipperSnapper_I2C_Driver_AHTX0.h"
25+
#include "drivers/WipperSnapper_I2C_Driver_APDS9999.h"
2526
#include "drivers/WipperSnapper_I2C_Driver_AS5600.h"
2627
#include "drivers/WipperSnapper_I2C_Driver_BH1750.h"
2728
#include "drivers/WipperSnapper_I2C_Driver_BME280.h"
@@ -165,6 +166,7 @@ class WipperSnapper_Component_I2C {
165166
_drivers_out; ///< List of i2c output drivers
166167
// Sensor driver objects
167168
WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr;
169+
WipperSnapper_I2C_Driver_APDS9999 *_apds9999 = nullptr;
168170
WipperSnapper_I2C_Driver_AS5600 *_as5600 = nullptr;
169171
WipperSnapper_I2C_Driver_D6T1A *_d6t1a = nullptr;
170172
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_APDS9999.h
3+
*
4+
* Device driver for the APDS-9999 proximity, lux light, and color sensor.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry 2026 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WipperSnapper_I2C_Driver_APDS9999_H
16+
#define WipperSnapper_I2C_Driver_APDS9999_H
17+
18+
#include "WipperSnapper_I2C_Driver.h"
19+
#include <Adafruit_APDS9999.h>
20+
21+
/**************************************************************************/
22+
/*!
23+
@brief Class that provides a driver interface for an APDS-9999 sensor.
24+
*/
25+
/**************************************************************************/
26+
class WipperSnapper_I2C_Driver_APDS9999 : public WipperSnapper_I2C_Driver {
27+
public:
28+
/*******************************************************************************/
29+
/*!
30+
@brief Constructor for an APDS-9999 sensor.
31+
@param i2c
32+
The I2C interface.
33+
@param sensorAddress
34+
The 7-bit I2C address of the sensor.
35+
*/
36+
/*******************************************************************************/
37+
WipperSnapper_I2C_Driver_APDS9999(TwoWire *i2c, uint16_t sensorAddress)
38+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
39+
_i2c = i2c;
40+
_sensorAddress = sensorAddress;
41+
}
42+
43+
/*******************************************************************************/
44+
/*!
45+
@brief Destructor for an APDS-9999 sensor.
46+
*/
47+
/*******************************************************************************/
48+
~WipperSnapper_I2C_Driver_APDS9999() { delete _apds9999; }
49+
50+
/*******************************************************************************/
51+
/*!
52+
@brief Initializes the APDS-9999 sensor and begins I2C.
53+
@returns True if initialized successfully, False otherwise.
54+
*/
55+
/*******************************************************************************/
56+
bool begin() {
57+
_apds9999 = new Adafruit_APDS9999();
58+
if (!_apds9999->begin((uint8_t)_sensorAddress, _i2c))
59+
return false;
60+
61+
// Enable light sensor and proximity sensor
62+
_apds9999->enableLightSensor(true);
63+
_apds9999->enableProximitySensor(true);
64+
65+
// Enable RGB mode for color data
66+
_apds9999->setRGBMode(true);
67+
68+
// Explicitly set light sensor defaults
69+
_apds9999->setLightGain(APDS9999_LIGHT_GAIN_3X);
70+
_apds9999->setLightResolution(APDS9999_LIGHT_RES_18BIT);
71+
_apds9999->setLightMeasRate(APDS9999_LIGHT_RATE_100MS);
72+
73+
// Explicitly set proximity sensor defaults
74+
_apds9999->setProxResolution(APDS9999_PROX_RES_11BIT);
75+
_apds9999->setProxMeasRate(APDS9999_PROX_RATE_100MS);
76+
77+
return true;
78+
}
79+
80+
/*******************************************************************************/
81+
/*!
82+
@brief Reads the APDS-9999 sensor data.
83+
@returns True if the sensor was read successfully, False otherwise.
84+
*/
85+
/*******************************************************************************/
86+
bool ReadSensorData() {
87+
unsigned long now = millis();
88+
// Don't read sensor more than once per second
89+
if (_lastRead != 0 && (now - _lastRead < 1000))
90+
return true;
91+
92+
uint32_t r, g, b, ir;
93+
if (!_apds9999->getRGBIRData(&r, &g, &b, &ir))
94+
return false;
95+
96+
_cachedLight.light = _apds9999->calculateLux(g);
97+
98+
uint16_t prox;
99+
if (!_apds9999->readProximity(&prox))
100+
return false;
101+
102+
_cachedProximity.data[0] = (float)prox;
103+
_lastRead = now;
104+
return true;
105+
}
106+
107+
/*******************************************************************************/
108+
/*!
109+
@brief Gets the APDS-9999's current light reading in lux.
110+
@param lightEvent
111+
Pointer to an Adafruit_Sensor event.
112+
@returns True if the light reading was obtained successfully, False
113+
otherwise.
114+
*/
115+
/*******************************************************************************/
116+
bool getEventLight(sensors_event_t *lightEvent) {
117+
if (!ReadSensorData())
118+
return false;
119+
120+
*lightEvent = _cachedLight;
121+
return true;
122+
}
123+
124+
/*******************************************************************************/
125+
/*!
126+
@brief Gets the APDS-9999's current proximity reading.
127+
@param proximityEvent
128+
Pointer to an Adafruit_Sensor event.
129+
@returns True if the proximity was obtained successfully, False
130+
otherwise.
131+
*/
132+
/*******************************************************************************/
133+
bool getEventProximity(sensors_event_t *proximityEvent) {
134+
if (!ReadSensorData())
135+
return false;
136+
137+
*proximityEvent = _cachedProximity;
138+
return true;
139+
}
140+
141+
protected:
142+
Adafruit_APDS9999 *_apds9999 = nullptr; ///< APDS-9999 driver object
143+
unsigned long _lastRead = 0; ///< Last sensor read time
144+
sensors_event_t _cachedLight = {0}; ///< Cached light reading
145+
sensors_event_t _cachedProximity = {0}; ///< Cached proximity reading
146+
};
147+
148+
#endif // WipperSnapper_I2C_Driver_APDS9999_H

0 commit comments

Comments
 (0)