20
20
#include < vl53l4cx_def.h>
21
21
22
22
#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
24
24
25
25
/* *************************************************************************/
26
26
/* !
@@ -141,41 +141,45 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
141
141
WS_DEBUG_PRINTLN (status);
142
142
return false ;
143
143
}
144
+
145
+ // Wait for data read period then update data ready status
144
146
WS_DEBUG_PRINT (" Waiting for VL53L4CX data ready..." );
145
147
delay (VL53_READING_DELAY);
148
+ status = _VL53L4CX->VL53L4CX_GetMeasurementDataReady (&NewDataReady);
146
149
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 )) {
169
151
// error or no data ready
170
152
WS_DEBUG_PRINT (" VL53L4CX Error checking for data ready: " );
171
153
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 ;
172
163
}
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);
174
178
}
175
179
176
180
/* ******************************************************************************/
177
181
/* !
178
- @brief Gets the VL53L4CX's current proximity (first or second object) .
182
+ @brief Checks the VL53L4CX's proximity result and sets event value .
179
183
@param rangingData
180
184
The ranging data to check.
181
185
@param proximityEvent
@@ -194,27 +198,6 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
194
198
return false ;
195
199
}
196
200
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
-
218
201
protected:
219
202
VL53L4CX *_VL53L4CX; // /< Pointer to VL53L4CX temperature sensor object
220
203
};
0 commit comments