1 /*
2  * Copyright (c) 2020 TDK Invensense
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 
now_str(void)12 static const char *now_str(void)
13 {
14 	static char buf[16]; /* ...HH:MM:SS.MMM */
15 	uint32_t now = k_uptime_get_32();
16 	unsigned int ms = now % MSEC_PER_SEC;
17 	unsigned int s;
18 	unsigned int min;
19 	unsigned int h;
20 
21 	now /= MSEC_PER_SEC;
22 	s = now % 60U;
23 	now /= 60U;
24 	min = now % 60U;
25 	now /= 60U;
26 	h = now;
27 
28 	snprintf(buf, sizeof(buf), "%u:%02u:%02u.%03u",
29 		 h, min, s, ms);
30 	return buf;
31 }
32 
process_icm42605(const struct device * dev)33 static int process_icm42605(const struct device *dev)
34 {
35 	struct sensor_value temperature;
36 	struct sensor_value accel[3];
37 	struct sensor_value gyro[3];
38 	int rc = sensor_sample_fetch(dev);
39 
40 	if (rc == 0) {
41 		rc = sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ,
42 					accel);
43 	}
44 	if (rc == 0) {
45 		rc = sensor_channel_get(dev, SENSOR_CHAN_GYRO_XYZ,
46 					gyro);
47 	}
48 	if (rc == 0) {
49 		rc = sensor_channel_get(dev, SENSOR_CHAN_DIE_TEMP,
50 					&temperature);
51 	}
52 	if (rc == 0) {
53 		printf("[%s]:% g Cel\n"
54 		       "  accel % f % f % f m/s/s\n"
55 		       "  gyro  % f % f % f rad/s\n",
56 		       now_str(),
57 		       sensor_value_to_double(&temperature),
58 		       sensor_value_to_double(&accel[0]),
59 		       sensor_value_to_double(&accel[1]),
60 		       sensor_value_to_double(&accel[2]),
61 		       sensor_value_to_double(&gyro[0]),
62 		       sensor_value_to_double(&gyro[1]),
63 		       sensor_value_to_double(&gyro[2]));
64 	} else {
65 		printf("sample fetch/get failed: %d\n", rc);
66 	}
67 
68 	return rc;
69 }
70 
71 static struct sensor_trigger data_trigger;
72 static struct sensor_trigger tap_trigger;
73 static struct sensor_trigger double_tap_trigger;
74 
handle_icm42605_drdy(const struct device * dev,const struct sensor_trigger * trig)75 static void handle_icm42605_drdy(const struct device *dev,
76 				 const struct sensor_trigger *trig)
77 {
78 	int rc = process_icm42605(dev);
79 
80 	if (rc != 0) {
81 		printf("cancelling trigger due to failure: %d\n", rc);
82 		(void)sensor_trigger_set(dev, trig, NULL);
83 		return;
84 	}
85 }
86 
handle_icm42605_tap(const struct device * dev,const struct sensor_trigger * trig)87 static void handle_icm42605_tap(const struct device *dev,
88 				const struct sensor_trigger *trig)
89 {
90 	printf("Tap Detected!\n");
91 }
92 
handle_icm42605_double_tap(const struct device * dev,const struct sensor_trigger * trig)93 static void handle_icm42605_double_tap(const struct device *dev,
94 				       const struct sensor_trigger *trig)
95 {
96 	printf("Double Tap detected!\n");
97 }
98 
main(void)99 int main(void)
100 {
101 	const struct device *const icm42605 = DEVICE_DT_GET_ONE(invensense_icm42605);
102 
103 	if (!device_is_ready(icm42605)) {
104 		printk("sensor: device not ready.\n");
105 		return 0;
106 	}
107 
108 	tap_trigger = (struct sensor_trigger) {
109 		.type = SENSOR_TRIG_TAP,
110 		.chan = SENSOR_CHAN_ALL,
111 	};
112 
113 	if (sensor_trigger_set(icm42605, &tap_trigger,
114 			       handle_icm42605_tap) < 0) {
115 		printf("Cannot configure tap trigger!!!\n");
116 		return 0;
117 	}
118 
119 	double_tap_trigger = (struct sensor_trigger) {
120 		.type = SENSOR_TRIG_DOUBLE_TAP,
121 		.chan = SENSOR_CHAN_ALL,
122 	};
123 
124 	if (sensor_trigger_set(icm42605, &double_tap_trigger,
125 			       handle_icm42605_double_tap) < 0) {
126 		printf("Cannot configure double tap trigger!!!\n");
127 		return 0;
128 	}
129 
130 	data_trigger = (struct sensor_trigger) {
131 		.type = SENSOR_TRIG_DATA_READY,
132 		.chan = SENSOR_CHAN_ALL,
133 	};
134 
135 	if (sensor_trigger_set(icm42605, &data_trigger,
136 			       handle_icm42605_drdy) < 0) {
137 		printf("Cannot configure data trigger!!!\n");
138 		return 0;
139 	}
140 
141 	printf("Configured for triggered sampling.\n");
142 	return 0;
143 }
144