samples: sensor: lps22hh: add standalone sample

Although this sensor is demonstrated by the X-NUCLEO-IKS01A3 sample,
maintenance of the driver is simplified if it can be tested in
isolation.  Provide a sample modeled on hts221.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2019-11-19 09:49:04 -06:00 committed by Anas Nashif
commit c2723054f6
5 changed files with 168 additions and 0 deletions

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(lps22hh)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,54 @@
.. _lps22hh:
LPS22HH: Temperature and Pressure Monitor
#########################################
Overview
********
This sample periodically reads pressure from the LPS22HH MEMS pressure
sensor and displays it on the console.
Requirements
************
This sample uses the LPS22HH sensor controlled using the I2C interface.
References
**********
- LPS22HH: http://www.st.com/en/mems-and-sensors/lps22hh.html
Building and Running
********************
This project outputs sensor data to the console. It requires an LPS22HH
sensor, which is present on the X-NUCLEO-IKS01A3 shield.
.. zephyr-app-commands::
:zephyr-app: samples/sensor/lps22hh
:board: nrf52_pca10040
:shield: x_nucleo_iks01a3
:goals: build
:compact:
Sample Output
=============
.. code-block:: console
Configured for triggered collection at 1 Hz
Observation: 1
Pressure: 97.474 kPa
Temperature: 22.19 C
Observation: 2
Pressure: 97.466 kPa
Temperature: 22.21 C
Observation: 3
Pressure: 97.473 kPa
Temperature: 22.21 C
Observation: 4
Pressure: 97.455 kPa
Temperature: 22.21 C
<repeats endlessly every second>

View file

@ -0,0 +1,4 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_LPS22HH=y

View file

@ -0,0 +1,15 @@
sample:
name: LPS22HH Temperature and Pressure Monitor
tests:
sample.sensor.lps22hh.nrf52.iks01a3:
harness: console
platform_whitelist: nrf52_pca10040
tags: sensors
depends_on: i2c
extra_args: SHIELD=x_nucleo_iks01a3
harness_config:
type: multi_line
ordered: yes
regex:
- "Temperature: (.*)"
- "Pressure: (.*)"

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2017 Linaro Limited
* Copyright (c) 2019 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <stdio.h>
#include <sys/util.h>
static void process_sample(struct device *dev)
{
static unsigned int obs;
struct sensor_value pressure, temp;
if (sensor_sample_fetch(dev) < 0) {
printf("Sensor sample update error\n");
return;
}
if (sensor_channel_get(dev, SENSOR_CHAN_PRESS, &pressure) < 0) {
printf("Cannot read LPS22HH pressure channel\n");
return;
}
if (sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp) < 0) {
printf("Cannot read LPS22HH temperature channel\n");
return;
}
++obs;
printf("Observation: %u\n", obs);
/* display pressure */
printf("Pressure: %.3f kPa\n", sensor_value_to_double(&pressure));
/* display temperature */
printf("Temperature: %.2f C\n", sensor_value_to_double(&temp));
}
static void lps22hh_handler(struct device *dev,
struct sensor_trigger *trig)
{
process_sample(dev);
}
void main(void)
{
struct device *dev = device_get_binding("LPS22HH");
if (dev == NULL) {
printf("Could not get LPS22HH device\n");
return;
}
if (IS_ENABLED(CONFIG_LPS22HH_TRIGGER)) {
struct sensor_value attr = {
.val1 = 1,
};
struct sensor_trigger trig = {
.type = SENSOR_TRIG_DATA_READY,
.chan = SENSOR_CHAN_ALL,
};
if (sensor_attr_set(dev, SENSOR_CHAN_ALL,
SENSOR_ATTR_SAMPLING_FREQUENCY, &attr) < 0) {
printf("Cannot configure sampling rate\n");
return;
}
if (sensor_trigger_set(dev, &trig, lps22hh_handler) < 0) {
printf("Cannot configure trigger\n");
return;
};
printk("Configured for triggered collection at %u Hz\n",
attr.val1);
}
while (!IS_ENABLED(CONFIG_LPS22HH_TRIGGER)) {
process_sample(dev);
k_sleep(K_MSEC(2000));
}
k_sleep(K_FOREVER);
}