1 /*
2  * Copyright (c) 2024 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/sensor.h>
8 
9 
10 const struct device *const accel0 = DEVICE_DT_GET(DT_ALIAS(accel0));
11 
12 static struct tap_count_state {
13 	struct sensor_trigger trig;
14 	uint32_t count;
15 } tap_count_state = {
16 	.trig = {
17 		.chan = SENSOR_CHAN_ACCEL_XYZ,
18 		.type = SENSOR_TRIG_TAP,
19 	},
20 	.count = 0,
21 };
22 
tap_handler(const struct device * dev,const struct sensor_trigger * trig)23 void tap_handler(const struct device *dev, const struct sensor_trigger *trig)
24 {
25 	struct tap_count_state *state = CONTAINER_OF(trig, struct tap_count_state, trig);
26 
27 	state->count++;
28 
29 	printk("Tap! Total Taps: %u\n", state->count);
30 }
31 
main(void)32 int main(void)
33 {
34 	int rc;
35 
36 	printk("Tap Counter Example (%s)\n", CONFIG_ARCH);
37 
38 	rc = sensor_trigger_set(accel0, &tap_count_state.trig, tap_handler);
39 
40 	if (rc != 0) {
41 		printk("Failed to set trigger handler for taps, error %d\n", rc);
42 		return rc;
43 	}
44 
45 	return rc;
46 }
47