|
| 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