Commit 9dabb21c authored by Benjamin Cabé's avatar Benjamin Cabé Committed by Chris Friedt
Browse files

samples: sensor: paj7620: refactor handling of trigger mode



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: default avatarBenjamin Cabé <benjamin@zephyrproject.org>
parent 5c95ff50
Loading
Loading
Loading
Loading
+0 −10
Original line number Diff line number Diff line
# Copyright (c) 2025 Paul Timke <ptimkec@live.com>
# 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"
+21 −4
Original line number Diff line number Diff line
@@ -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,10 +23,28 @@ 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
+0 −2
Original line number Diff line number Diff line
CONFIG_GPIO=y
CONFIG_STDOUT_CONSOLE=y
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_PAJ7620_TRIGGER_OWN_THREAD=y
+10 −7
Original line number Diff line number Diff line
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
+24 −16
Original line number Diff line number Diff line
@@ -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,26 +84,26 @@ 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);

	if (!device_is_ready(dev)) {
		printf("Device %s is not ready\n", dev->name);
		return -ENODEV;
	}

#ifdef CONFIG_PAJ7620_TRIGGER
	struct sensor_trigger trig = {
		.type = SENSOR_TRIG_MOTION,
		.chan = (enum sensor_channel)SENSOR_CHAN_PAJ7620_GESTURES,
	};
	int ret;

	if (!device_is_ready(dev)) {
		printf("Device %s is not ready\n", dev->name);
		return -ENODEV;
	}
	printf("PAJ7620 gesture sensor sample - trigger mode\n");

	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");
@@ -106,5 +111,8 @@ int main(void)
	}

	trigger_main_loop(dev);
	}
#else
	printf("PAJ7620 gesture sensor sample - polling mode\n");
	polling_main_loop(dev);
#endif
}
Loading