diff --git a/.github/workflows/LibraryBuild.yml b/.github/workflows/LibraryBuild.yml
index 743b2d3..1586da5 100644
--- a/.github/workflows/LibraryBuild.yml
+++ b/.github/workflows/LibraryBuild.yml
@@ -10,26 +10,26 @@
# This is the name of the workflow, visible on GitHub UI.
name: LibraryBuild
-on: [push, pull_request] # see: https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request
+on:
+ push: # see: https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request
+ paths:
+ - '**.ino'
+ - '**.cpp'
+ - '**.h'
+ - '**LibraryBuild.yml'
+ pull_request:
jobs:
build:
name: ${{ matrix.arduino-boards-fqbn }} - test compiling examples
- runs-on: ubuntu-latest # I picked Ubuntu to use shell scripts.
+ runs-on: ubuntu-18.04 # I picked Ubuntu to use shell scripts.
env:
- # Space separated list without double quotes around the list.
- # If you need a library with a space in its name, like Adafruit NeoPixel or Adafruit INA219, you must use double quotes
- # around the name and have at least 2 entries, where the first must be without double quotes! You may use Servo as dummy entry.
+ # Comma separated list without double quotes around the list.
+ PLATFORM_DEFAULT_URL: https://raw.githubusercontent.com/sparkfun/Arduino_Boards/master/IDE_Board_Manager/package_sparkfun_index.json
REQUIRED_LIBRARIES: EspSoftwareSerial
- # Global color definitions for output colors
- RED: '\033[0;31m'
- GREEN: '\033[0;32m'
- YELLOW: '\033[1;33m'
- BLUE: '\033[0;34m'
-
strategy:
matrix:
# The matrix will produce one job for each configuration parameter of type `arduino-boards-fqbn`
@@ -47,6 +47,13 @@ jobs:
- arduino:avr:leonardo
- arduino:avr:mega
- arduino:sam:arduino_due_x
+ - SparkFun:apollo3:artemis
+ - SparkFun:apollo3:amap3redboard
+ - SparkFun:apollo3:amap3nano
+ - SparkFun:apollo3:amap3atp
+ - SparkFun:apollo3:amap3thing
+ - SparkFun:apollo3:edge
+ - SparkFun:apollo3:edge2
- esp8266:esp8266:huzzah:eesz=4M3M,xtal=80
- esp32:esp32:featheresp32:FlashFreq=80
- STM32:stm32:GenF1:pnum=BLUEPILL_F103C8
@@ -57,31 +64,17 @@ jobs:
# You may exclude specific examples for a board with examples-exclude: Use a space separated list.
#############################################################################################################
include:
- - arduino-boards-fqbn: arduino:avr:uno
- platform: arduino:avr
-
- - arduino-boards-fqbn: arduino:avr:leonardo
- platform: arduino:avr
-
- - arduino-boards-fqbn: arduino:avr:mega
- platform: arduino:avr
-
- arduino-boards-fqbn: arduino:sam:arduino_due_x
- platform: arduino:sam
examples-exclude: Example5_LCDDemo # No SoftwareSerial available. Space separated list of (unique substrings of) example names to exclude in build
- arduino-boards-fqbn: esp8266:esp8266:huzzah:eesz=4M3M,xtal=80
- platform: esp8266:esp8266
+ platform-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json
- arduino-boards-fqbn: esp32:esp32:featheresp32:FlashFreq=80
- platform: esp32:esp32
+ platform-url: https://dl.espressif.com/dl/package_esp32_index.json
- arduino-boards-fqbn: STM32:stm32:GenF1:pnum=BLUEPILL_F103C8
- platform: STM32:stm32
-
-######################################################
-# End of configuration, start of fixed script section
-######################################################
+ platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/dev/STM32/package_stm_index.json
# Do not cancel all jobs / architectures if one job fails
fail-fast: false
@@ -91,68 +84,13 @@ jobs:
# First of all, we clone the repo using the `checkout` action.
- name: Checkout
- uses: actions/checkout@master
-
- # We use the `arduino/setup-arduino-cli` action to install and
- # configure the Arduino CLI on the system.
- - name: Setup Arduino CLI
- uses: arduino/setup-arduino-cli@v1.0.0
-
- - name: Link this repository as Arduino library
- run: |
- mkdir -p $HOME/Arduino/libraries
- ln -s $PWD $HOME/Arduino/libraries/.
-
- - name: Install platform from build matrix
- env:
- FQBN: ${{ matrix.arduino-boards-fqbn }}
- run: |
- arduino-cli core update-index
- if [ "${{ matrix.platform }}" == "" ]; then echo -e ""$RED"ERROR: platform missing for board ${FQBN%|*}. Check your matrix.includes entries"; exit 1; fi
- if [[ ${{ matrix.platform }} != *"arduino"* && ! -f ./arduino-cli.yaml ]]; then echo -e ""$RED"Non Arduino platform ${{ matrix.platform }} requested, but file arduino-cli.yaml is missing."; exit 1; fi
- arduino-cli core install ${{ matrix.platform }} # for each job / board one platform is installed
- arduino-cli board listall
- if [ ${{ matrix.platform }} == "esp32:esp32" ]; then pip install pyserial; fi
-
- - name: List installed boards with their FQBN
- run: |
- arduino-cli board listall
- # ls -l $HOME/.arduino15/packages/ # I see only arduino and one of the Attiny cores but not all 3 together
- # echo -e HOME=\"$HOME\" # /home/runner
- # echo PWD=$PWD # /home/runner/work/Github-Actions-Test/Github-Actions-Test
- # which arduino-cli # /opt/hostedtoolcache/arduino-cli/0.9.0/x64/arduino-cli
-
- - name: Install libraries
- run: if [[ "$REQUIRED_LIBRARIES" != "" ]]; then arduino-cli lib install ${{ env.REQUIRED_LIBRARIES }}; fi
+ uses: actions/checkout@v2
- # Finally, we compile the sketch, using the FQBN that was set in the build matrix.
- name: Compile all examples
- env:
- FQBN: ${{ matrix.arduino-boards-fqbn }}
- BUILD_PROPERTIES: ${{ toJson(matrix.examples-build-properties) }}
- run: |
- BUILD_PROPERTIES=${BUILD_PROPERTIES#\{} # remove "{"
- # if matrix.examples-build-properties are specified, create an associative shell array
- if [[ $BUILD_PROPERTIES != "null" ]]; then declare -A PROP_MAP="( $(echo $BUILD_PROPERTIES | sed -E 's/"(\w*)": *([^,}]*)[,}]/\[\1\]=\2/g' ) )"; fi
- echo -e "Compiling examples for board ${{ matrix.arduino-boards-fqbn }} \n"
- EXAMPLES=($(find . -name "*.ino"))
- for example in "${EXAMPLES[@]}"; do # Loop over all example directories
- EXAMPLE_NAME=$(basename $(dirname $example))
- if [[ "${{ matrix.examples-exclude }}" == *"$EXAMPLE_NAME"* ]]; then
- echo -e "Skipping $EXAMPLE_NAME \xe2\x9e\x9e" # Right arrow
- else
- # check if there is an entry in the associative array and create a compile parameter
- echo -n "Compiling $EXAMPLE_NAME "
- if [[ "${PROP_MAP[$EXAMPLE_NAME]}" != "" ]]; then echo -n "with ${PROP_MAP[$EXAMPLE_NAME]} "; fi
- build_stdout=$(arduino-cli compile --verbose --warnings all --fqbn ${FQBN%|*} --build-properties compiler.cpp.extra_flags="${PROP_MAP[$EXAMPLE_NAME]}" $(dirname $example) 2>&1);
- if [ $? -ne 0 ]; then
- echo -e ""$RED"\xe2\x9c\x96" # If ok output a green checkmark else a red X and the command output.
- exit_code=1
- echo -e "$build_stdout \n"
- else
- echo -e ""$GREEN"\xe2\x9c\x93"
- fi
- fi
- done
- exit $exit_code
- shell: bash {0} # Needed to avoid an exit at first error
+ uses: ArminJo/arduino-test-compile@v2
+ with:
+ required-libraries: ${{ env.REQUIRED_LIBRARIES }}
+ arduino-board-fqbn: ${{ matrix.arduino-boards-fqbn }}
+ platform-default-url: ${{ env.PLATFORM_DEFAULT_URL }}
+ platform-url: ${{ matrix.platform-url }}
+ examples-exclude: ${{ matrix.examples-exclude }}
diff --git a/.github/workflows/add_issue_to_project.yml b/.github/workflows/add_issue_to_project.yml
new file mode 100644
index 0000000..6b60cc3
--- /dev/null
+++ b/.github/workflows/add_issue_to_project.yml
@@ -0,0 +1,18 @@
+name: Add new issue to our main project
+
+on:
+ issues:
+ types:
+ - opened
+
+jobs:
+ add-to-project:
+ name: Add issue to project
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/add-to-project@main
+ with:
+ # You can target a project in a different organization
+ # to the issue
+ project-url: https://github.com/orgs/sparkfun/projects/19
+ github-token: ${{ secrets.DEFECT_ADD_TO_PROJECT }}
diff --git a/LICENSE.md b/LICENSE.md
index 5c9ff4a..1105cff 100644
--- a/LICENSE.md
+++ b/LICENSE.md
@@ -1,23 +1,12 @@
-COPYRIGHT(c) 2018 STMicroelectronics
+Copyright © 2022 SparkFun Electronics
-Redistribution and use in source and binary forms, with or without modification,
-are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of STMicroelectronics nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”),
+to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
diff --git a/README.md b/README.md
index 0c524f8..2721f22 100644
--- a/README.md
+++ b/README.md
@@ -3,19 +3,28 @@ SparkFun Qwiic 4m Distance Sensor with VL53L1X
[](https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library/actions)
-
+
+
+  |
+  |
+
+
+ SparkFun Distance Sensor Breakout - 4 Meter, VL53L1X (Qwiic) [SEN-14722] |
+ SparkFun Distance Sensor - 1.3 Meter, VL53L4CD (Qwiic)[SEN-18993] |
+
+
-[*SparkFun Distance Sensor Breakout - 4 Meter, VL53L1X (Qwiic)(SEN-14722)*](https://www.sparkfun.com/products/14722)
+The VL53L1X is a Time Of Flight (ToF) sensor that use 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? Using the VL53L1X, you can measure the distance to an object up to 4 meters away with millimeter resolution! That’s pretty incredible.
-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! That’s pretty incredible.
+We’re far from done: The VL53L1X is a highly complex sensor with a multitude of options and configurations. We’ve written example sketches that allow you to read the distance, signal rate, and range status. Because STMicroelectronics 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. If you’re into puzzles we could use your help to make the library better!
-We’re far from done: The VL53L1X is a highly complex sensor with a multitude of options and configurations. We’ve written example sketches that allow you to read the distance, signal rate, and range status. 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. If you’re into puzzles we could use your help to make the library better!
-
-We’ve found the precision of the sensor to be 1mm but the accuracy is around +/-5mm.
+We’ve found the precision of VL53L1X sensor to be 1mm but the accuracy is around +/-5mm.
SparkFun labored with love to create this code. Feel like supporting open source hardware?
Buy a [breakout board](https://www.sparkfun.com/products/14722) from SparkFun!
+*Note: The VL53L4CD is the cousin of VL53L1X. Overall, the sensor functions the same except for a few differences in the specifications. We've also found the precision of VL53L4CD sensor to be 1mm but the accuracy is around +/-7mm (white target: 88%, indoor, no infrared).*
+
Repository Contents
-------------------
@@ -25,17 +34,20 @@ Repository Contents
Documentation
--------------
-* **[Hookup Guide](https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-hookup-guide)**
+* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library.
+* **[Hookup Guide](https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-vl53l4cd-hookup-guide)**- Basic tutorial for the VL53L1X and VL53L4CD
-Product Versions
+Products that use this Library
--------------
-* **[SEN-14722](https://www.sparkfun.com/products/14722)** - SparkFun red version
-* **[SPX-14667](https://www.sparkfun.com/products/14667)** - SparkX Version
+
+* **[SEN-18993](https://www.sparkfun.com/products/18993)** - SparkFun red version for VL53L4CD
+* **[SEN-14722](https://www.sparkfun.com/products/14722)** - SparkFun red version for VL53L1X
+* **[SPX-14667](https://www.sparkfun.com/products/14667)** - SparkX Version for VL53L1X
License Information
-------------------
-This product is _**open source**_!
+SparkFun's source files are _**open source**_!
Please review the LICENSE.md file for license information.
@@ -45,4 +57,6 @@ Please use, reuse, and modify these files as you see fit. Please maintain attrib
Distributed as-is; no warranty is given.
+The source files included in the subfolder **st_src** are licensed differently. They are licensed under the BSD-3 license, check the License.md in
+that subfolder for specifics.
- Your friends at SparkFun.
diff --git a/arduino-cli.yaml b/arduino-cli.yaml
deleted file mode 100644
index 98981f2..0000000
--- a/arduino-cli.yaml
+++ /dev/null
@@ -1,7 +0,0 @@
-board_manager:
- additional_urls:
- - http://digistump.com/package_digistump_index.json
- - http://drazzy.com/package_drazzy.com_index.json
- - http://arduino.esp8266.com/stable/package_esp8266com_index.json
- - https://dl.espressif.com/dl/package_esp32_index.json
- - https://github.com/stm32duino/BoardManagerFiles/raw/dev/STM32/package_stm_index.json
diff --git a/examples/Example1_ReadDistance/Example1_ReadDistance.ino b/examples/Example1_ReadDistance/Example1_ReadDistance.ino
index 65cd86a..24a6c17 100644
--- a/examples/Example1_ReadDistance/Example1_ReadDistance.ino
+++ b/examples/Example1_ReadDistance/Example1_ReadDistance.ino
@@ -1,9 +1,3 @@
-#include
-#include
-#include
-#include
-#include
-
/*
Reading distance from the laser based VL53L1X
By: Nathan Seidle
@@ -20,7 +14,7 @@
*/
#include
-#include "SparkFun_VL53L1X.h"
+#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
//Optional interrupt and shutdown pins.
#define SHUTDOWN_PIN 2
@@ -34,19 +28,23 @@ void setup(void)
{
Wire.begin();
- Serial.begin(9600);
+ Serial.begin(115200);
Serial.println("VL53L1X Qwiic Test");
- if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
+ if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
{
- Serial.println("Sensor online!");
+ Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
+ while (1)
+ ;
}
+ Serial.println("Sensor online!");
}
void loop(void)
{
distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
- while (!distanceSensor.checkForDataReady()) {
+ while (!distanceSensor.checkForDataReady())
+ {
delay(1);
}
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
@@ -64,4 +62,3 @@ void loop(void)
Serial.println();
}
-
diff --git a/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino b/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino
index addff07..212fada 100644
--- a/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino
+++ b/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino
@@ -15,7 +15,7 @@
*/
#include
-#include "SparkFun_VL53L1X.h"
+#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
//Optional interrupt and shutdown pins.
#define SHUTDOWN_PIN 2
@@ -29,14 +29,17 @@ void setup(void)
{
Wire.begin();
- Serial.begin(9600);
+ Serial.begin(115200);
Serial.println("VL53L1X Qwiic Test");
- if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
+ if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
{
- Serial.println("Sensor online!");
+ Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
+ while (1)
+ ;
}
-
+ Serial.println("Sensor online!");
+
distanceSensor.setDistanceModeShort();
//distanceSensor.setDistanceModeLong();
}
@@ -45,12 +48,13 @@ void loop(void)
{
distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
- while (!distanceSensor.checkForDataReady()) {
+ while (!distanceSensor.checkForDataReady())
+ {
delay(1);
}
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
distanceSensor.clearInterrupt();
-
+
distanceSensor.stopRanging();
Serial.print("Distance(mm): ");
@@ -64,4 +68,3 @@ void loop(void)
Serial.println();
}
-
diff --git a/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino b/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino
index 56e4e79..edbe1bd 100644
--- a/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino
+++ b/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino
@@ -14,7 +14,7 @@
*/
#include
-#include "SparkFun_VL53L1X.h"
+#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
//Optional interrupt and shutdown pins.
#define SHUTDOWN_PIN 2
@@ -33,13 +33,18 @@ void setup(void)
{
Wire.begin();
- Serial.begin(9600);
+ Serial.begin(115200);
Serial.println("VL53L1X Qwiic Test");
- if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
- Serial.println("Sensor online!");
+ if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
+ {
+ Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
+ while (1)
+ ;
+ }
+ Serial.println("Sensor online!");
- for (int x = 0 ; x < HISTORY_SIZE ; x++)
+ for (int x = 0; x < HISTORY_SIZE; x++)
history[x] = 0;
}
@@ -47,22 +52,24 @@ void loop(void)
{
long startTime = millis();
distanceSensor.startRanging(); //Write configuration block of 135 bytes to setup a measurement
- while (!distanceSensor.checkForDataReady()) {
+ while (!distanceSensor.checkForDataReady())
+ {
delay(1);
}
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
distanceSensor.clearInterrupt();
distanceSensor.stopRanging();
long endTime = millis();
-
+
Serial.print("Distance(mm): ");
Serial.print(distance);
history[historySpot] = distance;
- if (++historySpot == HISTORY_SIZE) historySpot = 0;
+ if (++historySpot == HISTORY_SIZE)
+ historySpot = 0;
long avgDistance = 0;
- for (int x = 0 ; x < HISTORY_SIZE ; x++)
+ for (int x = 0; x < HISTORY_SIZE; x++)
avgDistance += history[x];
avgDistance /= HISTORY_SIZE;
@@ -85,22 +92,22 @@ void loop(void)
//Make it human readable
switch (rangeStatus)
{
- case 0:
- Serial.print("Good");
- break;
- case 1:
- Serial.print("Sigma fail");
- break;
- case 2:
- Serial.print("Signal fail");
- break;
- case 7:
- Serial.print("Wrapped target fail");
- break;
- default:
- Serial.print("Unknown: ");
- Serial.print(rangeStatus);
- break;
+ case 0:
+ Serial.print("Good");
+ break;
+ case 1:
+ Serial.print("Sigma fail");
+ break;
+ case 2:
+ Serial.print("Signal fail");
+ break;
+ case 7:
+ Serial.print("Wrapped target fail");
+ break;
+ default:
+ Serial.print("Unknown: ");
+ Serial.print(rangeStatus);
+ break;
}
Serial.print("\tHz: ");
@@ -108,4 +115,3 @@ void loop(void)
Serial.println();
}
-
diff --git a/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino b/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino
index 8647864..7791f6f 100644
--- a/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino
+++ b/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino
@@ -14,7 +14,7 @@
*/
#include
-#include "SparkFun_VL53L1X.h"
+#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
//Optional interrupt and shutdown pins.
#define SHUTDOWN_PIN 2
@@ -28,12 +28,17 @@ void setup(void)
{
Wire.begin();
- Serial.begin(9600);
+ Serial.begin(115200);
Serial.println("VL53L1X Qwiic Test");
- if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
+ if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
+ {
+ Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
+ while (1)
+ ;
+ }
Serial.println("Sensor online!");
-
+
// Intermeasurement period must be >= timing budget. Default = 100 ms.
distanceSensor.setIntermeasurementPeriod(200);
Serial.println(distanceSensor.getIntermeasurementPeriod());
@@ -42,7 +47,8 @@ void setup(void)
void loop(void)
{
- while (!distanceSensor.checkForDataReady()) {
+ while (!distanceSensor.checkForDataReady())
+ {
delay(1);
}
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
@@ -59,4 +65,3 @@ void loop(void)
Serial.println();
}
-
diff --git a/examples/Example5_LCDDemo/Example5_LCDDemo.ino b/examples/Example5_LCDDemo/Example5_LCDDemo.ino
index e9d1645..6642303 100644
--- a/examples/Example5_LCDDemo/Example5_LCDDemo.ino
+++ b/examples/Example5_LCDDemo/Example5_LCDDemo.ino
@@ -15,7 +15,7 @@
*/
#include
-#include "SparkFun_VL53L1X.h"
+#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
#include
//Optional interrupt and shutdown pins.
@@ -28,6 +28,8 @@ SFEVL53L1X distanceSensor;
#if defined(ESP8266)
SoftwareSerial lcd(10, 9); // RX, TX
+#elif defined(ARDUINO_ARCH_APOLLO3)
+SoftwareSerial lcd(14, 15); // use RX1, TX1 of Apollo boards
#else
SoftwareSerial lcd(10, A3); // RX, TX
#endif
@@ -50,7 +52,7 @@ byte deltaSpot = 0; //Keeps track of where we are within the deltas array
//Too quickly and it gets twitchy. Too slow and it doesn't seem like it's responding.
#define LOOPTIME 50
-int maxMPH = 0; //Keeps track of what the latest fastest speed is
+int maxMPH = 0; //Keeps track of what the latest fastest speed is
long maxMPH_timeout = 0; //Forget the max speed after some length of time
#define maxMPH_remember 3000 //After this number of ms the system will forget the max speed
@@ -73,12 +75,15 @@ void setup(void)
lcd.print("Distance: 3426 ");
lcd.print("12 mph ");
- if (distanceSensor.begin() == 0) //Begin returns 0 on a good init
+ if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
{
- Serial.println("Sensor online!");
+ Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
+ while (1)
+ ;
}
+ Serial.println("Sensor online!");
- for (int x = 0 ; x < HISTORY_SIZE ; x++)
+ for (int x = 0; x < HISTORY_SIZE; x++)
history[x] = 0;
}
@@ -87,25 +92,26 @@ void loop(void)
//Write configuration block of 135 bytes to setup a measurement
distanceSensor.startRanging();
- while (!distanceSensor.checkForDataReady()) {
+ while (!distanceSensor.checkForDataReady())
+ {
delay(1);
}
int distanceMM = distanceSensor.getDistance();
distanceSensor.clearInterrupt();
distanceSensor.stopRanging();
-
+
lastReading = millis();
history[historySpot] = distanceMM;
- if (historySpot++ == HISTORY_SIZE) historySpot = 0;
+ if (historySpot++ == HISTORY_SIZE)
+ historySpot = 0;
long avgDistance = 0;
- for (int x = 0 ; x < HISTORY_SIZE ; x++)
+ for (int x = 0; x < HISTORY_SIZE; x++)
avgDistance += history[x];
avgDistance /= HISTORY_SIZE;
-
//Every loop let's get a reading
newDistance = distanceMM / 10; //Go get distance in cm
@@ -114,24 +120,26 @@ void loop(void)
//Scan delta array to see if this new delta is sane or not
boolean safeDelta = true;
- for (int x = 0 ; x < numberOfDeltas ; x++)
+ for (int x = 0; x < numberOfDeltas; x++)
{
//We don't want to register jumps greater than 30cm in 50ms
//But if we're less than 1000cm then maybe
//30 works well
- if ( abs(deltaDistance - deltas[x]) > 40) safeDelta = false;
+ if (abs(deltaDistance - deltas[x]) > 40)
+ safeDelta = false;
}
//Insert this new delta into the array
if (safeDelta)
{
deltas[deltaSpot++] = deltaDistance;
- if (deltaSpot > numberOfDeltas) deltaSpot = 0; //Wrap this variable
+ if (deltaSpot > numberOfDeltas)
+ deltaSpot = 0; //Wrap this variable
}
//Get average of the current deltas array
float avgDeltas = 0.0;
- for (byte x = 0 ; x < numberOfDeltas ; x++)
+ for (byte x = 0; x < numberOfDeltas; x++)
avgDeltas += (float)deltas[x];
avgDeltas /= numberOfDeltas;
@@ -162,10 +170,12 @@ void loop(void)
else
{
validCount++;
- if (avgDistance > maxDistance) maxDistance = avgDistance;
+ if (avgDistance > maxDistance)
+ maxDistance = avgDistance;
}
- if (validCount > 10) readingValid = true;
+ if (validCount > 10)
+ readingValid = true;
if (readingValid == false)
{
@@ -195,4 +205,3 @@ void loop(void)
delay(25);
}
-
diff --git a/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino b/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino
index df060fc..d086e6a 100644
--- a/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino
+++ b/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino
@@ -1,11 +1,3 @@
-#include
-
-#include
-#include
-#include
-#include
-#include
-
/*
Reading distance from the laser based VL53L1X
By: Armin Joachimsmeyer
@@ -22,7 +14,7 @@
*/
#include
-#include "SparkFun_VL53L1X.h"
+#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
//Optional interrupt and shutdown pins.
#define SHUTDOWN_PIN 2
@@ -33,80 +25,88 @@ SFEVL53L1X distanceSensor;
//Uncomment the following line to use the optional shutdown and interrupt pins.
//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
-void setup(void) {
- Wire.begin();
-
- // initialize the digital pin as an output.
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.begin(9600);
- while (!Serial)
- ; //delay for Leonardo
- // Just to know which program is running on my Arduino
- Serial.println(F("START " __FILE__));
-
-#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__)
- // test beep for the connected speaker
- tone(TONE_PIN,1000,100);
- delay(200);
+void setup(void)
+{
+ Wire.begin();
+
+ // initialize the digital pin as an output.
+ pinMode(LED_BUILTIN, OUTPUT);
+ Serial.begin(115200);
+ while (!Serial)
+ ; //delay for Leonardo
+ // Just to know which program is running on my Arduino
+ Serial.println(F("START " __FILE__));
+
+#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__)
+ // test beep for the connected speaker
+ tone(TONE_PIN, 1000, 100);
+ delay(200);
#endif
- if (distanceSensor.begin() == 0) { //Begin returns 0 on a good init
- Serial.println("Sensor online!");
- }
+ if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
+ {
+ Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
+ while (1)
+ ;
+ }
+ Serial.println("Sensor online!");
- // Short mode max distance is limited to 1.3 m but has a better ambient immunity.
- // Above 1.3 meter error 4 is thrown (wrap around).
- distanceSensor.setDistanceModeShort();
- //distanceSensor.setDistanceModeLong(); // default
+ // Short mode max distance is limited to 1.3 m but has a better ambient immunity.
+ // Above 1.3 meter error 4 is thrown (wrap around).
+ distanceSensor.setDistanceModeShort();
+ //distanceSensor.setDistanceModeLong(); // default
- // Print Legend
- Serial.println("Distance Signal-Rate/100 Ambient-Rate/100");
+ // Print Legend
+ Serial.println("Distance Signal-Rate/100 Ambient-Rate/100");
- /*
+ /*
* The minimum timing budget is 20 ms for the short distance mode and 33 ms for the medium and long distance modes.
* Predefined values = 15, 20, 33, 50, 100(default), 200, 500.
* This function must be called after SetDistanceMode.
*/
- distanceSensor.setTimingBudgetInMs(50);
-
- // measure periodically. Intermeasurement period must be >/= timing budget.
- distanceSensor.setIntermeasurementPeriod(100);
- distanceSensor.startRanging(); // Start once
+ distanceSensor.setTimingBudgetInMs(50);
+ // measure periodically. Intermeasurement period must be >/= timing budget.
+ distanceSensor.setIntermeasurementPeriod(100);
+ distanceSensor.startRanging(); // Start once
}
-void loop(void) {
- while (!distanceSensor.checkForDataReady()) {
- delay(1);
- }
- byte rangeStatus = distanceSensor.getRangeStatus();
- unsigned int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
- distanceSensor.clearInterrupt();
-
- /*
+void loop(void)
+{
+ while (!distanceSensor.checkForDataReady())
+ {
+ delay(1);
+ }
+ byte rangeStatus = distanceSensor.getRangeStatus();
+ unsigned int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
+ distanceSensor.clearInterrupt();
+
+ /*
* With signed int we get overflow at short distances.
* With unsigned we get an overflow below around 2.5 cm.
*/
- unsigned int tSignalRate = distanceSensor.getSignalRate();
- unsigned int tAmbientRate = distanceSensor.getAmbientRate();
+ unsigned int tSignalRate = distanceSensor.getSignalRate();
+ unsigned int tAmbientRate = distanceSensor.getAmbientRate();
- if (rangeStatus == 0) {
-#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__)
- tone(11, distance + 500);
+ if (rangeStatus == 0)
+ {
+#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__)
+ tone(11, distance + 500);
#endif
- } else {
- // if tAmbientRate > tSignalRate we likely get a signal fail error condition
- // in Distance mode short we get error 4 (out of bounds) or 7 (wrap around) if the distance is greater than 1.3 meter.
- distance = rangeStatus;
-#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__)
- noTone(11);
+ }
+ else
+ {
+ // if tAmbientRate > tSignalRate we likely get a signal fail error condition
+ // in Distance mode short we get error 4 (out of bounds) or 7 (wrap around) if the distance is greater than 1.3 meter.
+ distance = rangeStatus;
+#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__)
+ noTone(11);
#endif
- }
+ }
- Serial.print(distance);
- Serial.print(' ');
- Serial.print(tSignalRate / 100);
- Serial.print(' ');
- Serial.println(tAmbientRate / 100);
+ Serial.print(distance);
+ Serial.print(' ');
+ Serial.print(tSignalRate / 100);
+ Serial.print(' ');
+ Serial.println(tAmbientRate / 100);
}
-
diff --git a/examples/Example7_Calibration/Example7_Calibration.ino b/examples/Example7_Calibration/Example7_Calibration.ino
index 4fbe130..d960a5d 100644
--- a/examples/Example7_Calibration/Example7_Calibration.ino
+++ b/examples/Example7_Calibration/Example7_Calibration.ino
@@ -1,11 +1,3 @@
-#include
-
-#include
-#include
-#include
-#include
-#include
-
/*
Calling distance offset calibration for the laser based VL53L1X
By: Armin Joachimsmeyer
@@ -22,7 +14,7 @@
*/
#include
-#include "SparkFun_VL53L1X.h"
+#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
#define VERSION_EXAMPLE "1.0"
@@ -37,108 +29,118 @@ SFEVL53L1X distanceSensor;
//Uncomment the following line to use the optional shutdown and interrupt pins.
//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
-void setup(void) {
- Wire.begin();
+void setup(void)
+{
+ Wire.begin();
- // initialize the digital pin as an output.
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.begin(9600);
+ // initialize the digital pin as an output.
+ pinMode(LED_BUILTIN, OUTPUT);
+ Serial.begin(115200);
#if defined(__AVR_ATmega32U4__)
- while (!Serial); //delay for Leonardo, but this loops forever for Maple Serial
+ while (!Serial)
+ ; //delay for Leonardo, but this loops forever for Maple Serial
#endif
#if defined(SERIAL_USB)
- delay(2000); // To be able to connect Serial monitor after reset and before first printout
+ delay(2000); // To be able to connect Serial monitor after reset and before first printout
#endif
- // Just to know which program is running on my Arduino
- Serial.println(F("START " __FILE__ "\r\nVersion " VERSION_EXAMPLE " from " __DATE__));
+ // Just to know which program is running on my Arduino
+ Serial.println(F("START " __FILE__ "\r\nVersion " VERSION_EXAMPLE " from " __DATE__));
-#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__)
- tone(TONE_PIN, 1000, 100);
- delay(200);
+#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__)
+ tone(TONE_PIN, 1000, 100);
+ delay(200);
#endif
- Serial.println();
- Serial.println("*****************************************************************************************************");
- Serial.println(" Offset calibration");
- Serial.println("Place a light grey (17 % gray) target at a distance of 140mm in front of the VL53L1X sensor.");
- Serial.println("The calibration will start 5 seconds after a distance below 10 cm was detected for 1 second.");
- Serial.println("Use the resulting offset distance as parameter for the setOffset() function called after begin().");
- Serial.println("*****************************************************************************************************");
- Serial.println();
+ Serial.println();
+ Serial.println("*****************************************************************************************************");
+ Serial.println(" Offset calibration");
+ Serial.println("Place a light grey (17 % gray) target at a distance of 140mm in front of the VL53L1X sensor.");
+ Serial.println("The calibration will start 5 seconds after a distance below 10 cm was detected for 1 second.");
+ Serial.println("Use the resulting offset distance as parameter for the setOffset() function called after begin().");
+ Serial.println("*****************************************************************************************************");
+ Serial.println();
#ifdef AVR
- Serial.print("Old offset value read from EEPROM=");
- Serial.print((int16_t) eeprom_read_word((uint16_t*) EEPROM_ADDRESS_FOR_OFFSET));
- Serial.println(" mm");
+ Serial.print("Old offset value read from EEPROM=");
+ Serial.print((int16_t)eeprom_read_word((uint16_t *)EEPROM_ADDRESS_FOR_OFFSET));
+ Serial.println(" mm");
#endif
- if (distanceSensor.begin() == 0) { //Begin returns 0 on a good init
- Serial.println("Sensor online!");
+ if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
+ {
+ Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
+ while (1)
+ ;
+ }
+ Serial.println("Sensor online!");
+
+ // Short mode max distance is limited to 1.3 m but has a better ambient immunity.
+ // Above 1.3 meter error 4 is thrown (wrap around).
+ distanceSensor.setDistanceModeShort();
+ //distanceSensor.setDistanceModeLong(); // default
+ distanceSensor.setTimingBudgetInMs(50);
+
+ int tLowDistanceCount = 0;
+ while (tLowDistanceCount < 20)
+ {
+ while (!distanceSensor.checkForDataReady())
+ {
+ delay(1);
}
-
- // Short mode max distance is limited to 1.3 m but has a better ambient immunity.
- // Above 1.3 meter error 4 is thrown (wrap around).
- distanceSensor.setDistanceModeShort();
- //distanceSensor.setDistanceModeLong(); // default
- distanceSensor.setTimingBudgetInMs(50);
-
- int tLowDistanceCount = 0;
- while (tLowDistanceCount < 20) {
- while (!distanceSensor.checkForDataReady()) {
- delay(1);
- }
- if (distanceSensor.getDistance() < 100) {
- tLowDistanceCount++;
- } else {
- tLowDistanceCount = 0;
- }
- distanceSensor.clearInterrupt();
+ if (distanceSensor.getDistance() < 100)
+ {
+ tLowDistanceCount++;
}
-
- Serial.println("Distance below 10cm detected for more than a second, start offset calibration in 5 seconds");
-#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__)
- tone(TONE_PIN, 1000, 100);
- delay(1000);
- tone(TONE_PIN, 1000, 100);
- delay(1000);
- tone(TONE_PIN, 1000, 100);
- delay(1000);
- tone(TONE_PIN, 1000, 100);
- delay(1000);
- tone(TONE_PIN, 1000, 900);
- delay(1000);
+ else
+ {
+ tLowDistanceCount = 0;
+ }
+ distanceSensor.clearInterrupt();
+ }
+
+ Serial.println("Distance below 10cm detected for more than a second, start offset calibration in 5 seconds");
+#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__)
+ tone(TONE_PIN, 1000, 100);
+ delay(1000);
+ tone(TONE_PIN, 1000, 100);
+ delay(1000);
+ tone(TONE_PIN, 1000, 100);
+ delay(1000);
+ tone(TONE_PIN, 1000, 100);
+ delay(1000);
+ tone(TONE_PIN, 1000, 900);
+ delay(1000);
#else
- delay(5000);
+ delay(5000);
#endif
- /*
+ /*
* Place a target, 17 % gray, at a distance of 140 mm from the sensor and call the VL53L1X_CalibrateOffset (dev, 140, &offset) function.
* The calibration may take a few seconds. The offset correction is applied to the sensor at the end of calibration.
*
* The calibration function takes 50 measurements and then takes the difference between the target distance
* and the average distance and then calls setOffset() with this value. Thats all. No magic.
*/
- distanceSensor.calibrateOffset(140);
- Serial.print("Result of offset calibration. RealDistance - MeasuredDistance=");
- Serial.print(distanceSensor.getOffset());
- Serial.print(" mm");
- Serial.println();
+ distanceSensor.calibrateOffset(140);
+ Serial.print("Result of offset calibration. RealDistance - MeasuredDistance=");
+ Serial.print(distanceSensor.getOffset());
+ Serial.print(" mm");
+ Serial.println();
#ifdef AVR
- eeprom_write_word((uint16_t*) EEPROM_ADDRESS_FOR_OFFSET, distanceSensor.getOffset());
- Serial.println("Offset value written to end of EEPROM.");
+ eeprom_write_word((uint16_t *)EEPROM_ADDRESS_FOR_OFFSET, distanceSensor.getOffset());
+ Serial.println("Offset value written to end of EEPROM.");
#endif
- // measure periodically. Intermeasurement period must be >/= timing budget.
- distanceSensor.setIntermeasurementPeriod(100);
- distanceSensor.startRanging(); // Start once
+ // measure periodically. Intermeasurement period must be >/= timing budget.
+ distanceSensor.setIntermeasurementPeriod(100);
+ distanceSensor.startRanging(); // Start once
-#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__)
- tone(TONE_PIN, 1000, 900);
+#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__)
+ tone(TONE_PIN, 1000, 900);
#endif
}
-void loop(void) {
-
+void loop(void)
+{
}
-
diff --git a/examples/Example8_SetGetDetectionThresholds/Example8_SetGetDetectionThresholds.ino b/examples/Example8_SetGetDetectionThresholds/Example8_SetGetDetectionThresholds.ino
new file mode 100644
index 0000000..16bfbfb
--- /dev/null
+++ b/examples/Example8_SetGetDetectionThresholds/Example8_SetGetDetectionThresholds.ino
@@ -0,0 +1,126 @@
+/*
+ Reading distance from the laser based VL53L1X
+ By: Nathan Seidle
+ Revised by: Andy England and Ricardo Ramos
+ SparkFun Electronics
+ Date: January 21st, 2022
+ License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
+
+ SparkFun labored with love to create this code. Feel like supporting open source hardware?
+ Buy a board from SparkFun! https://www.sparkfun.com/products/14667
+
+ This example sets high and low thresholds for detection.
+
+ Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
+*/
+
+#include
+#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
+
+//Optional interrupt and shutdown pins.
+#define SHUTDOWN_PIN 2
+#define INTERRUPT_PIN 3
+
+SFEVL53L1X distanceSensor;
+//Uncomment the following line to use the optional shutdown and interrupt pins.
+//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
+
+void setup(void)
+{
+ Wire.begin();
+
+ Serial.begin(115200);
+ Serial.println("VL53L1X Qwiic Test");
+
+ if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
+ {
+ Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
+ while (1)
+ ;
+ }
+ Serial.println("Sensor online!");
+
+ DetectionConfig dc; // struct instance which holds the detection configuration
+ dc.IntOnNoTarget = 1; // No longer used - just use 1 per ST
+ dc.distanceMode = DISTANCE_SHORT; // short distance mode
+ dc.thresholdHigh = 300; // high threshold of 300 mm
+ dc.thresholdLow = 70; // low threshold of 70 mm
+ dc.windowMode = WINDOW_IN; // will measure and trigger interrrupt when measurement fall between 70 and 300 mm
+
+ if(distanceSensor.setThresholdConfig(&dc) == true)
+ {
+ Serial.println("Thresholds programmed. Reading configuration back...");
+ dc = {};
+ if(distanceSensor.getThresholdConfig(&dc) == true)
+ {
+ Serial.print("IntOnNoTarget: ");
+ Serial.println(dc.IntOnNoTarget);
+ Serial.print("Distance Mode: ");
+
+ if(dc.distanceMode == DISTANCE_SHORT)
+ Serial.println("DISTANCE_SHORT (0)");
+ else
+ Serial.println("DISTANCE_LONG (1)");
+
+ Serial.print("Threshold High: ");
+ Serial.println(dc.thresholdHigh);
+ Serial.print("Threshold Low: ");
+ Serial.println(dc.thresholdLow);
+ Serial.print("Window mode: ");
+ switch (dc.windowMode)
+ {
+ case WINDOW_BELOW:
+ Serial.println("WINDOW_BELOW (0)");
+ break;
+
+ case WINDOW_ABOVE:
+ Serial.println("WINDOW_ABOVE (1)");
+ break;
+
+ case WINDOW_OUT:
+ Serial.println("WINDOW_OUT (2)");
+ break;
+
+ case WINDOW_IN:
+ Serial.println("WINDOW_IN (3)");
+ break;
+
+ default:
+ break;
+ }
+ }
+ }
+ else
+ {
+ Serial.println("Could not set threshold configuration.");
+ }
+ Serial.println("Starting measurements in 2 seconds.");
+ delay(2000);
+ Serial.println("Measurements started!");
+
+}
+
+void loop(void)
+{
+ distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
+
+ while (!distanceSensor.checkForDataReady())
+ {
+ delay(1);
+ }
+ int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
+ distanceSensor.clearInterrupt();
+
+ distanceSensor.stopRanging();
+
+ Serial.print("Distance(mm): ");
+ Serial.print(distance);
+
+ float distanceInches = distance * 0.0393701;
+ float distanceFeet = distanceInches / 12.0;
+
+ Serial.print("\tDistance(ft): ");
+ Serial.print(distanceFeet, 2);
+
+ Serial.println();
+}
diff --git a/examples/Example9_OneShotMeasurement/Example9_OneShotMeasurement.ino b/examples/Example9_OneShotMeasurement/Example9_OneShotMeasurement.ino
new file mode 100644
index 0000000..37ae7e1
--- /dev/null
+++ b/examples/Example9_OneShotMeasurement/Example9_OneShotMeasurement.ino
@@ -0,0 +1,66 @@
+/*
+ Reading distance from the laser based VL53L1X using a one-shot measurement.
+ By: Nathan Seidle
+ Revised by: Andy England, Ricardo Ramos
+ Pull request by: Joseph Duchesne
+ SparkFun Electronics
+ Date: January 31st, 2022
+ License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
+
+ SparkFun labored with love to create this code. Feel like supporting open source hardware?
+ Buy a board from SparkFun! https://www.sparkfun.com/products/14667
+
+ This example makes a one shot measurement.
+
+ Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
+*/
+
+#include
+#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
+
+//Optional interrupt and shutdown pins.
+#define SHUTDOWN_PIN 2
+#define INTERRUPT_PIN 3
+
+SFEVL53L1X distanceSensor;
+//Uncomment the following line to use the optional shutdown and interrupt pins.
+//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
+
+void setup(void)
+{
+ Wire.begin();
+
+ Serial.begin(115200);
+ Serial.println("VL53L1X Qwiic Test");
+
+ if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
+ {
+ Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
+ while (1)
+ ;
+ }
+ Serial.println("Sensor online!");
+
+
+}
+
+void loop(void)
+{
+ distanceSensor.startOneshotRanging(); //Write configuration bytes to initiate measurement
+ while (!distanceSensor.checkForDataReady())
+ {
+ delay(1);
+ }
+ int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
+
+ Serial.print("Distance(mm): ");
+ Serial.print(distance);
+
+ float distanceInches = distance * 0.0393701;
+ float distanceFeet = distanceInches / 12.0;
+
+ Serial.print("\tDistance(ft): ");
+ Serial.print(distanceFeet, 2);
+
+ Serial.println();
+}
diff --git a/library.properties b/library.properties
index 81ed7b9..36652fc 100644
--- a/library.properties
+++ b/library.properties
@@ -1,5 +1,5 @@
name=SparkFun VL53L1X 4m Laser Distance Sensor
-version=1.2.6
+version=1.2.12
author=SparkFun Electronics
maintainer=SparkFun Electronics
sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X
diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp
index 2e07a18..4ea1cb4 100644
--- a/src/SparkFun_VL53L1X.cpp
+++ b/src/SparkFun_VL53L1X.cpp
@@ -13,20 +13,33 @@
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.
+ ==== MIT License ====
+ Copyright © 2022 SparkFun Electronics
- You should have received a copy of the GNU General Public License
- along with this program. If not, see .
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ software and associated documentation files (the “Software”), to deal in the Software without restriction,
+ including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+ OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ =====================
*/
#include
-#include "Arduino.h"
#include "SparkFun_VL53L1X.h"
-#include "vl53l1x_class.h"
+SFEVL53L1X::SFEVL53L1X()
+{
+ _i2cPort = &Wire;
+ _shutdownPin = -1;
+ _interruptPin = -1;
+ _device = new VL53L1X(&Wire, -1, -1);
+}
SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin)
{
_i2cPort = &i2cPort;
@@ -34,6 +47,10 @@ SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin)
_interruptPin = interruptPin;
_device = new VL53L1X(&i2cPort, shutdownPin, interruptPin);
}
+SFEVL53L1X::~SFEVL53L1X()
+{
+ delete (VL53L1X *)_device;
+}
bool SFEVL53L1X::init()
{
@@ -42,41 +59,51 @@ bool SFEVL53L1X::init()
bool SFEVL53L1X::begin()
{
+ if (checkID() == false)
+ return (VL53L1_ERROR_PLATFORM_SPECIFIC_START);
+
return _device->VL53L1X_SensorInit();
}
+bool SFEVL53L1X::begin(TwoWire &i2cPort)
+{
+ _i2cPort = &i2cPort;
+ _device->dev_i2c = &i2cPort;
+
+ return begin();
+}
+
/*Checks the ID of the device, returns true if ID is correct*/
bool SFEVL53L1X::checkID()
{
uint16_t sensorId;
_device->VL53L1X_GetSensorId(&sensorId);
- if (sensorId == 0xEEAC)
+ if ((sensorId == 0xEACC) || (sensorId == 0xEBAA))
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);
+ 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);
+ if (_shutdownPin >= 0)
+ {
+ digitalWrite(_shutdownPin, LOW);
+ }
+ delay(10);
}
/*Gets the software version number of the current library installed.*/
@@ -118,7 +145,7 @@ void SFEVL53L1X::setInterruptPolarityLow()
* This function gets the interrupt polarity\n
* 1=active high (default), 0=active low
*/
-
+
uint8_t SFEVL53L1X::getInterruptPolarity()
{
uint8_t tmp;
@@ -131,6 +158,11 @@ void SFEVL53L1X::startRanging()
_device->VL53L1X_StartRanging();
}
+void SFEVL53L1X::startOneshotRanging()
+{
+ _device->VL53L1X_StartOneshotRanging();
+}
+
void SFEVL53L1X::stopRanging()
{
_device->VL53L1X_StopRanging();
@@ -145,7 +177,7 @@ bool SFEVL53L1X::checkForDataReady()
void SFEVL53L1X::setTimingBudgetInMs(uint16_t timingBudget)
{
- _device->VL53L1X_SetTimingBudgetInMs(timingBudget);
+ _device->VL53L1X_SetTimingBudgetInMs(timingBudget);
}
uint16_t SFEVL53L1X::getTimingBudgetInMs()
@@ -157,12 +189,12 @@ uint16_t SFEVL53L1X::getTimingBudgetInMs()
void SFEVL53L1X::setDistanceModeLong()
{
- _device->VL53L1X_SetDistanceMode(2);
+ _device->VL53L1X_SetDistanceMode(DISTANCE_LONG);
}
void SFEVL53L1X::setDistanceModeShort()
{
- _device->VL53L1X_SetDistanceMode(1);
+ _device->VL53L1X_SetDistanceMode(DISTANCE_SHORT);
}
uint8_t SFEVL53L1X::getDistanceMode()
@@ -357,4 +389,44 @@ void SFEVL53L1X::calibrateXTalk(uint16_t targetDistanceInMm)
{
uint16_t xTalk = getXTalk();
_device->VL53L1X_CalibrateXtalk(targetDistanceInMm, &xTalk);
-};
\ No newline at end of file
+};
+
+bool SFEVL53L1X::setThresholdConfig(DetectionConfig *config)
+{
+ return _device->VL53L1X_SetDistanceMode(config->distanceMode) == VL53L1_ERROR_NONE &&
+ _device->VL53L1X_SetDistanceThreshold(config->thresholdLow, config->thresholdHigh,
+ (uint8_t)config->windowMode, (uint8_t)config->IntOnNoTarget) == VL53L1_ERROR_NONE;
+}
+
+bool SFEVL53L1X::getThresholdConfig(DetectionConfig *config)
+{
+ uint16_t temp16 = 0;
+
+ VL53L1X_ERROR error = _device->VL53L1X_GetDistanceMode(&temp16);
+ if (error != 0)
+ return false;
+ else
+ config->distanceMode = temp16;
+
+ error = _device->VL53L1X_GetDistanceThresholdWindow(&temp16);
+ if (error != 0)
+ return false;
+ else
+ config->windowMode = temp16;
+
+ config->IntOnNoTarget = 1;
+
+ error = _device->VL53L1X_GetDistanceThresholdLow(&temp16);
+ if (error != 0)
+ return false;
+ else
+ config->thresholdLow = temp16;
+
+ error = _device->VL53L1X_GetDistanceThresholdHigh(&temp16);
+ if (error != 0)
+ return false;
+ else
+ config->thresholdHigh = temp16;
+
+ return true;
+}
diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h
index 712233d..fbb940a 100644
--- a/src/SparkFun_VL53L1X.h
+++ b/src/SparkFun_VL53L1X.h
@@ -13,29 +13,59 @@
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.
+ ==== MIT License ====
+ Copyright © 2022 SparkFun Electronics
- You should have received a copy of the GNU General Public License
- along with this program. If not, see .
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ software and associated documentation files (the “Software”), to deal in the Software without restriction,
+ including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+ OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ =====================
*/
#pragma once
#include "Arduino.h"
#include "Wire.h"
-#include "RangeSensor.h"
-#include "vl53l1_error_codes.h"
-#include "vl53l1x_class.h"
+#include "st_src/RangeSensor.h"
+#include "st_src/vl53l1_error_codes.h"
+#include "st_src/vl53l1x_class.h"
+#include "st_src/ComponentObject.h"
+#include "st_src/RangeSensor.h"
+
+#define DISTANCE_SHORT 1
+#define DISTANCE_LONG 2
+#define WINDOW_BELOW 0
+#define WINDOW_ABOVE 1
+#define WINDOW_OUT 2
+#define WINDOW_IN 3
+
+struct DetectionConfig
+{
+ uint16_t distanceMode = DISTANCE_SHORT; // distance mode : DISTANCE_SHORT (0) or DISTANCE_LONG (1)
+ uint16_t windowMode = WINDOW_IN; // window mode : WINDOW_BELOW (0), WINDOW_ABOVE (1), WINDOW_OUT (2), WINDOW_IN (3)
+ uint8_t IntOnNoTarget = 1; // = 1 (No longer used - just use 1)
+ uint16_t thresholdHigh = 0; // (in mm) : the threshold above which one the device raises an interrupt if Window = 1
+ uint16_t thresholdLow = 0; // (in mm) : the threshold under which one the device raises an interrupt if Window = 0
+};
class SFEVL53L1X
{
public:
- SFEVL53L1X(TwoWire &i2cPort = Wire, int shutdownPin = -1, int interruptPin = -1); //Constructs our Distance sensor without an interrupt or shutdown pin
+ //Constructs our Distance sensor
+ SFEVL53L1X(); // Default to Wire. Set both pins to -1 (undefined).
+ SFEVL53L1X(TwoWire &i2cPort, int shutdownPin = -1, int interruptPin = -1);
+ ~SFEVL53L1X();
bool init(); //Deprecated version of begin
bool begin(); //Initialization of sensor
+ bool begin(TwoWire &i2cPort); //Initialization of sensor
bool checkID(); //Check the ID of the sensor, returns true if ID is correct
void sensorOn(); //Toggles shutdown pin to turn sensor on and off
void sensorOff(); //Toggles shutdown pin to turn sensor on and off
@@ -47,6 +77,7 @@ class SFEVL53L1X
void setInterruptPolarityLow(); //Set the polarity of an active interrupt to Low
uint8_t getInterruptPolarity(); //get the current interrupt polarity
void startRanging(); //Begins taking measurements
+ void startOneshotRanging(); // Start one-shot ranging
void stopRanging(); //Stops taking measurements
bool checkForDataReady(); //Checks the to see if data is ready
void setTimingBudgetInMs(uint16_t timingBudget); //Set the timing budget for a measurement
@@ -108,10 +139,13 @@ class SFEVL53L1X
void startTemperatureUpdate(); //Recalibrates the sensor for temperature changes. Run this any time the temperature has changed by more than 8°C
void calibrateOffset(uint16_t targetDistanceInMm); //Autocalibrate the offset by placing a target a known distance away from the sensor and passing this known distance into the function.
void calibrateXTalk(uint16_t targetDistanceInMm); //Autocalibrate the crosstalk by placing a target a known distance away from the sensor and passing this known distance into the function.
+ bool setThresholdConfig(DetectionConfig* config); //Sets threshold configuration struct
+ bool getThresholdConfig(DetectionConfig* config); //Gets current threshold settings. Returns false on error, true otherwise. Stores results in pointer to struct argument.
+
private:
TwoWire *_i2cPort;
int _shutdownPin;
int _interruptPin;
int _i2cAddress = 0x52;
VL53L1X* _device;
-};
\ No newline at end of file
+};
diff --git a/src/ComponentObject.h b/src/st_src/ComponentObject.h
similarity index 100%
rename from src/ComponentObject.h
rename to src/st_src/ComponentObject.h
diff --git a/src/st_src/LICENSE.md b/src/st_src/LICENSE.md
new file mode 100644
index 0000000..5c9ff4a
--- /dev/null
+++ b/src/st_src/LICENSE.md
@@ -0,0 +1,23 @@
+COPYRIGHT(c) 2018 STMicroelectronics
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+ 3. Neither the name of STMicroelectronics nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/src/RangeSensor.h b/src/st_src/RangeSensor.h
similarity index 98%
rename from src/RangeSensor.h
rename to src/st_src/RangeSensor.h
index 119112e..0643268 100644
--- a/src/RangeSensor.h
+++ b/src/st_src/RangeSensor.h
@@ -41,7 +41,7 @@
#define __RANGE_SENSOR_CLASS_H
/* Includes ------------------------------------------------------------------*/
-#include
+#include
/* Classes ------------------------------------------------------------------*/
/** An abstract class for range sensors
diff --git a/src/vl53l1_error_codes.h b/src/st_src/vl53l1_error_codes.h
similarity index 100%
rename from src/vl53l1_error_codes.h
rename to src/st_src/vl53l1_error_codes.h
diff --git a/src/vl53l1x_class.cpp b/src/st_src/vl53l1x_class.cpp
similarity index 56%
rename from src/vl53l1x_class.cpp
rename to src/st_src/vl53l1x_class.cpp
index cf7bb7b..4d161aa 100644
--- a/src/vl53l1x_class.cpp
+++ b/src/st_src/vl53l1x_class.cpp
@@ -40,113 +40,107 @@
#include "Arduino.h"
#include "vl53l1x_class.h"
-
-#define ALGO__PART_TO_PART_RANGE_OFFSET_MM 0x001E
-#define MM_CONFIG__INNER_OFFSET_MM 0x0020
-#define MM_CONFIG__OUTER_OFFSET_MM 0x0022
-
+#define ALGO__PART_TO_PART_RANGE_OFFSET_MM 0x001E
+#define MM_CONFIG__INNER_OFFSET_MM 0x0020
+#define MM_CONFIG__OUTER_OFFSET_MM 0x0022
//#define DEBUG_MODE
-
const uint8_t VL51L1X_DEFAULT_CONFIGURATION[] = {
-0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch */
-0x01, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
-0x01, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
-0x01, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1), use SetInterruptPolarity() */
-0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, use CheckForDataReady() */
-0x00, /* 0x32 : not user-modifiable */
-0x02, /* 0x33 : not user-modifiable */
-0x08, /* 0x34 : not user-modifiable */
-0x00, /* 0x35 : not user-modifiable */
-0x08, /* 0x36 : not user-modifiable */
-0x10, /* 0x37 : not user-modifiable */
-0x01, /* 0x38 : not user-modifiable */
-0x01, /* 0x39 : not user-modifiable */
-0x00, /* 0x3a : not user-modifiable */
-0x00, /* 0x3b : not user-modifiable */
-0x00, /* 0x3c : not user-modifiable */
-0x00, /* 0x3d : not user-modifiable */
-0xff, /* 0x3e : not user-modifiable */
-0x00, /* 0x3f : not user-modifiable */
-0x0F, /* 0x40 : not user-modifiable */
-0x00, /* 0x41 : not user-modifiable */
-0x00, /* 0x42 : not user-modifiable */
-0x00, /* 0x43 : not user-modifiable */
-0x00, /* 0x44 : not user-modifiable */
-0x00, /* 0x45 : not user-modifiable */
-0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */
-0x0b, /* 0x47 : not user-modifiable */
-0x00, /* 0x48 : not user-modifiable */
-0x00, /* 0x49 : not user-modifiable */
-0x02, /* 0x4a : not user-modifiable */
-0x0a, /* 0x4b : not user-modifiable */
-0x21, /* 0x4c : not user-modifiable */
-0x00, /* 0x4d : not user-modifiable */
-0x00, /* 0x4e : not user-modifiable */
-0x05, /* 0x4f : not user-modifiable */
-0x00, /* 0x50 : not user-modifiable */
-0x00, /* 0x51 : not user-modifiable */
-0x00, /* 0x52 : not user-modifiable */
-0x00, /* 0x53 : not user-modifiable */
-0xc8, /* 0x54 : not user-modifiable */
-0x00, /* 0x55 : not user-modifiable */
-0x00, /* 0x56 : not user-modifiable */
-0x38, /* 0x57 : not user-modifiable */
-0xff, /* 0x58 : not user-modifiable */
-0x01, /* 0x59 : not user-modifiable */
-0x00, /* 0x5a : not user-modifiable */
-0x08, /* 0x5b : not user-modifiable */
-0x00, /* 0x5c : not user-modifiable */
-0x00, /* 0x5d : not user-modifiable */
-0x01, /* 0x5e : not user-modifiable */
-0xdb, /* 0x5f : not user-modifiable */
-0x0f, /* 0x60 : not user-modifiable */
-0x01, /* 0x61 : not user-modifiable */
-0xf1, /* 0x62 : not user-modifiable */
-0x0d, /* 0x63 : not user-modifiable */
-0x01, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), use SetSigmaThreshold(), default value 90 mm */
-0x68, /* 0x65 : Sigma threshold LSB */
-0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), use SetSignalThreshold() */
-0x80, /* 0x67 : Min count Rate LSB */
-0x08, /* 0x68 : not user-modifiable */
-0xb8, /* 0x69 : not user-modifiable */
-0x00, /* 0x6a : not user-modifiable */
-0x00, /* 0x6b : not user-modifiable */
-0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, use SetIntermeasurementInMs() */
-0x00, /* 0x6d : Intermeasurement period */
-0x0f, /* 0x6e : Intermeasurement period */
-0x89, /* 0x6f : Intermeasurement period LSB */
-0x00, /* 0x70 : not user-modifiable */
-0x00, /* 0x71 : not user-modifiable */
-0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), use SetD:tanceThreshold() */
-0x00, /* 0x73 : distance threshold high LSB */
-0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), use SetD:tanceThreshold() */
-0x00, /* 0x75 : distance threshold low LSB */
-0x00, /* 0x76 : not user-modifiable */
-0x01, /* 0x77 : not user-modifiable */
-0x0f, /* 0x78 : not user-modifiable */
-0x0d, /* 0x79 : not user-modifiable */
-0x0e, /* 0x7a : not user-modifiable */
-0x0e, /* 0x7b : not user-modifiable */
-0x00, /* 0x7c : not user-modifiable */
-0x00, /* 0x7d : not user-modifiable */
-0x02, /* 0x7e : not user-modifiable */
-0xc7, /* 0x7f : ROI center, use SetROI() */
-0xff, /* 0x80 : XY ROI (X=Width, Y=Height), use SetROI() */
-0x9B, /* 0x81 : not user-modifiable */
-0x00, /* 0x82 : not user-modifiable */
-0x00, /* 0x83 : not user-modifiable */
-0x00, /* 0x84 : not user-modifiable */
-0x01, /* 0x85 : not user-modifiable */
-0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */
-0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), If you want an automatic start after VL53L1X_init() call, put 0x40 in location 0x87 */
+ 0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch */
+ 0x01, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
+ 0x01, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
+ 0x01, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1), use SetInterruptPolarity() */
+ 0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, use CheckForDataReady() */
+ 0x00, /* 0x32 : not user-modifiable */
+ 0x02, /* 0x33 : not user-modifiable */
+ 0x08, /* 0x34 : not user-modifiable */
+ 0x00, /* 0x35 : not user-modifiable */
+ 0x08, /* 0x36 : not user-modifiable */
+ 0x10, /* 0x37 : not user-modifiable */
+ 0x01, /* 0x38 : not user-modifiable */
+ 0x01, /* 0x39 : not user-modifiable */
+ 0x00, /* 0x3a : not user-modifiable */
+ 0x00, /* 0x3b : not user-modifiable */
+ 0x00, /* 0x3c : not user-modifiable */
+ 0x00, /* 0x3d : not user-modifiable */
+ 0xff, /* 0x3e : not user-modifiable */
+ 0x00, /* 0x3f : not user-modifiable */
+ 0x0F, /* 0x40 : not user-modifiable */
+ 0x00, /* 0x41 : not user-modifiable */
+ 0x00, /* 0x42 : not user-modifiable */
+ 0x00, /* 0x43 : not user-modifiable */
+ 0x00, /* 0x44 : not user-modifiable */
+ 0x00, /* 0x45 : not user-modifiable */
+ 0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */
+ 0x0b, /* 0x47 : not user-modifiable */
+ 0x00, /* 0x48 : not user-modifiable */
+ 0x00, /* 0x49 : not user-modifiable */
+ 0x02, /* 0x4a : not user-modifiable */
+ 0x0a, /* 0x4b : not user-modifiable */
+ 0x21, /* 0x4c : not user-modifiable */
+ 0x00, /* 0x4d : not user-modifiable */
+ 0x00, /* 0x4e : not user-modifiable */
+ 0x05, /* 0x4f : not user-modifiable */
+ 0x00, /* 0x50 : not user-modifiable */
+ 0x00, /* 0x51 : not user-modifiable */
+ 0x00, /* 0x52 : not user-modifiable */
+ 0x00, /* 0x53 : not user-modifiable */
+ 0xc8, /* 0x54 : not user-modifiable */
+ 0x00, /* 0x55 : not user-modifiable */
+ 0x00, /* 0x56 : not user-modifiable */
+ 0x38, /* 0x57 : not user-modifiable */
+ 0xff, /* 0x58 : not user-modifiable */
+ 0x01, /* 0x59 : not user-modifiable */
+ 0x00, /* 0x5a : not user-modifiable */
+ 0x08, /* 0x5b : not user-modifiable */
+ 0x00, /* 0x5c : not user-modifiable */
+ 0x00, /* 0x5d : not user-modifiable */
+ 0x01, /* 0x5e : not user-modifiable */
+ 0xdb, /* 0x5f : not user-modifiable */
+ 0x0f, /* 0x60 : not user-modifiable */
+ 0x01, /* 0x61 : not user-modifiable */
+ 0xf1, /* 0x62 : not user-modifiable */
+ 0x0d, /* 0x63 : not user-modifiable */
+ 0x01, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), use SetSigmaThreshold(), default value 90 mm */
+ 0x68, /* 0x65 : Sigma threshold LSB */
+ 0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), use SetSignalThreshold() */
+ 0x80, /* 0x67 : Min count Rate LSB */
+ 0x08, /* 0x68 : not user-modifiable */
+ 0xb8, /* 0x69 : not user-modifiable */
+ 0x00, /* 0x6a : not user-modifiable */
+ 0x00, /* 0x6b : not user-modifiable */
+ 0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, use SetIntermeasurementInMs() */
+ 0x00, /* 0x6d : Intermeasurement period */
+ 0x0f, /* 0x6e : Intermeasurement period */
+ 0x89, /* 0x6f : Intermeasurement period LSB */
+ 0x00, /* 0x70 : not user-modifiable */
+ 0x00, /* 0x71 : not user-modifiable */
+ 0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), use SetD:tanceThreshold() */
+ 0x00, /* 0x73 : distance threshold high LSB */
+ 0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), use SetD:tanceThreshold() */
+ 0x00, /* 0x75 : distance threshold low LSB */
+ 0x00, /* 0x76 : not user-modifiable */
+ 0x01, /* 0x77 : not user-modifiable */
+ 0x0f, /* 0x78 : not user-modifiable */
+ 0x0d, /* 0x79 : not user-modifiable */
+ 0x0e, /* 0x7a : not user-modifiable */
+ 0x0e, /* 0x7b : not user-modifiable */
+ 0x00, /* 0x7c : not user-modifiable */
+ 0x00, /* 0x7d : not user-modifiable */
+ 0x02, /* 0x7e : not user-modifiable */
+ 0xc7, /* 0x7f : ROI center, use SetROI() */
+ 0xff, /* 0x80 : XY ROI (X=Width, Y=Height), use SetROI() */
+ 0x9B, /* 0x81 : not user-modifiable */
+ 0x00, /* 0x82 : not user-modifiable */
+ 0x00, /* 0x83 : not user-modifiable */
+ 0x00, /* 0x84 : not user-modifiable */
+ 0x01, /* 0x85 : not user-modifiable */
+ 0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */
+ 0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), If you want an automatic start after VL53L1X_init() call, put 0x40 in location 0x87 */
};
-
-
- /* VL53L1X_api.h functions */
-
+/* VL53L1X_api.h functions */
VL53L1X_ERROR VL53L1X::VL53L1X_GetSWVersion(VL53L1X_Version_t *pVersion)
{
@@ -166,37 +160,36 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetI2CAddress(uint8_t new_address)
status = VL53L1_WrByte(Device, VL53L1_I2C_SLAVE__DEVICE_ADDRESS, new_address >> 1);
Device->I2cDevAddr = new_address;
-
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SensorInit()
{
VL53L1X_ERROR status = 0;
- uint8_t Addr = 0x00, tmp=0, timeout = 0;
+ uint8_t Addr = 0x00, dataReady = 0, timeout = 0;
- for (Addr = 0x2D; Addr <= 0x87; Addr++){
+ for (Addr = 0x2D; Addr <= 0x87; Addr++)
+ {
status = VL53L1_WrByte(Device, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]);
}
status = VL53L1X_StartRanging();
- while(tmp==0){
- status = VL53L1X_CheckForDataReady(&tmp);
- timeout++;
- if (timeout > 50)
- {
- status = VL53L1_ERROR_TIME_OUT;
- return status;
- }
+
+ //We need to wait at least the default intermeasurement period of 103ms before dataready will occur
+ //But if a unit has already been powered and polling, it may happen much faster
+ while (dataReady == 0)
+ {
+ status = VL53L1X_CheckForDataReady(&dataReady);
+ if (timeout++ > 150)
+ return VL53L1_ERROR_TIME_OUT;
+ delay(1);
}
- tmp = 0;
status = VL53L1X_ClearInterrupt();
status = VL53L1X_StopRanging();
status = VL53L1_WrByte(Device, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
- status = VL53L1_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */
+ status = VL53L1_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_ClearInterrupt()
{
VL53L1X_ERROR status = 0;
@@ -205,7 +198,6 @@ VL53L1X_ERROR VL53L1X::VL53L1X_ClearInterrupt()
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_SetInterruptPolarity(uint8_t NewPolarity)
{
uint8_t Temp;
@@ -217,8 +209,6 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetInterruptPolarity(uint8_t NewPolarity)
return status;
}
-
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetInterruptPolarity(uint8_t *pInterruptPolarity)
{
uint8_t Temp;
@@ -226,30 +216,36 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetInterruptPolarity(uint8_t *pInterruptPolarity)
status = VL53L1_RdByte(Device, GPIO_HV_MUX__CTRL, &Temp);
Temp = Temp & 0x10;
- *pInterruptPolarity = !(Temp>>4);
+ *pInterruptPolarity = !(Temp >> 4);
return status;
}
-
-
VL53L1X_ERROR VL53L1X::VL53L1X_StartRanging()
{
VL53L1X_ERROR status = 0;
+ VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01); /* clear interrupt trigger */
+ status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */
+ return status;
+}
- status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */
+
+VL53L1X_ERROR VL53L1X::VL53L1X_StartOneshotRanging()
+{
+ VL53L1X_ERROR status = 0;
+ VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01); /* clear interrupt trigger */
+ status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x10); /* Enable VL53L1X one-shot ranging */
return status;
}
+
VL53L1X_ERROR VL53L1X::VL53L1X_StopRanging()
{
VL53L1X_ERROR status = 0;
- status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x00); /* Disable VL53L1X */
+ status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x00); /* Disable VL53L1X */
return status;
}
-
-
VL53L1X_ERROR VL53L1X::VL53L1X_CheckForDataReady(uint8_t *isDataReady)
{
uint8_t Temp;
@@ -259,7 +255,8 @@ VL53L1X_ERROR VL53L1X::VL53L1X_CheckForDataReady(uint8_t *isDataReady)
status = VL53L1X_GetInterruptPolarity(&IntPol);
status = VL53L1_RdByte(Device, GPIO__TIO_HV_STATUS, &Temp);
/* Read in the register to check if a new value is available */
- if (status == 0){
+ if (status == 0)
+ {
if ((Temp & 1) == IntPol)
*isDataReady = 1;
else
@@ -268,100 +265,104 @@ VL53L1X_ERROR VL53L1X::VL53L1X_CheckForDataReady(uint8_t *isDataReady)
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_SetTimingBudgetInMs(uint16_t TimingBudgetInMs)
{
uint16_t DM;
- VL53L1X_ERROR status=0;
+ VL53L1X_ERROR status = 0;
status = VL53L1X_GetDistanceMode(&DM);
if (DM == 0)
return 1;
- else if (DM == 1) { /* Short DistanceMode */
- switch (TimingBudgetInMs) {
+ else if (DM == 1)
+ { /* Short DistanceMode */
+ switch (TimingBudgetInMs)
+ {
case 15: /* only available in short distance mode */
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x01D);
+ 0x01D);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x0027);
+ 0x0027);
break;
case 20:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x0051);
+ 0x0051);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x006E);
+ 0x006E);
break;
case 33:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x00D6);
+ 0x00D6);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x006E);
+ 0x006E);
break;
case 50:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x1AE);
+ 0x1AE);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x01E8);
+ 0x01E8);
break;
case 100:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x02E1);
+ 0x02E1);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x0388);
+ 0x0388);
break;
case 200:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x03E1);
+ 0x03E1);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x0496);
+ 0x0496);
break;
case 500:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x0591);
+ 0x0591);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x05C1);
+ 0x05C1);
break;
default:
status = 1;
break;
}
- } else {
- switch (TimingBudgetInMs) {
+ }
+ else
+ {
+ switch (TimingBudgetInMs)
+ {
case 20:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x001E);
+ 0x001E);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x0022);
+ 0x0022);
break;
case 33:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x0060);
+ 0x0060);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x006E);
+ 0x006E);
break;
case 50:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x00AD);
+ 0x00AD);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x00C6);
+ 0x00C6);
break;
case 100:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x01CC);
+ 0x01CC);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x01EA);
+ 0x01EA);
break;
case 200:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x02D9);
+ 0x02D9);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x02F8);
+ 0x02F8);
break;
case 500:
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
- 0x048F);
+ 0x048F);
VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
- 0x04A4);
+ 0x04A4);
break;
default:
status = 1;
@@ -377,49 +378,50 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetTimingBudgetInMs(uint16_t *pTimingBudget)
VL53L1X_ERROR status = 0;
status = VL53L1_RdWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, &Temp);
- switch (Temp) {
- case 0x001D :
- *pTimingBudget = 15;
- break;
- case 0x0051 :
- case 0x001E :
- *pTimingBudget = 20;
- break;
- case 0x00D6 :
- case 0x0060 :
- *pTimingBudget = 33;
- break;
- case 0x1AE :
- case 0x00AD :
- *pTimingBudget = 50;
- break;
- case 0x02E1 :
- case 0x01CC :
- *pTimingBudget = 100;
- break;
- case 0x03E1 :
- case 0x02D9 :
- *pTimingBudget = 200;
- break;
- case 0x0591 :
- case 0x048F :
- *pTimingBudget = 500;
- break;
- default:
- *pTimingBudget = 0;
- break;
+ switch (Temp)
+ {
+ case 0x001D:
+ *pTimingBudget = 15;
+ break;
+ case 0x0051:
+ case 0x001E:
+ *pTimingBudget = 20;
+ break;
+ case 0x00D6:
+ case 0x0060:
+ *pTimingBudget = 33;
+ break;
+ case 0x1AE:
+ case 0x00AD:
+ *pTimingBudget = 50;
+ break;
+ case 0x02E1:
+ case 0x01CC:
+ *pTimingBudget = 100;
+ break;
+ case 0x03E1:
+ case 0x02D9:
+ *pTimingBudget = 200;
+ break;
+ case 0x0591:
+ case 0x048F:
+ *pTimingBudget = 500;
+ break;
+ default:
+ *pTimingBudget = 0;
+ break;
}
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_SetDistanceMode(uint16_t DM)
{
uint16_t TB;
VL53L1X_ERROR status = 0;
status = VL53L1X_GetTimingBudgetInMs(&TB);
- switch (DM) {
+ switch (DM)
+ {
case 1:
status = VL53L1_WrByte(Device, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x14);
status = VL53L1_WrByte(Device, RANGE_CONFIG__VCSEL_PERIOD_A, 0x07);
@@ -443,63 +445,54 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetDistanceMode(uint16_t DM)
return status;
}
-
-
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceMode(uint16_t *DM)
{
- uint8_t TempDM, status=0;
+ uint8_t TempDM, status = 0;
- status = VL53L1_RdByte(Device,PHASECAL_CONFIG__TIMEOUT_MACROP, &TempDM);
+ status = VL53L1_RdByte(Device, PHASECAL_CONFIG__TIMEOUT_MACROP, &TempDM);
if (TempDM == 0x14)
- *DM=1;
- if(TempDM == 0x0A)
- *DM=2;
+ *DM = 1;
+ if (TempDM == 0x0A)
+ *DM = 2;
return status;
}
-
-
VL53L1X_ERROR VL53L1X::VL53L1X_SetInterMeasurementInMs(uint16_t InterMeasMs)
{
uint16_t ClockPLL;
VL53L1X_ERROR status = 0;
status = VL53L1_RdWord(Device, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
- ClockPLL = ClockPLL&0x3FF;
+ ClockPLL = ClockPLL & 0x3FF;
VL53L1_WrDWord(Device, VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD,
- (uint32_t)(ClockPLL * InterMeasMs * 1.075));
+ (uint32_t)(ClockPLL * InterMeasMs * 1.075));
return status;
-
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetInterMeasurementInMs(uint16_t *pIM)
{
uint16_t ClockPLL;
VL53L1X_ERROR status = 0;
uint32_t tmp;
- status = VL53L1_RdDWord(Device,VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD, &tmp);
+ status = VL53L1_RdDWord(Device, VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD, &tmp);
*pIM = (uint16_t)tmp;
status = VL53L1_RdWord(Device, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
- ClockPLL = ClockPLL&0x3FF;
- *pIM= (uint16_t)(*pIM/(ClockPLL*1.065));
+ ClockPLL = ClockPLL & 0x3FF;
+ *pIM = (uint16_t)(*pIM / (ClockPLL * 1.065));
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_BootState(uint8_t *state)
{
VL53L1X_ERROR status = 0;
uint8_t tmp = 0;
- status = VL53L1_RdByte(Device,VL53L1_FIRMWARE__SYSTEM_STATUS, &tmp);
+ status = VL53L1_RdByte(Device, VL53L1_FIRMWARE__SYSTEM_STATUS, &tmp);
*state = tmp;
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetSensorId(uint16_t *sensorId)
{
VL53L1X_ERROR status = 0;
@@ -510,14 +503,13 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetSensorId(uint16_t *sensorId)
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetDistance(uint16_t *distance)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = (VL53L1_RdWord(Device,
- VL53L1_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0, &tmp));
+ VL53L1_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0, &tmp));
*distance = tmp;
return status;
}
@@ -525,72 +517,68 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetDistance(uint16_t *distance)
VL53L1X_ERROR VL53L1X::VL53L1X_GetSignalPerSpad(uint16_t *signalRate)
{
VL53L1X_ERROR status = 0;
- uint16_t SpNb=1, signal;
+ uint16_t SpNb = 1, signal;
status = VL53L1_RdWord(Device,
- VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &signal);
+ VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &signal);
status = VL53L1_RdWord(Device,
- VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb);
- *signalRate = (uint16_t) (2000.0*signal/SpNb);
+ VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb);
+ *signalRate = (uint16_t)(2000.0 * signal / SpNb);
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetAmbientPerSpad(uint16_t *ambPerSp)
{
- VL53L1X_ERROR status=0;
- uint16_t AmbientRate, SpNb=1;
+ VL53L1X_ERROR status = 0;
+ uint16_t AmbientRate, SpNb = 1;
status = VL53L1_RdWord(Device, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &AmbientRate);
status = VL53L1_RdWord(Device, VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb);
- *ambPerSp=(uint16_t) (2000.0 * AmbientRate / SpNb);
+ *ambPerSp = (uint16_t)(2000.0 * AmbientRate / SpNb);
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetSignalRate(uint16_t *signal)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1_RdWord(Device,
- VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &tmp);
- *signal = tmp*8;
+ VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &tmp);
+ *signal = tmp * 8;
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetSpadNb(uint16_t *spNb)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1_RdWord(Device,
- VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &tmp);
+ VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &tmp);
*spNb = tmp >> 8;
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetAmbientRate(uint16_t *ambRate)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
status = VL53L1_RdWord(Device, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &tmp);
- *ambRate = tmp*8;
+ *ambRate = tmp * 8;
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetRangeStatus(uint8_t *rangeStatus)
{
VL53L1X_ERROR status = 0;
uint8_t RgSt;
status = VL53L1_RdByte(Device, VL53L1_RESULT__RANGE_STATUS, &RgSt);
- RgSt = RgSt&0x1F;
- switch (RgSt) {
+ RgSt = RgSt & 0x1F;
+ switch (RgSt)
+ {
case 9:
RgSt = 0;
break;
@@ -638,98 +626,95 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetRangeStatus(uint8_t *rangeStatus)
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_SetOffset(int16_t OffsetValue)
{
VL53L1X_ERROR status = 0;
int16_t Temp;
- Temp = (OffsetValue*4);
+ Temp = (OffsetValue * 4);
VL53L1_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM,
- (uint16_t)Temp);
+ (uint16_t)Temp);
VL53L1_WrWord(Device, MM_CONFIG__INNER_OFFSET_MM, 0x0);
VL53L1_WrWord(Device, MM_CONFIG__OUTER_OFFSET_MM, 0x0);
return status;
}
-
-VL53L1X_ERROR VL53L1X::VL53L1X_GetOffset(int16_t *offset)
+VL53L1X_ERROR VL53L1X::VL53L1X_GetOffset(int16_t *offset)
{
VL53L1X_ERROR status = 0;
uint16_t Temp;
- status = VL53L1_RdWord(Device,ALGO__PART_TO_PART_RANGE_OFFSET_MM, &Temp);
- Temp = Temp<<3;
- Temp = Temp >>5;
+ status = VL53L1_RdWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, &Temp);
+ Temp = Temp << 3;
+ Temp = Temp >> 5;
*offset = (int16_t)(Temp);
return status;
}
VL53L1X_ERROR VL53L1X::VL53L1X_SetXtalk(uint16_t XtalkValue)
{
-/* XTalkValue in count per second to avoid float type */
+ /* XTalkValue in count per second to avoid float type */
VL53L1X_ERROR status = 0;
status = VL53L1_WrWord(Device,
- ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS,
- 0x0000);
+ ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS,
+ 0x0000);
status = VL53L1_WrWord(Device, ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS,
- 0x0000);
+ 0x0000);
status = VL53L1_WrWord(Device, ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS,
- (XtalkValue<<9)/1000); /* * << 9 (7.9 format) and /1000 to convert cps to kpcs */
+ (XtalkValue << 9) / 1000); /* * << 9 (7.9 format) and /1000 to convert cps to kpcs */
return status;
}
-
-VL53L1X_ERROR VL53L1X::VL53L1X_GetXtalk(uint16_t *xtalk )
+VL53L1X_ERROR VL53L1X::VL53L1X_GetXtalk(uint16_t *xtalk)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
- status = VL53L1_RdWord(Device,ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS, &tmp);
- *xtalk = (tmp*1000)>>9; /* * 1000 to convert kcps to cps and >> 9 (7.9 format) */
+ status = VL53L1_RdWord(Device, ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS, &tmp);
+ *xtalk = (tmp * 1000) >> 9; /* * 1000 to convert kcps to cps and >> 9 (7.9 format) */
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_SetDistanceThreshold(uint16_t ThreshLow,
- uint16_t ThreshHigh, uint8_t Window,
- uint8_t IntOnNoTarget)
+ uint16_t ThreshHigh, uint8_t Window,
+ uint8_t IntOnNoTarget)
{
VL53L1X_ERROR status = 0;
uint8_t Temp = 0;
status = VL53L1_RdByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO, &Temp);
Temp = Temp & 0x47;
- if (IntOnNoTarget == 0) {
+ if (IntOnNoTarget == 0)
+ {
status = VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO,
- (Temp | (Window & 0x07)));
- } else {
+ (Temp | (Window & 0x07)));
+ }
+ else
+ {
status = VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO,
- ((Temp | (Window & 0x07)) | 0x40));
+ ((Temp | (Window & 0x07)) | 0x40));
}
status = VL53L1_WrWord(Device, SYSTEM__THRESH_HIGH, ThreshHigh);
status = VL53L1_WrWord(Device, SYSTEM__THRESH_LOW, ThreshLow);
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceThresholdWindow(uint16_t *window)
{
VL53L1X_ERROR status = 0;
uint8_t tmp;
- status = VL53L1_RdByte(Device,SYSTEM__INTERRUPT_CONFIG_GPIO, &tmp);
+ status = VL53L1_RdByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO, &tmp);
*window = (uint16_t)(tmp & 0x7);
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceThresholdLow(uint16_t *low)
{
VL53L1X_ERROR status = 0;
uint16_t tmp;
- status = VL53L1_RdWord(Device,SYSTEM__THRESH_LOW, &tmp);
+ status = VL53L1_RdWord(Device, SYSTEM__THRESH_LOW, &tmp);
*low = tmp;
return status;
}
@@ -739,7 +724,7 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceThresholdHigh(uint16_t *high)
VL53L1X_ERROR status = 0;
uint16_t tmp;
- status = VL53L1_RdWord(Device,SYSTEM__THRESH_HIGH, &tmp);
+ status = VL53L1_RdWord(Device, SYSTEM__THRESH_HIGH, &tmp);
*high = tmp;
return status;
}
@@ -752,12 +737,13 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetROI(uint8_t X, uint8_t Y, uint8_t opticalCente
X = 16;
if (Y > 16)
Y = 16;
- if (X > 10 || Y > 10){
+ if (X > 10 || Y > 10)
+ {
opticalCenter = 199;
}
status = VL53L1_WrByte(Device, ROI_CONFIG__USER_ROI_CENTRE_SPAD, opticalCenter);
status = VL53L1_WrByte(Device, ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE,
- (Y - 1) << 4 | (X - 1));
+ (Y - 1) << 4 | (X - 1));
return status;
}
@@ -766,7 +752,7 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetROI_XY(uint16_t *ROI_X, uint16_t *ROI_Y)
VL53L1X_ERROR status = 0;
uint8_t tmp;
- status = VL53L1_RdByte(Device,ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE, &tmp);
+ status = VL53L1_RdByte(Device, ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE, &tmp);
*ROI_X = ((uint16_t)tmp & 0x0F) + 1;
*ROI_Y = (((uint16_t)tmp & 0xF0) >> 4) + 1;
return status;
@@ -776,7 +762,7 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetSignalThreshold(uint16_t Signal)
{
VL53L1X_ERROR status = 0;
- VL53L1_WrWord(Device,RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS,Signal>>3);
+ VL53L1_WrWord(Device, RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS, Signal >> 3);
return status;
}
@@ -786,21 +772,21 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetSignalThreshold(uint16_t *signal)
uint16_t tmp;
status = VL53L1_RdWord(Device,
- RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp);
- *signal = tmp <<3;
+ RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp);
+ *signal = tmp << 3;
return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1X_SetSigmaThreshold(uint16_t Sigma)
{
VL53L1X_ERROR status = 0;
- if(Sigma>(0xFFFF>>2)){
+ if (Sigma > (0xFFFF >> 2))
+ {
return 1;
}
/* 16 bits register 14.2 format */
- status = VL53L1_WrWord(Device,RANGE_CONFIG__SIGMA_THRESH,Sigma<<2);
+ status = VL53L1_WrWord(Device, RANGE_CONFIG__SIGMA_THRESH, Sigma << 2);
return status;
}
@@ -809,33 +795,33 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetSigmaThreshold(uint16_t *sigma)
VL53L1X_ERROR status = 0;
uint16_t tmp;
- status = VL53L1_RdWord(Device,RANGE_CONFIG__SIGMA_THRESH, &tmp);
+ status = VL53L1_RdWord(Device, RANGE_CONFIG__SIGMA_THRESH, &tmp);
*sigma = tmp >> 2;
return status;
-
}
VL53L1X_ERROR VL53L1X::VL53L1X_StartTemperatureUpdate()
{
VL53L1X_ERROR status = 0;
- uint8_t tmp=0;
+ uint8_t tmp = 0;
- status = VL53L1_WrByte(Device,VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND,0x81); /* full VHV */
- status = VL53L1_WrByte(Device,0x0B,0x92);
+ status = VL53L1_WrByte(Device, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x81); /* full VHV */
+ status = VL53L1_WrByte(Device, 0x0B, 0x92);
status = VL53L1X_StartRanging();
- while(tmp==0){
+ while (tmp == 0)
+ {
status = VL53L1X_CheckForDataReady(&tmp);
}
- tmp = 0;
+ tmp = 0;
status = VL53L1X_ClearInterrupt();
status = VL53L1X_StopRanging();
status = VL53L1_WrByte(Device, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
- status = VL53L1_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */
+ status = VL53L1_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */
return status;
}
- /* VL53L1X_calibration.h functions */
-
+/* VL53L1X_calibration.h functions */
+
int8_t VL53L1X::VL53L1X_CalibrateOffset(uint16_t TargetDistInMm, int16_t *offset)
{
uint8_t i = 0, tmp;
@@ -846,9 +832,11 @@ int8_t VL53L1X::VL53L1X_CalibrateOffset(uint16_t TargetDistInMm, int16_t *offset
status = VL53L1_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, 0x0);
status = VL53L1_WrWord(Device, MM_CONFIG__INNER_OFFSET_MM, 0x0);
status = VL53L1_WrWord(Device, MM_CONFIG__OUTER_OFFSET_MM, 0x0);
- status = VL53L1X_StartRanging(); /* Enable VL53L1X sensor */
- for (i = 0; i < 50; i++) {
- while (tmp == 0){
+ status = VL53L1X_StartRanging(); /* Enable VL53L1X sensor */
+ for (i = 0; i < 50; i++)
+ {
+ while (tmp == 0)
+ {
status = VL53L1X_CheckForDataReady(&tmp);
}
tmp = 0;
@@ -859,14 +847,13 @@ int8_t VL53L1X::VL53L1X_CalibrateOffset(uint16_t TargetDistInMm, int16_t *offset
status = VL53L1X_StopRanging();
AverageDistance = AverageDistance / 50;
*offset = TargetDistInMm - AverageDistance;
- status = VL53L1_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, *offset*4);
+ status = VL53L1_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, *offset * 4);
return status;
}
-
int8_t VL53L1X::VL53L1X_CalibrateXtalk(uint16_t TargetDistInMm, uint16_t *xtalk)
{
- uint8_t i, tmp= 0;
+ uint8_t i, tmp = 0;
float AverageSignalRate = 0;
float AverageDistance = 0;
float AverageSpadNb = 0;
@@ -874,216 +861,217 @@ int8_t VL53L1X::VL53L1X_CalibrateXtalk(uint16_t TargetDistInMm, uint16_t *xtalk)
uint16_t sr;
VL53L1X_ERROR status = 0;
- status = VL53L1_WrWord(Device, 0x0016,0);
+ status = VL53L1_WrWord(Device, 0x0016, 0);
status = VL53L1X_StartRanging();
- for (i = 0; i < 50; i++) {
- while (tmp == 0){
+ for (i = 0; i < 50; i++)
+ {
+ while (tmp == 0)
+ {
status = VL53L1X_CheckForDataReady(&tmp);
}
- tmp=0;
- status= VL53L1X_GetSignalRate(&sr);
- status= VL53L1X_GetDistance(&distance);
+ tmp = 0;
+ status = VL53L1X_GetSignalRate(&sr);
+ status = VL53L1X_GetDistance(&distance);
status = VL53L1X_ClearInterrupt();
AverageDistance = AverageDistance + distance;
status = VL53L1X_GetSpadNb(&spadNum);
AverageSpadNb = AverageSpadNb + spadNum;
AverageSignalRate =
- AverageSignalRate + sr;
+ AverageSignalRate + sr;
}
status = VL53L1X_StopRanging();
AverageDistance = AverageDistance / 50;
AverageSpadNb = AverageSpadNb / 50;
AverageSignalRate = AverageSignalRate / 50;
/* Calculate Xtalk value */
- *xtalk = (uint16_t)(512*(AverageSignalRate*(1-(AverageDistance/TargetDistInMm)))/AverageSpadNb);
+ *xtalk = (uint16_t)(512 * (AverageSignalRate * (1 - (AverageDistance / TargetDistInMm))) / AverageSpadNb);
status = VL53L1_WrWord(Device, 0x0016, *xtalk);
return status;
}
-
-
-
- /* Write and read functions from I2C */
-
+/* Write and read functions from I2C */
VL53L1X_ERROR VL53L1X::VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count)
{
- int status;
+ int status;
- status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, pdata, (uint16_t)count);
- return status;
+ status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, pdata, (uint16_t)count);
+ return status;
}
VL53L1X_ERROR VL53L1X::VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count)
{
- int status;
+ int status;
- status = VL53L1_I2CRead(Dev->I2cDevAddr, index, pdata, (uint16_t)count);
+ status = VL53L1_I2CRead(Dev->I2cDevAddr, index, pdata, (uint16_t)count);
- return status;
+ return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data)
{
- int status;
+ int status;
- status=VL53L1_I2CWrite(Dev->I2cDevAddr, index, &data, 1);
- return status;
+ status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, &data, 1);
+ return status;
}
VL53L1X_ERROR VL53L1X::VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data)
{
- int status;
- uint8_t buffer[2];
+ int status;
+ uint8_t buffer[2];
- buffer[0] = data >> 8;
- buffer[1] = data & 0x00FF;
- status=VL53L1_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 2);
- return status;
+ buffer[0] = data >> 8;
+ buffer[1] = data & 0x00FF;
+ status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 2);
+ return status;
}
VL53L1X_ERROR VL53L1X::VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data)
{
- int status;
- uint8_t buffer[4];
-
- buffer[0] = (data >> 24) & 0xFF;
- buffer[1] = (data >> 16) & 0xFF;
- buffer[2] = (data >> 8) & 0xFF;
- buffer[3] = (data >> 0) & 0xFF;
- status=VL53L1_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 4);
- return status;
+ int status;
+ uint8_t buffer[4];
+
+ buffer[0] = (data >> 24) & 0xFF;
+ buffer[1] = (data >> 16) & 0xFF;
+ buffer[2] = (data >> 8) & 0xFF;
+ buffer[3] = (data >> 0) & 0xFF;
+ status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 4);
+ return status;
}
-
VL53L1X_ERROR VL53L1X::VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data)
{
- int status;
+ int status;
- status = VL53L1_I2CRead(Dev->I2cDevAddr, index, data, 1);
+ status = VL53L1_I2CRead(Dev->I2cDevAddr, index, data, 1);
- if(status)
- return -1;
+ if (status)
+ return -1;
- return 0;
+ return 0;
}
VL53L1X_ERROR VL53L1X::VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data)
{
- int status;
- uint8_t buffer[2] = {0,0};
-
- status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 2);
- if (!status)
- {
- *data = (buffer[0] << 8) + buffer[1];
- }
- return status;
+ int status;
+ uint8_t buffer[2] = {0, 0};
+ status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 2);
+ if (!status)
+ {
+ *data = (buffer[0] << 8) + buffer[1];
+ }
+ return status;
}
VL53L1X_ERROR VL53L1X::VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data)
{
- int status;
- uint8_t buffer[4] = {0,0,0,0};
-
- status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 4);
- if(!status)
- {
- *data = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
- }
- return status;
+ int status;
+ uint8_t buffer[4] = {0, 0, 0, 0};
+ status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 4);
+ if (!status)
+ {
+ *data = (buffer[0] << 24) + (buffer[1] << 16U) + (buffer[2] << 8) + buffer[3];
+ }
+ return status;
}
VL53L1X_ERROR VL53L1X::VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, uint8_t OrData)
{
- int status;
- uint8_t buffer = 0;
-
- /* read data direct onto buffer */
- status = VL53L1_I2CRead(Dev->I2cDevAddr, index, &buffer,1);
- if (!status)
- {
- buffer = (buffer & AndData) | OrData;
- status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, &buffer, (uint16_t)1);
- }
- return status;
+ int status;
+ uint8_t buffer = 0;
+
+ /* read data direct onto buffer */
+ status = VL53L1_I2CRead(Dev->I2cDevAddr, index, &buffer, 1);
+ if (!status)
+ {
+ buffer = (buffer & AndData) | OrData;
+ status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, &buffer, (uint16_t)1);
+ }
+ return status;
}
-VL53L1X_ERROR VL53L1X::VL53L1_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToWrite)
+VL53L1X_ERROR VL53L1X::VL53L1_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToWrite)
{
#ifdef DEBUG_MODE
- Serial.print("Beginning transmission to ");
- Serial.println(((DeviceAddr) >> 1) & 0x7F);
+ Serial.print("Beginning transmission to ");
+ Serial.println(((DeviceAddr) >> 1) & 0x7F);
#endif
- dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)));
+ dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)));
#ifdef DEBUG_MODE
- Serial.print("Writing port number ");
- Serial.println(RegisterAddr);
+ Serial.print("Writing port number ");
+ Serial.println(RegisterAddr);
#endif
- uint8_t buffer[2];
- buffer[0]=(uint8_t) RegisterAddr>>8;
- buffer[1]=(uint8_t) RegisterAddr&0xFF;
- dev_i2c->write(buffer, 2);
- for (int i = 0 ; i < NumByteToWrite ; i++)
- dev_i2c->write(pBuffer[i]);
-
- dev_i2c->endTransmission(true);
- return 0;
+ uint8_t buffer[2];
+ buffer[0] = RegisterAddr >> 8;
+ buffer[1] = RegisterAddr & 0xFF;
+ dev_i2c->write(buffer, 2);
+ for (uint16_t i = 0; i < NumByteToWrite; i++)
+ dev_i2c->write(pBuffer[i]);
+
+ dev_i2c->endTransmission(true);
+ return 0;
}
-VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
+VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToRead)
{
- int status = 0;
-//Loop until the port is transmitted correctly
- do {
+ int status = 0;
+
+ //Loop until the port is transmitted correctly
+ uint8_t maxAttempts = 5;
+ for (uint8_t x = 0; x < maxAttempts; x++)
+ {
#ifdef DEBUG_MODE
- Serial.print("Beginning transmission to ");
- Serial.println(((DeviceAddr) >> 1) & 0x7F);
+ Serial.print("Beginning transmission to ");
+ Serial.println(((DeviceAddr) >> 1) & 0x7F);
#endif
- dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)));
+ dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)));
#ifdef DEBUG_MODE
- Serial.print("Writing port number ");
- Serial.println(RegisterAddr);
+ Serial.print("Writing port number ");
+ Serial.println(RegisterAddr);
#endif
- uint8_t buffer[2];
- buffer[0]=(uint8_t) RegisterAddr>>8;
- buffer[1]=(uint8_t) RegisterAddr&0xFF;
- dev_i2c->write(buffer, 2);
- status = dev_i2c->endTransmission(false);
+ uint8_t buffer[2];
+ buffer[0] = RegisterAddr >> 8;
+ buffer[1] = RegisterAddr & 0xFF;
+ dev_i2c->write(buffer, 2);
+ status = dev_i2c->endTransmission(false);
+
+ if (status == 0)
+ break;
+
//Fix for some STM32 boards
//Reinitialize th i2c bus with the default parameters
#ifdef ARDUINO_ARCH_STM32
- if (status){
- dev_i2c->end();
- dev_i2c->begin();
+ if (status)
+ {
+ dev_i2c->end();
+ dev_i2c->begin();
}
-#endif
-//End of fix
- } while(status != 0);
+#endif
+ //End of fix
+ }
- dev_i2c->requestFrom(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)), (byte) NumByteToRead);
+ dev_i2c->requestFrom(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)), (byte)NumByteToRead);
- int i=0;
- while (dev_i2c->available())
- {
- pBuffer[i] = dev_i2c->read();
- i++;
- }
+ int i = 0;
+ while (dev_i2c->available())
+ {
+ pBuffer[i] = dev_i2c->read();
+ i++;
+ }
- return 0;
+ return 0;
}
-
VL53L1X_ERROR VL53L1X::VL53L1_GetTickCount(
uint32_t *ptick_count_ms)
{
- /* Returns current tick count in [ms] */
+ /* Returns current tick count in [ms] */
- VL53L1X_ERROR status = VL53L1_ERROR_NONE;
+ VL53L1X_ERROR status = VL53L1_ERROR_NONE;
//*ptick_count_ms = timeGetTime();
*ptick_count_ms = 0;
@@ -1091,29 +1079,27 @@ VL53L1X_ERROR VL53L1X::VL53L1_GetTickCount(
return status;
}
-
-
-VL53L1X_ERROR VL53L1X::VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us){
+VL53L1X_ERROR VL53L1X::VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us)
+{
(void)pdev;
- delay(wait_us/1000);
- return VL53L1_ERROR_NONE;
+ delay(wait_us / 1000);
+ return VL53L1_ERROR_NONE;
}
-
-VL53L1X_ERROR VL53L1X::VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms){
+VL53L1X_ERROR VL53L1X::VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms)
+{
(void)pdev;
delay(wait_ms);
- return VL53L1_ERROR_NONE;
+ return VL53L1_ERROR_NONE;
}
-
VL53L1X_ERROR VL53L1X::VL53L1_WaitValueMaskEx(
VL53L1_Dev_t *pdev,
- uint32_t timeout_ms,
- uint16_t index,
- uint8_t value,
- uint8_t mask,
- uint32_t poll_delay_ms)
+ uint32_t timeout_ms,
+ uint16_t index,
+ uint8_t value,
+ uint8_t mask,
+ uint32_t poll_delay_ms)
{
/*
@@ -1127,60 +1113,52 @@ VL53L1X_ERROR VL53L1X::VL53L1_WaitValueMaskEx(
* poll_delay_ms);
*/
- VL53L1_Error status = VL53L1_ERROR_NONE;
- uint32_t start_time_ms = 0;
- uint32_t current_time_ms = 0;
- uint32_t polling_time_ms = 0;
- uint8_t byte_value = 0;
- uint8_t found = 0;
-
-
+ VL53L1_Error status = VL53L1_ERROR_NONE;
+ uint32_t start_time_ms = 0;
+ uint32_t current_time_ms = 0;
+ uint32_t polling_time_ms = 0;
+ uint8_t byte_value = 0;
+ uint8_t found = 0;
/* calculate time limit in absolute time */
- VL53L1_GetTickCount(&start_time_ms);
+ VL53L1_GetTickCount(&start_time_ms);
/* remember current trace functions and temporarily disable
* function logging
*/
-
/* wait until value is found, timeout reached on error occurred */
while ((status == VL53L1_ERROR_NONE) &&
(polling_time_ms < timeout_ms) &&
- (found == 0)) {
+ (found == 0))
+ {
if (status == VL53L1_ERROR_NONE)
status = VL53L1_RdByte(
- pdev,
- index,
- &byte_value);
+ pdev,
+ index,
+ &byte_value);
if ((byte_value & mask) == value)
found = 1;
- if (status == VL53L1_ERROR_NONE &&
+ if (status == VL53L1_ERROR_NONE &&
found == 0 &&
poll_delay_ms > 0)
status = VL53L1_WaitMs(
- pdev,
- poll_delay_ms);
+ pdev,
+ poll_delay_ms);
/* Update polling time (Compare difference rather than absolute to
negate 32bit wrap around issue) */
VL53L1_GetTickCount(¤t_time_ms);
polling_time_ms = current_time_ms - start_time_ms;
-
}
-
if (found == 0 && status == VL53L1_ERROR_NONE)
status = VL53L1_ERROR_TIME_OUT;
return status;
}
-
-
-
-
diff --git a/src/vl53l1x_class.h b/src/st_src/vl53l1x_class.h
similarity index 99%
rename from src/vl53l1x_class.h
rename to src/st_src/vl53l1x_class.h
index 606aabf..0af5eea 100644
--- a/src/vl53l1x_class.h
+++ b/src/st_src/vl53l1x_class.h
@@ -304,6 +304,11 @@ class VL53L1X : public RangeSensor
*/
VL53L1X_ERROR VL53L1X_StartRanging();
+ /**
+ * @brief This function starts a one-shot ranging distance operation\n
+ */
+ VL53L1X_ERROR VL53L1X_StartOneshotRanging();
+
/**
* @brief This function stops the ranging.
*/
@@ -548,10 +553,13 @@ class VL53L1X : public RangeSensor
- protected:
+ public:
/* IO Device */
TwoWire *dev_i2c;
+
+ protected:
+
/* Digital out pin */
int gpio0;
int gpio1Int;