Skip to content

Commit 5a563ed

Browse files
committed
Refactor: VL53L4CX rename passed param + whitespace
1 parent 2b7e1ed commit 5a563ed

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/components/i2c/drivers/WipperSnapper_I2C_Driver_VL53L4CX.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,38 +120,54 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
120120
@brief Gets the VL53L4CX's current proximity (first or second object).
121121
@param proximityEvent
122122
Pointer to an Adafruit_Sensor event.
123-
@param index
124-
Index of the proximity to get (0 or 1).
123+
@param whichObject
124+
Index of the proximity object to get (0, or 1 for second
125+
object).
125126
@returns True if the proximity was obtained successfully, False
126127
otherwise.
127128
*/
128129
/*******************************************************************************/
129-
bool getProximity(sensors_event_t *proximityEvent, int index = 0) {
130+
bool getProximity(sensors_event_t *proximityEvent, int whichObject = 0) {
130131
VL53L4CX_MultiRangingData_t MultiRangingData;
131132
VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
132133
uint8_t NewDataReady = 0;
133134
int status;
135+
134136
// Start fresh reading, seemed to be accepting stale value
135137
status = _VL53L4CX->VL53L4CX_ClearInterruptAndStartMeasurement();
138+
if (status != VL53L4CX_ERROR_NONE) {
139+
WS_DEBUG_PRINT(
140+
"VL53L4CX Error clearing interrupt and starting measurement: ");
141+
{ USBSerial.println(status); };
142+
return false;
143+
}
136144
WS_DEBUG_PRINT("Waiting for VL53L4CX data ready...");
137145
delay(VL53_READING_DELAY);
138146

139147
awaitDataReady(status, NewDataReady);
148+
140149
if ((status == VL53L4CX_ERROR_NONE) && (NewDataReady != 0)) {
150+
// data ready - still to verify if one or two objects found and which
141151
status = _VL53L4CX->VL53L4CX_GetMultiRangingData(pMultiRangingData);
142152
int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
143-
if (no_of_object_found - 1 < index) {
153+
154+
// zero based index, return NaN / (Object not found) if too few objects
155+
if (no_of_object_found - 1 < whichObject) {
144156
WS_DEBUG_PRINT("Object not found at index #");
145-
WS_DEBUG_PRINT(index);
157+
WS_DEBUG_PRINT(whichObject);
146158
WS_DEBUG_PRINTLN(", returning NaN");
147159
proximityEvent->data[0] = NAN;
148160
return true;
149161
}
150-
bool retVal = updateDataPointIfValid(pMultiRangingData->RangeData[index],
151-
proximityEvent);
152-
return retVal;
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+
153168
} else {
154-
WS_DEBUG_PRINT("VL53L4CX Error: ");
169+
// error or no data ready
170+
WS_DEBUG_PRINT("VL53L4CX Error checking for data ready: ");
155171
WS_DEBUG_PRINTLN(status);
156172
}
157173
return false;
@@ -170,9 +186,7 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
170186
/*******************************************************************************/
171187
bool updateDataPointIfValid(VL53L4CX_TargetRangeData_t rangingData,
172188
sensors_event_t *proximityEvent) {
173-
if (rangingData.RangeStatus == VL53L4CX_RANGESTATUS_RANGE_VALID ||
174-
rangingData.RangeStatus ==
175-
VL53L4CX_RANGESTATUS_RANGE_VALID_MERGED_PULSE) {
189+
if (rangingData.RangeStatus == VL53L4CX_RANGESTATUS_RANGE_VALID) {
176190
int16_t mm = rangingData.RangeMilliMeter;
177191
proximityEvent->data[0] = (float)mm;
178192
return true;

0 commit comments

Comments
 (0)