7
7
* please support Adafruit and open-source hardware by purchasing
8
8
* products from Adafruit!
9
9
*
10
- * Copyright (c) 2022 Tyeth Gundry for Adafruit Industries
10
+ * Copyright (c) 2024 Tyeth Gundry for Adafruit Industries
11
11
*
12
12
* MIT license, all text here must be included in any redistribution.
13
13
*
19
19
#include < vl53l4cx_class.h>
20
20
#include < vl53l4cx_def.h>
21
21
22
+ #define VL53_SHUTDOWN_PIN -1
23
+ #define VL53_READING_DELAY 350
24
+
22
25
/* *************************************************************************/
23
26
/* !
24
27
@brief Class that provides a driver interface for a VL53L4CX sensor.
@@ -58,7 +61,7 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
58
61
*/
59
62
/* ******************************************************************************/
60
63
bool begin () {
61
- _VL53L4CX = new VL53L4CX (_i2c, - 1 );
64
+ _VL53L4CX = new VL53L4CX (_i2c, VL53_SHUTDOWN_PIN );
62
65
63
66
if (_VL53L4CX->InitSensor ((uint8_t )_sensorAddress) != VL53L4CX_ERROR_NONE) {
64
67
WS_DEBUG_PRINTLN (" Failed to initialize VL53L4CX sensor!" );
@@ -75,6 +78,7 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
75
78
if (_VL53L4CX->VL53L4CX_SetDistanceMode (VL53L4CX_DISTANCEMODE_LONG) !=
76
79
VL53L4CX_ERROR_NONE) {
77
80
WS_DEBUG_PRINTLN (" Failed to set VL53L4CX distance mode to long!" );
81
+ return false ;
78
82
}
79
83
80
84
if (_VL53L4CX->VL53L4CX_StartMeasurement () != VL53L4CX_ERROR_NONE) {
@@ -126,48 +130,72 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
126
130
VL53L4CX_MultiRangingData_t MultiRangingData;
127
131
VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
128
132
uint8_t NewDataReady = 0 ;
129
- int no_of_object_found = 0 ;
130
- int currentObject = 0 ;
131
133
int status;
132
134
// Start fresh reading, seemed to be accepting stale value
133
135
status = _VL53L4CX->VL53L4CX_ClearInterruptAndStartMeasurement ();
134
136
WS_DEBUG_PRINT (" Waiting for VL53L4CX data ready..." );
135
- delay (250 );
137
+ delay (VL53_READING_DELAY );
136
138
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);
146
140
if ((status == VL53L4CX_ERROR_NONE) && (NewDataReady != 0 )) {
147
141
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 ;
163
147
}
148
+ bool retVal = updateDataPointIfValid (pMultiRangingData->RangeData [index],
149
+ proximityEvent);
164
150
} else {
165
151
WS_DEBUG_PRINT (" VL53L4CX Error: " );
166
152
WS_DEBUG_PRINTLN (status);
167
153
}
168
154
return false ;
169
155
}
170
156
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
+
171
199
protected:
172
200
VL53L4CX *_VL53L4CX; // /< Pointer to VL53L4CX temperature sensor object
173
201
};
0 commit comments