Skip to content

Commit a43957d

Browse files
refactoring of attach and detect function
1 parent fe5e8d2 commit a43957d

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

examples/Touch_IRQ/Touch_IRQ.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "Arduino_GigaDisplayTouch.h"
99

10-
Arduino_GigaDisplayTouch touch;
10+
Arduino_GigaDisplayTouch touchDetector;
1111

1212
void gigaTouchHandler(uint8_t contacts, GDTpoint_t* points) {
1313
Serial.print("Contacts: ");
@@ -25,14 +25,14 @@ void setup() {
2525
Serial.begin(115200);
2626
while(!Serial) {}
2727

28-
if (touch.begin()) {
28+
if (touchDetector.begin()) {
2929
Serial.println("Touch controller init - OK");
3030
} else {
3131
Serial.println("Touch controller init - FAILED");
3232
while(1) ;
3333
}
3434

35-
touch.attach(gigaTouchHandler);
35+
touchDetector.onDetect(gigaTouchHandler);
3636
}
3737

3838
void loop() { }

examples/Touch_Polling/Touch_Polling.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
#include "Arduino_GigaDisplayTouch.h"
99

10-
Arduino_GigaDisplayTouch touch;
10+
Arduino_GigaDisplayTouch touchDetector;
1111

1212
void setup() {
1313
Serial.begin(115200);
1414
while(!Serial) {}
1515

16-
if (touch.begin()) {
16+
if (touchDetector.begin()) {
1717
Serial.print("Touch controller init - OK");
1818
} else {
1919
Serial.print("Touch controller init - FAILED");
@@ -25,7 +25,9 @@ void loop() {
2525
uint8_t contacts;
2626
GDTpoint_t points[5];
2727

28-
if(touch.detect(contacts, points)) {
28+
contacts = touchDetector.getTouchPoints(points);
29+
30+
if (contacts > 0) {
2931
Serial.print("Contacts: ");
3032
Serial.println(contacts);
3133

src/Arduino_GigaDisplayTouch.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ void _lvglTouchCb(lv_indev_drv_t * indev, lv_indev_data_t * data) {
101101
uint8_t contacts;
102102
GDTpoint_t points[5];
103103

104-
if(gThis->detect(contacts, points)) {
104+
contacts = gThis->getTouchPoints(points);
105+
106+
if(contacts > 0) {
105107
data->state = LV_INDEV_STATE_PR;
106108
data->point.x = points[0].x;
107109
data->point.y = points[0].y;
@@ -116,14 +118,16 @@ void _lvglTouchCb(lv_indev_drv_t * indev, lv_indev_data_t * data) {
116118
void Arduino_GigaDisplayTouch::end()
117119
{ }
118120

119-
bool Arduino_GigaDisplayTouch::detect(uint8_t& contacts, GDTpoint_t* points) {
121+
uint8_t Arduino_GigaDisplayTouch::getTouchPoints(GDTpoint_t* points) {
120122
uint8_t rawpoints[GT911_MAX_CONTACTS * GT911_CONTACT_SIZE];
123+
uint8_t contacts;
121124
uint8_t error;
122125

123-
error = _gt911ReadInputCoord(rawpoints, contacts);
126+
contacts = 0;
127+
error = _gt911ReadInputCoord(rawpoints, contacts);
124128

125129
if (error) {
126-
return false;
130+
return 0;
127131
}
128132

129133
for (uint8_t i = 0; i < contacts; i++) {
@@ -135,11 +139,10 @@ bool Arduino_GigaDisplayTouch::detect(uint8_t& contacts, GDTpoint_t* points) {
135139

136140
_gt911WriteOp(GT911_REG_GESTURE_START_POINT, 0); /* Reset buffer status to finish the reading */
137141

138-
if (contacts > 0) return true;
139-
else return false;
142+
return contacts;
140143
}
141144

142-
void Arduino_GigaDisplayTouch::attach(void (*handler)(uint8_t, GDTpoint_t*)) {
145+
void Arduino_GigaDisplayTouch::onDetect(void (*handler)(uint8_t, GDTpoint_t*)) {
143146
_gt911TouchHandler = handler;
144147
t.start(callback(&queue, &events::EventQueue::dispatch_forever));
145148
_irqInt.rise(queue.event(mbed::callback(this, &Arduino_GigaDisplayTouch::_gt911onIrq)));

src/Arduino_GigaDisplayTouch.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ class Arduino_GigaDisplayTouch {
9898
/**
9999
* @brief Initialize the touch controller.
100100
*
101-
* @return true If the touch controller is successfully initialized
102-
* @return false Otherwise
101+
* @return true If the touch controller is successfully initialized, false Otherwise
103102
*/
104103
bool begin();
105104

@@ -110,18 +109,16 @@ class Arduino_GigaDisplayTouch {
110109

111110
/**
112111
* @brief Check if a touch event is detected and get the touch points.
113-
* @param contacts The number of detected touch points.
114112
* @param points The array containing the coordinates of the touch points.
115-
* @return true If a touch event is detected
116-
* @return false Otherwise
113+
* @return uint8_t The number of detected touch points.
117114
*/
118-
bool detect(uint8_t& contacts, GDTpoint_t* points);
115+
uint8_t getTouchPoints(GDTpoint_t* points);
119116

120117
/**
121118
* @brief Attach an interrupt handler function for touch detection callbacks.
122119
* @param handler The pointer to the user-defined handler function.
123120
*/
124-
void attach(void (*handler)(uint8_t, GDTpoint_t*));
121+
void onDetect(void (*handler)(uint8_t, GDTpoint_t*));
125122
private:
126123
TwoWire& _wire;
127124
uint8_t _intPin;

0 commit comments

Comments
 (0)