Skip to content

Commit 8a1b100

Browse files
committed
samples: sensor: nooploop: add TOFSense uart demo
Provide a demo of the Nooploop TOFSense sensor on a nucleo_u575zi_q board. Signed-off-by: Alex Fabre <alex.fabre@rtone.fr>
1 parent d23e4cb commit 8a1b100

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(tofsense)
6+
7+
target_sources(app PRIVATE src/main.c)

samples/sensor/tofsense/README.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.. zephyr:code-sample:: tofsense
2+
:name: TOFSense Time Of Flight sensor
3+
:relevant-api: sensor_interface
4+
5+
Get distance data from a TOFSense sensor (interrupt mode).
6+
7+
Overview
8+
********
9+
10+
This sample periodically measures distance between TOFSense sensor
11+
and target. The result is displayed on the console.
12+
13+
Requirements
14+
************
15+
16+
This sample uses the TOFSense sensor with its factory default settings, on the UART interface.
17+
18+
References
19+
**********
20+
21+
- Datasheet: https://ftp.nooploop.com/downloads/tofsense/TOFSense_Datasheet_V3.0_en.pdf
22+
- User manual: https://ftp.nooploop.com/downloads/tofsense/TOFSense_User_Manual_V3.0_en.pdf
23+
24+
Building and Running
25+
********************
26+
27+
This project outputs sensor data to the console. It requires a TOFSense
28+
sensor to be plugged on UART2 (UART on CN9 connector of all ST nucleo boards).
29+
30+
.. zephyr-app-commands::
31+
:zephyr-app: samples/sensor/tofsense/
32+
:board: nucleo_u575zi_q
33+
:goals: build flash
34+
35+
36+
Sample Output
37+
=============
38+
39+
.. code-block:: console
40+
41+
distance is 000 mm
42+
43+
distance is 1888 mm
44+
45+
<repeats endlessly every 5 seconds>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2025 Alex Fabre
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/dt-bindings/sensor/tofsense.h>
8+
9+
/ {
10+
aliases {
11+
tof = &tofsense;
12+
};
13+
};
14+
15+
&usart2 {
16+
current-speed = <115200>;
17+
tofsense: tofsense {
18+
compatible = "nooploop,tofsense";
19+
status = "okay";
20+
id = <0>;
21+
operating-mode = <TOFSENSE_MODE_ACTIVE>;
22+
active-mode-frequency = <30>;
23+
};
24+
};

samples/sensor/tofsense/prj.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CONFIG_SENSOR=y
2+
CONFIG_UART_INTERRUPT_DRIVEN=y
3+
4+
CONFIG_CAN=y
5+
6+
#debug
7+
CONFIG_DEBUG=y
8+
CONFIG_LOG=y
9+
CONFIG_SENSOR_LOG_LEVEL_INF=y
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sample:
2+
name: Sample for Nooploop TOFsens Time Of Flight sensor
3+
tests:
4+
sample.sensor.tofsense:
5+
depends_on:
6+
- uart
7+
tags:
8+
- sensors
9+
filter: dt_compat_enabled("nooploop,tofsense")
10+
harness: console
11+
harness_config:
12+
type: multi_line
13+
regex:
14+
- "distance is .*"

samples/sensor/tofsense/src/main.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2025 Alex Fabre
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/sensor.h>
10+
#include <zephyr/sys/printk.h>
11+
12+
int main(void)
13+
{
14+
const struct device *const dev = DEVICE_DT_GET_ONE(nooploop_tofsense);
15+
struct sensor_value value;
16+
int ret;
17+
18+
if (!device_is_ready(dev)) {
19+
printk("sensor: device not ready.\n");
20+
return 0;
21+
}
22+
23+
while (1) {
24+
ret = sensor_sample_fetch(dev);
25+
if (ret) {
26+
printk("sensor_sample_fetch failed ret %d\n", ret);
27+
return 0;
28+
}
29+
30+
ret = sensor_channel_get(dev, SENSOR_CHAN_DISTANCE, &value);
31+
printk("distance is %3lld mm\n", sensor_value_to_milli(&value));
32+
33+
printk("\n");
34+
k_sleep(K_MSEC(5000));
35+
}
36+
return 0;
37+
}

0 commit comments

Comments
 (0)