diff --git a/tests/driver_hm330x/Makefile b/tests/driver_hm330x/Makefile new file mode 100644 index 0000000000..8cc0b2464e --- /dev/null +++ b/tests/driver_hm330x/Makefile @@ -0,0 +1,11 @@ +BOARD ?= nrf52840-mdk + +include ../Makefile.tests_common + +DRIVER ?= hm3301 +USEMODULE += $(DRIVER) +USEMODULE += ztimer_usec +USEMODULE += ztimer_msec +USEMODULE += fmt + +include $(RIOTBASE)/Makefile.include diff --git a/tests/driver_hm330x/README.md b/tests/driver_hm330x/README.md new file mode 100644 index 0000000000..d83841c1d0 --- /dev/null +++ b/tests/driver_hm330x/README.md @@ -0,0 +1,51 @@ +driver_hm330x +============== + +This is a simple test application for the HM330X I2C particle sensor. The test +will read and print the sensor data every second. + + +Note that particle counting is only available with hm3302. + +- `hm3302` output; + +``` +main(): This is RIOT! (Version: 2021.04-devel-1342-g93325-pr_driver_hm330x) +HM330X test application ++------------Initializing------------+ ++------------------------+------------------------+----------------------------------------------+ +| Standard concentration | Atmospheric Environment| # Particles in 0.1l air of diameter >= | +| PM1.0 | PM2.5 | PM10.0 | PM1.0 | PM2.5 | PM10.0 | 0.3µm | 0.5µm | 1.0µm | 2.5µm | 5.0µm | 10µm | ++-------+-------+--------+-------+-------+--------+-------+-------+-------+-------+-------+------+ +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +| 5| 7| 7| 5| 7| 7| 0| 0| 0| 0| 0| 0| +``` + +- `hm3301` output; + +``` +main(): This is RIOT! (Version: ) +HM330X test application ++------------Initializing------------+ ++------------------------+------------------------+ +| Standard concentration | Atmospheric Environment| +| PM1.0 | PM2.5 | PM10.0 | PM1.0 | PM2.5 | PM10.0 | ++-------+-------+--------+-------+-------+--------+ +| 8| 11| 11| 8| 11| 11| +| 8| 11| 11| 8| 11| 11| +| 8| 11| 11| 8| 11| 11| +| 9| 13| 13| 9| 13| 13| +| 9| 13| 13| 9| 13| 13| +| 9| 13| 13| 9| 13| 13| +| 9| 13| 13| 9| 13| 13| +| 9| 13| 13| 9| 13| 13| +``` diff --git a/tests/driver_hm330x/app.config.test b/tests/driver_hm330x/app.config.test new file mode 100644 index 0000000000..6f6737b02c --- /dev/null +++ b/tests/driver_hm330x/app.config.test @@ -0,0 +1,8 @@ +# this file enables modules defined in Kconfig. Do not use this file for +# application configuration. This is only needed during migration. +CONFIG_MODULE_HM330X=y +CONFIG_MODULE_HM3301=y +CONFIG_MODULE_FMT=y +CONFIG_ZTIMER_USEC=y +CONFIG_MODULE_ZTIMER=y +CONFIG_MODULE_ZTIMER_MSEC=y diff --git a/tests/driver_hm330x/main.c b/tests/driver_hm330x/main.c new file mode 100644 index 0000000000..a0016f17e9 --- /dev/null +++ b/tests/driver_hm330x/main.c @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2021 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief HM330X driver test application + * + * @author Marian Buschsieweke + * @author Francisco Molina + * + * @} + */ + +#include +#include + +#include "fmt.h" +#include "ztimer.h" +#include "timex.h" + +#include "hm330x.h" +#include "hm330x_params.h" + +static const char spaces[16] = " "; + +static void print_col_u32_dec(uint32_t number, size_t width) +{ + char sbuf[10]; /* "4294967295" */ + size_t slen; + + slen = fmt_u32_dec(sbuf, number); + if (width > slen) { + width -= slen; + while (width > sizeof(spaces)) { + print(spaces, sizeof(spaces)); + } + print(spaces, width); + } + print(sbuf, slen); +} + +int main(void) +{ + hm330x_t dev; + + print_str("HM330X test application\n"); + + print_str("+------------Initializing------------+\n"); + + /* initialize the sensor with default configuration parameters */ + if (hm330x_init(&dev, &hm330x_params[0])) { + print_str("Initialization failed\n"); + return 1; + } + + +#if IS_USED(MODULE_HM3302) + print_str( + "+------------------------+------------------------+----------------------------------------------+\n" + "| Standard concentration | Atmospheric Environment| # Particles in 0.1l air of diameter >= |\n" + "| PM1.0 | PM2.5 | PM10.0 | PM1.0 | PM2.5 | PM10.0 | 0.3µm | 0.5µm | 1.0µm | 2.5µm | 5.0µm | 10µm |\n" + "+-------+-------+--------+-------+-------+--------+-------+-------+-------+-------+-------+------+\n" + ); +#else + print_str( + "+------------------------+------------------------+\n" + "| Standard concentration | Atmospheric Environment|\n" + "| PM1.0 | PM2.5 | PM10.0 | PM1.0 | PM2.5 | PM10.0 |\n" + "+-------+-------+--------+-------+-------+--------+\n" + ); +#endif + + hm330x_data_t data; + while (1) { + ztimer_sleep(ZTIMER_MSEC, 1 * MS_PER_SEC); + + /* read the data and print them on success */ + if (hm330x_read(&dev, &data) == 0) { + print("|", 1); + print_col_u32_dec(data.mc_pm_1, 7); + print("|", 1); + print_col_u32_dec(data.mc_pm_2p5, 7); + print("|", 1); + print_col_u32_dec(data.mc_pm_10, 8); + print("|", 1); + print_col_u32_dec(data.amc_pm_1, 7); + print("|", 1); + print_col_u32_dec(data.amc_pm_2p5, 7); + print("|", 1); + print_col_u32_dec(data.amc_pm_10, 8); +#if IS_USED(MODULE_HM3302) + print("|", 1); + print_col_u32_dec(data.nc_pm_0p3, 7); + print("|", 1); + print_col_u32_dec(data.nc_pm_0p5, 7); + print("|", 1); + print_col_u32_dec(data.nc_pm_1, 7); + print("|", 1); + print_col_u32_dec(data.nc_pm_2p5, 7); + print("|", 1); + print_col_u32_dec(data.nc_pm_5, 7); + print("|", 1); + print_col_u32_dec(data.nc_pm_10, 6); +#endif + print("|\n", 2); + } + else { + print_str("Could not read data from sensor\n"); + } + } + + return 0; +}