1 /*
2 * Copyright (c) 2018 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
13 #ifdef CONFIG_LIS3MDL_TRIGGER
14 static int lis3mdl_trig_cnt;
15
lis3mdl_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)16 static void lis3mdl_trigger_handler(const struct device *dev,
17 const struct sensor_trigger *trig)
18 {
19 sensor_sample_fetch_chan(dev, trig->chan);
20 lis3mdl_trig_cnt++;
21 }
22 #endif
23
main(void)24 int main(void)
25 {
26 struct sensor_value temp, hum, press;
27 struct sensor_value magn_xyz[3], accel_xyz[3];
28 const struct device *const hts221 = DEVICE_DT_GET_ONE(st_hts221);
29 const struct device *const lis3mdl = DEVICE_DT_GET_ONE(st_lis3mdl_magn);
30 const struct device *const lsm6ds0 = DEVICE_DT_GET_ONE(st_lsm6ds0);
31 const struct device *const lps25hb = DEVICE_DT_GET_ONE(st_lps25hb_press);
32 #if defined(CONFIG_LIS3MDL_TRIGGER)
33 struct sensor_trigger trig;
34 int cnt = 1;
35 #endif
36
37 if (!device_is_ready(hts221)) {
38 printk("%s: device not ready.\n", hts221->name);
39 return 0;
40 }
41 if (!device_is_ready(lis3mdl)) {
42 printk("%s: device not ready.\n", lis3mdl->name);
43 return 0;
44 }
45 if (!device_is_ready(lsm6ds0)) {
46 printk("%s: device not ready.\n", lsm6ds0->name);
47 return 0;
48 }
49 if (!device_is_ready(lps25hb)) {
50 printk("%s: device not ready.\n", lps25hb->name);
51 return 0;
52 }
53
54 #ifdef CONFIG_LIS3MDL_TRIGGER
55 trig.type = SENSOR_TRIG_DATA_READY;
56 trig.chan = SENSOR_CHAN_MAGN_XYZ;
57 sensor_trigger_set(lis3mdl, &trig, lis3mdl_trigger_handler);
58 #endif
59
60 while (1) {
61
62 /* Get sensor samples */
63
64 if (sensor_sample_fetch(hts221) < 0) {
65 printf("HTS221 Sensor sample update error\n");
66 return 0;
67 }
68 if (sensor_sample_fetch(lps25hb) < 0) {
69 printf("LPS25HB Sensor sample update error\n");
70 return 0;
71 }
72 #ifndef CONFIG_LIS3MDL_TRIGGER
73 if (sensor_sample_fetch(lis3mdl) < 0) {
74 printf("LIS3MDL Sensor sample update error\n");
75 return 0;
76 }
77 #endif
78 if (sensor_sample_fetch(lsm6ds0) < 0) {
79 printf("LSM6DS0 Sensor sample update error\n");
80 return 0;
81 }
82
83 /* Get sensor data */
84
85 sensor_channel_get(hts221, SENSOR_CHAN_AMBIENT_TEMP, &temp);
86 sensor_channel_get(hts221, SENSOR_CHAN_HUMIDITY, &hum);
87 sensor_channel_get(lps25hb, SENSOR_CHAN_PRESS, &press);
88 sensor_channel_get(lis3mdl, SENSOR_CHAN_MAGN_XYZ, magn_xyz);
89 sensor_channel_get(lsm6ds0, SENSOR_CHAN_ACCEL_XYZ, accel_xyz);
90
91 /* Display sensor data */
92
93 /* Erase previous */
94 printf("\0033\014");
95
96 printf("X-NUCLEO-IKS01A1 sensor dashboard\n\n");
97
98 /* temperature */
99 printf("HTS221: Temperature: %.1f C\n",
100 sensor_value_to_double(&temp));
101
102 /* humidity */
103 printf("HTS221: Relative Humidity: %.1f%%\n",
104 sensor_value_to_double(&hum));
105
106 /* pressure */
107 printf("LPS25HB: Pressure:%.1f kpa\n",
108 sensor_value_to_double(&press));
109
110 /* magneto data */
111 printf(
112 "LIS3MDL: Magnetic field (gauss): x: %.1f, y: %.1f, z: %.1f\n",
113 sensor_value_to_double(&magn_xyz[0]),
114 sensor_value_to_double(&magn_xyz[1]),
115 sensor_value_to_double(&magn_xyz[2]));
116
117 /* acceleration */
118 printf(
119 "LSM6DS0: Acceleration (m.s-2): x: %.1f, y: %.1f, z: %.1f\n",
120 sensor_value_to_double(&accel_xyz[0]),
121 sensor_value_to_double(&accel_xyz[1]),
122 sensor_value_to_double(&accel_xyz[2]));
123
124
125 #if defined(CONFIG_LIS3MDL_TRIGGER)
126 printk("%d:: lis3mdl trig %d\n", cnt, lis3mdl_trig_cnt);
127 cnt++;
128 #endif
129
130 k_sleep(K_MSEC(2000));
131 }
132 }
133