samples: sensors: add generic co2 sensor polling
Add polling sample for any CO2 sensor. Signed-off-by: Jeppe Odgaard <jeppe.odgaard@prevas.dk>
This commit is contained in:
parent
b0fdbce4df
commit
171dbd0435
7
samples/sensor/co2_polling/CMakeLists.txt
Normal file
7
samples/sensor/co2_polling/CMakeLists.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(co2_polling)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
30
samples/sensor/co2_polling/README.rst
Normal file
30
samples/sensor/co2_polling/README.rst
Normal file
|
@ -0,0 +1,30 @@
|
|||
.. co2:
|
||||
|
||||
Generic CO2 polling sample
|
||||
##########################
|
||||
|
||||
Overview
|
||||
********
|
||||
|
||||
A sensor sample that demonstrates how to poll a CO2 sensor.
|
||||
|
||||
Building and Running
|
||||
********************
|
||||
|
||||
This sample reads the CO2 sensor and print the values continuously.
|
||||
|
||||
.. zephyr-app-commands::
|
||||
:zephyr-app: samples/sensor/co2_polling
|
||||
:board: <board to use>
|
||||
:goals: build flash
|
||||
:compact:
|
||||
|
||||
Sample Output
|
||||
=============
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
CO2 940 ppm
|
||||
CO2 950 ppm
|
||||
|
||||
<repeats endlessly>
|
1
samples/sensor/co2_polling/boards/nucleo_h563zi.conf
Normal file
1
samples/sensor/co2_polling/boards/nucleo_h563zi.conf
Normal file
|
@ -0,0 +1 @@
|
|||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
15
samples/sensor/co2_polling/boards/nucleo_h563zi.overlay
Normal file
15
samples/sensor/co2_polling/boards/nucleo_h563zi.overlay
Normal file
|
@ -0,0 +1,15 @@
|
|||
/ {
|
||||
aliases {
|
||||
co2 = &explorir_m;
|
||||
};
|
||||
};
|
||||
|
||||
&lpuart1 {
|
||||
status = "okay";
|
||||
current-speed = <9600>;
|
||||
|
||||
explorir_m: explorir_m {
|
||||
compatible = "gss,explorir-m";
|
||||
status = "okay";
|
||||
};
|
||||
};
|
7
samples/sensor/co2_polling/prj.conf
Normal file
7
samples/sensor/co2_polling/prj.conf
Normal file
|
@ -0,0 +1,7 @@
|
|||
CONFIG_STDOUT_CONSOLE=y
|
||||
CONFIG_LOG=y
|
||||
CONFIG_SHELL=y
|
||||
CONFIG_SENSOR_SHELL=y
|
||||
CONFIG_SERIAL=y
|
||||
CONFIG_SENSOR=y
|
||||
CONFIG_SENSOR_LOG_LEVEL_DBG=y
|
8
samples/sensor/co2_polling/sample.yaml
Normal file
8
samples/sensor/co2_polling/sample.yaml
Normal file
|
@ -0,0 +1,8 @@
|
|||
sample:
|
||||
name: CO2 sensor sample
|
||||
tests:
|
||||
sample.sensor.co2:
|
||||
harness: sensor
|
||||
tags: sensors
|
||||
filter: dt_alias_exists("co2")
|
||||
depends_on: serial uart_interrupt_driven
|
33
samples/sensor/co2_polling/src/main.c
Normal file
33
samples/sensor/co2_polling/src/main.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Vitrolife A/S
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
LOG_MODULE_REGISTER(MAIN);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct sensor_value value;
|
||||
const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(co2));
|
||||
|
||||
if (!device_is_ready(dev)) {
|
||||
LOG_ERR("%s is not ready", dev->name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (sensor_sample_fetch(dev) == 0 &&
|
||||
sensor_channel_get(dev, SENSOR_CHAN_CO2, &value) == 0) {
|
||||
LOG_INF("CO2 %d ppm", value.val1);
|
||||
}
|
||||
|
||||
k_msleep(1000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue