1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

tests: add test application for BME680 driver

This commit is contained in:
Gunar Schorcht 2020-03-10 15:31:12 +01:00
parent 9dd11eb188
commit 2338944a1c
5 changed files with 213 additions and 0 deletions

View File

@ -0,0 +1,12 @@
include ../Makefile.tests_common
DRIVER ?= bme680_i2c
USEMODULE += $(DRIVER)
USEMODULE += xtimer
ifeq ($(ENABLE_FP),1)
USEMODULE += bme680_fp
endif
include $(RIOTBASE)/Makefile.include

View File

@ -0,0 +1,11 @@
BOARD_INSUFFICIENT_MEMORY := \
arduino-duemilanove \
arduino-uno \
chronos \
msb-430 \
msb-430h \
nucleo-f031k6 \
telosb \
wsn430-v1_3b \
wsn430-v1_4 z1 \
#

View File

@ -0,0 +1,45 @@
# BME680 driver test
## About
This is a test application for the BME680 driver.
This driver depends on the Bosch Sensortech
[BME680 driver](https://github.com/BoschSensortec/BME680_driver).
## Usage
This test application will initialize one or more BME680 devices to output
the following every 5 seconds:
* Temperature
* Humidity
* Pressure
* Resistance value (depending on VOC gas)
The driver can use either fixed-point or floating-point arithmetic for all
conversions. By default fixed-point arithmetic is used. To use floating-point
arithmetic, the `bme680_fp` module has to be enabled. This can be done in the
test application by setting the environment variable `ENABLE_FP`:
```
ENABLE_FP=1 make BOARD=... -C tests/driver_bme680
```
## Interface
BME680 sensors can be used with I2C and/or SPI. Which interface is used by
which BME680 sensor is defined in the `bme680_params` parameters. The
respective implementation is enabled by the modules `bme680_i2c` and
`bme680_spi`.
Which implementation is used for the test application is defined by the
`DRIVER` environment variable. By default `bme680_i2c` is used. To use
`bme680_spi`, the `DRIVER` variable could be set at the make command line:
```
DRIVER=bme680_spi make BOARD=... -C tests/driver_bme680
```
It is also possible to use I2C as well SPI simultaneously in the test
application:
```
DRIVER=bme680_spi bme680_i2c' make BOARD=... -C tests/driver_bme680
```

116
tests/driver_bme680/main.c Normal file
View File

@ -0,0 +1,116 @@
/*
* Copyright (C) 2018 Mesotic SAS
* 2020 Gunar Schorcht
*
* 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 Test application for the bme680_driver package.
*
* @author Dylan Laduranty <dylan.laduranty@mesotic.com>
* @author Gunar Schorcht <gunar@schorcht.net>
* @}
*/
#include <stdio.h>
#include "board.h"
#include "bme680.h"
#include "bme680_params.h"
#include "mutex.h"
#include "xtimer.h"
#define BME680_TEST_PERIOD_US (5 * US_PER_SEC)
xtimer_t timer;
static void _timer_cb(void *arg)
{
xtimer_set(&timer, BME680_TEST_PERIOD_US);
mutex_unlock(arg);
}
int main(void)
{
mutex_t timer_mtx = MUTEX_INIT_LOCKED;
bme680_t dev[BME680_NUMOF];
for (unsigned i = 0; i < BME680_NUMOF; i++) {
/*
* We use a fix temperature here. The ambient temperature could be
* determined by performing a few temperature readings without
* operating the gas sensor or by another temperature sensor. Function
* bme680_set_ambient_temp can be used at any time to change it.
*/
BME680_SENSOR(&dev[i]).amb_temp = 25;
printf("Initialize BME680 sensor %u ... ", i);
if (bme680_init(&dev[i], &bme680_params[i]) != BME680_OK) {
puts("failed");
}
else {
puts("OK");
}
}
timer.callback = _timer_cb;
timer.arg = &timer_mtx;
xtimer_set(&timer, BME680_TEST_PERIOD_US);
while (1)
{
struct bme680_field_data data;
for (unsigned i = 0; i < BME680_NUMOF; i++) {
/* trigger one measuerment */
bme680_force_measurement(&dev[i]);
/* get the duration for the measurement */
int duration = bme680_get_duration(&dev[i]);
/* wait for the duration */
xtimer_usleep(duration * US_PER_MS);
/* read the data */
int res = bme680_get_data(&dev[i], &data);
if (res == 0 && dev[i].sensor.new_fields) {
#ifndef MODULE_BME680_FP
printf("[bme680]: dev=%u, "
"T = %02d.%02d degC, "
"P = %" PRIu32 " Pa, H = %02" PRIu32 ".%03" PRIu32 " %%",
i, data.temperature / 100, data.temperature % 100,
data.pressure,
data.humidity / 1000, data.humidity % 1000);
/* Avoid using measurements from an unstable heating setup */
if (data.status & BME680_GASM_VALID_MSK) {
printf(", G = %" PRIu32 " ohms", data.gas_resistance);
}
#else
printf("[bme680]: dev=%u T = %.2f degC, P = %.2f Pa, H %.3f %%",
i, data.temperature, data.pressure, data.humidity);
/* Avoid using measurements from an unstable heating setup */
if (data.status & BME680_GASM_VALID_MSK) {
printf(", G = %.0f ohms", data.gas_resistance);
}
#endif
printf("\n");
}
else if (res == 0) {
printf("[bme680]: no new data\n");
}
else {
printf("[bme680]: read data failed with reason %d\n", res);
}
}
printf("+-----------------------------------------+\n");
mutex_lock(&timer_mtx);
}
/* Should never reach here */
return 0;
}

View File

@ -0,0 +1,29 @@
#!/usr/bin/env python3
# Copyright (C) 2020 Gunar Schorcht <gunar@schorcht.net>
#
# 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.
import sys
from testrunner import run
def testfunc(child):
child.expect('Initialize BME680 sensor 0 ... ')
i = child.expect(['[OK]', '[failed]'])
if i == 1:
print('FAILED')
return
child.expect('\[bme680\]: dev=0, ')
child.expect(r'T = \d+.\d+ degC, ')
child.expect(r'P = \d+ Pa, ')
child.expect(r'H = \d+.\d+ \%, ')
child.expect(r'G = \d+ ohms')
print('SUCCESS')
return
if __name__ == "__main__":
sys.exit(run(testfunc))