From 79917842970bb325f7bb7fa79831fcf6640533c2 Mon Sep 17 00:00:00 2001 From: Erwan Gouriou Date: Fri, 28 Apr 2017 13:44:07 +0200 Subject: [PATCH] samples: sensor: hts221 This commit provides sample application for sensor hts221. By default, it is enabled on board disco_l475_iot1 Change-Id: I535fac8a670fa89cc1cae15ea1abe9cfe4b6c56b Signed-off-by: Erwan Gouriou --- samples/sensor/hts221/Makefile | 4 +++ samples/sensor/hts221/README.rst | 47 +++++++++++++++++++++++++++++ samples/sensor/hts221/prj.conf | 4 +++ samples/sensor/hts221/src/Makefile | 1 + samples/sensor/hts221/src/main.c | 48 ++++++++++++++++++++++++++++++ samples/sensor/hts221/testcase.ini | 4 +++ 6 files changed, 108 insertions(+) create mode 100644 samples/sensor/hts221/Makefile create mode 100644 samples/sensor/hts221/README.rst create mode 100644 samples/sensor/hts221/prj.conf create mode 100644 samples/sensor/hts221/src/Makefile create mode 100644 samples/sensor/hts221/src/main.c create mode 100644 samples/sensor/hts221/testcase.ini diff --git a/samples/sensor/hts221/Makefile b/samples/sensor/hts221/Makefile new file mode 100644 index 00000000000..63cb452ce0e --- /dev/null +++ b/samples/sensor/hts221/Makefile @@ -0,0 +1,4 @@ +BOARD ?= disco_l475_iot1 +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/samples/sensor/hts221/README.rst b/samples/sensor/hts221/README.rst new file mode 100644 index 00000000000..bdd91fbc51d --- /dev/null +++ b/samples/sensor/hts221/README.rst @@ -0,0 +1,47 @@ +.. _hts221: + +HTS221: Temperature and Humidity Monitor +######################################## + +Overview +******** +This sample periodically reads temperature and humidity from the HTS221 +Temperature & Humidity Sensor and displays it on the console + + +Requirements +************ + +This sample uses the HTS221 sensor controlled using the I2C interface. + +References +********** + + - HTS211: http://www.st.com/en/mems-and-sensors/hts221.html + +Building and Running +******************** + + This project outputs sensor data to the console. It requires an HTS221 + sensor, which is present on the disco_l475_iot1 board. + + .. code-block:: console + + $ cd samples/sensors/hts221 + $ make BOARD=disco_l475_iot1 + +Sample Output +============= + + .. code-block:: console + + Temperature:25.3 C + Relative Humidity:40% + Temperature:25.3 C + Relative Humidity:40% + Temperature:25.3 C + Relative Humidity:40% + Temperature:25.3 C + Relative Humidity:40% + + diff --git a/samples/sensor/hts221/prj.conf b/samples/sensor/hts221/prj.conf new file mode 100644 index 00000000000..6031b70dff7 --- /dev/null +++ b/samples/sensor/hts221/prj.conf @@ -0,0 +1,4 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_I2C=y +CONFIG_SENSOR=y +CONFIG_HTS221=y diff --git a/samples/sensor/hts221/src/Makefile b/samples/sensor/hts221/src/Makefile new file mode 100644 index 00000000000..b666967fd57 --- /dev/null +++ b/samples/sensor/hts221/src/Makefile @@ -0,0 +1 @@ +obj-y += main.o diff --git a/samples/sensor/hts221/src/main.c b/samples/sensor/hts221/src/main.c new file mode 100644 index 00000000000..2a5f856ad9d --- /dev/null +++ b/samples/sensor/hts221/src/main.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2017 Linaro Limited + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +void main(void) +{ + struct sensor_value temp, hum; + struct device *dev = device_get_binding("HTS221"); + + if (dev == NULL) { + printf("Could not get HTS221 device\n"); + return; + } + + while (1) { + if (sensor_sample_fetch(dev) < 0) { + printf("Sensor sample update error\n"); + return; + } + + if (sensor_channel_get(dev, SENSOR_CHAN_TEMP, &temp) < 0) { + printf("Cannot read HTS221 temperature channel\n"); + return; + } + + if (sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &hum) < 0) { + printf("Cannot read HTS221 humidity channel\n"); + return; + } + + /* display temperature */ + printf("Temperature:%.1f C\n", sensor_value_to_double(&temp)); + + /* display humidity (converted from millipercent) */ + printf("Relative Humidity:%.0f%%\n", + sensor_value_to_double(&hum) / 1000); + + k_sleep(2000); + } +} diff --git a/samples/sensor/hts221/testcase.ini b/samples/sensor/hts221/testcase.ini new file mode 100644 index 00000000000..7260b929f71 --- /dev/null +++ b/samples/sensor/hts221/testcase.ini @@ -0,0 +1,4 @@ +[test] +build_only = true +tags = samples sensor +platform_whitelist = disco_l475_iot1