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