Skip to content

Commit 3b6ad34

Browse files
add LVGL touch support
1 parent 0b16eaa commit 3b6ad34

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

examples/LVGL_Touch/LVGL_Touch.ino

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
LVGL_Touch
3+
4+
created 05 May 2023
5+
by Leonardo Cavagnis
6+
*/
7+
8+
#include "Arduino_H7_Video.h"
9+
#include "Arduino_GigaDisplayTouch.h"
10+
11+
#include "lvgl.h"
12+
#include "../examples/lv_examples.h"
13+
14+
Arduino_H7_Video Display(800, 480, GigaDisplayShield);
15+
Arduino_GigaDisplayTouch Touch;
16+
17+
void setup() {
18+
Display.begin();
19+
Touch.begin();
20+
21+
/* Example: Button click event - https://docs.lvgl.io/master/examples.html#events */
22+
lv_example_event_1();
23+
}
24+
25+
void loop() {
26+
/* Feed LVGL engine */
27+
lv_timer_handler();
28+
}

examples/Touch_IRQ/Touch_IRQ.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ void gigaTouchHandler(uint8_t contacts, GDTpoint_t* points) {
1313
Serial.print("Contacts: ");
1414
Serial.println(contacts);
1515

16-
for (uint8_t i = 0; i < contacts; i++) {
17-
Serial.print(points[i].x);
16+
if (contacts > 0) {
17+
/* First touch point */
18+
Serial.print(points[0].x);
1819
Serial.print(" ");
19-
Serial.println(points[i].y);
20+
Serial.println(points[0].y);
2021
}
2122
}
2223

src/Arduino_GigaDisplayTouch.cpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
/* Includes -----------------------------------------------------------------*/
2626
#include "Arduino_GigaDisplayTouch.h"
2727

28+
#if __has_include ("lvgl.h")
29+
#include "lvgl.h"
30+
#endif
31+
2832
/* Private defines -----------------------------------------------------------*/
2933
#define GT911_REG_GESTURE_START_POINT 0x814E
3034
#define GT911_REG_CONFIG_VERSION 0x8047
@@ -33,7 +37,16 @@
3337
rtos::Thread t;
3438
events::EventQueue queue(32 * EVENTS_EVENT_SIZE);
3539

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
45+
3646
/* Private function prototypes -----------------------------------------------*/
47+
#if __has_include ("lvgl.h")
48+
void _lvglTouchCb(lv_indev_drv_t * indev, lv_indev_data_t * data);
49+
#endif
3750

3851
/* Functions -----------------------------------------------------------------*/
3952
Arduino_GigaDisplayTouch::Arduino_GigaDisplayTouch(TwoWire& wire, uint8_t intPin, uint8_t rstPin, uint8_t addr)
@@ -70,14 +83,37 @@ bool Arduino_GigaDisplayTouch::begin() {
7083
pinMode(_intPin, INPUT);
7184

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

7489
/* GT911 test communication */
7590
uint8_t testByte;
7691
uint8_t error = _gt911ReadOp(GT911_REG_CONFIG_VERSION, &testByte, 1);
7792

93+
#if __has_include ("lvgl.h")
94+
static lv_indev_drv_t indev_drv; /* Descriptor of a input device driver */
95+
lv_indev_drv_init(&indev_drv); /* Basic initialization */
96+
indev_drv.type = LV_INDEV_TYPE_POINTER; /* Touch pad is a pointer-like device */
97+
indev_drv.read_cb = _lvglTouchCb; /* Set your driver function */
98+
lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv); /* Register the driver in LVGL and save the created input device object */
99+
#endif
100+
78101
return (error == 0);
79102
}
80103

104+
#if __has_include ("lvgl.h")
105+
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;
114+
}
115+
#endif
116+
81117
void Arduino_GigaDisplayTouch::end()
82118
{ }
83119

@@ -105,9 +141,6 @@ bool Arduino_GigaDisplayTouch::detect(uint8_t& contacts, GDTpoint_t* points) {
105141
}
106142

107143
void Arduino_GigaDisplayTouch::attach(void (*handler)(uint8_t, GDTpoint_t*)) {
108-
t.start(callback(&queue, &events::EventQueue::dispatch_forever));
109-
_irqInt.rise(queue.event(mbed::callback(this, &Arduino_GigaDisplayTouch::_gt911onIrq)));
110-
111144
_gt911TouchHandler = handler;
112145
}
113146

@@ -175,6 +208,14 @@ void Arduino_GigaDisplayTouch::_gt911onIrq() {
175208
}
176209

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

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

0 commit comments

Comments
 (0)