1 /*
2 * Copyright (c) 2016 Intel Corporation
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 <zephyr/sys/printk.h>
11 #include <stdio.h>
12
main(void)13 int main(void)
14 {
15 const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(magn0));
16 struct sensor_value value_x, value_y, value_z;
17 int ret;
18
19 if (!device_is_ready(dev)) {
20 printk("sensor: device not ready.\n");
21 return 0;
22 }
23
24 printk("Polling magnetometer data from %s.\n", dev->name);
25
26 while (1) {
27 ret = sensor_sample_fetch(dev);
28 if (ret) {
29 printk("sensor_sample_fetch failed ret %d\n", ret);
30 return 0;
31 }
32
33 ret = sensor_channel_get(dev, SENSOR_CHAN_MAGN_X, &value_x);
34 ret = sensor_channel_get(dev, SENSOR_CHAN_MAGN_Y, &value_y);
35 ret = sensor_channel_get(dev, SENSOR_CHAN_MAGN_Z, &value_z);
36 printf("( x y z ) = ( %f %f %f )\n",
37 sensor_value_to_double(&value_x),
38 sensor_value_to_double(&value_y),
39 sensor_value_to_double(&value_z));
40
41 k_sleep(K_MSEC(500));
42 }
43 return 0;
44 }
45