From 8e0409bf23d0c037d1910c7b39f0acf93353c030 Mon Sep 17 00:00:00 2001 From: Pierrick Curt Date: Sun, 26 Jan 2025 17:05:35 +0100 Subject: [PATCH] samples: sensor: accel_trig: add sample with double tap detection. Add the tap detection trigger to the existing example. For this example it uses lis2dw12 accelerometer to detect a double tap. Signed-off-by: Pierrick Curt --- samples/sensor/accel_trig/README.rst | 33 +++++++++++++++++-- .../accel_trig/boards/stm32f3_disco.conf | 4 +++ .../accel_trig/boards/stm32f3_disco.overlay | 33 +++++++++++++++++++ samples/sensor/accel_trig/src/main.c | 23 +++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 samples/sensor/accel_trig/boards/stm32f3_disco.conf create mode 100644 samples/sensor/accel_trig/boards/stm32f3_disco.overlay diff --git a/samples/sensor/accel_trig/README.rst b/samples/sensor/accel_trig/README.rst index 37a3f18e8c8e..26d3d956bb25 100644 --- a/samples/sensor/accel_trig/README.rst +++ b/samples/sensor/accel_trig/README.rst @@ -7,6 +7,10 @@ Overview ******** This sample application demonstrates how to use 3-Axis accelerometers with triggers. +By defaut it uses a data ready trigger to read the accelerometer data and print it to the console. + +If the accelerometer is enabled with a tap trigger, the sample uses the tap trigger event to +read the accelerometer data and print it to the console. Building and Running ******************** @@ -27,8 +31,8 @@ Make sure the aliases are in devicetree, then build and run with: :goals: build flash :compact: -Sample Output -============= +Sample Output (SENSOR_TRIG_DATA_READY) +======================================= .. code-block:: console @@ -43,3 +47,28 @@ Sample Output fxos8700@1d [m/s^2]: ( -0.105345, -0.028731, 9.921571) fxos8700@1d [m/s^2]: ( -0.095769, -0.028731, 9.931148) fxos8700@1d [m/s^2]: ( -0.095769, -0.009577, 9.940725) + + +Sample Output (SENSOR_TRIG_DOUBLE_TAP) +====================================== + +.. code-block:: console + + TAP detected + lis2dw12@19 [m/s^2]: ( -1.899901, -12.550355, -2.742174) + TAP detected + lis2dw12@19 [m/s^2]: ( 12.349357, -18.125630, 6.015556) + TAP detected + lis2dw12@19 [m/s^2]: ( -11.385050, -7.274181, -9.229117) + TAP detected + lis2dw12@19 [m/s^2]: ( 9.214760, -9.286545, 2.311466) + TAP detected + lis2dw12@19 [m/s^2]: ( 10.090533, -17.391034, 12.320643) + TAP detected + lis2dw12@19 [m/s^2]: ( -0.478564, 2.390429, 15.876378) + TAP detected + lis2dw12@19 [m/s^2]: ( -5.668596, -13.138989, 0.741775) + TAP detected + lis2dw12@19 [m/s^2]: ( -2.385644, -10.559526, 9.899107) + TAP detected + lis2dw12@19 [m/s^2]: ( 7.537391, -8.551948, 16.740187) diff --git a/samples/sensor/accel_trig/boards/stm32f3_disco.conf b/samples/sensor/accel_trig/boards/stm32f3_disco.conf new file mode 100644 index 000000000000..67dc73ec3425 --- /dev/null +++ b/samples/sensor/accel_trig/boards/stm32f3_disco.conf @@ -0,0 +1,4 @@ +CONFIG_I2C=y +CONFIG_SENSOR_LOG_LEVEL_DBG=y +CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD=y +CONFIG_LIS2DW12_TAP=y diff --git a/samples/sensor/accel_trig/boards/stm32f3_disco.overlay b/samples/sensor/accel_trig/boards/stm32f3_disco.overlay new file mode 100644 index 000000000000..79d5a106a889 --- /dev/null +++ b/samples/sensor/accel_trig/boards/stm32f3_disco.overlay @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024, Pierrick Curt. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +/ { + aliases { + accel0 = &lis2dw12_accel; + }; +}; + +&i2c2 { + pinctrl-0 = <&i2c2_scl_pa9 &i2c2_sda_pa10>; + pinctrl-names = "default"; + status = "okay"; + clock-frequency = ; + + lis2dw12_accel: lis2dw12@19 { + compatible = "st,lis2dw12"; + reg = <0x19>; + odr = <400>; + tap-mode = ; + tap-threshold = <12>, <12>, <12>; + power-mode = ; + tap-shock = <0x03>; + tap-quiet = <0x03>; + tap-latency = <0x03>; + irq-gpios = <&gpioe 6 GPIO_ACTIVE_HIGH>; + }; +}; diff --git a/samples/sensor/accel_trig/src/main.c b/samples/sensor/accel_trig/src/main.c index e5ddde1e2bef..007bfbeccfa3 100644 --- a/samples/sensor/accel_trig/src/main.c +++ b/samples/sensor/accel_trig/src/main.c @@ -11,6 +11,19 @@ K_SEM_DEFINE(sem, 0, 1); /* starts off "not available" */ +#if DT_NODE_HAS_PROP(DT_ALIAS(accel0), tap_mode) +static void tap_trigger_handler(const struct device *dev, const struct sensor_trigger *trigger) +{ + ARG_UNUSED(trigger); + printf("TAP detected\n"); + + if (sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ) < 0) { + printf("ERROR: SENSOR_CHAN_ACCEL_XYZ fetch failed\n"); + } + + k_sem_give(&sem); +} +#else static void trigger_handler(const struct device *dev, const struct sensor_trigger *trigger) { ARG_UNUSED(trigger); @@ -25,6 +38,7 @@ static void trigger_handler(const struct device *dev, const struct sensor_trigge k_sem_give(&sem); } +#endif int main(void) { @@ -41,10 +55,19 @@ int main(void) return 0; } +#if DT_NODE_HAS_PROP(DT_ALIAS(accel0), tap_mode) + trig.type = SENSOR_TRIG_DOUBLE_TAP; + trig.chan = SENSOR_CHAN_ACCEL_XYZ; + if (sensor_trigger_set(dev, &trig, tap_trigger_handler) < 0) { + printf("Could not set tap trigger\n"); + return 0; + } +#else if (sensor_trigger_set(dev, &trig, trigger_handler)) { printf("Could not set trigger\n"); return 0; } +#endif while (1) { k_sem_take(&sem, K_FOREVER);