|
| 1 | +/* |
| 2 | + This is a library written for the VL53L1X |
| 3 | + SparkFun sells these at its website: www.sparkfun.com |
| 4 | + Do you like this library? Help support SparkFun. Buy a board! |
| 5 | + https://www.sparkfun.com/products/14667 |
| 6 | +
|
| 7 | + Written by Nathan Seidle @ SparkFun Electronics, April 12th, 2018 |
| 8 | +
|
| 9 | + The VL53L1X is the latest Time Of Flight (ToF) sensor to be released. It uses |
| 10 | + a VCSEL (vertical cavity surface emitting laser) to emit a class 1 IR laser |
| 11 | + and time the reflection to the target. What does all this mean? You can measure |
| 12 | + the distance to an object up to 4 meters away with millimeter resolution! |
| 13 | +
|
| 14 | + We’ve found the precision of the sensor to be 1mm but the accuracy is around +/-5mm. |
| 15 | +
|
| 16 | + This library handles the initialization of the VL53L1X and is able to query the sensor |
| 17 | + for different readings. |
| 18 | +
|
| 19 | + Because ST has chosen not to release a complete datasheet we are forced to reverse |
| 20 | + engineer the interface from their example code and I2C data stream captures. |
| 21 | + For ST's reference code see STSW-IMG007 |
| 22 | +
|
| 23 | + The device operates as a normal I2C device. There are *many* registers. Reading and |
| 24 | + writing happens with a 16-bit address. The VL53L1X auto-increments the memory |
| 25 | + pointer with each read or write. |
| 26 | +
|
| 27 | + Development environment specifics: |
| 28 | + Arduino IDE 1.8.5 |
| 29 | +
|
| 30 | + This program is distributed in the hope that it will be useful, |
| 31 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 32 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 33 | + GNU General Public License for more details. |
| 34 | +
|
| 35 | + You should have received a copy of the GNU General Public License |
| 36 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 37 | +*/ |
| 38 | + |
| 39 | +#ifndef SPARKFUN_VL53L1X_ARDUINO_LIBRARY_H |
| 40 | +#define SPARKFUN_VL53L1X_ARDUINO_LIBRARY_H |
| 41 | + |
| 42 | +#if (ARDUINO >= 100) |
| 43 | +#include "Arduino.h" |
| 44 | +#else |
| 45 | +#include "WProgram.h" |
| 46 | +#endif |
| 47 | + |
| 48 | +#include <Wire.h> |
| 49 | + |
| 50 | +#include "vl53l1_register_map.h" |
| 51 | +const byte defaultAddress_VL53L1X = 0x29; //The default I2C address for the VL53L1X on the SparkFun breakout is 0x29. |
| 52 | + |
| 53 | +//Platform specific configurations |
| 54 | + |
| 55 | +//Define the size of the I2C buffer based on the platform the user has |
| 56 | +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 57 | +#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) |
| 58 | + |
| 59 | +//I2C_BUFFER_LENGTH is defined in Wire.H |
| 60 | +#define I2C_BUFFER_LENGTH BUFFER_LENGTH |
| 61 | + |
| 62 | +#elif defined(__SAMD21G18A__) |
| 63 | + |
| 64 | +//SAMD21 uses RingBuffer.h |
| 65 | +#define I2C_BUFFER_LENGTH SERIAL_BUFFER_SIZE |
| 66 | + |
| 67 | +#elif __MK20DX256__ |
| 68 | +//Teensy |
| 69 | + |
| 70 | +#elif ARDUINO_ARCH_ESP32 |
| 71 | +//ESP32 based platforms |
| 72 | + |
| 73 | +#else |
| 74 | + |
| 75 | +//The catch-all default is 32 |
| 76 | +#define I2C_BUFFER_LENGTH 32 |
| 77 | + |
| 78 | +#endif |
| 79 | +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 80 | + |
| 81 | +class VL53L1X { |
| 82 | + public: |
| 83 | + |
| 84 | + boolean begin(uint8_t deviceAddress = defaultAddress_VL53L1X, TwoWire &wirePort = Wire); //By default use the default I2C addres, and use Wire port |
| 85 | + |
| 86 | + void softReset(); //Reset the sensor via software |
| 87 | + void writeConfiguration(uint8_t offset = 0); //Write a block of bytes to the sensor to configure it to take a measurement |
| 88 | + boolean newDataReady(); //Polls the measurement completion bit |
| 89 | + uint16_t getDistance(); //Returns the results from the last measurement, distance in mm |
| 90 | + uint16_t getSignalRate(); //Returns the results from the last measurement, signal rate |
| 91 | + uint8_t getRangeStatus(); //Returns the results from the last measurement, 0 = valid |
| 92 | + |
| 93 | + uint8_t readRegister(uint16_t addr); //Read a byte from a 16-bit address |
| 94 | + uint16_t readRegister16(uint16_t addr); //Read two bytes from a 16-bit address |
| 95 | + boolean writeRegister(uint16_t addr, uint8_t val); //Write a byte to a spot |
| 96 | + boolean writeRegister16(uint16_t addr, uint16_t val); //Write two bytes to a spot |
| 97 | + |
| 98 | + private: |
| 99 | + |
| 100 | + //Variables |
| 101 | + TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware |
| 102 | + uint8_t _deviceAddress; //Keeps track of I2C address. setI2CAddress changes this. |
| 103 | +}; |
| 104 | + |
| 105 | +#endif |
| 106 | + |
0 commit comments