samples: sensor: vl53l0x: Include private channels

Demonstrate usage of newly added private channels with
measurement metadata.

Signed-off-by: Michal Piekos <michal.piekos@wp.pl>
This commit is contained in:
Michal Piekos 2024-12-26 14:47:01 +01:00 committed by Benjamin Cabé
parent d318726209
commit f6faecd008
2 changed files with 47 additions and 20 deletions

View file

@ -9,7 +9,7 @@ Overview
This sample periodically measures distance between vl53l0x sensor This sample periodically measures distance between vl53l0x sensor
and target. The result is displayed on the console. 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 Requirements
************ ************
@ -24,10 +24,10 @@ References
Building and Running Building and Running
******************** ********************
This project outputs sensor data to the console. It requires a VL53L0X This project outputs sensor data to the console. It requires a VL53L0X
sensor, which is present on the disco_l475_iot1 board. sensor, which is present on the disco_l475_iot1 board.
.. zephyr-app-commands:: .. zephyr-app-commands::
:zephyr-app: samples/sensor/vl53l0x/ :zephyr-app: samples/sensor/vl53l0x/
:goals: build flash :goals: build flash
@ -35,13 +35,22 @@ Building and Running
Sample Output Sample Output
============= =============
.. code-block:: console .. code-block:: console
prox is 0 prox is 0
distance is 1938 distance is 1874 mm
prox is 1 Max distance is 000 mm
distance is 70 Signal rate is 33435 Cps
prox is 0 Ambient rate is 17365 Cps
distance is 1995 SPADs used: 195
Status: OK
<repeats endlessly every second> 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>

View file

@ -7,8 +7,8 @@
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/device.h> #include <zephyr/device.h>
#include <zephyr/drivers/sensor.h> #include <zephyr/drivers/sensor.h>
#include <stdio.h>
#include <zephyr/sys/printk.h> #include <zephyr/sys/printk.h>
#include <zephyr/drivers/sensor/vl53l0x.h>
int main(void) int main(void)
{ {
@ -31,12 +31,30 @@ int main(void)
ret = sensor_channel_get(dev, SENSOR_CHAN_PROX, &value); ret = sensor_channel_get(dev, SENSOR_CHAN_PROX, &value);
printk("prox is %d\n", value.val1); printk("prox is %d\n", value.val1);
ret = sensor_channel_get(dev, ret = sensor_channel_get(dev, SENSOR_CHAN_DISTANCE, &value);
SENSOR_CHAN_DISTANCE, printk("distance is %.3lld mm\n", sensor_value_to_milli(&value));
&value);
printf("distance is %.3fm\n", sensor_value_to_double(&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; return 0;
} }