Skip to content

Commit 54bd49c

Browse files
committed
Zephyr: Added in our preliminary support for touch on the display
Zephyr: Added in our preliminary support for touch on the display In ArduinoCore-zephyr added a callback to register with the input class. When set it simply forwards the callbacks to this function. Then added some support. We still don't have things like gestures as I don't think the zephyr input class has support for it. Have a simple touch paint sketch (now supporting multiple touches, that appears to work Edit: The header file now combines the two different versions of the class into one. Added #ifdef __MBED__ in the mbed implementation file so that the code would not compile when we are doing zephyr builds
1 parent ec68545 commit 54bd49c

File tree

3 files changed

+148
-31
lines changed

3 files changed

+148
-31
lines changed

src/Arduino_GigaDisplayTouch.h

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030

3131
/* Includes ------------------------------------------------------------------*/
3232
#include "Wire.h"
33+
#include <Arduino.h>
34+
#ifdef __MBED__
3335
#include "mbed.h"
3436
#include "pinDefinitions.h"
35-
#include <Arduino.h>
37+
#endif
3638

3739
/* Exported defines ----------------------------------------------------------*/
3840
#define GT911_I2C_ADDR_BA_BB (0x5D | 0x80) // 0xBA/0xBB - 0x5D (7bit address)
@@ -70,28 +72,31 @@ struct GDTpoint_s {
7072
class Arduino_GigaDisplayTouch {
7173
public:
7274
/**
73-
* @brief Construct a new touch controller for Giga Display Shield.
74-
*
75-
* @param wire A reference to the Wire interface to be used for communication
76-
* with the touch controller.
77-
* @param intPin The interrupt pin number for the touch controller.
78-
* @param rstPin The reset pin number for the touch controller.
79-
* @param addr The device address for the touch controller.
80-
*/
81-
#if defined(ARDUINO_GIGA)
82-
Arduino_GigaDisplayTouch(TwoWire &wire = Wire1,
83-
uint8_t intPin = PinNameToIndex(PI_1),
84-
uint8_t rstPin = PinNameToIndex(PI_2),
85-
uint8_t addr = GT911_I2C_ADDR_BA_BB);
86-
#elif defined(ARDUINO_PORTENTA_H7_M7)
87-
Arduino_GigaDisplayTouch(TwoWire &wire = Wire,
88-
uint8_t intPin = PinNameToIndex(PD_4),
89-
uint8_t rstPin = PinNameToIndex(PD_5),
90-
uint8_t addr = GT911_I2C_ADDR_BA_BB);
91-
#else
92-
Arduino_GigaDisplayTouch(TwoWire &wire, uint8_t intPin, uint8_t rstPin,
93-
uint8_t addr);
94-
#endif
75+
* @brief Construct a new touch controller for Giga Display Shield.
76+
*
77+
* @param wire A reference to the Wire interface to be used for communication
78+
* with the touch controller.
79+
* @param intPin The interrupt pin number for the touch controller.
80+
* @param rstPin The reset pin number for the touch controller.
81+
* @param addr The device address for the touch controller.
82+
*/
83+
#if defined(__ZEPHYR__)
84+
Arduino_GigaDisplayTouch();
85+
#elif defined(ARDUINO_GIGA)
86+
Arduino_GigaDisplayTouch(TwoWire &wire = Wire1,
87+
uint8_t intPin = PinNameToIndex(PI_1),
88+
uint8_t rstPin = PinNameToIndex(PI_2),
89+
uint8_t addr = GT911_I2C_ADDR_BA_BB);
90+
#elif defined(ARDUINO_PORTENTA_H7_M7)
91+
Arduino_GigaDisplayTouch(TwoWire &wire = Wire,
92+
uint8_t intPin = PinNameToIndex(PD_4),
93+
uint8_t rstPin = PinNameToIndex(PD_5),
94+
uint8_t addr = GT911_I2C_ADDR_BA_BB);
95+
#else
96+
Arduino_GigaDisplayTouch(TwoWire &wire, uint8_t intPin, uint8_t rstPin,
97+
uint8_t addr);
98+
#endif
99+
95100
~Arduino_GigaDisplayTouch();
96101

97102
/**
@@ -107,12 +112,12 @@ class Arduino_GigaDisplayTouch {
107112
*/
108113
void end();
109114

110-
/**
111-
* @brief Check if a touch event is detected and get the touch points.
112-
* @param points The array containing the coordinates of the touch points.
113-
* @return uint8_t The number of detected touch points.
114-
*/
115-
uint8_t getTouchPoints(GDTpoint_t *points);
115+
/**
116+
* @brief Check if a touch event is detected and get the touch points.
117+
* @param points The array containing the coordinates of the touch points.
118+
* @return uint8_t The number of detected touch points.
119+
*/
120+
uint8_t getTouchPoints(GDTpoint_t *points);
116121

117122
/**
118123
* @brief Attach an interrupt handler function for touch detection callbacks.
@@ -121,11 +126,12 @@ class Arduino_GigaDisplayTouch {
121126
void onDetect(void (*handler)(uint8_t, GDTpoint_t *));
122127

123128
private:
129+
#if defined(__MBED__)
124130
TwoWire &_wire;
125131
uint8_t _intPin;
126-
mbed::InterruptIn _irqInt;
127132
uint8_t _rstPin;
128133
uint8_t _addr;
134+
mbed::InterruptIn _irqInt;
129135
GDTpoint_t _points[GT911_MAX_CONTACTS];
130136
void (*_gt911TouchHandler)(uint8_t, GDTpoint_t *);
131137

@@ -134,6 +140,8 @@ class Arduino_GigaDisplayTouch {
134140
uint8_t _gt911ReadOp(uint16_t reg, uint8_t *data, uint8_t len);
135141
void _gt911onIrq();
136142
uint8_t _gt911ReadInputCoord(uint8_t *pointsbuf, uint8_t &contacts);
143+
144+
#endif
137145
};
138146

139-
#endif /* __ARDUINO_GIGADISPLAYTOUCH_H */
147+
#endif /* __ARDUINO_GIGADISPLAYTOUCH_H */

src/Arduino_GigaDisplayTouchMbed.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
/* Includes -----------------------------------------------------------------*/
26+
#ifdef __MBED__
2627
#include "Arduino_GigaDisplayTouch.h"
2728

2829
#if __has_include("lvgl.h")
@@ -281,4 +282,5 @@ uint8_t Arduino_GigaDisplayTouch::_gt911ReadInputCoord(uint8_t *pointsbuf,
281282
return 0;
282283
}
283284

285+
#endif /* __MBED__ */
284286
/**** END OF FILE ****/
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2025 Arduino SA
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
#ifdef __ZEPHYR__
19+
20+
#include <zephyr/device.h>
21+
#include <zephyr/drivers/display.h>
22+
#include <zephyr/input/input.h>
23+
#include <zephyr/kernel.h>
24+
#include <zephyr/sys/util.h>
25+
#include "Arduino_GigaDisplayTouch.h"
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];
36+
37+
typedef void (*zephyr_input_callback_t)(struct input_event *evt, void *user_data);
38+
extern "C" void zephyr_input_register_callback(zephyr_input_callback_t cb);
39+
static void touch_event_callback(struct input_event *evt, void *user_data);
40+
41+
Arduino_GigaDisplayTouch::Arduino_GigaDisplayTouch() {
42+
43+
}
44+
45+
Arduino_GigaDisplayTouch::~Arduino_GigaDisplayTouch() {
46+
47+
}
48+
49+
bool Arduino_GigaDisplayTouch::begin() {
50+
k_sem_init(&zephyr_touch_event_sync, 0, 1);
51+
zephyr_input_register_callback(touch_event_callback);
52+
return true;
53+
}
54+
55+
void Arduino_GigaDisplayTouch::end() {
56+
57+
}
58+
59+
uint8_t Arduino_GigaDisplayTouch::getTouchPoints(GDTpoint_t *points) {
60+
// First wait to see if we get any events.
61+
if (k_sem_take(&zephyr_touch_event_sync, K_NO_WAIT) != 0) {
62+
return 0;
63+
}
64+
65+
uint8_t count_pressed = 0;
66+
for (uint8_t i = 0; i <= zephyr_touch_cb_slot_num; i++) {
67+
if (zephyr_touch_points[i].pressed) {
68+
points[count_pressed].x = zephyr_touch_points[i].x;
69+
points[count_pressed].y = zephyr_touch_points[i].y;
70+
count_pressed++;
71+
}
72+
}
73+
return count_pressed;
74+
}
75+
76+
void Arduino_GigaDisplayTouch::onDetect(void (*handler)(uint8_t,
77+
GDTpoint_t *)) {
78+
UNUSED(handler);
79+
}
80+
81+
static void touch_event_callback(struct input_event *evt, void *user_data) {
82+
static const struct device *const touch_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_touch));
83+
84+
if (evt->dev != touch_dev) {
85+
return;
86+
}
87+
88+
switch (evt->code) {
89+
case INPUT_ABS_MT_SLOT:
90+
zephyr_touch_cb_slot_num = evt->value;
91+
break;
92+
case INPUT_ABS_X:
93+
zephyr_touch_points[zephyr_touch_cb_slot_num].x = evt->value;
94+
break;
95+
case INPUT_ABS_Y:
96+
zephyr_touch_points[zephyr_touch_cb_slot_num].y = evt->value;
97+
break;
98+
case INPUT_BTN_TOUCH:
99+
zephyr_touch_points[zephyr_touch_cb_slot_num].pressed = evt->value;
100+
break;
101+
}
102+
103+
if (evt->sync) {
104+
k_sem_give(&zephyr_touch_event_sync);
105+
}
106+
}
107+
#endif

0 commit comments

Comments
 (0)