Repository navigation
Expand file tree
/
Copy pathVL53L1X.cpp
More file actions
362 lines (298 loc) · 7.03 KB
/
Copy pathVL53L1X.cpp
File metadata and controls
362 lines (298 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
This is a library written for the VL53L1X I2C Distance sensor.
Written by Andy England @ SparkFun Electronics, October 17th, 2017
The sensor uses I2C to communicate, as well as a single (optional)
interrupt line that is not currently supported in this driver.
https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library
Do you like this library? Help support SparkFun. Buy a board!
Development environment specifics:
Arduino IDE 1.8.1
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include "Arduino.h"
#include "VL53L1X.h"
#include "vl53l1x_class.h"
SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin)
{
_i2cPort = &i2cPort;
_shutdownPin = shutdownPin;
_interruptPin = interruptPin;
_device = new VL53L1X(&i2cPort, shutdownPin, interruptPin);
}
bool SFEVL53L1X::init()
{
return _device->VL53L1X_SensorInit();
}
bool SFEVL53L1X::begin()
{
if (checkID() == false)
return (VL53L1_ERROR_PLATFORM_SPECIFIC_START);
return _device->VL53L1X_SensorInit();
}
/*Checks the ID of the device, returns true if ID is correct*/
bool SFEVL53L1X::checkID()
{
uint16_t sensorId;
_device->VL53L1X_GetSensorId(&sensorId);
if (sensorId == 0xEACC)
return true;
return false;
}
/*Turns the sensor on if the Shutdown pin is connected*/
void SFEVL53L1X::sensorOn()
{
if (_shutdownPin >= 0)
{
digitalWrite(_shutdownPin, HIGH);
}
delay(10);
}
/*Turns the sensor off if the Shutdown pin is connected*/
void SFEVL53L1X::sensorOff()
{
if (_shutdownPin >= 0)
{
digitalWrite(_shutdownPin, LOW);
}
delay(10);
}
/*Gets the software version number of the current library installed.*/
VL53L1X_Version_t SFEVL53L1X::getSoftwareVersion()
{
VL53L1X_Version_t tempVersion;
_device->VL53L1X_GetSWVersion(&tempVersion);
return tempVersion;
}
void SFEVL53L1X::setI2CAddress(uint8_t addr)
{
_i2cAddress = addr;
_device->VL53L1X_SetI2CAddress(addr);
}
int SFEVL53L1X::getI2CAddress()
{
return _i2cAddress;
}
void SFEVL53L1X::clearInterrupt()
{
_device->VL53L1X_ClearInterrupt();
}
void SFEVL53L1X::setInterruptPolarityHigh()
{
_device->VL53L1X_SetInterruptPolarity(1);
}
void SFEVL53L1X::setInterruptPolarityLow()
{
_device->VL53L1X_SetInterruptPolarity(0);
}
/**
* This function gets the interrupt polarity\n
* 1=active high (default), 0=active low
*/
uint8_t SFEVL53L1X::getInterruptPolarity()
{
uint8_t tmp;
_device->VL53L1X_GetInterruptPolarity(&tmp);
return tmp;
}
void SFEVL53L1X::startRanging()
{
_device->VL53L1X_StartRanging();
}
void SFEVL53L1X::stopRanging()
{
_device->VL53L1X_StopRanging();
}
bool SFEVL53L1X::checkForDataReady()
{
uint8_t dataReady;
_device->VL53L1X_CheckForDataReady(&dataReady);
return (bool)dataReady;
}
void SFEVL53L1X::setTimingBudgetInMs(uint16_t timingBudget)
{
_device->VL53L1X_SetTimingBudgetInMs(timingBudget);
}
uint16_t SFEVL53L1X::getTimingBudgetInMs()
{
uint16_t timingBudget;
_device->VL53L1X_GetTimingBudgetInMs(&timingBudget);
return timingBudget;
}
void SFEVL53L1X::setDistanceModeLong()
{
_device->VL53L1X_SetDistanceMode(2);
}
void SFEVL53L1X::setDistanceModeShort()
{
_device->VL53L1X_SetDistanceMode(1);
}
uint8_t SFEVL53L1X::getDistanceMode()
{
uint16_t distanceMode;
_device->VL53L1X_GetDistanceMode(&distanceMode);
return distanceMode;
}
void SFEVL53L1X::setIntermeasurementPeriod(uint16_t intermeasurement)
{
_device->VL53L1X_SetInterMeasurementInMs(intermeasurement);
}
uint16_t SFEVL53L1X::getIntermeasurementPeriod()
{
uint16_t intermeasurement;
_device->VL53L1X_GetInterMeasurementInMs(&intermeasurement);
return intermeasurement;
}
bool SFEVL53L1X::checkBootState()
{
uint8_t bootState;
_device->VL53L1X_BootState(&bootState);
return (bool)bootState;
}
uint16_t SFEVL53L1X::getSensorID()
{
uint16_t id;
_device->VL53L1X_GetSensorId(&id);
return id;
}
uint16_t SFEVL53L1X::getDistance()
{
uint16_t distance;
_device->VL53L1X_GetDistance(&distance);
return (int)distance;
}
uint16_t SFEVL53L1X::getSignalPerSpad()
{
uint16_t temp;
_device->VL53L1X_GetSignalPerSpad(&temp);
return temp;
}
uint16_t SFEVL53L1X::getAmbientPerSpad()
{
uint16_t temp;
_device->VL53L1X_GetAmbientPerSpad(&temp);
return temp;
}
uint16_t SFEVL53L1X::getSignalRate()
{
uint16_t temp;
_device->VL53L1X_GetSignalRate(&temp);
return temp;
}
uint16_t SFEVL53L1X::getSpadNb()
{
uint16_t temp;
_device->VL53L1X_GetSpadNb(&temp);
return temp;
}
uint16_t SFEVL53L1X::getAmbientRate()
{
uint16_t temp;
_device->VL53L1X_GetAmbientRate(&temp);
return temp;
}
uint8_t SFEVL53L1X::getRangeStatus()
{
uint8_t temp;
_device->VL53L1X_GetRangeStatus(&temp);
return temp;
}
void SFEVL53L1X::setOffset(int16_t offset)
{
_device->VL53L1X_SetOffset(offset);
}
int16_t SFEVL53L1X::getOffset()
{
int16_t temp;
_device->VL53L1X_GetOffset(&temp);
return temp;
}
void SFEVL53L1X::setXTalk(uint16_t xTalk)
{
_device->VL53L1X_SetXtalk(xTalk);
}
uint16_t SFEVL53L1X::getXTalk()
{
uint16_t temp;
_device->VL53L1X_GetXtalk(&temp);
return temp;
}
void SFEVL53L1X::setDistanceThreshold(uint16_t lowThresh, uint16_t hiThresh, uint8_t window)
{
_device->VL53L1X_SetDistanceThreshold(lowThresh, hiThresh, window, 1);
}
uint16_t SFEVL53L1X::getDistanceThresholdWindow()
{
uint16_t temp;
_device->VL53L1X_GetDistanceThresholdWindow(&temp);
return temp;
}
uint16_t SFEVL53L1X::getDistanceThresholdLow()
{
uint16_t temp;
_device->VL53L1X_GetDistanceThresholdLow(&temp);
return temp;
}
uint16_t SFEVL53L1X::getDistanceThresholdHigh()
{
uint16_t temp;
_device->VL53L1X_GetDistanceThresholdHigh(&temp);
return temp;
}
void SFEVL53L1X::setROI(uint8_t x, uint8_t y, uint8_t opticalCenter)
{
_device->VL53L1X_SetROI(x, y, opticalCenter);
}
uint16_t SFEVL53L1X::getROIX()
{
uint16_t tempX;
uint16_t tempY;
_device->VL53L1X_GetROI_XY(&tempX, &tempY);
return tempX;
}
uint16_t SFEVL53L1X::getROIY()
{
uint16_t tempX;
uint16_t tempY;
_device->VL53L1X_GetROI_XY(&tempX, &tempY);
return tempY;
}
void SFEVL53L1X::setSignalThreshold(uint16_t signalThreshold)
{
_device->VL53L1X_SetSignalThreshold(signalThreshold);
}
uint16_t SFEVL53L1X::getSignalThreshold()
{
uint16_t temp;
_device->VL53L1X_GetSignalThreshold(&temp);
return temp;
}
void SFEVL53L1X::setSigmaThreshold(uint16_t sigmaThreshold)
{
_device->VL53L1X_SetSigmaThreshold(sigmaThreshold);
}
uint16_t SFEVL53L1X::getSigmaThreshold()
{
uint16_t temp;
_device->VL53L1X_GetSigmaThreshold(&temp);
return temp;
}
void SFEVL53L1X::startTemperatureUpdate()
{
_device->VL53L1X_StartTemperatureUpdate();
}
void SFEVL53L1X::calibrateOffset(uint16_t targetDistanceInMm)
{
int16_t offset = getOffset();
_device->VL53L1X_CalibrateOffset(targetDistanceInMm, &offset);
}
void SFEVL53L1X::calibrateXTalk(uint16_t targetDistanceInMm)
{
uint16_t xTalk = getXTalk();
_device->VL53L1X_CalibrateXtalk(targetDistanceInMm, &xTalk);
};