Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: sensor: access_trig: add double tap detection. #83818

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem to be generic enough since only ST sensors have this property. You want this generic sample to also work for the other sensors that support tap trigger (fxos8700, icm42605...), no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this is generic with sensor implementing the tap_mode, so only ST sensors.
But the others sensors doesn't have a tap configuration read from the device tree.
The other way to have a full generic sample would be to manage the trigger selection with a define in prj.conf. But I think that is not a very beautiful solution. What do you think ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @kartben . I think you could just add a Kconfig option to the sample to enable/disable using the tap trigger.

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
Loading