Skip to content

Commit f91f0ca

Browse files
committed
samples: sensor: Add vl53l1x time of flight sensor sample
vl53l1x time of flight (TOF) sensor sample, fetching proximity and distance data in every second. Add overlay file for nucleo_f401re to make this sample works out-of-box. Signed-off-by: Aaron Tsui <[email protected]>
1 parent 0cb9b44 commit f91f0ca

File tree

6 files changed

+158
-0
lines changed

6 files changed

+158
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
5+
project(vl53l1x)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

samples/sensor/vl53l1x/README.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.. _vl53l1x:
2+
3+
VL53L1X: Time Of Flight sensor
4+
##############################
5+
6+
Overview
7+
********
8+
This sample periodically measures the distance between the VL53L1X
9+
sensor and a target. The result is displayed on the console.
10+
It also shows how we can use the VL53L1X as a proximity sensor.
11+
12+
13+
Requirements
14+
************
15+
16+
This sample uses the VL53L1X sensor controlled using the I2C interface.
17+
18+
References
19+
**********
20+
21+
- VL53L1X: http://www.st.com/en/imaging-and-photonics-solutions/vl53l1x.html
22+
23+
Building and Running
24+
********************
25+
26+
This project outputs sensor data to the console. It requires a VL53L1X
27+
sensor.
28+
29+
.. zephyr-app-commands::
30+
:app: samples/sensor/vl53l1x/
31+
:goals: build flash
32+
33+
34+
Sample Output
35+
=============
36+
37+
.. code-block:: console
38+
39+
prox is 1
40+
distance is 0.053000m
41+
prox is 1
42+
distance is 0.051000m
43+
prox is 0
44+
distance is 1.929000m
45+
prox is 0
46+
distance is 1.930000m
47+
48+
<repeats endlessly every second>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2019 Peter Bigot Consulting, LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&i2c1 { /* SDA CN5.9=PB9, SCL CN5.10=PB8, INT CN8.3=A2=PA4 */
8+
vl53l1x@29 {
9+
compatible = "st,vl53l1x";
10+
reg = <0x29>;
11+
label = "VL53L1X";
12+
drdy-gpios = <&gpioa 4 GPIO_INT_ACTIVE_LOW>;
13+
};
14+
};
15+

samples/sensor/vl53l1x/prj.conf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CONFIG_I2C=y
2+
CONFIG_GPIO=y
3+
CONFIG_SENSOR=y
4+
CONFIG_VL53L1X=y
5+
CONFIG_VL53L1X_PROXIMITY_THRESHOLD=100
6+
#CONFIG_VL53L1X_XSHUT_CONTROL_ENABLE=y
7+
#CONFIG_VL53L1X_TRIGGER_NONE=y
8+
CONFIG_VL53L1X_TRIGGER_OWN_THREAD=y
9+
#CONFIG_VL53L1X_TRIGGER_GLOBAL_THREAD=n
10+
11+
12+
#debug
13+
#CONFIG_DEBUG=y
14+
#CONFIG_LOG=y
15+
#CONFIG_SENSOR_LOG_LEVEL_DBG=y

samples/sensor/vl53l1x/sample.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sample:
2+
name: Sample for MEMS sensors VL53L1X
3+
tests:
4+
sample.sensor.vl53l1x:
5+
build_only: true
6+
depends_on: i2c vl53l1x
7+
tags: sensors

samples/sensor/vl53l1x/src/main.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2017 STMicroelectronics
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <device.h>
9+
#include <sensor.h>
10+
#include <misc/printk.h>
11+
12+
static void process_sample(struct device *dev)
13+
{
14+
int ret;
15+
struct sensor_value value;
16+
17+
ret = sensor_sample_fetch(dev);
18+
19+
if (ret) {
20+
printk("sensor_sample_fetch failed ret %d\n", ret);
21+
return;
22+
}
23+
24+
ret = sensor_channel_get(dev, SENSOR_CHAN_PROX, &value);
25+
printk("prox is %d\n", value.val1);
26+
27+
ret = sensor_channel_get(dev,
28+
SENSOR_CHAN_DISTANCE,
29+
&value);
30+
printk("distance is %d.%06dm\n", value.val1, value.val2);
31+
}
32+
33+
static void vl53l1x_handler(struct device *dev,
34+
struct sensor_trigger *trig)
35+
{
36+
process_sample(dev);
37+
}
38+
39+
40+
void main(void)
41+
{
42+
struct device *dev = device_get_binding(DT_ST_VL53L1X_0_LABEL);
43+
44+
if (dev == NULL) {
45+
printk("Could not get VL53L1X device\n");
46+
return;
47+
}
48+
49+
if (IS_ENABLED(CONFIG_VL53L1X_TRIGGER)) {
50+
struct sensor_trigger trig = {
51+
.type = SENSOR_TRIG_DATA_READY,
52+
.chan = SENSOR_CHAN_ALL,
53+
};
54+
if (sensor_trigger_set(dev, &trig, vl53l1x_handler) < 0) {
55+
printk("Cannot configure trigger\n");
56+
return;
57+
};
58+
}
59+
60+
while (!IS_ENABLED(CONFIG_VL53L1X_TRIGGER)) {
61+
process_sample(dev);
62+
k_sleep(1000);
63+
}
64+
k_sleep(K_FOREVER);
65+
}

0 commit comments

Comments
 (0)