Skip to content

Commit df388f2

Browse files
authored
Merge pull request #564 from tyeth/add-VL53L4CX
Refactored commit - VL53L4CX default settings
2 parents c3c475c + cbc36ca commit df388f2

File tree

5 files changed

+221
-1
lines changed

5 files changed

+221
-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=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, 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 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, 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
10+
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, 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 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

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ lib_deps =
5353
adafruit/Adafruit_VL53L0X
5454
adafruit/Adafruit VL53L1X
5555
stm32duino/STM32duino VL53L4CD
56+
stm32duino/STM32duino VL53L4CX
5657
adafruit/Adafruit_VL6180X
5758
adafruit/Adafruit PM25 AQI Sensor
5859
adafruit/Adafruit VEML7700 Library

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
716716
_vl53l4cd->configureDriver(msgDeviceInitReq);
717717
drivers.push_back(_vl53l4cd);
718718
WS_DEBUG_PRINTLN("VL53L4CD Initialized Successfully!");
719+
} else if (strcmp("vl53l4cx", msgDeviceInitReq->i2c_device_name) == 0) {
720+
_vl53l4cx = new WipperSnapper_I2C_Driver_VL53L4CX(this->_i2c, i2cAddress);
721+
if (!_vl53l4cx->begin()) {
722+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize VL53L4CX!");
723+
_busStatusResponse =
724+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
725+
return false;
726+
}
727+
_vl53l4cx->configureDriver(msgDeviceInitReq);
728+
drivers.push_back(_vl53l4cx);
729+
WS_DEBUG_PRINTLN("VL53L4CX Initialized Successfully!");
719730
} else if (strcmp("vl6180x", msgDeviceInitReq->i2c_device_name) == 0) {
720731
_vl6180x = new WipperSnapper_I2C_Driver_VL6180X(this->_i2c, i2cAddress);
721732
if (!_vl6180x->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "drivers/WipperSnapper_I2C_Driver_VL53L0X.h"
6767
#include "drivers/WipperSnapper_I2C_Driver_VL53L1X.h"
6868
#include "drivers/WipperSnapper_I2C_Driver_VL53L4CD.h"
69+
#include "drivers/WipperSnapper_I2C_Driver_VL53L4CX.h"
6970
#include "drivers/WipperSnapper_I2C_Driver_VL6180X.h"
7071

7172
#define I2C_TIMEOUT_MS 50 ///< Default I2C timeout, in milliseconds.
@@ -159,6 +160,7 @@ class WipperSnapper_Component_I2C {
159160
WipperSnapper_I2C_Driver_VL53L0X *_vl53l0x = nullptr;
160161
WipperSnapper_I2C_Driver_VL53L1X *_vl53l1x = nullptr;
161162
WipperSnapper_I2C_Driver_VL53L4CD *_vl53l4cd = nullptr;
163+
WipperSnapper_I2C_Driver_VL53L4CX *_vl53l4cx = nullptr;
162164
WipperSnapper_I2C_Driver_VL6180X *_vl6180x = nullptr;
163165
WipperSnapper_I2C_Driver_MAX17048 *_max17048 = nullptr;
164166
WipperSnapper_I2C_Driver_ADT7410 *_adt7410 = nullptr;
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_VL53L4CX.h
3+
*
4+
* Device driver for the VL53L4CX 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) 2024 Tyeth Gundry for Adafruit Industries
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WipperSnapper_I2C_Driver_VL53L4CX_H
16+
#define WipperSnapper_I2C_Driver_VL53L4CX_H
17+
18+
#include "WipperSnapper_I2C_Driver.h"
19+
#include <vl53l4cx_class.h>
20+
#include <vl53l4cx_def.h>
21+
22+
#define VL53_SHUTDOWN_PIN -1 ///< Shutdown pin for VL53L4CX sensor
23+
#define VL53_READING_DELAY 250 ///< Delay for reading data attempts
24+
#define VL53_TIMING_BUDGET_NS 200000 ///< Timing budget for VL53L4CX sensor
25+
26+
/**************************************************************************/
27+
/*!
28+
@brief Class that provides a driver interface for a VL53L4CX sensor.
29+
*/
30+
/**************************************************************************/
31+
class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
32+
public:
33+
/*******************************************************************************/
34+
/*!
35+
@brief Constructor for a VL53L4CX sensor.
36+
@param i2c
37+
The I2C interface.
38+
@param sensorAddress
39+
7-bit device address.
40+
*/
41+
/*******************************************************************************/
42+
WipperSnapper_I2C_Driver_VL53L4CX(TwoWire *i2c, uint16_t sensorAddress)
43+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
44+
_i2c = i2c;
45+
_sensorAddress = sensorAddress;
46+
}
47+
48+
/*******************************************************************************/
49+
/*!
50+
@brief Destructor for an VL53L4CX sensor.
51+
*/
52+
/*******************************************************************************/
53+
~WipperSnapper_I2C_Driver_VL53L4CX() {
54+
// Called when a VL53L4CX component is deleted.
55+
delete _VL53L4CX;
56+
}
57+
58+
/*******************************************************************************/
59+
/*!
60+
@brief Initializes the VL53L4CX sensor and begins I2C.
61+
@returns True if initialized successfully, False otherwise.
62+
*/
63+
/*******************************************************************************/
64+
bool begin() {
65+
_VL53L4CX = new VL53L4CX(_i2c, VL53_SHUTDOWN_PIN);
66+
67+
if (_VL53L4CX->InitSensor((uint8_t)_sensorAddress) != VL53L4CX_ERROR_NONE) {
68+
WS_DEBUG_PRINTLN("Failed to initialize VL53L4CX sensor!");
69+
return false;
70+
}
71+
72+
if (_VL53L4CX->VL53L4CX_SetDistanceMode(VL53L4CX_DISTANCEMODE_LONG) !=
73+
VL53L4CX_ERROR_NONE) {
74+
WS_DEBUG_PRINTLN("Failed to set VL53L4CX distance mode to long!");
75+
return false;
76+
}
77+
78+
// Set 200ms measurement time, the possible TimingBudget is 8-200ms
79+
if (_VL53L4CX->VL53L4CX_SetMeasurementTimingBudgetMicroSeconds(
80+
VL53_TIMING_BUDGET_NS) != VL53L4CX_ERROR_NONE) {
81+
WS_DEBUG_PRINTLN("Failed to set VL53L4CX timing budget!");
82+
return false;
83+
}
84+
85+
if (_VL53L4CX->VL53L4CX_StartMeasurement() != VL53L4CX_ERROR_NONE) {
86+
WS_DEBUG_PRINTLN("Failed to start VL53L4CX ranging!");
87+
return false;
88+
}
89+
return true;
90+
}
91+
92+
/*******************************************************************************/
93+
/*!
94+
@brief Gets the VL53L4CX's current proximity for first object if found.
95+
@param proximityEvent
96+
Pointer to an Adafruit_Sensor event.
97+
@returns True if the proximity was obtained successfully, False
98+
otherwise.
99+
*/
100+
/*******************************************************************************/
101+
bool getEventProximity(sensors_event_t *proximityEvent) {
102+
return getProximity(proximityEvent, 0);
103+
}
104+
105+
/*******************************************************************************/
106+
/*!
107+
@brief Gets the VL53L4CX's current proximity for second object if
108+
found.
109+
@param proximityEvent
110+
Pointer to an Adafruit_Sensor event.
111+
@returns True if the proximity was obtained successfully, False
112+
otherwise.
113+
*/
114+
/*******************************************************************************/
115+
bool getEventRaw(sensors_event_t *proximityEvent) {
116+
return getProximity(proximityEvent, 1);
117+
}
118+
119+
/*******************************************************************************/
120+
/*!
121+
@brief Gets the VL53L4CX's current proximity (first or second object).
122+
@param proximityEvent
123+
Pointer to an Adafruit_Sensor event.
124+
@param whichObject
125+
Index of the proximity object to get (0, or 1 for second
126+
object).
127+
@returns True if the proximity was obtained successfully, False
128+
otherwise.
129+
*/
130+
/*******************************************************************************/
131+
bool getProximity(sensors_event_t *proximityEvent, int whichObject = 0) {
132+
VL53L4CX_MultiRangingData_t MultiRangingData;
133+
VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
134+
uint8_t NewDataReady = 0;
135+
int status;
136+
137+
// Start fresh reading, seemed to be accepting stale value
138+
status = _VL53L4CX->VL53L4CX_ClearInterruptAndStartMeasurement();
139+
if (status != VL53L4CX_ERROR_NONE) {
140+
WS_DEBUG_PRINT(
141+
"VL53L4CX Error clearing interrupt and starting measurement: ");
142+
WS_DEBUG_PRINTLN(status);
143+
return false;
144+
}
145+
146+
// Wait for data read period then update data ready status
147+
WS_DEBUG_PRINT("Waiting for VL53L4CX data ready...");
148+
delay(VL53_READING_DELAY);
149+
status = _VL53L4CX->VL53L4CX_GetMeasurementDataReady(&NewDataReady);
150+
151+
if ((status != VL53L4CX_ERROR_NONE) || (NewDataReady == 0)) {
152+
// error or no data ready
153+
WS_DEBUG_PRINT("VL53L4CX Error checking for data ready: ");
154+
WS_DEBUG_PRINTLN(status);
155+
return false;
156+
}
157+
158+
// get data - still to verify which of one or two objects found
159+
status = _VL53L4CX->VL53L4CX_GetMultiRangingData(pMultiRangingData);
160+
if (status != VL53L4CX_ERROR_NONE) {
161+
WS_DEBUG_PRINT("VL53L4CX Error getting multi ranging data: ");
162+
WS_DEBUG_PRINTLN(status);
163+
return false;
164+
}
165+
166+
// whichObject: 0-based index, return NaN(Object not found) if too few found
167+
if (pMultiRangingData->NumberOfObjectsFound - 1 < whichObject) {
168+
WS_DEBUG_PRINT("Object not found at index #");
169+
WS_DEBUG_PRINT(whichObject + 1); // human readable 1-based index
170+
WS_DEBUG_PRINTLN(", returning NaN");
171+
proximityEvent->data[0] = NAN;
172+
return true;
173+
}
174+
175+
// RESULT: take the first or second detected object from ranging data,
176+
// if valid then set event data in proximityEvent or return false
177+
return updateDataPointIfValid(pMultiRangingData->RangeData[whichObject],
178+
proximityEvent);
179+
}
180+
181+
/*******************************************************************************/
182+
/*!
183+
@brief Checks the VL53L4CX's proximity result and sets event value.
184+
@param rangingData
185+
The ranging data to check.
186+
@param proximityEvent
187+
Pointer to an Adafruit_Sensor event.
188+
@returns True if the proximity was obtained successfully, False
189+
otherwise.
190+
*/
191+
/*******************************************************************************/
192+
bool updateDataPointIfValid(VL53L4CX_TargetRangeData_t rangingData,
193+
sensors_event_t *proximityEvent) {
194+
if (rangingData.RangeStatus == VL53L4CX_RANGESTATUS_RANGE_VALID) {
195+
int16_t mm = rangingData.RangeMilliMeter;
196+
proximityEvent->data[0] = (float)mm;
197+
return true;
198+
}
199+
return false;
200+
}
201+
202+
protected:
203+
VL53L4CX *_VL53L4CX; ///< Pointer to VL53L4CX temperature sensor object
204+
};
205+
206+
#endif // WipperSnapper_I2C_Driver_VL53L4CX

0 commit comments

Comments
 (0)