Skip to content

Commit 246c610

Browse files
authored
Merge pull request #528 from tyeth/add-VL6180X
Initial VL6180X implementation
2 parents 4bf2bbf + a0463d0 commit 246c610

File tree

6 files changed

+167
-20
lines changed

6 files changed

+167
-20
lines changed

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ maintainer=Adafruit <[email protected]>
55
sentence=Arduino application for Adafruit.io WipperSnapper
66
paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
8-
url=https://github.com/adafruit/Adafruit_IO_Arduino
8+
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
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 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
10+
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 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

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ lib_deps =
4949
adafruit/Adafruit TMP117
5050
adafruit/Adafruit TSL2591 Library
5151
adafruit/Adafruit_VL53L0X
52+
adafruit/Adafruit_VL6180X
5253
adafruit/Adafruit PM25 AQI Sensor
5354
adafruit/Adafruit VEML7700 Library
5455
adafruit/Adafruit LC709203F

src/Wippersnapper.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@
4040
#include "Wippersnapper_Boards.h"
4141
#include "components/statusLED/Wippersnapper_StatusLED.h"
4242

43+
#define WS_DEBUG ///< Define to enable debugging to serial terminal
44+
#define WS_PRINTER Serial ///< Where debug messages will be printed
45+
46+
// Define actual debug output functions when necessary.
47+
#ifdef WS_DEBUG
48+
#define WS_DEBUG_PRINT(...) \
49+
{ WS_PRINTER.print(__VA_ARGS__); } ///< Prints debug output.
50+
#define WS_DEBUG_PRINTLN(...) \
51+
{ WS_PRINTER.println(__VA_ARGS__); } ///< Prints line from debug output.
52+
#define WS_DEBUG_PRINTHEX(...) \
53+
{ WS_PRINTER.print(__VA_ARGS__, HEX); } ///< Prints debug output.
54+
#else
55+
#define WS_DEBUG_PRINT(...) \
56+
{} ///< Prints debug output
57+
#define WS_DEBUG_PRINTLN(...) \
58+
{} ///< Prints line from debug output.
59+
#endif
60+
4361
// Wippersnapper components
4462
#include "components/analogIO/Wippersnapper_AnalogIO.h"
4563
#include "components/digitalIO/Wippersnapper_DigitalGPIO.h"
@@ -87,24 +105,6 @@
87105
#define MQTT_TOPIC_PIXELS_BROKER \
88106
"/signals/broker/pixel" ///< Pixels broker->device topic
89107

