Skip to content

Commit

Permalink
Add VCNL4200
Browse files Browse the repository at this point in the history
  • Loading branch information
tyeth committed Dec 19, 2024
1 parent 16bc28a commit 5bfd4e2
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
architectures=*
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, 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 VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, 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
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, 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 VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, 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
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ lib_deps =
adafruit/Adafruit Si7021 Library
adafruit/Adafruit VCNL4020 Library
adafruit/Adafruit VCNL4040
adafruit/Adafruit VCNL4200 Library
adafruit/Adafruit MCP3421
adafruit/Adafruit MCP9808 Library
adafruit/Adafruit MCP9600 Library
Expand Down
11 changes: 11 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_vcnl4040->configureDriver(msgDeviceInitReq);
drivers.push_back(_vcnl4040);
WS_DEBUG_PRINTLN("VCNL4040 Initialized Successfully!");
} else if (strcmp("vcnl4200", msgDeviceInitReq->i2c_device_name) == 0) {
_vcnl4200 = new WipperSnapper_I2C_Driver_VCNL4200(this->_i2c, i2cAddress);
if (!_vcnl4200->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize VCNL4200!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_vcnl4200->configureDriver(msgDeviceInitReq);
drivers.push_back(_vcnl4200);
WS_DEBUG_PRINTLN("VCNL4200 Initialized Successfully!");
} else if (strcmp("veml7700", msgDeviceInitReq->i2c_device_name) == 0) {
_veml7700 = new WipperSnapper_I2C_Driver_VEML7700(this->_i2c, i2cAddress);
if (!_veml7700->begin()) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "drivers/WipperSnapper_I2C_Driver_TSL2591.h"
#include "drivers/WipperSnapper_I2C_Driver_VCNL4020.h"
#include "drivers/WipperSnapper_I2C_Driver_VCNL4040.h"
#include "drivers/WipperSnapper_I2C_Driver_VCNL4200.h"
#include "drivers/WipperSnapper_I2C_Driver_VEML7700.h"
#include "drivers/WipperSnapper_I2C_Driver_VL53L0X.h"
#include "drivers/WipperSnapper_I2C_Driver_VL53L1X.h"
Expand Down Expand Up @@ -159,6 +160,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_TSL2591 *_tsl2591 = nullptr;
WipperSnapper_I2C_Driver_VCNL4020 *_vcnl4020 = nullptr;
WipperSnapper_I2C_Driver_VCNL4040 *_vcnl4040 = nullptr;
WipperSnapper_I2C_Driver_VCNL4200 *_vcnl4200 = nullptr;
WipperSnapper_I2C_Driver_VEML7700 *_veml7700 = nullptr;
WipperSnapper_I2C_Driver_SCD4X *_scd40 = nullptr;
WipperSnapper_I2C_Driver_SEN5X *_sen5x = nullptr;
Expand Down
106 changes: 106 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_VCNL4200.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*!
* @file WipperSnapper_I2C_Driver_VCNL4200.h
*
* Device driver for the VCNL4200 light + proximity sensor.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2024 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_VCNL4200_H
#define WipperSnapper_I2C_Driver_VCNL4200_H

#include "WipperSnapper_I2C_Driver.h"
#include <Adafruit_VCNL4200.h>

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a VCNL4200 sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_VCNL4200 : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for a VCNL4200 sensor.
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_VCNL4200(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an VCNL4200 sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_VCNL4200() { delete _vcnl4200; }

/*******************************************************************************/
/*!
@brief Initializes the VCNL4200 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_vcnl4200 = new Adafruit_VCNL4200();
bool status = false;
// Attempt to initialize and configure VCNL4200
if (!_vcnl4200->begin(_sensorAddress, _i2c)) {
return false;
}
status = _vcnl4200->setALSshutdown(false);
status &= _vcnl4200->setProxShutdown(false);
status &= _vcnl4200->setProxHD(true); // 16bit instead of 12bit
status &= _vcnl4200->setALSIntegrationTime(VCNL4200_ALS_IT_400MS);
status &= _vcnl4200->setProxLEDCurrent(VCNL4200_LED_I_200MA);
status &= _vcnl4200->setProxIntegrationTime(VCNL4200_PS_IT_9T);
return status;
}

/*******************************************************************************/
/*!
@brief Performs a light sensor read using the Adafruit
Unified Sensor API.
@param lightEvent
Light sensor reading, in lux.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventLight(sensors_event_t *lightEvent) {
// Get sensor event populated in lux via AUTO integration and gain
lightEvent->light = _vcnl4200->readALSdata();
return true;
}

/*******************************************************************************/
/*!
@brief Reads the VCNL4200's proximity value into an event (no unit).
@param proximityEvent
Pointer to an Adafruit_Sensor event.
@returns True if the proximity was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventProximity(sensors_event_t *proximityEvent) {
proximityEvent->data[0] = (float)_vcnl4200->readProxData();
return true;
}

protected:
Adafruit_VCNL4200 *_vcnl4200; ///< Pointer to VCNL4200 light sensor object
};

#endif // WipperSnapper_I2C_Driver_VCNL4200

0 comments on commit 5bfd4e2

Please sign in to comment.