Skip to content

Commit 0f0964f

Browse files
paultimkekartben
authored andcommitted
samples: sensor: paj7620_gesture: added sample
Added a sample for using the PAJ7620 gesture sensor Signed-off-by: Paul Timke Contreras <[email protected]>
1 parent 4fd5ab9 commit 0f0964f

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2025 Paul Timke <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(paj7620_gesture)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Paul Timke <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "PAJ7620 sample application"
5+
6+
config APP_USE_POLLING
7+
bool "Select y to use polling, otherwise the sample will use triggers"
8+
default y
9+
10+
source "Kconfig.zephyr"
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. zephyr:code-sample:: paj7620_gesture
2+
:name: PAJ7620 Gesture Sensor
3+
:relevant-api: sensor_interface
4+
5+
Get hand gesture data from PAJ7620 sensor.
6+
7+
Overview
8+
********
9+
10+
This sample application gets the output of a gesture sensor (paj7620) using either polling or
11+
triggers (depending on CONFIG_APP_USE_POLLING) and outputs the corresponding gesture to the
12+
console, each time one is detected.
13+
14+
Requirements
15+
************
16+
17+
To use this sample, the following hardware is required:
18+
19+
* A board with I2C support and GPIO to detect external interrutps
20+
* PAJ7620 sensor
21+
22+
Building and Running
23+
********************
24+
25+
This sample outputs data to the console. It requires a PAJ7620 sensor.
26+
27+
.. zephyr-app-commands::
28+
:zephyr-app: samples/sensor/paj7620_gesture
29+
:board: nucleo_f334r8
30+
:goals: build
31+
:compact:
32+
33+
Sample Output
34+
=============
35+
36+
.. code-block:: console
37+
38+
Gesture LEFT
39+
Gesture RIGHT
40+
Gesture UP
41+
Gesture DOWN
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
&gpioa {
2+
status = "okay";
3+
};
4+
5+
&i2c1 {
6+
status = "okay";
7+
8+
paj7620: paj7620@73 {
9+
compatible = "pixart,paj7620";
10+
reg = <0x73>;
11+
int-gpios = <&gpioa 9 (GPIO_PULL_UP)>;
12+
};
13+
};
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_GPIO=y
2+
CONFIG_STDOUT_CONSOLE=y
3+
CONFIG_I2C=y
4+
CONFIG_SENSOR=y
5+
CONFIG_PAJ7620_TRIGGER_OWN_THREAD=y
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
sample:
2+
name: PAJ7620 gesture trigger sample
3+
tests:
4+
sample.sensor.paj7620_gesture_trig:
5+
build_only: true
6+
tags: sensors
7+
platform_allow: nucleo_f334r8
8+
depends_on:
9+
- i2c
10+
- gpio
11+
filter: dt_compat_enabled("pixart,paj7620")
12+
sample.sensor.paj7620_gesture_polling:
13+
build_only: true
14+
tags: sensors
15+
platform_allow: nucleo_f334r8
16+
depends_on: i2c
17+
filter: dt_compat_enabled("pixart,paj7620")
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2025 Paul Timke <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/drivers/sensor.h>
9+
#include <zephyr/drivers/sensor/paj7620.h>
10+
11+
#define GESTURE_POLL_TIME_MS 100
12+
13+
K_SEM_DEFINE(sem, 0, 1); /* starts off "not available" */
14+
15+
static void trigger_handler(const struct device *dev, const struct sensor_trigger *trigger)
16+
{
17+
ARG_UNUSED(trigger);
18+
19+
if (sensor_sample_fetch(dev) < 0) {
20+
printf("sensor_sample_fetch failed\n");
21+
return;
22+
}
23+
24+
k_sem_give(&sem);
25+
}
26+
27+
static void print_hand_gesture(uint16_t gest_flags)
28+
{
29+
if ((gest_flags & PAJ7620_FLAG_GES_UP) != 0) {
30+
printf("Gesture UP\n");
31+
}
32+
if ((gest_flags & PAJ7620_FLAG_GES_DOWN) != 0) {
33+
printf("Gesture DOWN\n");
34+
}
35+
if ((gest_flags & PAJ7620_FLAG_GES_LEFT) != 0) {
36+
printf("Gesture LEFT\n");
37+
}
38+
if ((gest_flags & PAJ7620_FLAG_GES_RIGHT) != 0) {
39+
printf("Gesture RIGHT\n");
40+
}
41+
if ((gest_flags & PAJ7620_FLAG_GES_FORWARD) != 0) {
42+
printf("Gesture FORWARD\n");
43+
}
44+
if ((gest_flags & PAJ7620_FLAG_GES_BACKWARD) != 0) {
45+
printf("Gesture BACKWARD\n");
46+
}
47+
if ((gest_flags & PAJ7620_FLAG_GES_CLOCKWISE) != 0) {
48+
printf("Gesture CLOCKWISE\n");
49+
}
50+
if ((gest_flags & PAJ7620_FLAG_GES_COUNTERCLOCKWISE) != 0) {
51+
printf("Gesture COUNTER CLOCKWISE\n");
52+
}
53+
}
54+
55+
static void trigger_main_loop(const struct device *dev)
56+
{
57+
struct sensor_value data;
58+
59+
while (1) {
60+
k_sem_take(&sem, K_FOREVER);
61+
62+
(void)sensor_channel_get(dev, (enum sensor_channel)SENSOR_CHAN_PAJ7620_GESTURES,
63+
&data);
64+
65+
print_hand_gesture(data.val1);
66+
}
67+
}
68+
69+
static void polling_main_loop(const struct device *dev)
70+
{
71+
struct sensor_value data;
72+
73+
while (1) {
74+
(void)sensor_sample_fetch(dev);
75+
(void)sensor_channel_get(dev, (enum sensor_channel)SENSOR_CHAN_PAJ7620_GESTURES,
76+
&data);
77+
78+
print_hand_gesture(data.val1);
79+
k_msleep(GESTURE_POLL_TIME_MS);
80+
}
81+
}
82+
83+
int main(void)
84+
{
85+
int ret;
86+
const struct device *dev = DEVICE_DT_GET_ONE(pixart_paj7620);
87+
88+
struct sensor_trigger trig = {
89+
.type = SENSOR_TRIG_MOTION,
90+
.chan = (enum sensor_channel)SENSOR_CHAN_PAJ7620_GESTURES,
91+
};
92+
93+
if (!device_is_ready(dev)) {
94+
printf("Device %s is not ready\n", dev->name);
95+
return -ENODEV;
96+
}
97+
98+
if (IS_ENABLED(CONFIG_APP_USE_POLLING)) {
99+
polling_main_loop(dev);
100+
} else {
101+
/* App was configured to NOT use polling, so use triggers */
102+
ret = sensor_trigger_set(dev, &trig, trigger_handler);
103+
if (ret < 0) {
104+
printf("Could not set trigger\n");
105+
return ret;
106+
}
107+
108+
trigger_main_loop(dev);
109+
}
110+
}

0 commit comments

Comments
 (0)