Skip to content

Commit 1442a52

Browse files
Copilottyeth
andcommitted
Add APDS9999 I2C driver for proximity, lux light, and color sensor
Co-authored-by: tyeth <6692083+tyeth@users.noreply.github.com> Agent-Logs-Url: https://github.com/adafruit/Adafruit_Wippersnapper_Arduino/sessions/80f4d06b-e0b8-4f72-ab45-5384676defbf
1 parent e80b041 commit 1442a52

File tree

5 files changed

+163
-1
lines changed

5 files changed

+163
-1
lines changed

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ 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 =
244+
new WipperSnapper_I2C_Driver_APDS9999(this->_i2c, i2cAddress);
245+
if (!_apds9999->begin()) {
246+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize APDS9999!");
247+
_busStatusResponse =
248+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
249+
return false;
250+
}
251+
_apds9999->configureDriver(msgDeviceInitReq);
252+
drivers.push_back(_apds9999);
253+
WS_DEBUG_PRINTLN("APDS9999 Initialized Successfully!");
242254
} else if (strcmp("as5600", msgDeviceInitReq->i2c_device_name) == 0) {
243255
_as5600 = new WipperSnapper_I2C_Driver_AS5600(this->_i2c, i2cAddress);
244256
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"
@@ -164,6 +165,7 @@ class WipperSnapper_Component_I2C {
164165
_drivers_out; ///< List of i2c output drivers
165166
// Sensor driver objects
166167
WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr;
168+
WipperSnapper_I2C_Driver_APDS9999 *_apds9999 = nullptr;
167169
WipperSnapper_I2C_Driver_AS5600 *_as5600 = nullptr;
168170
WipperSnapper_I2C_Driver_D6T1A *_d6t1a = nullptr;
169171
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_APDS9999.h
3+
*
4+
* Device driver for the APDS9999 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) Adafruit Industries 2025.
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 APDS9999 sensor.
24+
*/
25+
/**************************************************************************/
26+
class WipperSnapper_I2C_Driver_APDS9999 : public WipperSnapper_I2C_Driver {
27+
public:
28+
/*******************************************************************************/
29+
/*!
30+
@brief Constructor for an APDS9999 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 APDS9999 sensor.
46+
*/
47+
/*******************************************************************************/
48+
~WipperSnapper_I2C_Driver_APDS9999() { delete _apds9999; }
49+
50+
/*******************************************************************************/
51+
/*!
52+
@brief Initializes the APDS9999 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 APDS9999 sensor data.
83+
@returns True if the sensor was read successfully, False otherwise.
84+
*/
85+
/*******************************************************************************/
86+
bool ReadSensorData() {
87+
// Don't read sensor more than once per second
88+
if (_lastRead != 0 && (millis() - _lastRead < 1000))
89+
return true;
90+
91+
uint32_t r, g, b, ir;
92+
if (!_apds9999->getRGBIRData(&r, &g, &b, &ir))
93+
return false;
94+
95+
_cachedLight.light = _apds9999->calculateLux(g);
96+
97+
uint16_t prox;
98+
if (!_apds9999->readProximity(&prox))
99+
return false;
100+
101+
_cachedProximity.data[0] = (float)prox;
102+
_lastRead = millis();
103+
return true;
104+
}
105+
106+
/*******************************************************************************/
107+
/*!
108+
@brief Gets the APDS9999's current light reading in lux.
109+
@param lightEvent
110+
Pointer to an Adafruit_Sensor event.
111+
@returns True if the light reading was obtained successfully, False
112+
otherwise.
113+
*/
114+
/*******************************************************************************/
115+
bool getEventLight(sensors_event_t *lightEvent) {
116+
if (!ReadSensorData())
117+
return false;
118+
119+
*lightEvent = _cachedLight;
120+
return true;
121+
}
122+
123+
/*******************************************************************************/
124+
/*!
125+
@brief Gets the APDS9999's current proximity reading.
126+
@param proximityEvent
127+
Pointer to an Adafruit_Sensor event.
128+
@returns True if the proximity was obtained successfully, False
129+
otherwise.
130+
*/
131+
/*******************************************************************************/
132+
bool getEventProximity(sensors_event_t *proximityEvent) {
133+
if (!ReadSensorData())
134+
return false;
135+
136+
*proximityEvent = _cachedProximity;
137+
return true;
138+
}
139+
140+
protected:
141+
Adafruit_APDS9999 *_apds9999 = nullptr; ///< APDS9999 driver object
142+
unsigned long _lastRead = 0; ///< Last sensor read time
143+
sensors_event_t _cachedLight = {0}; ///< Cached light reading
144+
sensors_event_t _cachedProximity = {0}; ///< Cached proximity reading
145+
};
146+
147+
#endif // WipperSnapper_I2C_Driver_APDS9999_H

0 commit comments

Comments
 (0)