1
+ /* !
2
+ * @file WipperSnapper_I2C_Driver_VL53L4CX.h
3
+ *
4
+ * Device driver for the VL53L4CX ToF sensor.
5
+ *
6
+ * Adafruit invests time and resources providing this open source code,
7
+ * please support Adafruit and open-source hardware by purchasing
8
+ * products from Adafruit!
9
+ *
10
+ * Copyright (c) 2024 Tyeth Gundry for Adafruit Industries
11
+ *
12
+ * MIT license, all text here must be included in any redistribution.
13
+ *
14
+ */
15
+ #ifndef WipperSnapper_I2C_Driver_VL53L4CX_H
16
+ #define WipperSnapper_I2C_Driver_VL53L4CX_H
17
+
18
+ #include " WipperSnapper_I2C_Driver.h"
19
+ #include < vl53l4cx_class.h>
20
+ #include < vl53l4cx_def.h>
21
+
22
+ #define VL53_SHUTDOWN_PIN -1 // /< Shutdown pin for VL53L4CX sensor
23
+ #define VL53_READING_DELAY 250 // /< Delay for reading data attempts
24
+ #define VL53_TIMING_BUDGET_NS 200000 // /< Timing budget for VL53L4CX sensor
25
+
26
+ /* *************************************************************************/
27
+ /* !
28
+ @brief Class that provides a driver interface for a VL53L4CX sensor.
29
+ */
30
+ /* *************************************************************************/
31
+ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
32
+ public:
33
+ /* ******************************************************************************/
34
+ /* !
35
+ @brief Constructor for a VL53L4CX sensor.
36
+ @param i2c
37
+ The I2C interface.
38
+ @param sensorAddress
39
+ 7-bit device address.
40
+ */
41
+ /* ******************************************************************************/
42
+ WipperSnapper_I2C_Driver_VL53L4CX (TwoWire *i2c, uint16_t sensorAddress)
43
+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
44
+ _i2c = i2c;
45
+ _sensorAddress = sensorAddress;
46
+ }
47
+
48
+ /* ******************************************************************************/
49
+ /* !
50
+ @brief Destructor for an VL53L4CX sensor.
51
+ */
52
+ /* ******************************************************************************/
53
+ ~WipperSnapper_I2C_Driver_VL53L4CX () {
54
+ // Called when a VL53L4CX component is deleted.
55
+ delete _VL53L4CX;
56
+ }
57
+
58
+ /* ******************************************************************************/
59
+ /* !
60
+ @brief Initializes the VL53L4CX sensor and begins I2C.
61
+ @returns True if initialized successfully, False otherwise.
62
+ */
63
+ /* ******************************************************************************/
64
+ bool begin () {
65
+ _VL53L4CX = new VL53L4CX (_i2c, VL53_SHUTDOWN_PIN);
66
+
67
+ if (_VL53L4CX->InitSensor ((uint8_t )_sensorAddress) != VL53L4CX_ERROR_NONE) {
68
+ WS_DEBUG_PRINTLN (" Failed to initialize VL53L4CX sensor!" );
69
+ return false ;
70
+ }
71
+
72
+ if (_VL53L4CX->VL53L4CX_SetDistanceMode (VL53L4CX_DISTANCEMODE_LONG) !=
73
+ VL53L4CX_ERROR_NONE) {
74
+ WS_DEBUG_PRINTLN (" Failed to set VL53L4CX distance mode to long!" );
75
+ return false ;
76
+ }
77
+
78
+ // Set 200ms measurement time, the possible TimingBudget is 8-200ms
79
+ if (_VL53L4CX->VL53L4CX_SetMeasurementTimingBudgetMicroSeconds (
80
+ VL53_TIMING_BUDGET_NS) != VL53L4CX_ERROR_NONE) {
81
+ WS_DEBUG_PRINTLN (" Failed to set VL53L4CX timing budget!" );
82
+ return false ;
83
+ }
84
+
85
+ if (_VL53L4CX->VL53L4CX_StartMeasurement () != VL53L4CX_ERROR_NONE) {
86
+ WS_DEBUG_PRINTLN (" Failed to start VL53L4CX ranging!" );
87
+ return false ;
88
+ }
89
+ return true ;
90
+ }
91
+
92
+ /* ******************************************************************************/
93
+ /* !
94
+ @brief Gets the VL53L4CX's current proximity for first object if found.
95
+ @param proximityEvent
96
+ Pointer to an Adafruit_Sensor event.
97
+ @returns True if the proximity was obtained successfully, False
98
+ otherwise.
99
+ */
100
+ /* ******************************************************************************/
101
+ bool getEventProximity (sensors_event_t *proximityEvent) {
102
+ return getProximity (proximityEvent, 0 );
103
+ }
104
+
105
+ /* ******************************************************************************/
106
+ /* !
107
+ @brief Gets the VL53L4CX's current proximity for second object if
108
+ found.
109
+ @param proximityEvent
110
+ Pointer to an Adafruit_Sensor event.
111
+ @returns True if the proximity was obtained successfully, False
112
+ otherwise.
113
+ */
114
+ /* ******************************************************************************/
115
+ bool getEventRaw (sensors_event_t *proximityEvent) {
116
+ return getProximity (proximityEvent, 1 );
117
+ }
118
+
119
+ /* ******************************************************************************/
120
+ /* !
121
+ @brief Gets the VL53L4CX's current proximity (first or second object).
122
+ @param proximityEvent
123
+ Pointer to an Adafruit_Sensor event.
124
+ @param whichObject
125
+ Index of the proximity object to get (0, or 1 for second
126
+ object).
127
+ @returns True if the proximity was obtained successfully, False
128
+ otherwise.
129
+ */
130
+ /* ******************************************************************************/
131
+ bool getProximity (sensors_event_t *proximityEvent, int whichObject = 0 ) {
132
+ VL53L4CX_MultiRangingData_t MultiRangingData;
133
+ VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
134
+ uint8_t NewDataReady = 0 ;
135
+ int status;
136
+
137
+ // Start fresh reading, seemed to be accepting stale value
138
+ status = _VL53L4CX->VL53L4CX_ClearInterruptAndStartMeasurement ();
139
+ if (status != VL53L4CX_ERROR_NONE) {
140
+ WS_DEBUG_PRINT (
141
+ " VL53L4CX Error clearing interrupt and starting measurement: " );
142
+ WS_DEBUG_PRINTLN (status);
143
+ return false ;
144
+ }
145
+
146
+ // Wait for data read period then update data ready status
147
+ WS_DEBUG_PRINT (" Waiting for VL53L4CX data ready..." );
148
+ delay (VL53_READING_DELAY);
149
+ status = _VL53L4CX->VL53L4CX_GetMeasurementDataReady (&NewDataReady);
150
+
151
+ if ((status != VL53L4CX_ERROR_NONE) || (NewDataReady == 0 )) {
152
+ // error or no data ready
153
+ WS_DEBUG_PRINT (" VL53L4CX Error checking for data ready: " );
154
+ WS_DEBUG_PRINTLN (status);
155
+ return false ;
156
+ }
157
+
158
+ // get data - still to verify which of one or two objects found
159
+ status = _VL53L4CX->VL53L4CX_GetMultiRangingData (pMultiRangingData);
160
+ if (status != VL53L4CX_ERROR_NONE) {
161
+ WS_DEBUG_PRINT (" VL53L4CX Error getting multi ranging data: " );
162
+ WS_DEBUG_PRINTLN (status);
163
+ return false ;
164
+ }
165
+
166
+ // whichObject: 0-based index, return NaN(Object not found) if too few found
167
+ if (pMultiRangingData->NumberOfObjectsFound - 1 < whichObject) {
168
+ WS_DEBUG_PRINT (" Object not found at index #" );
169
+ WS_DEBUG_PRINT (whichObject + 1 ); // human readable 1-based index
170
+ WS_DEBUG_PRINTLN (" , returning NaN" );
171
+ proximityEvent->data [0 ] = NAN;
172
+ return true ;
173
+ }
174
+
175
+ // RESULT: take the first or second detected object from ranging data,
176
+ // if valid then set event data in proximityEvent or return false
177
+ return updateDataPointIfValid (pMultiRangingData->RangeData [whichObject],
178
+ proximityEvent);
179
+ }
180
+
181
+ /* ******************************************************************************/
182
+ /* !
183
+ @brief Checks the VL53L4CX's proximity result and sets event value.
184
+ @param rangingData
185
+ The ranging data to check.
186
+ @param proximityEvent
187
+ Pointer to an Adafruit_Sensor event.
188
+ @returns True if the proximity was obtained successfully, False
189
+ otherwise.
190
+ */
191
+ /* ******************************************************************************/
192
+ bool updateDataPointIfValid (VL53L4CX_TargetRangeData_t rangingData,
193
+ sensors_event_t *proximityEvent) {
194
+ if (rangingData.RangeStatus == VL53L4CX_RANGESTATUS_RANGE_VALID) {
195
+ int16_t mm = rangingData.RangeMilliMeter ;
196
+ proximityEvent->data [0 ] = (float )mm;
197
+ return true ;
198
+ }
199
+ return false ;
200
+ }
201
+
202
+ protected:
203
+ VL53L4CX *_VL53L4CX; // /< Pointer to VL53L4CX temperature sensor object
204
+ };
205
+
206
+ #endif // WipperSnapper_I2C_Driver_VL53L4CX
0 commit comments