Skip to content

Commit 1fd7877

Browse files
committed
Restructure and add examples
1 parent a1d39b9 commit 1fd7877

File tree

17 files changed

+296
-63
lines changed

17 files changed

+296
-63
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
build/*
2-
tmp/*
3-
external/*
1+
examples/build/*

CMakeLists.txt

Lines changed: 0 additions & 36 deletions
This file was deleted.

README.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ Please see [CONTRIBUTING.md](CONTRIBUTING.md)
1313
## Future Features
1414
- C++ object-oriented library
1515
- Easy way to configure defaults passed into initialization functions
16-
17-
18-
# Usage
19-
To use this library in your own project, include the `VL53L1X_pico_api` directory in your CMake script (e.g. `add_subdirectory(VL53L1X_pico_api)`). Then, include the needed headers as specified below, and add `VL53L1X_pico_api` to the `target_link_libraries` for your project. Running `cmake` for your project should then include and compile this API as needed.
16+
- Thread worker
2017

2118
# Examples
22-
To build the project, first clone the code and then run the following:
19+
To build the examples for the project, first clone the code and then run the following:
2320
```
21+
cd examples
2422
mkdir build
2523
cd build
2624
cmake ..
2725
make
2826
```
2927
Note: if you have a Pico W, append `-DPICO_BOARD=pico_w` to the `cmake` command.
3028

31-
This will build the example code. The resulting uf2 file in the `build/examples/continuous_measurement` can be uploaded to a Pico/Pico W. This example shows how to initialize and use the VL53L1X sensor to continously measure. The sensor must be attached to the following pins:
29+
This will build the example code. Either of th resulting `uf2` files in `build/examples/continuous_measurement` and `build/examples/sampling` can be uploaded to a Pico/Pico W. The former will continously print the sensor reading to stdout (over USB), whereas the second only prints samples when input is recieved from stdin (also over USB). To use these, leave your Pico connected to your computer after uploading the `uf2` file, and open a USB serial connection to it (for example, using Putty on Windows or native tools on UNIX-based systems).
30+
31+
For the examples to work correctly, the sensor must be attached to the following pins:
3232

3333
| VL53L1X | Pico |
3434
| ------ | ----- |
@@ -39,6 +39,34 @@ This will build the example code. The resulting uf2 file in the `build/examples/
3939
| SDA | GPIO5 |
4040
| XSHUT | Unconnected (Used to reset device)|
4141

42+
# Installation
43+
To use/install this library in your own project, acquire a copy of this repo in your project (such as using `git submodule`), and then make the following modifications to your project's `CMakeLists.txt`:
44+
45+
If your project structure is something like this:
46+
``` root/ (for your project)
47+
yourapp.h
48+
yourapp.c
49+
CMakeLists.txt
50+
build/
51+
...
52+
VL53L1X-C-API-Pico/
53+
library/
54+
import.cmake
55+
...
56+
...
57+
```
58+
Then, add this line to your `CMakeLists.txt` file before defining/linking your executables (e.g. building `yourapp`):
59+
```
60+
include(${PROJECT_SOURCE_DIR}/VL53L1X-C-API-Pico/library/import.cmake)
61+
```
62+
63+
Then, to link `<your executable>` with libraries you already have (`<your libraries>`) and this VL53L1X library, change the `target_link_libraries` line for `<your executable>` as follows:
64+
```
65+
target_link_libraries(<your executable> PUBLIC pico_stdlib hardware_i2c VL53L1X_pico_api <your libraries> )
66+
```
67+
68+
Running `cmake` for your project will now include and compile this API.
69+
4270
# Documentation
4371
The header files all have extensive documentation on the purpose of each function, which can be supplemented by the *STMicroelectronics VL53L1X Ultra Lite Driver User Manual (UM2510)*.
4472

VL53L1X_pico_api/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/CMakeLists.txt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
# Use this CMake file to compile the examples
2+
# This file can also serve as a template for how
3+
# to use the library in your own code. Note that
4+
# more details on how to use the library are
5+
# specified in library/import.cmake.
6+
7+
cmake_minimum_required(VERSION 3.25)
8+
9+
# Pull in SDK by downloading the pico_sdk_import
10+
set(PICO_SDK_IMPORT ${BINARY_DIR}/external/pico_sdk_import.cmake)
11+
if (NOT EXISTS ${PICO_SDK_IMPORT})
12+
file(DOWNLOAD
13+
https://raw.githubusercontent.com/raspberrypi/pico-sdk/2e6142b15b8a75c1227dd3edbe839193b2bf9041/external/pico_sdk_import.cmake
14+
${PICO_SDK_IMPORT}
15+
)
16+
endif()
17+
include(${PICO_SDK_IMPORT})
18+
19+
# Create project and set basic info
20+
project(VL53L1X_api
21+
DESCRIPTION "VL53L1X Raspberry Pi Pico/Pico W examples")
22+
set(CMAKE_C_STANDARD 11)
23+
set(CMAKE_CXX_STANDARD 17)
24+
add_compile_options(-Wall)
25+
pico_sdk_init()
26+
27+
# Pull in the API
28+
include(${PROJECT_SOURCE_DIR}/../library/import.cmake)
29+
130
# Define flag for Pico W
231
if(${PICO_BOARD} STREQUAL pico_w)
332
add_compile_definitions(PICO_W_BOARD)
@@ -6,4 +35,5 @@ else()
635
message("Compiling examples for Pico")
736
endif()
837

9-
add_subdirectory(continuous_measurement)
38+
add_subdirectory(continuous_measurement)
39+
add_subdirectory(sampling)

examples/continuous_measurement/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
add_executable(continuous continuous.c)
22

33
# Link to pico libraries as well as the VL53L1X_pico_api
4-
target_link_libraries(continuous PUBLIC VL53L1X_pico_api pico_stdlib)
4+
target_link_libraries(continuous PUBLIC pico_stdlib hardware_i2c VL53L1X_pico_api)
55

66
# Add needed library if Pico W
77
if(${PICO_BOARD} STREQUAL pico_w)

examples/continuous_measurement/continuous.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1+
/*
2+
* Example usage of the VL53L1X Pico Library for Pico/Pico W
3+
*
4+
* This program will continously print out packets read from the VL53L1X sensor
5+
* to USB (to read them, leave the Pico/Pico W connected to your computer via USB
6+
* and then open a serial connection to the corresponding port with baudrate 115200).
7+
*/
8+
9+
/*
10+
* Copyright 2022, Alex Mous
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a
13+
* copy of this software and associated documentation files (the "Software"),
14+
* to deal in the Software without restriction, including without limitation
15+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
16+
* and/or sell copies of the Software, and to permit persons to whom the
17+
* Software is furnished to do so, subject to the following conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be included in
20+
* all copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28+
* DEALINGS IN THE SOFTWARE.*/
29+
30+
#include <stdio.h>
31+
#include <stdint.h>
32+
133
#include "pico/stdlib.h"
234
#include "pico/binary_info.h"
3-
#include "hardware/i2c.h"
435

