Skip to content

Commit

Permalink
samples: sensor: accel_trig: add sample with double tap detection.
Browse files Browse the repository at this point in the history
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 <pierrickcurt@gmail.com>
  • Loading branch information
pcurt committed Jan 27, 2025
1 parent f07063d commit 8e0409b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
33 changes: 31 additions & 2 deletions samples/sensor/accel_trig/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
********************
Expand All @@ -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
Expand All @@ -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)
4 changes: 4 additions & 0 deletions samples/sensor/accel_trig/boards/stm32f3_disco.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONFIG_I2C=y
CONFIG_SENSOR_LOG_LEVEL_DBG=y
CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD=y
CONFIG_LIS2DW12_TAP=y
33 changes: 33 additions & 0 deletions samples/sensor/accel_trig/boards/stm32f3_disco.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024, Pierrick Curt.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/dt-bindings/sensor/lis2dw12.h>

/ {
aliases {
accel0 = &lis2dw12_accel;
};
};

&i2c2 {
pinctrl-0 = <&i2c2_scl_pa9 &i2c2_sda_pa10>;
pinctrl-names = "default";
status = "okay";
clock-frequency = <I2C_BITRATE_FAST>;

lis2dw12_accel: lis2dw12@19 {
compatible = "st,lis2dw12";
reg = <0x19>;
odr = <400>;
tap-mode = <LIS2DW12_DT_SINGLE_DOUBLE_TAP>;
tap-threshold = <12>, <12>, <12>;
power-mode = <LIS2DW12_DT_HP_MODE>;
tap-shock = <0x03>;
tap-quiet = <0x03>;
tap-latency = <0x03>;
irq-gpios = <&gpioe 6 GPIO_ACTIVE_HIGH>;
};
};
23 changes: 23 additions & 0 deletions samples/sensor/accel_trig/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -25,6 +38,7 @@ static void trigger_handler(const struct device *dev, const struct sensor_trigge

k_sem_give(&sem);
}
#endif

int main(void)
{
Expand All @@ -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);
Expand Down

0 comments on commit 8e0409b

Please sign in to comment.