1 /*
2  * Copyright (c) 2017, NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/sensor.h>
9 #include <stdio.h>
10 
11 K_SEM_DEFINE(sem, 0, 1);	/* starts off "not available" */
12 
trigger_handler(const struct device * dev,const struct sensor_trigger * trigger)13 static void trigger_handler(const struct device *dev,
14 			    const struct sensor_trigger *trigger)
15 {
16 	k_sem_give(&sem);
17 }
18 
main(void)19 int main(void)
20 {
21 	struct sensor_value gyro[3];
22 	const struct device *const dev = DEVICE_DT_GET_ANY(nxp_fxas21002);
23 
24 	if (dev == NULL || !device_is_ready(dev)) {
25 		printf("Could not get fxas21002 device\n");
26 		return 0;
27 	}
28 
29 	struct sensor_trigger trig = {
30 		.type = SENSOR_TRIG_DATA_READY,
31 		.chan = SENSOR_CHAN_GYRO_XYZ,
32 	};
33 
34 	if (sensor_trigger_set(dev, &trig, trigger_handler)) {
35 		printf("Could not set trigger\n");
36 		return 0;
37 	}
38 
39 	while (1) {
40 		k_sem_take(&sem, K_FOREVER);
41 		sensor_sample_fetch(dev);
42 		sensor_channel_get(dev, SENSOR_CHAN_GYRO_XYZ, gyro);
43 
44 		/* Print gyro x,y,z */
45 		printf("X=%10.3f Y=%10.3f Z=%10.3f\n",
46 		       sensor_value_to_double(&gyro[0]),
47 		       sensor_value_to_double(&gyro[1]),
48 		       sensor_value_to_double(&gyro[2]));
49 	}
50 	return 0;
51 }
52