|
24 | 24 | #include <zephyr/kernel.h>
|
25 | 25 | #include <zephyr/sys/util.h>
|
26 | 26 |
|
27 |
| -typedef struct { |
28 |
| - size_t x; |
29 |
| - size_t y; |
30 |
| - bool pressed; |
31 |
| -} touch_point_t; |
32 |
| - |
33 |
| -static uint8_t zephyr_touch_cb_slot_num; |
34 |
| -static struct k_sem zephyr_touch_event_sync; |
35 |
| -static touch_point_t zephyr_touch_points[CONFIG_INPUT_GT911_MAX_TOUCH_POINTS]; |
| 27 | +static struct k_sem touch_sem; |
36 | 28 |
|
37 | 29 | typedef void (*zephyr_input_callback_t)(struct input_event *evt,
|
38 | 30 | void *user_data);
|
39 |
| -extern "C" void zephyr_input_register_callback(zephyr_input_callback_t cb); |
40 |
| -static void touch_event_callback(struct input_event *evt, void *user_data); |
| 31 | +extern "C" void zephyr_input_register_callback(zephyr_input_callback_t cb, |
| 32 | + void *user_data); |
| 33 | +void touch_event_callback(struct input_event *evt, void *user_data); |
41 | 34 |
|
42 | 35 | Arduino_GigaDisplayTouch::Arduino_GigaDisplayTouch() {}
|
43 | 36 |
|
44 | 37 | Arduino_GigaDisplayTouch::~Arduino_GigaDisplayTouch() {}
|
45 | 38 |
|
46 | 39 | bool Arduino_GigaDisplayTouch::begin() {
|
47 |
| - k_sem_init(&zephyr_touch_event_sync, 0, 1); |
48 |
| - zephyr_input_register_callback(touch_event_callback); |
| 40 | + _gt911TouchHandler = nullptr; |
| 41 | + // Initialize to 1 to prevent deadlock by ensuring that |
| 42 | + // at least one function can always proceed initially. |
| 43 | + k_sem_init(&touch_sem, 1, 1); |
| 44 | + zephyr_input_register_callback(touch_event_callback, this); |
49 | 45 | return true;
|
50 | 46 | }
|
51 | 47 |
|
52 |
| -void Arduino_GigaDisplayTouch::end() {} |
| 48 | +void Arduino_GigaDisplayTouch::end() { |
| 49 | + _gt911TouchHandler = nullptr; |
| 50 | + zephyr_input_register_callback(NULL, NULL); |
| 51 | + memset(_points, 0, sizeof(_points)); |
| 52 | +} |
53 | 53 |
|
54 | 54 | uint8_t Arduino_GigaDisplayTouch::getTouchPoints(GDTpoint_t *points) {
|
55 | 55 | // First wait to see if we get any events.
|
56 |
| - if (k_sem_take(&zephyr_touch_event_sync, K_NO_WAIT) != 0) { |
| 56 | + if (k_sem_take(&touch_sem, K_NO_WAIT)) { |
57 | 57 | return 0;
|
58 | 58 | }
|
59 | 59 |
|
60 |
| - uint8_t count_pressed = 0; |
61 |
| - for (uint8_t i = 0; i <= zephyr_touch_cb_slot_num; i++) { |
62 |
| - if (zephyr_touch_points[i].pressed) { |
63 |
| - points[count_pressed].x = zephyr_touch_points[i].x; |
64 |
| - points[count_pressed].y = zephyr_touch_points[i].y; |
| 60 | + size_t count_pressed = 0; |
| 61 | + for (int i = 0; i < GT911_MAX_CONTACTS; i++) { |
| 62 | + if (_points[i].pressed) { |
| 63 | + _points[i].pressed = 0; |
| 64 | + points[count_pressed].trackId = _points[i].trackId; |
| 65 | + points[count_pressed].x = _points[i].x; |
| 66 | + points[count_pressed].y = _points[i].y; |
65 | 67 | count_pressed++;
|
66 | 68 | }
|
67 | 69 | }
|
| 70 | + |
| 71 | + k_sem_give(&touch_sem); |
68 | 72 | return count_pressed;
|
69 | 73 | }
|
70 | 74 |
|
71 |
| -void Arduino_GigaDisplayTouch::onDetect(void (*handler)(uint8_t, |
72 |
| - GDTpoint_t *)) { |
73 |
| - UNUSED(handler); |
| 75 | +void Arduino_GigaDisplayTouch::onDetect(GDTTouchHandler_t handler) { |
| 76 | + _gt911TouchHandler = handler; |
74 | 77 | }
|
75 | 78 |
|
76 |
| -static void touch_event_callback(struct input_event *evt, void *user_data) { |
77 |
| - static const struct device *const touch_dev = |
| 79 | +void touch_event_callback(struct input_event *evt, void *user_data) { |
| 80 | + static int8_t index = 0; |
| 81 | + static bool sem_taken = false; |
| 82 | + |
| 83 | + static const struct device *const dev = |
78 | 84 | DEVICE_DT_GET(DT_CHOSEN(zephyr_touch));
|
| 85 | + Arduino_GigaDisplayTouch *touch = (Arduino_GigaDisplayTouch *)user_data; |
79 | 86 |
|
80 |
| - if (evt->dev != touch_dev) { |
| 87 | + if (!touch || evt->dev != dev) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // Take semaphore on first event. |
| 92 | + if (evt->code == INPUT_ABS_MT_SLOT) { |
| 93 | + // Check if the semaphore is already taken by this callback. |
| 94 | + // This could only happen if the event queue dropped BTN_TOUCH. |
| 95 | + if (!sem_taken && k_sem_take(&touch_sem, K_NO_WAIT) != 0) { |
| 96 | + return; |
| 97 | + } |
| 98 | + sem_taken = true; |
| 99 | + } else if (!sem_taken) { |
| 100 | + // On subsequent events, return if we don't have the semaphore. |
81 | 101 | return;
|
82 | 102 | }
|
83 | 103 |
|
84 | 104 | switch (evt->code) {
|
85 | 105 | case INPUT_ABS_MT_SLOT:
|
86 |
| - zephyr_touch_cb_slot_num = evt->value; |
| 106 | + index = evt->value; |
| 107 | + touch->_points[index].trackId = evt->value; |
87 | 108 | break;
|
88 | 109 | case INPUT_ABS_X:
|
89 |
| - zephyr_touch_points[zephyr_touch_cb_slot_num].x = evt->value; |
| 110 | + touch->_points[index].x = evt->value; |
90 | 111 | break;
|
91 | 112 | case INPUT_ABS_Y:
|
92 |
| - zephyr_touch_points[zephyr_touch_cb_slot_num].y = evt->value; |
| 113 | + touch->_points[index].y = evt->value; |
93 | 114 | break;
|
94 | 115 | case INPUT_BTN_TOUCH:
|
95 |
| - zephyr_touch_points[zephyr_touch_cb_slot_num].pressed = evt->value; |
| 116 | + touch->_points[index].pressed = evt->value; |
96 | 117 | break;
|
97 | 118 | }
|
98 | 119 |
|
99 |
| - if (evt->sync) { |
100 |
| - k_sem_give(&zephyr_touch_event_sync); |
| 120 | + // Release the semaphore on the last event (BTN_TOUCH pressed). |
| 121 | + if (evt->code == INPUT_BTN_TOUCH && evt->value) { |
| 122 | + sem_taken = false; |
| 123 | + k_sem_give(&touch_sem); |
| 124 | + if (touch->_gt911TouchHandler) { |
| 125 | + touch->_gt911TouchHandler(GT911_MAX_CONTACTS, touch->_points); |
| 126 | + } |
101 | 127 | }
|
102 | 128 | }
|
103 | 129 | #endif
|
0 commit comments