From d3f207c8bee5e205ea917b9ccaf640c5a112d45a Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Wed, 24 Sep 2014 14:13:27 +0200 Subject: [PATCH] tests: added test for MQ-3 alcohol sensor driver --- tests/driver_mq3/Makefile | 28 +++++++++++++++++ tests/driver_mq3/main.c | 64 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tests/driver_mq3/Makefile create mode 100644 tests/driver_mq3/main.c diff --git a/tests/driver_mq3/Makefile b/tests/driver_mq3/Makefile new file mode 100644 index 0000000000..4b7d1ee7bf --- /dev/null +++ b/tests/driver_mq3/Makefile @@ -0,0 +1,28 @@ +APPLICATION = driver_mq3 +include ../Makefile.tests_common + +FEATURES_REQUIRED = periph_adc + +# Define default pin mappings for some boards: +ifneq (,$(filter stm32f4discovery,$(BOARD))) + export MQ3_ADC ?= ADC_1 + export MQ3_CHAN ?= 0 +endif + +USEMODULE += mq3 +USEMODULE += vtimer + +ifneq (,$(MQ3_ADC)) + CFLAGS += -DMQ3_ADC=$(MQ3_ADC) +else + # set random default + CFLAGS += -DMQ3_ADC=ADC_0 +endif +ifneq (,$(MQ3_CHAN)) + CFLAGS += -DMQ3_CHAN=$(MQ3_CHAN) +else + # set random default + CFLAGS += -DMQ3_CHAN=0 +endif + +include $(RIOTBASE)/Makefile.include diff --git a/tests/driver_mq3/main.c b/tests/driver_mq3/main.c new file mode 100644 index 0000000000..07bdb3c267 --- /dev/null +++ b/tests/driver_mq3/main.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2014 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 directory for more + * details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Test application for the MQ-3 alcohol sensor driver + * + * @author Hauke Petersen + * + * @} + */ + +#include + +#include "vtimer.h" +#include "mq3.h" +#include "periph_conf.h" +#include "periph/adc.h" + +#ifndef MQ3_ADC +#error "MQ3_ADC is not specified" +#endif +#ifndef MQ3_CHAN +#error "MQ3_CHAN is not specified" +#endif + +int main(void) +{ + mq3_t dev; + int res; + + puts("MQ-3 alcohol sensor test application\n"); + printf("Initializing MQ-3 sensor at ADC_%i, channel %i... ", MQ3_ADC, MQ3_CHAN); + res = mq3_init(&dev, MQ3_ADC, MQ3_CHAN); + if (res == 0) { + puts("[ok]\n"); + } + else { + puts("[failed]"); + return 1; + } + + puts("Will now read and print the sensor twice a second:"); + while (1) { + int raw, ppm, alc_a, alc_b; + raw = mq3_read_raw(&dev); + ppm = mq3_read(&dev); + alc_a = ppm / 1000; + alc_b = ppm - (alc_a * 1000); + + printf("RAW: %4i, per mille: %1i.%03i\n", raw, alc_a, alc_b); + + vtimer_usleep(500 * 1000); + } + return 0; +}