1 /* 2 * Copyright (c) 2017 Linaro Limited 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #include <zephyr/kernel.h> 8 #include <zephyr/device.h> 9 #include <zephyr/drivers/sensor.h> 10 #include <stdio.h> 11 #include <zephyr/sys/util.h> 12 process_sample(const struct device * dev)13static void process_sample(const struct device *dev) 14 { 15 static unsigned int obs; 16 struct sensor_value pressure, temp; 17 18 if (sensor_sample_fetch(dev) < 0) { 19 printf("Sensor sample update error\n"); 20 return; 21 } 22 23 if (sensor_channel_get(dev, SENSOR_CHAN_PRESS, &pressure) < 0) { 24 printf("Cannot read LPS22HB pressure channel\n"); 25 return; 26 } 27 28 if (sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp) < 0) { 29 printf("Cannot read LPS22HB temperature channel\n"); 30 return; 31 } 32 33 ++obs; 34 printf("Observation:%u\n", obs); 35 36 /* display pressure */ 37 printf("Pressure:%.1f kPa\n", sensor_value_to_double(&pressure)); 38 39 /* display temperature */ 40 printf("Temperature:%.1f C\n", sensor_value_to_double(&temp)); 41 42 } 43 main(void)44int main(void) 45 { 46 const struct device *const dev = DEVICE_DT_GET_ONE(st_lps22hb_press); 47 48 if (!device_is_ready(dev)) { 49 printf("Device %s is not ready\n", dev->name); 50 return 0; 51 } 52 53 while (true) { 54 process_sample(dev); 55 k_sleep(K_MSEC(2000)); 56 } 57 return 0; 58 } 59