diff --git a/samples/sensor/distance_polling/CMakeLists.txt b/samples/sensor/distance_polling/CMakeLists.txt new file mode 100644 index 00000000000..aa561618325 --- /dev/null +++ b/samples/sensor/distance_polling/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (c) 2025 Thiyagarajan Pandiyan +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(distance_polling) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/sensor/distance_polling/README.rst b/samples/sensor/distance_polling/README.rst new file mode 100644 index 00000000000..8cad7ae5b10 --- /dev/null +++ b/samples/sensor/distance_polling/README.rst @@ -0,0 +1,43 @@ +.. zephyr:code-sample:: distance_polling + :name: Generic distance measurement + :relevant-api: sensor_interface + + Measure distance to an object using a distance sensor + +Overview +******** + +This sample application periodically measures the distance of an object and +display it, via the console. + +Building and Running +******************** + +This sample supports up to 5 distance sensors. Each sensor needs to be aliased +as ``distanceN`` where ``N`` goes from ``0`` to ``4``. For example: + +.. code-block:: devicetree + + / { + aliases { + distance0 = &vl53l1x; + }; + }; + +Make sure the aliases are in devicetree, then build and run with: + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/distance_polling + :board: + :goals: build flash + :compact: + +Sample Output +============= + +.. code-block:: console + + vl53l1x: 0.153m + vl53l1x: 0.154m + vl53l1x: 0.154m + vl53l1x: 0.153m diff --git a/samples/sensor/distance_polling/boards/arduino_nicla_vision_stm32h747xx_m7.overlay b/samples/sensor/distance_polling/boards/arduino_nicla_vision_stm32h747xx_m7.overlay new file mode 100644 index 00000000000..d7b7a8a45ea --- /dev/null +++ b/samples/sensor/distance_polling/boards/arduino_nicla_vision_stm32h747xx_m7.overlay @@ -0,0 +1,11 @@ +/* + * Copyright (c) 2025 Thiyagarajan Pandiyan + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + aliases { + distance0 = &vl53l1x; + }; +}; diff --git a/samples/sensor/distance_polling/prj.conf b/samples/sensor/distance_polling/prj.conf new file mode 100644 index 00000000000..707735944e3 --- /dev/null +++ b/samples/sensor/distance_polling/prj.conf @@ -0,0 +1,3 @@ +CONFIG_LOG=y +CONFIG_GPIO=y +CONFIG_SENSOR=y diff --git a/samples/sensor/distance_polling/sample.yaml b/samples/sensor/distance_polling/sample.yaml new file mode 100644 index 00000000000..039e5bcc5de --- /dev/null +++ b/samples/sensor/distance_polling/sample.yaml @@ -0,0 +1,14 @@ +sample: + name: Distance measurement sample +common: + tags: sensors + harness: console + harness_config: + type: one_line + regex: + - "^[a-zA-Z0-9_,+-.]*: [0-9\\.]+m$" +tests: + sample.sensor.distance_polling: + filter: dt_alias_exists("distance0") + integration_platforms: + - arduino_nicla_vision/stm32h747xx/m7 # vl53l1x diff --git a/samples/sensor/distance_polling/src/main.c b/samples/sensor/distance_polling/src/main.c new file mode 100644 index 00000000000..aec436e86f0 --- /dev/null +++ b/samples/sensor/distance_polling/src/main.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Thiyagarajan Pandiyan + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +#define DISTANCE_ALIAS(i) DT_ALIAS(_CONCAT(distance, i)) +#define DISTANCE_SENSOR(i, _) \ + IF_ENABLED(DT_NODE_EXISTS(DISTANCE_ALIAS(i)), (DEVICE_DT_GET(DISTANCE_ALIAS(i)),)) + +/* support up to 5 distance sensors */ +static const struct device *const sensors[] = {LISTIFY(5, DISTANCE_SENSOR, ())}; + +static void fetch_and_display(const struct device *sensor) +{ + struct sensor_value distance; + int rc = sensor_sample_fetch(sensor); + + if (rc < 0) { + printf("ERROR: Fetch failed: %d\n", rc); + return; + } + + rc = sensor_channel_get(sensor, SENSOR_CHAN_DISTANCE, &distance); + if (rc < 0) { + printf("ERROR: get failed: %d\n", rc); + } else { + printf("%s: %d.%03dm\n", sensor->name, distance.val1, distance.val2); + } +} + +int main(void) +{ + size_t i = 0; + + for (i = 0; i < ARRAY_SIZE(sensors); i++) { + if (!device_is_ready(sensors[i])) { + printf("Error: Device \"%s\" is not ready\n", sensors[i]->name); + return 0; + } + } + + while (true) { + for (i = 0; i < ARRAY_SIZE(sensors); i++) { + fetch_and_display(sensors[i]); + k_sleep(K_MSEC(500)); + } + } + + return 0; +}