Skip to content

Commit

Permalink
samples: sensor: vl53l0x: Include private channels
Browse files Browse the repository at this point in the history
Demonstrate usage of newly added private channels with
measurement metadata.

Signed-off-by: Michal Piekos <[email protected]>
  • Loading branch information
michalpiekos committed Dec 27, 2024
1 parent 4c766d8 commit a99fa4f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
41 changes: 25 additions & 16 deletions samples/sensor/vl53l0x/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Overview

This sample periodically measures distance between vl53l0x sensor
and target. The result is displayed on the console.
It also shows how we can use the vl53l0x as a proximity sensor.
It shows the usage of all available channels including private ones.

Requirements
************
Expand All @@ -24,24 +24,33 @@ References
Building and Running
********************

This project outputs sensor data to the console. It requires a VL53L0X
sensor, which is present on the disco_l475_iot1 board.
This project outputs sensor data to the console. It requires a VL53L0X
sensor, which is present on the disco_l475_iot1 board.

.. zephyr-app-commands::
:zephyr-app: samples/sensor/vl53l0x/
:goals: build flash
.. zephyr-app-commands::
:zephyr-app: samples/sensor/vl53l0x/
:goals: build flash


Sample Output
=============

.. code-block:: console
prox is 0
distance is 1938
prox is 1
distance is 70
prox is 0
distance is 1995
<repeats endlessly every second>
.. code-block:: console
prox is 0
distance is 1874 mm
Max distance is 000 mm
Signal rate is 33435 Cps
Ambient rate is 17365 Cps
SPADs used: 195
Status: OK
prox is 0
distance is 1888 mm
Max distance is 000 mm
Signal rate is 20846 Cps
Ambient rate is 25178 Cps
SPADs used: 195
Status: OK
<repeats endlessly every 5 seconds>
30 changes: 24 additions & 6 deletions samples/sensor/vl53l0x/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <stdio.h>
#include <zephyr/sys/printk.h>
#include <zephyr/drivers/sensor/vl53l0x.h>

int main(void)
{
Expand All @@ -31,12 +31,30 @@ int main(void)
ret = sensor_channel_get(dev, SENSOR_CHAN_PROX, &value);
printk("prox is %d\n", value.val1);

ret = sensor_channel_get(dev,
SENSOR_CHAN_DISTANCE,
&value);
printf("distance is %.3fm\n", sensor_value_to_double(&value));
ret = sensor_channel_get(dev, SENSOR_CHAN_DISTANCE, &value);
printk("distance is %.3lld mm\n", sensor_value_to_milli(&value));

k_sleep(K_MSEC(1000));
ret = sensor_channel_get(dev, SENSOR_CHAN_VL53L0X_RANGE_DMAX, &value);
printk("Max distance is %.3lld mm\n", sensor_value_to_milli(&value));

ret = sensor_channel_get(dev, SENSOR_CHAN_VL53L0X_SIGNAL_RATE_RTN_CPS, &value);
printk("Signal rate is %d Cps\n", value.val1);

ret = sensor_channel_get(dev, SENSOR_CHAN_VL53L0X_AMBIENT_RATE_RTN_CPS, &value);
printk("Ambient rate is %d Cps\n", value.val1);

ret = sensor_channel_get(dev, SENSOR_CHAN_VL53L0X_EFFECTIVE_SPAD_RTN_COUNT, &value);
printk("SPADs used: %d\n", value.val1);

ret = sensor_channel_get(dev, SENSOR_CHAN_VL53L0X_RANGE_STATUS, &value);
if (value.val1 == VL53L0X_RANGE_STATUS_RANGE_VALID) {
printk("Status: OK\n");
} else {
printk("Status: Error code %d\n", value.val1);
}

printk("\n");
k_sleep(K_MSEC(5000));
}
return 0;
}

0 comments on commit a99fa4f

Please sign in to comment.