From 9dabb21ceddc938ea28eadde24f65748ba8ba1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Cab=C3=A9?= Date: Fri, 11 Jul 2025 23:51:54 +0200 Subject: [PATCH] samples: sensor: paj7620: refactor handling of trigger mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current sample was always trying to enable trigger mode in the driver no matter what, causing issues for instances where the sensor simply has no interrupt pin configured in Devicetree. Cleaned things up so that enabling trigger mode is done via the driver's Kconfig option, and cleaned up the Twister testcases accordingly (plus, made sure they are actually built by setting min_ram to a value compatibel with the nucleo_f334r8). README has also been updated to clearly document how to enable either mode. Signed-off-by: Benjamin Cabé --- samples/sensor/paj7620_gesture/Kconfig | 10 ------ samples/sensor/paj7620_gesture/README.rst | 25 ++++++++++--- samples/sensor/paj7620_gesture/prj.conf | 2 -- samples/sensor/paj7620_gesture/sample.yaml | 17 +++++---- samples/sensor/paj7620_gesture/src/main.c | 40 ++++++++++++--------- samples/sensor/paj7620_gesture/trigger.conf | 2 ++ 6 files changed, 57 insertions(+), 39 deletions(-) delete mode 100644 samples/sensor/paj7620_gesture/Kconfig create mode 100644 samples/sensor/paj7620_gesture/trigger.conf diff --git a/samples/sensor/paj7620_gesture/Kconfig b/samples/sensor/paj7620_gesture/Kconfig deleted file mode 100644 index b977690ddf3..00000000000 --- a/samples/sensor/paj7620_gesture/Kconfig +++ /dev/null @@ -1,10 +0,0 @@ -# Copyright (c) 2025 Paul Timke -# SPDX-License-Identifier: Apache-2.0 - -mainmenu "PAJ7620 sample application" - -config APP_USE_POLLING - bool "Select y to use polling, otherwise the sample will use triggers" - default y - -source "Kconfig.zephyr" diff --git a/samples/sensor/paj7620_gesture/README.rst b/samples/sensor/paj7620_gesture/README.rst index 788e7561888..f1a6e2438e0 100644 --- a/samples/sensor/paj7620_gesture/README.rst +++ b/samples/sensor/paj7620_gesture/README.rst @@ -7,16 +7,15 @@ Overview ******** -This sample application gets the output of a gesture sensor (paj7620) using either polling or -triggers (depending on CONFIG_APP_USE_POLLING) and outputs the corresponding gesture to the -console, each time one is detected. +This sample application gets the output of a gesture sensor (PAJ7620) using either polling or +triggers, and outputs the corresponding gesture to the console, each time one is detected. Requirements ************ To use this sample, the following hardware is required: -* A board with I2C support and GPIO to detect external interrutps +* A board with I2C support (and GPIO to detect external interrupts in trigger mode) * PAJ7620 sensor Building and Running @@ -24,12 +23,30 @@ Building and Running This sample outputs data to the console. It requires a PAJ7620 sensor. +Polling Mode +============ + .. zephyr-app-commands:: :zephyr-app: samples/sensor/paj7620_gesture :board: nucleo_f334r8 :goals: build :compact: +Trigger Mode +============ + +In trigger mode, the sample application uses a GPIO to detect external interrupts, therefore GPIO +support must be enabled. Just like every sensor supporting trigger mode, it is possible to choose +between using a global thread (``CONFIG_PAJ7620_TRIGGER_GLOBAL_THREAD``) or a dedicated thread +(``CONFIG_PAJ7620_TRIGGER_OWN_THREAD``) for the interrupt handling. + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/paj7620_gesture + :board: nucleo_f334r8 + :goals: build + :gen-args: -DEXTRA_CONF_FILE=trigger.conf + :compact: + Sample Output ============= diff --git a/samples/sensor/paj7620_gesture/prj.conf b/samples/sensor/paj7620_gesture/prj.conf index 845bcc219b1..d1027c82490 100644 --- a/samples/sensor/paj7620_gesture/prj.conf +++ b/samples/sensor/paj7620_gesture/prj.conf @@ -1,5 +1,3 @@ -CONFIG_GPIO=y CONFIG_STDOUT_CONSOLE=y CONFIG_I2C=y CONFIG_SENSOR=y -CONFIG_PAJ7620_TRIGGER_OWN_THREAD=y diff --git a/samples/sensor/paj7620_gesture/sample.yaml b/samples/sensor/paj7620_gesture/sample.yaml index 33ee9743178..440e16ed3a2 100644 --- a/samples/sensor/paj7620_gesture/sample.yaml +++ b/samples/sensor/paj7620_gesture/sample.yaml @@ -1,17 +1,20 @@ sample: name: PAJ7620 gesture trigger sample +common: + min_ram: 12 + tags: sensors + platform_allow: nucleo_f334r8 + filter: dt_compat_enabled("pixart,paj7620") tests: sample.sensor.paj7620_gesture_trig: build_only: true - tags: sensors - platform_allow: nucleo_f334r8 depends_on: - i2c - gpio - filter: dt_compat_enabled("pixart,paj7620") + extra_args: EXTRA_CONF_FILE=trigger.conf sample.sensor.paj7620_gesture_polling: build_only: true - tags: sensors - platform_allow: nucleo_f334r8 - depends_on: i2c - filter: dt_compat_enabled("pixart,paj7620") + depends_on: + - i2c + extra_configs: + - CONFIG_PAJ7620_TRIGGER_NONE=y diff --git a/samples/sensor/paj7620_gesture/src/main.c b/samples/sensor/paj7620_gesture/src/main.c index c0dbdc735c2..cae127d3f51 100644 --- a/samples/sensor/paj7620_gesture/src/main.c +++ b/samples/sensor/paj7620_gesture/src/main.c @@ -10,6 +10,7 @@ #define GESTURE_POLL_TIME_MS 100 +#ifdef CONFIG_PAJ7620_TRIGGER K_SEM_DEFINE(sem, 0, 1); /* starts off "not available" */ static void trigger_handler(const struct device *dev, const struct sensor_trigger *trigger) @@ -23,6 +24,7 @@ static void trigger_handler(const struct device *dev, const struct sensor_trigge k_sem_give(&sem); } +#endif static void print_hand_gesture(uint16_t gest_flags) { @@ -52,6 +54,7 @@ static void print_hand_gesture(uint16_t gest_flags) } } +#ifdef CONFIG_PAJ7620_TRIGGER_OWN_THREAD static void trigger_main_loop(const struct device *dev) { struct sensor_value data; @@ -65,7 +68,9 @@ static void trigger_main_loop(const struct device *dev) print_hand_gesture(data.val1); } } +#endif +#ifdef CONFIG_PAJ7620_TRIGGER_NONE static void polling_main_loop(const struct device *dev) { struct sensor_value data; @@ -79,32 +84,35 @@ static void polling_main_loop(const struct device *dev) k_msleep(GESTURE_POLL_TIME_MS); } } +#endif int main(void) { - int ret; const struct device *dev = DEVICE_DT_GET_ONE(pixart_paj7620); - struct sensor_trigger trig = { - .type = SENSOR_TRIG_MOTION, - .chan = (enum sensor_channel)SENSOR_CHAN_PAJ7620_GESTURES, - }; - if (!device_is_ready(dev)) { printf("Device %s is not ready\n", dev->name); return -ENODEV; } - if (IS_ENABLED(CONFIG_APP_USE_POLLING)) { - polling_main_loop(dev); - } else { - /* App was configured to NOT use polling, so use triggers */ - ret = sensor_trigger_set(dev, &trig, trigger_handler); - if (ret < 0) { - printf("Could not set trigger\n"); - return ret; - } +#ifdef CONFIG_PAJ7620_TRIGGER + struct sensor_trigger trig = { + .type = SENSOR_TRIG_MOTION, + .chan = (enum sensor_channel)SENSOR_CHAN_PAJ7620_GESTURES, + }; + int ret; - trigger_main_loop(dev); + printf("PAJ7620 gesture sensor sample - trigger mode\n"); + + ret = sensor_trigger_set(dev, &trig, trigger_handler); + if (ret < 0) { + printf("Could not set trigger\n"); + return ret; } + + trigger_main_loop(dev); +#else + printf("PAJ7620 gesture sensor sample - polling mode\n"); + polling_main_loop(dev); +#endif } diff --git a/samples/sensor/paj7620_gesture/trigger.conf b/samples/sensor/paj7620_gesture/trigger.conf new file mode 100644 index 00000000000..3f5f19b4096 --- /dev/null +++ b/samples/sensor/paj7620_gesture/trigger.conf @@ -0,0 +1,2 @@ +CONFIG_GPIO=y +CONFIG_PAJ7620_TRIGGER_OWN_THREAD=y