|
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; |
| 27 | +static struct k_sem zephyr_touch_sem; |
32 | 28 |
|
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]; |
| 29 | +typedef void (*zephyr_input_callback_t)(struct input_event *evt, void *user_data); |
| 30 | +extern "C" void zephyr_input_register_callback(zephyr_input_callback_t cb, void *user_data); |
| 31 | +void touch_event_callback(struct input_event *evt, void *user_data); |
36 | 32 |
|
37 |
| -typedef void (*zephyr_input_callback_t)(struct input_event *evt, |
38 |
| - 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); |
| 33 | +Arduino_GigaDisplayTouch::Arduino_GigaDisplayTouch() { |
41 | 34 |
|
42 |
| -Arduino_GigaDisplayTouch::Arduino_GigaDisplayTouch() {} |
| 35 | +} |
| 36 | + |
| 37 | +Arduino_GigaDisplayTouch::~Arduino_GigaDisplayTouch() { |
43 | 38 |
|
44 |
| -Arduino_GigaDisplayTouch::~Arduino_GigaDisplayTouch() {} |
| 39 | +} |
45 | 40 |
|
46 | 41 | bool Arduino_GigaDisplayTouch::begin() {
|
47 |
| - k_sem_init(&zephyr_touch_event_sync, 0, 1); |
48 |
| - zephyr_input_register_callback(touch_event_callback); |
| 42 | + _gt911TouchHandler = nullptr; |
| 43 | + k_sem_init(&zephyr_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 | +} |
53 | 52 |
|
54 | 53 | uint8_t Arduino_GigaDisplayTouch::getTouchPoints(GDTpoint_t *points) {
|
55 | 54 | // First wait to see if we get any events.
|
56 |
| - if (k_sem_take(&zephyr_touch_event_sync, K_NO_WAIT) != 0) { |
| 55 | + if (k_sem_take(&zephyr_touch_sem, K_NO_WAIT)) { |
57 | 56 | return 0;
|
58 | 57 | }
|
59 | 58 |
|
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; |
| 59 | + size_t count_pressed = 0; |
| 60 | + for (int i = 0; i < GT911_MAX_CONTACTS; i++) { |
| 61 | + if (_points[i].pressed) { |
| 62 | + points[count_pressed].trackId = _points[i].trackId; |
| 63 | + points[count_pressed].x = _points[i].x; |
| 64 | + points[count_pressed].y = _points[i].y; |
65 | 65 | count_pressed++;
|
66 | 66 | }
|
67 | 67 | }
|
| 68 | + |
| 69 | + k_sem_give(&zephyr_touch_sem); |
68 | 70 | return count_pressed;
|
69 | 71 | }
|
70 | 72 |
|
71 |
| -void Arduino_GigaDisplayTouch::onDetect(void (*handler)(uint8_t, |
72 |
| - GDTpoint_t *)) { |
73 |
| - UNUSED(handler); |
| 73 | +void Arduino_GigaDisplayTouch::onDetect(GDTTouchHandler_t handler) { |
| 74 | + _gt911TouchHandler = handler; |
74 | 75 | }
|
75 | 76 |
|
76 |
| -static void touch_event_callback(struct input_event *evt, void *user_data) { |
77 |
| - static const struct device *const touch_dev = |
78 |
| - DEVICE_DT_GET(DT_CHOSEN(zephyr_touch)); |
| 77 | +void touch_event_callback(struct input_event *evt, void *user_data) { |
| 78 | + static int8_t tp_index = 0; |
| 79 | + static bool sem_taken = false; |
| 80 | + static const struct device *const touch_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_touch)); |
| 81 | + Arduino_GigaDisplayTouch *touch = (Arduino_GigaDisplayTouch *) user_data; |
79 | 82 |
|
80 |
| - if (evt->dev != touch_dev) { |
| 83 | + if (!touch || evt->dev != touch_dev) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // Take semaphore on first event. |
| 88 | + if (evt->code == INPUT_ABS_MT_SLOT) { |
| 89 | + // If the semaphore is already taken do Not try to take it again. |
| 90 | + // This could only happen if the event queue dropped BTN_TOUCH. |
| 91 | + if (!sem_taken && k_sem_take(&zephyr_touch_sem, K_NO_WAIT)) { |
| 92 | + return; |
| 93 | + } |
| 94 | + sem_taken = true; |
| 95 | + } else if (!sem_taken) { |
| 96 | + // On subsequent events, return if we don't have the semaphore. |
81 | 97 | return;
|
82 | 98 | }
|
83 | 99 |
|
84 | 100 | switch (evt->code) {
|
85 | 101 | case INPUT_ABS_MT_SLOT:
|
86 |
| - zephyr_touch_cb_slot_num = evt->value; |
| 102 | + tp_index = evt->value; |
| 103 | + touch->_points[tp_index].trackId = evt->value; |
87 | 104 | break;
|
88 | 105 | case INPUT_ABS_X:
|
89 |
| - zephyr_touch_points[zephyr_touch_cb_slot_num].x = evt->value; |
| 106 | + touch->_points[tp_index].x = evt->value; |
90 | 107 | break;
|
91 | 108 | case INPUT_ABS_Y:
|
92 |
| - zephyr_touch_points[zephyr_touch_cb_slot_num].y = evt->value; |
| 109 | + touch->_points[tp_index].y = evt->value; |
93 | 110 | break;
|
94 | 111 | case INPUT_BTN_TOUCH:
|
95 |
| - zephyr_touch_points[zephyr_touch_cb_slot_num].pressed = evt->value; |
| 112 | + touch->_points[tp_index].pressed = evt->value; |
96 | 113 | break;
|
97 | 114 | }
|
98 | 115 |
|
99 |
| - if (evt->sync) { |
100 |
| - k_sem_give(&zephyr_touch_event_sync); |
| 116 | + // Release the semaphore only after updating all of the data. |
| 117 | + if (evt->code == INPUT_BTN_TOUCH) { |
| 118 | + sem_taken = false; |
| 119 | + k_sem_give(&zephyr_touch_sem); |
| 120 | + if (touch->_gt911TouchHandler) { |
| 121 | + touch->_gt911TouchHandler(GT911_MAX_CONTACTS, touch->_points); |
| 122 | + } |
101 | 123 | }
|
102 | 124 | }
|
103 | 125 | #endif
|
0 commit comments