/* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include #include #include int main(void) { const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(magn0)); struct sensor_value value_x, value_y, value_z; int ret; if (!device_is_ready(dev)) { printk("sensor: device not ready.\n"); return 0; } printk("Polling magnetometer data from %s.\n", dev->name); while (1) { ret = sensor_sample_fetch(dev); if (ret) { printk("sensor_sample_fetch failed ret %d\n", ret); return 0; } ret = sensor_channel_get(dev, SENSOR_CHAN_MAGN_X, &value_x); ret = sensor_channel_get(dev, SENSOR_CHAN_MAGN_Y, &value_y); ret = sensor_channel_get(dev, SENSOR_CHAN_MAGN_Z, &value_z); printf("( x y z ) = ( %f %f %f )\n", sensor_value_to_double(&value_x), sensor_value_to_double(&value_y), sensor_value_to_double(&value_z)); k_sleep(K_MSEC(500)); } return 0; }