1+ /* !
2+ * @file drvVncl4200.h
3+ *
4+ * Device driver for the VCNL4200 light + proximity 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 2025 for Adafruit Industries.
11+ *
12+ * MIT license, all text here must be included in any redistribution.
13+ *
14+ */
15+ #ifndef DRV_VCNL4200_H
16+ #define DRV_VCNL4200_H
17+
18+ #include " drvBase.h"
19+ #include < Adafruit_VCNL4200.h>
20+
21+ /* *************************************************************************/
22+ /* !
23+ @brief Class that provides a driver interface for a VCNL4200 sensor.
24+ */
25+ /* *************************************************************************/
26+ class drvVncl4200 : public drvBase {
27+ public:
28+ /* ******************************************************************************/
29+ /* !
30+ @brief Constructor for a VCNL4200 sensor.
31+ @param i2c
32+ The I2C interface.
33+ @param sensorAddress
34+ 7-bit device address.
35+ @param mux_channel
36+ The I2C multiplexer channel.
37+ @param driver_name
38+ The name of the driver.
39+ */
40+ /* ******************************************************************************/
41+ drvVncl4200 (TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
42+ const char *driver_name)
43+ : drvBase(i2c, sensorAddress, mux_channel, driver_name) {
44+ // Initialization handled by drvBase constructor
45+ }
46+
47+ /* ******************************************************************************/
48+ /* !
49+ @brief Destructor for an VCNL4200 sensor.
50+ */
51+ /* ******************************************************************************/
52+ ~drvVncl4200 () { delete _vcnl4200; }
53+
54+ /* ******************************************************************************/
55+ /* !
56+ @brief Initializes the VCNL4200 sensor and begins I2C.
57+ @returns True if initialized successfully, False otherwise.
58+ */
59+ /* ******************************************************************************/
60+ bool begin () {
61+ _vcnl4200 = new Adafruit_VCNL4200 ();
62+ bool status = false ;
63+ // Attempt to initialize and configure VCNL4200
64+ if (!_vcnl4200->begin (_address, _i2c)) {
65+ return false ;
66+ }
67+ status = _vcnl4200->setALSshutdown (false );
68+ status &= _vcnl4200->setProxShutdown (false );
69+ status &= _vcnl4200->setProxHD (true ); // 16bit instead of 12bit
70+ status &= _vcnl4200->setALSIntegrationTime (VCNL4200_ALS_IT_400MS);
71+ status &= _vcnl4200->setProxDuty (VCNL4200_PS_DUTY_1_160);
72+ status &= _vcnl4200->setProxLEDCurrent (VCNL4200_LED_I_200MA);
73+ status &= _vcnl4200->setProxIntegrationTime (VCNL4200_PS_IT_9T);
74+ return status;
75+ }
76+
77+ /* ******************************************************************************/
78+ /* !
79+ @brief Performs a light sensor read using the Adafruit
80+ Unified Sensor API.
81+ @param lightEvent
82+ Light sensor reading, in lux.
83+ @returns True if the sensor event was obtained successfully, False
84+ otherwise.
85+ */
86+ /* ******************************************************************************/
87+ bool getEventLight (sensors_event_t *lightEvent) {
88+ // Get sensor event populated in lux via AUTO integration and gain
89+ lightEvent->light = _vcnl4200->readALSdata ();
90+ return true ;
91+ }
92+
93+ /* ******************************************************************************/
94+ /* !
95+ @brief Reads the VCNL4200's proximity value into an event (no unit).
96+ @param proximityEvent
97+ Pointer to an Adafruit_Sensor event.
98+ @returns True if the proximity was obtained successfully, False
99+ otherwise.
100+ */
101+ /* ******************************************************************************/
102+ bool getEventProximity (sensors_event_t *proximityEvent) {
103+ proximityEvent->data [0 ] = (float )_vcnl4200->readProxData ();
104+ return true ;
105+ }
106+
107+ protected:
108+ Adafruit_VCNL4200 *_vcnl4200; // /< Pointer to VCNL4200 light sensor object
109+ };
110+
111+ #endif // DRV_VCNL4200_H
0 commit comments