Skip to content

Commit

Permalink
Initial VL6180X implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tyeth committed Jan 8, 2024
1 parent af60e82 commit 9009bf6
Show file tree
Hide file tree
Showing 5 changed files with 149 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_IO_Arduino
architectures=*
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, 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 PM25 AQI Sensor, 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 INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, 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_VL6180X, Adafruit PM25 AQI Sensor, 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 @@ -47,6 +47,7 @@ lib_deps =
adafruit/Adafruit TMP117
adafruit/Adafruit TSL2591 Library
adafruit/Adafruit_VL53L0X
adafruit/Adafruit_VL6180X
adafruit/Adafruit PM25 AQI Sensor
adafruit/Adafruit VEML7700 Library
adafruit/Adafruit LC709203F
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 @@ -647,6 +647,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_vl53l0x->configureDriver(msgDeviceInitReq);
drivers.push_back(_vl53l0x);
WS_DEBUG_PRINTLN("VL53L0X Initialized Successfully!");
} else if (strcmp("vl6180x", msgDeviceInitReq->i2c_device_name) == 0) {
_vl6180x = new WipperSnapper_I2C_Driver_VL6180X(this->_i2c, i2cAddress);
if (!_vl6180x->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize VL6180X!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_vl6180x->configureDriver(msgDeviceInitReq);
drivers.push_back(_vl6180x);
WS_DEBUG_PRINTLN("VL6180X Initialized Successfully!");
} else if (strcmp("max17048", msgDeviceInitReq->i2c_device_name) == 0) {
_max17048 = new WipperSnapper_I2C_Driver_MAX17048(this->_i2c, i2cAddress);
if (!_max17048->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 @@ -60,6 +60,7 @@
#include "drivers/WipperSnapper_I2C_Driver_TSL2591.h"
#include "drivers/WipperSnapper_I2C_Driver_VEML7700.h"
#include "drivers/WipperSnapper_I2C_Driver_VL53L0X.h"
#include "drivers/WipperSnapper_I2C_Driver_VL6180X.h"

#define I2C_TIMEOUT_MS 50 ///< Default I2C timeout, in milliseconds.

Expand Down Expand Up @@ -146,6 +147,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_LPS3XHW *_lps3xhw = nullptr;
WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor *_ss = nullptr;
WipperSnapper_I2C_Driver_VL53L0X *_vl53l0x = nullptr;
WipperSnapper_I2C_Driver_VL6180X *_vl6180x = nullptr;
WipperSnapper_I2C_Driver_MAX17048 *_max17048 = nullptr;
WipperSnapper_I2C_Driver_ADT7410 *_adt7410 = nullptr;
};
Expand Down
134 changes: 134 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_VL6180X.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*!
* @file WipperSnapper_I2C_Driver_VL6180X.h
*
* Device driver for the VL6180X ToF 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_VL6180X_H
#define WipperSnapper_I2C_Driver_VL6180X_H

#include "WipperSnapper_I2C_Driver.h"
#include "Wippersnapper.h"
#include <Adafruit_VL6180X.h>


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

/*******************************************************************************/
/*!
@brief Destructor for an VL6180X sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_VL6180X() {
// Called when a VL6180X component is deleted.
delete _vl6180x;
}

/*******************************************************************************/
/*!
@brief Initializes the VL6180X sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_vl6180x = new Adafruit_VL6180X(_sensorAddress);
return _vl6180x->begin(_i2c);
}

/*******************************************************************************/
/*!
@brief Gets the VL6180X's current proximity.
@param proximityEvent
Pointer to an Adafruit_Sensor event.
@returns True if the proximity was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventProximity(sensors_event_t *proximityEvent) {
uint8_t range = _vl6180x->readRange();
uint8_t status = _vl6180x->readRangeStatus();

if (status != VL6180X_ERROR_NONE) {
if ((status >= VL6180X_ERROR_SYSERR_1) &&
(status <= VL6180X_ERROR_SYSERR_5)) {
WS_DEBUG_PRINTLN("VL6180X: System error");
} else if (status == VL6180X_ERROR_ECEFAIL) {
WS_DEBUG_PRINTLN("VL6180X: ECE failure");
} else if (status == VL6180X_ERROR_NOCONVERGE) {
WS_DEBUG_PRINTLN("VL6180X: No convergence");
} else if (status == VL6180X_ERROR_RANGEIGNORE) {
WS_DEBUG_PRINTLN("VL6180X: Ignoring range");
} else if (status == VL6180X_ERROR_SNR) {
WS_DEBUG_PRINTLN("VL6180X: Signal/Noise error");
} else if (status == VL6180X_ERROR_RAWUFLOW) {
WS_DEBUG_PRINTLN("VL6180X: Raw reading underflow");
} else if (status == VL6180X_ERROR_RAWOFLOW) {
WS_DEBUG_PRINTLN("VL6180X: Raw reading overflow");
} else if (status == VL6180X_ERROR_RANGEUFLOW) {
WS_DEBUG_PRINTLN("VL6180X: Range reading underflow");
} else if (status == VL6180X_ERROR_RANGEOFLOW) {
WS_DEBUG_PRINTLN("VL6180X: Range reading overflow");
}
return false;
}

if (range <= 0 || range > 150) {
return false;
}
proximityEvent->data[0] = range;
return true;
}

/*******************************************************************************/
/*!
@brief Performs a light sensor reading.
@param lightEvent
Light sensor reading, in lux.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventLight(sensors_event_t *lightEvent) {
//TODO: Update when I2C Sensor Properties allow setting custom Gain, etc.
float notRealLux = _vl6180x->readLux(VL6180X_ALS_GAIN_5);
// Gain_5 results in max 41.6klux with cover glass - See 2.10.3 in datasheet
if (notRealLux < 0 || notRealLux > 41600) {
return false;
}
lightEvent->light = notRealLux;
return true;
}

protected:
Adafruit_VL6180X *_vl6180x; ///< Pointer to VL6180X temperature sensor object
};

#endif // WipperSnapper_I2C_Driver_VL6180X

0 comments on commit 9009bf6

Please sign in to comment.