From c6aa4133201c8588c47863acba3ca7332c00f529 Mon Sep 17 00:00:00 2001 From: Hauke Petersen <hauke.petersen@fu-berlin.de> Date: Tue, 7 Feb 2017 09:59:30 +0100 Subject: [PATCH] tests/driver_dht: adapted to driver changes --- tests/driver_dht/Makefile | 1 + tests/driver_dht/main.c | 53 ++++++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/tests/driver_dht/Makefile b/tests/driver_dht/Makefile index bacc50db43..6d18950e1d 100644 --- a/tests/driver_dht/Makefile +++ b/tests/driver_dht/Makefile @@ -2,6 +2,7 @@ APPLICATION = driver_dht include ../Makefile.tests_common USEMODULE += dht +USEMODULE += fmt USEMODULE += xtimer include $(RIOTBASE)/Makefile.include diff --git a/tests/driver_dht/main.c b/tests/driver_dht/main.c index b9357e2399..8f6e2d0433 100644 --- a/tests/driver_dht/main.c +++ b/tests/driver_dht/main.c @@ -1,6 +1,6 @@ /* * Copyright (C) 2015 Ludwig Knüpfer, Christian Mehlis - * 2016 Freie Universität Berlin + * 2016-2017 Freie Universität Berlin * * 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 @@ -24,41 +24,48 @@ #include <stdio.h> #include "xtimer.h" +#include "timex.h" +#include "fmt.h" #include "dht.h" #include "dht_params.h" -extern dht_t dht_devs[]; +#define DELAY (2 * US_PER_SEC) int main(void) { + dht_t dev; + int16_t temp, hum; + char temp_s[10]; + char hum_s[10]; + puts("DHT temperature and humidity sensor test application\n"); + /* initialize first configured sensor */ + printf("Initializing DHT sensor...\t"); + if (dht_init(&dev, &dht_params[0]) == DHT_OK) { + puts("[OK]\n"); + } + else { + puts("[Failed]"); + return 1; + } + /* periodically read temp and humidity values */ while (1) { - for (unsigned i = 0; i < DHT_NUMOF; i++) { - dht_t *dev = &dht_devs[i]; - int16_t temp, hum; - int16_t dec_temp, dec_hum, int_temp, int_hum; - - if (dht_read(dev, &temp, &hum) == -1) { - puts("error reading data"); - continue; - } + if (dht_read(&dev, &temp, &hum) != DHT_OK) { + puts("Error reading values"); + continue; + } - /* split up values into integral and fractional parts for nicer - * printing - * TODO: this should be done in some kind of library... */ - int_temp = temp / 10; - dec_temp = temp - (int_temp * 10); - int_hum = hum / 10; - dec_hum = hum - (int_hum * 10); + size_t n = fmt_s16_dfp(temp_s, temp, 1); + temp_s[n] = '\0'; + n = fmt_s16_dfp(hum_s, hum, 1); + hum_s[n] = '\0'; - printf("DHT device #%u - ", i); - printf("temp: %i.%i°C, ", int_temp, dec_temp); - printf("relative humidity: %i.%i%%\n", int_hum, dec_hum); + printf("DHT values - temp: %s°C - relative humidity: %s%%\n", + temp_s, hum_s); - xtimer_usleep(2000 * US_PER_MS); - } + xtimer_usleep(DELAY); } return 0; -- GitLab