-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparkFun_VL53L1X_Arduino_Library.h
107 lines (76 loc) · 3.9 KB
/
SparkFun_VL53L1X_Arduino_Library.h
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
/*
This is a library written for the VL53L1X
SparkFun sells these at its website: www.sparkfun.com
Do you like this library? Help support SparkFun. Buy a board!
https://www.sparkfun.com/products/14667
Written by Nathan Seidle @ SparkFun Electronics, April 12th, 2018
The VL53L1X is the latest Time Of Flight (ToF) sensor to be released. It uses
a VCSEL (vertical cavity surface emitting laser) to emit a class 1 IR laser
and time the reflection to the target. What does all this mean? You can measure
the distance to an object up to 4 meters away with millimeter resolution!
We’ve found the precision of the sensor to be 1mm but the accuracy is around +/-5mm.
This library handles the initialization of the VL53L1X and is able to query the sensor
for different readings.
Because ST has chosen not to release a complete datasheet we are forced to reverse
engineer the interface from their example code and I2C data stream captures.
For ST's reference code see STSW-IMG007
The device operates as a normal I2C device. There are *many* registers. Reading and
writing happens with a 16-bit address. The VL53L1X auto-increments the memory
pointer with each read or write.
Development environment specifics:
Arduino IDE 1.8.5
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/>.
*/
#ifndef SPARKFUN_VL53L1X_ARDUINO_LIBRARY_H
#define SPARKFUN_VL53L1X_ARDUINO_LIBRARY_H
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "vl53l1_register_map.h"
const byte defaultAddress_VL53L1X = 0x29; //The default I2C address for the VL53L1X on the SparkFun breakout is 0x29.
//#define defaultAddress_VL53L1X 0x29 //The default I2C address for the VL53L1X on the SparkFun breakout is 0x29.
//Platform specific configurations
//Define the size of the I2C buffer based on the platform the user has
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
//I2C_BUFFER_LENGTH is defined in Wire.H
#define I2C_BUFFER_LENGTH BUFFER_LENGTH
#elif defined(__SAMD21G18A__)
//SAMD21 uses RingBuffer.h
#define I2C_BUFFER_LENGTH SERIAL_BUFFER_SIZE
#elif __MK20DX256__
//Teensy
#elif ARDUINO_ARCH_ESP32
//ESP32 based platforms
#else
//The catch-all default is 32
#define I2C_BUFFER_LENGTH 32
#endif
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
class VL53L1X {
public:
boolean begin(uint8_t deviceAddress = defaultAddress_VL53L1X, TwoWire &wirePort = Wire); //By default use the default I2C address, and use Wire port
void softReset(); //Reset the sensor via software
void startMeasurement(uint8_t offset = 0); //Write a block of bytes to the sensor to configure it to take a measurement
boolean newDataReady(); //Polls the measurement completion bit
uint16_t getDistance(); //Returns the results from the last measurement, distance in mm
uint16_t getSignalRate(); //Returns the results from the last measurement, signal rate
uint8_t getRangeStatus(); //Returns the results from the last measurement, 0 = valid
uint8_t readRegister(uint16_t addr); //Read a byte from a 16-bit address
uint16_t readRegister16(uint16_t addr); //Read two bytes from a 16-bit address
boolean writeRegister(uint16_t addr, uint8_t val); //Write a byte to a spot
boolean writeRegister16(uint16_t addr, uint16_t val); //Write two bytes to a spot
private:
//Variables
TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware
uint8_t _deviceAddress; //Keeps track of I2C address. setI2CAddress changes this.
};
#endif