90-
#define WS_DEBUG ///< Define to enable debugging to serial terminal
91-
#define WS_PRINTER Serial ///< Where debug messages will be printed
92-
93-
// Define actual debug output functions when necessary.
94-
#ifdef WS_DEBUG
95-
#define WS_DEBUG_PRINT(...) \
96-
{ WS_PRINTER.print(__VA_ARGS__); } ///< Prints debug output.
97-
#define WS_DEBUG_PRINTLN(...) \
98-
{ WS_PRINTER.println(__VA_ARGS__); } ///< Prints line from debug output.
99-
#define WS_DEBUG_PRINTHEX(...) \
100-
{ WS_PRINTER.print(__VA_ARGS__, HEX); } ///< Prints debug output.
101-
#else
102-
#define WS_DEBUG_PRINT(...) \
103-
{} ///< Prints debug output
104-
#define WS_DEBUG_PRINTLN(...) \
105-
{} ///< Prints line from debug output.
106-
#endif
107-
108108
/** Defines the Adafruit IO connection status */
109109
typedef enum {
110110
WS_IDLE = 0, // Waiting for connection establishement

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
669669
_vl53l0x->configureDriver(msgDeviceInitReq);
670670
drivers.push_back(_vl53l0x);
671671
WS_DEBUG_PRINTLN("VL53L0X Initialized Successfully!");
672+
} else if (strcmp("vl6180x", msgDeviceInitReq->i2c_device_name) == 0) {
673+
_vl6180x = new WipperSnapper_I2C_Driver_VL6180X(this->_i2c, i2cAddress);
674+
if (!_vl6180x->begin()) {
675+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize VL6180X!");
676+
_busStatusResponse =
677+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
678+
return false;
679+
}
680+
_vl6180x->configureDriver(msgDeviceInitReq);
681+
drivers.push_back(_vl6180x);
682+
WS_DEBUG_PRINTLN("VL6180X Initialized Successfully!");
672683
} else if (strcmp("max17048", msgDeviceInitReq->i2c_device_name) == 0) {
673684
_max17048 = new WipperSnapper_I2C_Driver_MAX17048(this->_i2c, i2cAddress);
674685
if (!_max17048->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "drivers/WipperSnapper_I2C_Driver_VCNL4040.h"
6363
#include "drivers/WipperSnapper_I2C_Driver_VEML7700.h"
6464
#include "drivers/WipperSnapper_I2C_Driver_VL53L0X.h"
65+
#include "drivers/WipperSnapper_I2C_Driver_VL6180X.h"
6566

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

@@ -150,6 +151,7 @@ class WipperSnapper_Component_I2C {
150151
WipperSnapper_I2C_Driver_LPS3XHW *_lps3xhw = nullptr;
151152
WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor *_ss = nullptr;
152153
WipperSnapper_I2C_Driver_VL53L0X *_vl53l0x = nullptr;
154+
WipperSnapper_I2C_Driver_VL6180X *_vl6180x = nullptr;
153155
WipperSnapper_I2C_Driver_MAX17048 *_max17048 = nullptr;
154156
WipperSnapper_I2C_Driver_ADT7410 *_adt7410 = nullptr;
155157
};
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_VL6180X.h
3+
*
4+
* Device driver for the VL6180X ToF 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 2024 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WipperSnapper_I2C_Driver_VL6180X_H
16+
#define WipperSnapper_I2C_Driver_VL6180X_H
17+
18+
#include "WipperSnapper_I2C_Driver.h"
19+
#include "Wippersnapper.h"
20+
#include <Adafruit_VL6180X.h>
21+
22+
/**************************************************************************/
23+
/*!
24+
@brief Class that provides a driver interface for a VL6180X sensor.
25+
*/
26+
/**************************************************************************/
27+
class WipperSnapper_I2C_Driver_VL6180X : public WipperSnapper_I2C_Driver {
28+
public:
29+
/*******************************************************************************/
30+
/*!
31+
@brief Constructor for a VL6180X sensor.
32+
@param i2c
33+
The I2C interface.
34+
@param sensorAddress
35+
7-bit device address.
36+
*/
37+
/*******************************************************************************/
38+
WipperSnapper_I2C_Driver_VL6180X(TwoWire *i2c, uint16_t sensorAddress)
39+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
40+
_i2c = i2c;
41+
_sensorAddress = sensorAddress;
42+
}
43+
44+
/*******************************************************************************/
45+
/*!
46+
@brief Destructor for an VL6180X sensor.
47+
*/
48+
/*******************************************************************************/
49+
~WipperSnapper_I2C_Driver_VL6180X() {
50+
// Called when a VL6180X component is deleted.
51+
delete _vl6180x;
52+
}
53+
54+
/*******************************************************************************/
55+
/*!
56+
@brief Initializes the VL6180X sensor and begins I2C.
57+
@returns True if initialized successfully, False otherwise.
58+
*/
59+
/*******************************************************************************/
60+
bool begin() {
61+
_vl6180x = new Adafruit_VL6180X(_sensorAddress);
62+
return _vl6180x->begin(_i2c);
63+
}
64+
65+
/*******************************************************************************/
66+
/*!
67+
@brief Gets the VL6180X's current proximity.
68+
@param proximityEvent
69+
Pointer to an Adafruit_Sensor event.
70+
@returns True if the proximity was obtained successfully, False
71+
otherwise.
72+
*/
73+
/*******************************************************************************/
74+
bool getEventProximity(sensors_event_t *proximityEvent) {
75+
uint8_t range = _vl6180x->readRange();
76+
uint8_t status = _vl6180x->readRangeStatus();
77+
78+
if (status != VL6180X_ERROR_NONE) {
79+
if ((status >= VL6180X_ERROR_SYSERR_1) &&
80+
(status <= VL6180X_ERROR_SYSERR_5)) {
81+
WS_DEBUG_PRINTLN("VL6180X: System error");
82+
} else if (status == VL6180X_ERROR_ECEFAIL) {
83+
WS_DEBUG_PRINTLN("VL6180X: ECE failure");
84+
} else if (status == VL6180X_ERROR_NOCONVERGE) {
85+
WS_DEBUG_PRINTLN("VL6180X: No convergence");
86+
} else if (status == VL6180X_ERROR_RANGEIGNORE) {
87+
WS_DEBUG_PRINTLN("VL6180X: Ignoring range");
88+
} else if (status == VL6180X_ERROR_SNR) {
89+
WS_DEBUG_PRINTLN("VL6180X: Signal/Noise error");
90+
} else if (status == VL6180X_ERROR_RAWUFLOW) {
91+
WS_DEBUG_PRINTLN("VL6180X: Raw reading underflow");
92+
} else if (status == VL6180X_ERROR_RAWOFLOW) {
93+
WS_DEBUG_PRINTLN("VL6180X: Raw reading overflow");
94+
} else if (status == VL6180X_ERROR_RANGEUFLOW) {
95+
WS_DEBUG_PRINTLN("VL6180X: Range reading underflow");
96+
} else if (status == VL6180X_ERROR_RANGEOFLOW) {
97+
WS_DEBUG_PRINTLN("VL6180X: Range reading overflow");
98+
}
99+
return false;
100+
}
101+
102+
if (range <= 0 || range > 150) {
103+
return false;
104+
}
105+
proximityEvent->data[0] = range;
106+
return true;
107+
}
108+
109+
/*******************************************************************************/
110+
/*!
111+
@brief Performs a light sensor reading.
112+
@param lightEvent
113+
Light sensor reading, in lux.
114+
@returns True if the sensor event was obtained successfully, False
115+
otherwise.
116+
*/
117+
/*******************************************************************************/
118+
bool getEventLight(sensors_event_t *lightEvent) {
119+
// TODO: Update when I2C Sensor Properties allow setting custom Gain, etc.
120+
float notRealLux = _vl6180x->readLux(VL6180X_ALS_GAIN_5);
121+
// Gain_5 results in max 41.6klux with cover glass - See 2.10.3 in datasheet
122+
if (notRealLux < 0 || notRealLux > 41600) {
123+
return false;
124+
}
125+
lightEvent->light = notRealLux;
126+
return true;
127+
}
128+
129+
protected:
130+
Adafruit_VL6180X *_vl6180x; ///< Pointer to VL6180X temperature sensor object
131+
};
132+
133+
#endif // WipperSnapper_I2C_Driver_VL6180X

0 commit comments

Comments
 (0)