Skip to content

Commit fe5e8d2

Browse files
refactoring lvgl touch mngmt: add global class pointer
1 parent 3b6ad34 commit fe5e8d2

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/Arduino_GigaDisplayTouch.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@
3636
/* Private variables ---------------------------------------------------------*/
3737
rtos::Thread t;
3838
events::EventQueue queue(32 * EVENTS_EVENT_SIZE);
39-
40-
#if __has_include ("lvgl.h")
41-
bool lvgl_touch_pressed = false;
42-
uint16_t lvgl_touch_x_coord = 0;
43-
uint16_t lvgl_touch_y_coord = 0;
44-
#endif
39+
Arduino_GigaDisplayTouch * gThis;
4540

4641
/* Private function prototypes -----------------------------------------------*/
4742
#if __has_include ("lvgl.h")
@@ -83,8 +78,6 @@ bool Arduino_GigaDisplayTouch::begin() {
8378
pinMode(_intPin, INPUT);
8479

8580
_gt911TouchHandler = nullptr;
86-
t.start(callback(&queue, &events::EventQueue::dispatch_forever));
87-
_irqInt.rise(queue.event(mbed::callback(this, &Arduino_GigaDisplayTouch::_gt911onIrq)));
8881

8982
/* GT911 test communication */
9083
uint8_t testByte;
@@ -96,21 +89,27 @@ bool Arduino_GigaDisplayTouch::begin() {
9689
indev_drv.type = LV_INDEV_TYPE_POINTER; /* Touch pad is a pointer-like device */
9790
indev_drv.read_cb = _lvglTouchCb; /* Set your driver function */
9891
lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv); /* Register the driver in LVGL and save the created input device object */
92+
93+
gThis = this;
9994
#endif
10095

10196
return (error == 0);
10297
}
10398

10499
#if __has_include ("lvgl.h")
105100
void _lvglTouchCb(lv_indev_drv_t * indev, lv_indev_data_t * data) {
106-
data->state = (lvgl_touch_pressed) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
107-
if(data->state == LV_INDEV_STATE_PR) {
108-
data->point.x = lvgl_touch_x_coord;
109-
data->point.y = lvgl_touch_y_coord;
110-
lvgl_touch_pressed = false;
111-
}
112-
113-
return;
101+
uint8_t contacts;
102+
GDTpoint_t points[5];
103+
104+
if(gThis->detect(contacts, points)) {
105+
data->state = LV_INDEV_STATE_PR;
106+
data->point.x = points[0].x;
107+
data->point.y = points[0].y;
108+
} else {
109+
data->state = LV_INDEV_STATE_REL;
110+
}
111+
112+
return;
114113
}
115114
#endif
116115

@@ -142,6 +141,8 @@ bool Arduino_GigaDisplayTouch::detect(uint8_t& contacts, GDTpoint_t* points) {
142141

143142
void Arduino_GigaDisplayTouch::attach(void (*handler)(uint8_t, GDTpoint_t*)) {
144143
_gt911TouchHandler = handler;
144+
t.start(callback(&queue, &events::EventQueue::dispatch_forever));
145+
_irqInt.rise(queue.event(mbed::callback(this, &Arduino_GigaDisplayTouch::_gt911onIrq)));
145146
}
146147

147148
uint8_t Arduino_GigaDisplayTouch::_gt911WriteOp(uint16_t reg, uint8_t data) {
@@ -208,14 +209,6 @@ void Arduino_GigaDisplayTouch::_gt911onIrq() {
208209
}
209210

210211
if (contacts > 0 && _gt911TouchHandler != nullptr) _gt911TouchHandler(contacts, _points);
211-
212-
#if __has_include ("lvgl.h")
213-
if (contacts > 0) {
214-
lvgl_touch_pressed = true;
215-
lvgl_touch_x_coord = _points[0].x;
216-
lvgl_touch_y_coord = _points[0].y;
217-
}
218-
#endif
219212

220213
_gt911WriteOp(GT911_REG_GESTURE_START_POINT, 0); /* Reset buffer status to finish the reading */
221214
}

0 commit comments

Comments
 (0)