536
#ifdef PICO_W_BOARD
637
#include "pico/cyw43_arch.h"
738
#endif
839

9-
#include <stdio.h>
10-
#include <stdint.h>
11-
1240
#include "VL53L1X_api.h"
1341
#include "VL53L1X_types.h"
1442

@@ -20,6 +48,7 @@ int main() {
2048

2149
stdio_init_all();
2250

51+
// Blink the onboard LED to show booting
2352
#if PICO_W_BOARD
2453
if (cyw43_arch_init()) {
2554
printf("Failed to initialize Pico W.\n");

examples/sampling/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_executable(sampling sampling.c)
2+
3+
# Link to pico libraries as well as the VL53L1X_pico_api
4+
target_link_libraries(sampling PUBLIC pico_stdlib hardware_i2c VL53L1X_pico_api)
5+
6+
# Add needed library if Pico W
7+
if(${PICO_BOARD} STREQUAL pico_w)
8+
target_link_libraries(sampling PUBLIC pico_cyw43_arch_none)
9+
endif()
10+
11+
# Generate executable, enabling STDIO over USB
12+
pico_add_extra_outputs(sampling)
13+
pico_enable_stdio_usb(sampling 1)

examples/sampling/sampling.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Example usage of the VL53L1X Pico Library for Pico/Pico W
3+
*
4+
* This program will measure the VL53L1X sensor and print the result
5+
* every time a value is recieved from STDIN (e.g. over USB).
6+
*/
7+
8+
/*
9+
* Copyright 2022, Alex Mous
10+
*
11+
* Permission is hereby granted, free of charge, to any person obtaining a
12+
* copy of this software and associated documentation files (the "Software"),
13+
* to deal in the Software without restriction, including without limitation
14+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
15+
* and/or sell copies of the Software, and to permit persons to whom the
16+
* Software is furnished to do so, subject to the following conditions:
17+
*
18+
* The above copyright notice and this permission notice shall be included in
19+
* all copies or substantial portions of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27+
* DEALINGS IN THE SOFTWARE.*/
28+
29+
#include <stdio.h>
30+
#include <stdint.h>
31+
32+
#include "pico/stdlib.h"
33+
#include "pico/binary_info.h"
34+
35+
#ifdef PICO_W_BOARD
36+
#include "pico/cyw43_arch.h"
37+
#endif
38+
39+
#include "VL53L1X_api.h"
40+
#include "VL53L1X_types.h"
41+
42+
43+
int main() {
44+
VL53L1X_Status_t status;
45+
VL53L1X_Result_t results;
46+
uint8_t I2CDevAddr = 0x29;
47+
48+
stdio_init_all();
49+
50+
// Blink the onboard LED to show booting
51+
#if PICO_W_BOARD
52+
if (cyw43_arch_init()) {
53+
printf("Failed to initialize Pico W.\n");
54+
return 1;
55+
}
56+
#else
57+
gpio_init(PICO_DEFAULT_LED_PIN);
58+
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
59+
#endif
60+
61+
for (int i=0; i<10; i++) {
62+
#ifdef PICO_W_BOARD
63+
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
64+
#else
65+
gpio_put(PICO_DEFAULT_LED_PIN, 1);
66+
#endif
67+
68+
sleep_ms(250);
69+
70+
#ifdef PICO_W_BOARD
71+
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
72+
#else
73+
gpio_put(PICO_DEFAULT_LED_PIN, 0);
74+
#endif
75+
76+
sleep_ms(250);
77+
}
78+
79+
// Initialize Pico's I2C using PICO_DEFAULT_I2C_SDA_PIN
80+
// and PICO_DEFAULT_I2C_SCL_PIN (GPIO 4 and GPIO 5, respectively)
81+
if (VL53L1X_I2C_Init(I2CDevAddr, i2c0) < 0) {
82+
printf("Error initializing sensor.\n");
83+
return 0;
84+
}
85+
86+
// Ensure the sensor has booted
87+
uint8_t sensorState;
88+
do {
89+
status += VL53L1X_BootState(I2CDevAddr, &sensorState);
90+
VL53L1X_WaitMs(I2CDevAddr, 2);
91+
} while (sensorState == 0);
92+
printf("Sensor booted.\n");
93+
94+
// Initialize and configure sensor
95+
status = VL53L1X_SensorInit(I2CDevAddr);
96+
status += VL53L1X_SetDistanceMode(I2CDevAddr, 1);
97+
status += VL53L1X_SetTimingBudgetInMs(I2CDevAddr, 100);
98+
status += VL53L1X_SetInterMeasurementInMs(I2CDevAddr, 100);
99+
status += VL53L1X_StartRanging(I2CDevAddr);
100+
101+
// Measure and print when value from stdin received
102+
bool first_range = true;
103+
while (1) {
104+
// Read from stdin (blocking)
105+
getc(stdin);
106+
// Wait until we have new data
107+
uint8_t dataReady;
108+
do {
109+
status = VL53L1X_CheckForDataReady(I2CDevAddr, &dataReady);
110+
sleep_us(1);
111+
} while (dataReady == 0);
112+
113+
// Read and display result
114+
status += VL53L1X_GetResult(I2CDevAddr, &results);
115+
printf("Status = %2d, dist = %5d, Ambient = %2d, Signal = %5d, #ofSpads = %5d\n",
116+
results.status, results.distance, results.ambient, results.sigPerSPAD, results.numSPADs);
117+
118+
// Clear the sensor for a new measurement
119+
status += VL53L1X_ClearInterrupt(I2CDevAddr);
120+
if (first_range) { // Clear twice on first measurement
121+
status += VL53L1X_ClearInterrupt(I2CDevAddr);
122+
first_range = false;
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)