Skip to content

Commit c272305

Browse files
pabigotnashif
authored andcommitted
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 <[email protected]>
1 parent 2c529ce commit c272305

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
5+
project(lps22hh)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

samples/sensor/lps22hh/README.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.. _lps22hh:
2+
3+
LPS22HH: Temperature and Pressure Monitor
4+
#########################################
5+
6+
Overview
7+
********
8+
This sample periodically reads pressure from the LPS22HH MEMS pressure
9+
sensor and displays it on the console.
10+
11+
12+
Requirements
13+
************
14+
15+
This sample uses the LPS22HH sensor controlled using the I2C interface.
16+
17+
References
18+
**********
19+
20+
- LPS22HH: http://www.st.com/en/mems-and-sensors/lps22hh.html
21+
22+
Building and Running
23+
********************
24+
25+
This project outputs sensor data to the console. It requires an LPS22HH
26+
sensor, which is present on the X-NUCLEO-IKS01A3 shield.
27+
28+
.. zephyr-app-commands::
29+
:zephyr-app: samples/sensor/lps22hh
30+
:board: nrf52_pca10040
31+
:shield: x_nucleo_iks01a3
32+
:goals: build
33+
:compact:
34+
35+
Sample Output
36+
=============
37+
38+
.. code-block:: console
39+
40+
Configured for triggered collection at 1 Hz
41+
Observation: 1
42+
Pressure: 97.474 kPa
43+
Temperature: 22.19 C
44+
Observation: 2
45+
Pressure: 97.466 kPa
46+
Temperature: 22.21 C
47+
Observation: 3
48+
Pressure: 97.473 kPa
49+
Temperature: 22.21 C
50+
Observation: 4
51+
Pressure: 97.455 kPa
52+
Temperature: 22.21 C
53+
54+
<repeats endlessly every second>

samples/sensor/lps22hh/prj.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_STDOUT_CONSOLE=y
2+
CONFIG_I2C=y
3+
CONFIG_SENSOR=y
4+
CONFIG_LPS22HH=y

samples/sensor/lps22hh/sample.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
sample:
2+
name: LPS22HH Temperature and Pressure Monitor
3+
tests:
4+
sample.sensor.lps22hh.nrf52.iks01a3:
5+
harness: console
6+
platform_whitelist: nrf52_pca10040
7+
tags: sensors
8+
depends_on: i2c
9+
extra_args: SHIELD=x_nucleo_iks01a3
10+
harness_config:
11+
type: multi_line
12+
ordered: yes
13+
regex:
14+
- "Temperature: (.*)"
15+
- "Pressure: (.*)"

samples/sensor/lps22hh/src/main.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2017 Linaro Limited
3+
* Copyright (c) 2019 Nordic Semiconductor ASA
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <zephyr.h>
9+
#include <device.h>
10+
#include <drivers/sensor.h>
11+
#include <stdio.h>
12+
#include <sys/util.h>
13+
14+
static void process_sample(struct device *dev)
15+
{
16+
static unsigned int obs;
17+
struct sensor_value pressure, temp;
18+
19+
if (sensor_sample_fetch(dev) < 0) {
20+
printf("Sensor sample update error\n");
21+
return;
22+
}
23+
24+
if (sensor_channel_get(dev, SENSOR_CHAN_PRESS, &pressure) < 0) {
25+
printf("Cannot read LPS22HH pressure channel\n");
26+
return;
27+
}
28+
29+
if (sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp) < 0) {
30+
printf("Cannot read LPS22HH temperature channel\n");
31+
return;
32+
}
33+
34+
++obs;
35+
printf("Observation: %u\n", obs);
36+
37+
/* display pressure */
38+
printf("Pressure: %.3f kPa\n", sensor_value_to_double(&pressure));
39+
40+
/* display temperature */
41+
printf("Temperature: %.2f C\n", sensor_value_to_double(&temp));
42+
43+
}
44+
45+
static void lps22hh_handler(struct device *dev,
46+
struct sensor_trigger *trig)
47+
{
48+
process_sample(dev);
49+
}
50+
51+
void main(void)
52+
{
53+
struct device *dev = device_get_binding("LPS22HH");
54+
55+
if (dev == NULL) {
56+
printf("Could not get LPS22HH device\n");
57+
return;
58+
}
59+
60+
if (IS_ENABLED(CONFIG_LPS22HH_TRIGGER)) {
61+
struct sensor_value attr = {
62+
.val1 = 1,
63+
};
64+
struct sensor_trigger trig = {
65+
.type = SENSOR_TRIG_DATA_READY,
66+
.chan = SENSOR_CHAN_ALL,
67+
};
68+
69+
if (sensor_attr_set(dev, SENSOR_CHAN_ALL,
70+
SENSOR_ATTR_SAMPLING_FREQUENCY, &attr) < 0) {
71+
printf("Cannot configure sampling rate\n");
72+
return;
73+
}
74+
if (sensor_trigger_set(dev, &trig, lps22hh_handler) < 0) {
75+
printf("Cannot configure trigger\n");
76+
return;
77+
};
78+
printk("Configured for triggered collection at %u Hz\n",
79+
attr.val1);
80+
}
81+
82+
while (!IS_ENABLED(CONFIG_LPS22HH_TRIGGER)) {
83+
process_sample(dev);
84+
k_sleep(K_MSEC(2000));
85+
}
86+
k_sleep(K_FOREVER);
87+
}

0 commit comments

Comments
 (0)