Skip to content

Commit d36d2ba

Browse files
committed
Refactor VL53L4CX
1 parent 8b59a83 commit d36d2ba

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

src/components/i2c/drivers/WipperSnapper_I2C_Driver_VL53L4CX.h

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* please support Adafruit and open-source hardware by purchasing
88
* products from Adafruit!
99
*
10-
* Copyright (c) 2022 Tyeth Gundry for Adafruit Industries
10+
* Copyright (c) 2024 Tyeth Gundry for Adafruit Industries
1111
*
1212
* MIT license, all text here must be included in any redistribution.
1313
*
@@ -19,6 +19,9 @@
1919
#include <vl53l4cx_class.h>
2020
#include <vl53l4cx_def.h>
2121

22+
#define VL53_SHUTDOWN_PIN -1
23+
#define VL53_READING_DELAY 350
24+
2225
/**************************************************************************/
2326
/*!
2427
@brief Class that provides a driver interface for a VL53L4CX sensor.
@@ -58,7 +61,7 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
5861
*/
5962
/*******************************************************************************/
6063
bool begin() {
61-
_VL53L4CX = new VL53L4CX(_i2c, -1);
64+
_VL53L4CX = new VL53L4CX(_i2c, VL53_SHUTDOWN_PIN);
6265

6366
if (_VL53L4CX->InitSensor((uint8_t)_sensorAddress) != VL53L4CX_ERROR_NONE) {
6467
WS_DEBUG_PRINTLN("Failed to initialize VL53L4CX sensor!");
@@ -75,6 +78,7 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
7578
if (_VL53L4CX->VL53L4CX_SetDistanceMode(VL53L4CX_DISTANCEMODE_LONG) !=
7679
VL53L4CX_ERROR_NONE) {
7780
WS_DEBUG_PRINTLN("Failed to set VL53L4CX distance mode to long!");
81+
return false;
7882
}
7983

8084
if (_VL53L4CX->VL53L4CX_StartMeasurement() != VL53L4CX_ERROR_NONE) {
@@ -126,48 +130,72 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
126130
VL53L4CX_MultiRangingData_t MultiRangingData;
127131
VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
128132
uint8_t NewDataReady = 0;
129-
int no_of_object_found = 0;
130-
int currentObject = 0;
131133
int status;
132134
// Start fresh reading, seemed to be accepting stale value
133135
status = _VL53L4CX->VL53L4CX_ClearInterruptAndStartMeasurement();
134136
WS_DEBUG_PRINT("Waiting for VL53L4CX data ready...");
135-
delay(250);
137+
delay(VL53_READING_DELAY);
136138

137-
for (uint8_t retries = 0;
138-
(status =
139-
_VL53L4CX->VL53L4CX_GetMeasurementDataReady(&NewDataReady)) &&
140-
!NewDataReady && retries < 3;
141-
retries++) {
142-
delay(350);
143-
WS_DEBUG_PRINT(" .");
144-
}
145-
WS_DEBUG_PRINTLN("");
139+
awaitDataReady(status, NewDataReady);
146140
if ((status == VL53L4CX_ERROR_NONE) && (NewDataReady != 0)) {
147141
status = _VL53L4CX->VL53L4CX_GetMultiRangingData(pMultiRangingData);
148-
no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
149-
150-
for (currentObject = 0; currentObject < no_of_object_found;
151-
currentObject++) {
152-
if (pMultiRangingData->RangeData[currentObject].RangeStatus ==
153-
VL53L4CX_RANGESTATUS_RANGE_VALID ||
154-
pMultiRangingData->RangeData[currentObject].RangeStatus ==
155-
VL53L4CX_RANGESTATUS_RANGE_VALID_MERGED_PULSE) {
156-
int16_t mm =
157-
pMultiRangingData->RangeData[currentObject].RangeMilliMeter;
158-
if (currentObject == index) {
159-
proximityEvent->data[0] = (float)mm;
160-
return true;
161-
}
162-
}
142+
int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
143+
if (no_of_object_found - 1 < index) {
144+
WS_DEBUG_PRINT("Object not found at index #");
145+
WS_DEBUG_PRINTLN(index);
146+
return false;
163147
}
148+
bool retVal = updateDataPointIfValid(pMultiRangingData->RangeData[index],
149+
proximityEvent);
164150
} else {
165151
WS_DEBUG_PRINT("VL53L4CX Error: ");
166152
WS_DEBUG_PRINTLN(status);
167153
}
168154
return false;
169155
}
170156

157+
/*******************************************************************************/
158+
/*!
159+
@brief Gets the VL53L4CX's current proximity (first or second object).
160+
@param rangingData
161+
The ranging data to check.
162+
@param proximityEvent
163+
Pointer to an Adafruit_Sensor event.
164+
@returns True if the proximity was obtained successfully, False
165+
otherwise.
166+
*/
167+
/*******************************************************************************/
168+
bool updateDataPointIfValid(VL53L4CX_TargetRangeData_t rangingData,
169+
sensors_event_t *proximityEvent) {
170+
if (rangingData.RangeStatus == VL53L4CX_RANGESTATUS_RANGE_VALID ||
171+
rangingData.RangeStatus ==
172+
VL53L4CX_RANGESTATUS_RANGE_VALID_MERGED_PULSE) {
173+
int16_t mm = rangingData.RangeMilliMeter;
174+
proximityEvent->data[0] = (float)mm;
175+
return true;
176+
}
177+
return false;
178+
}
179+
180+
/*******************************************************************************/
181+
/*!
182+
@brief Gets the sensor_t data for the VL53L4CX sensor.
183+
@param sensor
184+
Pointer to an Adafruit_Sensor sensor_t structure.
185+
*/
186+
/*******************************************************************************/
187+
void awaitDataReady(int &status, uint8_t &NewDataReady) {
188+
for (uint8_t retries = 0;
189+
(status =
190+
_VL53L4CX->VL53L4CX_GetMeasurementDataReady(&NewDataReady)) &&
191+
!NewDataReady && retries < 3;
192+
retries++) {
193+
delay(VL53_READING_DELAY);
194+
WS_DEBUG_PRINT(" .");
195+
}
196+
WS_DEBUG_PRINTLN("");
197+
}
198+
171199
protected:
172200
VL53L4CX *_VL53L4CX; ///< Pointer to VL53L4CX temperature sensor object
173201
};

0 commit comments

Comments
 (0)