Skip to content

Commit d5439f2

Browse files
committed
Remove IF nesting. Avoid retry delays for VL53L4CX
1 parent 0545387 commit d5439f2

File tree

1 file changed

+29
-46
lines changed

1 file changed

+29
-46
lines changed

src/components/i2c/drivers/WipperSnapper_I2C_Driver_VL53L4CX.h

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <vl53l4cx_def.h>
2121

2222
#define VL53_SHUTDOWN_PIN -1 ///< Shutdown pin for VL53L4CX sensor
23-
#define VL53_READING_DELAY 350 ///< Delay for reading data attempts
23+
#define VL53_READING_DELAY 250 ///< Delay for reading data attempts
2424

2525
/**************************************************************************/
2626
/*!
@@ -141,41 +141,45 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
141141
WS_DEBUG_PRINTLN(status);
142142
return false;
143143
}
144+
145+
// Wait for data read period then update data ready status
144146
WS_DEBUG_PRINT("Waiting for VL53L4CX data ready...");
145147
delay(VL53_READING_DELAY);
148+
status = _VL53L4CX->VL53L4CX_GetMeasurementDataReady(&NewDataReady);
146149

147-
awaitDataReady(status, NewDataReady);
148-
149-
if ((status == VL53L4CX_ERROR_NONE) && (NewDataReady != 0)) {
150-
// data ready - still to verify if one or two objects found and which
151-
status = _VL53L4CX->VL53L4CX_GetMultiRangingData(pMultiRangingData);
152-
int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
153-
154-
// zero based index, return NaN / (Object not found) if too few objects
155-
if (no_of_object_found - 1 < whichObject) {
156-
WS_DEBUG_PRINT("Object not found at index #");
157-
WS_DEBUG_PRINT(whichObject);
158-
WS_DEBUG_PRINTLN(", returning NaN");
159-
proximityEvent->data[0] = NAN;
160-
return true;
161-
}
162-
163-
// take the first or second detected object from ranging data, verify if
164-
// valid and then set the event data in proximityEvent or return false
165-
return updateDataPointIfValid(pMultiRangingData->RangeData[whichObject],
166-
proximityEvent);
167-
168-
} else {
150+
if ((status != VL53L4CX_ERROR_NONE) || (NewDataReady == 0)) {
169151
// error or no data ready
170152
WS_DEBUG_PRINT("VL53L4CX Error checking for data ready: ");
171153
WS_DEBUG_PRINTLN(status);
154+
return false;
155+
}
156+
157+
// get data - still to verify which of one or two objects found
158+
status = _VL53L4CX->VL53L4CX_GetMultiRangingData(pMultiRangingData);
159+
if (status != VL53L4CX_ERROR_NONE) {
160+
WS_DEBUG_PRINT("VL53L4CX Error getting multi ranging data: ");
161+
WS_DEBUG_PRINTLN(status);
162+
return false;
172163
}
173-
return false;
164+
165+
// whichObject: 0-based index, return NaN(Object not found) if too few found
166+
if (pMultiRangingData->NumberOfObjectsFound - 1 < whichObject) {
167+
WS_DEBUG_PRINT("Object not found at index #");
168+
WS_DEBUG_PRINT(whichObject + 1); // human readable 1-based index
169+
WS_DEBUG_PRINTLN(", returning NaN");
170+
proximityEvent->data[0] = NAN;
171+
return true;
172+
}
173+
174+
// RESULT: take the first or second detected object from ranging data,
175+
// if valid then set event data in proximityEvent or return false
176+
return updateDataPointIfValid(pMultiRangingData->RangeData[whichObject],
177+
proximityEvent);
174178
}
175179

176180
/*******************************************************************************/
177181
/*!
178-
@brief Gets the VL53L4CX's current proximity (first or second object).
182+
@brief Checks the VL53L4CX's proximity result and sets event value.
179183
@param rangingData
180184
The ranging data to check.
181185
@param proximityEvent
@@ -194,27 +198,6 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
194198
return false;
195199
}
196200

197-
/*******************************************************************************/
198-
/*!
199-
@brief Ensures the data is available for the VL53L4CX sensor.
200-
@param status
201-
Pointer to the returned error status
202-
@param NewDataReady
203-
Pointer to the returned data ready status
204-
*/
205-
/*******************************************************************************/
206-
void awaitDataReady(int &status, uint8_t &NewDataReady) {
207-
for (uint8_t retries = 0;
208-
(status =
209-
_VL53L4CX->VL53L4CX_GetMeasurementDataReady(&NewDataReady)) &&
210-
!NewDataReady && retries < 3;
211-
retries++) {
212-
delay(VL53_READING_DELAY);
213-
WS_DEBUG_PRINT(" .");
214-
}
215-
WS_DEBUG_PRINTLN("");
216-
}
217-
218201
protected:
219202
VL53L4CX *_VL53L4CX; ///< Pointer to VL53L4CX temperature sensor object
220203
};

0 commit comments

Comments
 (0)