Skip to content

Commit 1e58d44

Browse files
committed
Preliminary Giga Touch support on Zephyr
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. Split the implemention into two .cpp files. One for MBED, when I mainly only added #if/endif that it only compiles on __MBED. I did however remove trailing blanks from lines. Also updated header file order of variables in the mbed case as to compile the examples without warnings. And the other cpp file has the zephyr implementation, that was also run through clang-format
1 parent 24490f4 commit 1e58d44

File tree

3 files changed

+261
-86
lines changed

3 files changed

+261
-86
lines changed

src/Arduino_GigaDisplayTouch.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* @brief Source file for the Arduino Giga Display Touch library.
2323
*/
2424

25+
26+
#ifdef __MBED__
27+
2528
/* Includes -----------------------------------------------------------------*/
2629
#include "Arduino_GigaDisplayTouch.h"
2730

@@ -35,7 +38,7 @@
3538

3639
/* Private variables ---------------------------------------------------------*/
3740
rtos::Thread t;
38-
events::EventQueue queue(32 * EVENTS_EVENT_SIZE);
41+
events::EventQueue queue(32 * EVENTS_EVENT_SIZE);
3942
Arduino_GigaDisplayTouch * gThis;
4043

4144
/* Private function prototypes -----------------------------------------------*/
@@ -52,7 +55,7 @@ Arduino_GigaDisplayTouch::Arduino_GigaDisplayTouch(TwoWire& wire, uint8_t intPin
5255
: _wire{wire}, _intPin{intPin}, _rstPin{rstPin}, _addr{addr}, _irqInt{digitalPinToPinName(intPin)}
5356
{ }
5457

55-
Arduino_GigaDisplayTouch::~Arduino_GigaDisplayTouch()
58+
Arduino_GigaDisplayTouch::~Arduino_GigaDisplayTouch()
5659
{ }
5760

5861
bool Arduino_GigaDisplayTouch::begin() {
@@ -143,7 +146,7 @@ void _lvglTouchCb(lv_indev_drv_t * indev, lv_indev_data_t * data) {
143146
#endif
144147
#endif
145148

146-
void Arduino_GigaDisplayTouch::end()
149+
void Arduino_GigaDisplayTouch::end()
147150
{ }
148151

149152
uint8_t Arduino_GigaDisplayTouch::getTouchPoints(GDTpoint_t* points) {
@@ -159,12 +162,12 @@ uint8_t Arduino_GigaDisplayTouch::getTouchPoints(GDTpoint_t* points) {
159162
}
160163

161164
for (uint8_t i = 0; i < contacts; i++) {
162-
points[i].trackId = rawpoints[1 + 8*i];
165+
points[i].trackId = rawpoints[1 + 8*i];
163166
points[i].x = ((uint16_t)rawpoints[3 + 8*i] << 8) + rawpoints[2 + 8*i];
164167
points[i].y = ((uint16_t)rawpoints[5 + 8*i] << 8) + rawpoints[4 + 8*i];
165168
points[i].area = ((uint16_t)rawpoints[7 + 8*i] << 8) + rawpoints[6 + 8*i];
166169
}
167-
170+
168171
_gt911WriteOp(GT911_REG_GESTURE_START_POINT, 0); /* Reset buffer status to finish the reading */
169172

170173
return contacts;
@@ -192,7 +195,7 @@ uint8_t Arduino_GigaDisplayTouch::_gt911WriteBytesOp(uint16_t reg, uint8_t * dat
192195

193196
/* Data [0..n] */
194197
for (uint8_t i = 0; i < len; i++) {
195-
_wire.write(data[i]);
198+
_wire.write(data[i]);
196199
}
197200

198201
status = _wire.endTransmission();
@@ -233,33 +236,35 @@ void Arduino_GigaDisplayTouch::_gt911onIrq() {
233236
}
234237

235238
for (uint8_t i = 0; i < contacts; i++) {
236-
_points[i].trackId = rawpoints[1 + 8*i];
239+
_points[i].trackId = rawpoints[1 + 8*i];
237240
_points[i].x = ((uint16_t)rawpoints[3 + 8*i] << 8) + rawpoints[2 + 8*i];
238241
_points[i].y = ((uint16_t)rawpoints[5 + 8*i] << 8) + rawpoints[4 + 8*i];
239242
_points[i].area = ((uint16_t)rawpoints[7 + 8*i] << 8) + rawpoints[6 + 8*i];
240243
}
241244

242245
if (contacts > 0 && _gt911TouchHandler != nullptr) _gt911TouchHandler(contacts, _points);
243-
246+
244247
_gt911WriteOp(GT911_REG_GESTURE_START_POINT, 0); /* Reset buffer status to finish the reading */
245248
}
246249

247250
uint8_t Arduino_GigaDisplayTouch::_gt911ReadInputCoord(uint8_t * pointsbuf, uint8_t& contacts) {
248251
uint8_t error;
249-
252+
250253
contacts = 0;
251254
error = _gt911ReadOp(GT911_REG_GESTURE_START_POINT, pointsbuf, GT911_CONTACT_SIZE * GT911_MAX_CONTACTS);
252255

253256
if (error) {
254257
return 1; /* I2C comm error */
255258
}
256-
257-
if (!(pointsbuf[0] & 0x80)) {
259+
260+
if (!(pointsbuf[0] & 0x80)) {
258261
return 2; /* Data buffer not ready */
259262
}
260263

261264
contacts = pointsbuf[0] & 0xF;
262265
return 0;
263266
}
264267

268+
#endif /* MBED */
269+
265270
/**** END OF FILE ****/

src/Arduino_GigaDisplayTouch.h

Lines changed: 113 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,27 @@
2121
* @author Leonardo Cavagnis
2222
* @brief Header file for the Arduino Giga Display Touch library.
2323
*
24-
* This library allows to capture up to 5 concurrent touch points on Arduino Giga Display Shield.
25-
* Supported controller: Goodix GT911
24+
* This library allows to capture up to 5 concurrent touch points on Arduino
25+
* Giga Display Shield. Supported controller: Goodix GT911
2626
*/
2727

2828
#ifndef __ARDUINO_GIGADISPLAYTOUCH_H
2929
#define __ARDUINO_GIGADISPLAYTOUCH_H
3030

3131
/* Includes ------------------------------------------------------------------*/
32-
#include <Arduino.h>
3332
#include "Wire.h"
33+
#include <Arduino.h>
34+
#ifdef __MBED__
3435
#include "mbed.h"
3536
#include "pinDefinitions.h"
37+
#endif
3638

3739
/* Exported defines ----------------------------------------------------------*/
38-
#define GT911_I2C_ADDR_BA_BB (0x5D | 0x80) // 0xBA/0xBB - 0x5D (7bit address)
39-
#define GT911_I2C_ADDR_28_29 (0x14 | 0x80) // 0x28/0x29 - 0x14 (7bit address)
40+
#define GT911_I2C_ADDR_BA_BB (0x5D | 0x80) // 0xBA/0xBB - 0x5D (7bit address)
41+
#define GT911_I2C_ADDR_28_29 (0x14 | 0x80) // 0x28/0x29 - 0x14 (7bit address)
4042

41-
#define GT911_CONTACT_SIZE 8
42-
#define GT911_MAX_CONTACTS 5
43+
#define GT911_CONTACT_SIZE 8
44+
#define GT911_MAX_CONTACTS 5
4345

4446
/* Exported types ------------------------------------------------------------*/
4547
typedef struct GDTpoint_s GDTpoint_t;
@@ -52,87 +54,123 @@ typedef struct GDTpoint_s GDTpoint_t;
5254
* @brief Struct representing a touch point.
5355
*/
5456
struct GDTpoint_s {
55-
// 0x814F-0x8156, ... 0x8176 (5 points)
57+
// 0x814F-0x8156, ... 0x8176 (5 points)
5658
uint8_t trackId;
5759
uint16_t x;
5860
uint16_t y;
5961
uint16_t area;
6062
uint8_t reserved;
6163
};
6264

63-
/* Class ----------------------------------------------------------------------*/
65+
/* Class
66+
* ----------------------------------------------------------------------*/
6467

6568
/**
6669
* @class Arduino_GigaDisplayTouch
6770
* @brief Class for Giga Display Touch controller driver.
6871
*/
6972
class Arduino_GigaDisplayTouch {
70-
public:
71-
/**
72-
* @brief Construct a new touch controller for Giga Display Shield.
73-
*
74-
* @param wire A reference to the Wire interface to be used for communication with the touch controller.
75-
* @param intPin The interrupt pin number for the touch controller.
76-
* @param rstPin The reset pin number for the touch controller.
77-
* @param addr The device address for the touch controller.
78-
*/
79-
#if defined(ARDUINO_GIGA)
80-
Arduino_GigaDisplayTouch(TwoWire& wire = Wire1,
81-
uint8_t intPin = PinNameToIndex(PI_1),
82-
uint8_t rstPin = PinNameToIndex(PI_2),
83-
uint8_t addr = GT911_I2C_ADDR_BA_BB);
84-
#elif defined(ARDUINO_PORTENTA_H7_M7)
85-
Arduino_GigaDisplayTouch(TwoWire& wire = Wire,
86-
uint8_t intPin = PinNameToIndex(PD_4),
87-
uint8_t rstPin = PinNameToIndex(PD_5),
88-
uint8_t addr = GT911_I2C_ADDR_BA_BB);
89-
#else
90-
Arduino_GigaDisplayTouch(TwoWire& wire,
91-
uint8_t intPin,
92-
uint8_t rstPin,
93-
uint8_t addr);
94-
#endif
95-
~Arduino_GigaDisplayTouch();
96-
97-
98-
/**
99-
* @brief Initialize the touch controller.
100-
*
101-
* @return true If the touch controller is successfully initialized, false Otherwise
102-
*/
103-
bool begin();
104-
105-
/**
106-
* @brief De-initialize the touch controller.
107-
*/
108-
void end();
109-
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);
116-
117-
/**
118-
* @brief Attach an interrupt handler function for touch detection callbacks.
119-
* @param handler The pointer to the user-defined handler function.
120-
*/
121-
void onDetect(void (*handler)(uint8_t, GDTpoint_t*));
122-
private:
123-
TwoWire& _wire;
124-
uint8_t _intPin;
125-
mbed::InterruptIn _irqInt;
126-
uint8_t _rstPin;
127-
uint8_t _addr;
128-
GDTpoint_t _points[GT911_MAX_CONTACTS];
129-
void (*_gt911TouchHandler)(uint8_t, GDTpoint_t*);
130-
131-
uint8_t _gt911WriteOp(uint16_t reg, uint8_t data);
132-
uint8_t _gt911WriteBytesOp(uint16_t reg, uint8_t * data, uint8_t len);
133-
uint8_t _gt911ReadOp(uint16_t reg, uint8_t * data, uint8_t len);
134-
void _gt911onIrq();
135-
uint8_t _gt911ReadInputCoord(uint8_t * pointsbuf, uint8_t& contacts);
73+
public:
74+
#ifdef __MBED__
75+
/**
76+
* @brief Construct a new touch controller for Giga Display Shield.
77+
*
78+
* @param wire A reference to the Wire interface to be used for communication
79+
* with the touch controller.
80+
* @param intPin The interrupt pin number for the touch controller.
81+
* @param rstPin The reset pin number for the touch controller.
82+
* @param addr The device address for the touch controller.
83+
*/
84+
#if defined(ARDUINO_GIGA)
85+
Arduino_GigaDisplayTouch(TwoWire &wire = Wire1,
86+
uint8_t intPin = PinNameToIndex(PI_1),
87+
uint8_t rstPin = PinNameToIndex(PI_2),
88+
uint8_t addr = GT911_I2C_ADDR_BA_BB);
89+
#elif defined(ARDUINO_PORTENTA_H7_M7)
90+
Arduino_GigaDisplayTouch(TwoWire &wire = Wire,
91+
uint8_t intPin = PinNameToIndex(PD_4),
92+
uint8_t rstPin = PinNameToIndex(PD_5),
93+
uint8_t addr = GT911_I2C_ADDR_BA_BB);
94+
#else
95+
Arduino_GigaDisplayTouch(TwoWire &wire, uint8_t intPin, uint8_t rstPin,
96+
uint8_t addr);
97+
#endif
98+
#elif defined(__ZEPHYR__)
99+
Arduino_GigaDisplayTouch();
100+
#endif
101+
102+
~Arduino_GigaDisplayTouch();
103+
104+
/**
105+
* @brief Initialize the touch controller.
106+
*
107+
* @return true If the touch controller is successfully initialized, false
108+
* Otherwise
109+
*/
110+
bool begin();
111+
112+
/**
113+
* @brief De-initialize the touch controller.
114+
*/
115+
void end();
116+
117+
#if defined(__MBED__)
118+
/**
119+
* @brief Check if a touch event is detected and get the touch points.
120+
* @param points The array containing the coordinates of the touch points.
121+
* @return uint8_t The number of detected touch points.
122+
*/
123+
uint8_t getTouchPoints(GDTpoint_t *points);
124+
#elif defined(__ZEPHYR__)
125+
/**
126+
* @brief Check if a touch event is detected and get the touch points.
127+
* @param points The array containing the coordinates of the touch points.
128+
* @param timeout in microseconds.
129+
* @return uint8_t The number of detected touch points.
130+
*/
131+
uint8_t getTouchPoints(GDTpoint_t *points, uint32_t timeout = 0);
132+
133+
#endif
134+
/**
135+
* @brief Attach an interrupt handler function for touch detection callbacks.
136+
* @param handler The pointer to the user-defined handler function.
137+
*/
138+
void onDetect(void (*handler)(uint8_t, GDTpoint_t *));
139+
140+
private:
141+
#if defined(__MBED__)
142+
TwoWire &_wire;
143+
uint8_t _intPin;
144+
uint8_t _rstPin;
145+
uint8_t _addr;
146+
mbed::InterruptIn _irqInt;
147+
GDTpoint_t _points[GT911_MAX_CONTACTS];
148+
void (*_gt911TouchHandler)(uint8_t, GDTpoint_t *);
149+
150+
uint8_t _gt911WriteOp(uint16_t reg, uint8_t data);
151+
uint8_t _gt911WriteBytesOp(uint16_t reg, uint8_t *data, uint8_t len);
152+
uint8_t _gt911ReadOp(uint16_t reg, uint8_t *data, uint8_t len);
153+
void _gt911onIrq();
154+
uint8_t _gt911ReadInputCoord(uint8_t *pointsbuf, uint8_t &contacts);
155+
#elif defined(__ZEPHYR__)
156+
/*
157+
* @brief Support for callbacks from zephyr
158+
*/
159+
typedef struct {
160+
size_t x;
161+
size_t y;
162+
bool pressed;
163+
} touch_point_t;
164+
165+
static touch_point_t
166+
_zephyr_touch_points[CONFIG_INPUT_GT911_MAX_TOUCH_POINTS];
167+
168+
static uint8_t _zephyr_touch_cb_slot_num;
169+
static struct k_sem _zephyr_touch_event_sync;
170+
171+
static void _touch_event_callback(struct input_event *evt, void *user_data);
172+
173+
#endif
136174
};
137175

138176
#endif /* __ARDUINO_GIGADISPLAYTOUCH_H */

0 commit comments

Comments
 (0)