39
39
#include " tft.h"
40
40
41
41
bool Touch::enabled = true ;
42
- int16_t Touch::x, Touch::y ;
42
+ xy_int_t Touch::point ;
43
43
touch_control_t Touch::controls[];
44
44
touch_control_t *Touch::current_control;
45
45
uint16_t Touch::controls_count;
@@ -67,26 +67,22 @@ void Touch::add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t
67
67
if (controls_count == MAX_CONTROLS) return ;
68
68
69
69
controls[controls_count].type = type;
70
- controls[controls_count].x = x;
71
- controls[controls_count].y = y;
72
- controls[controls_count].width = width;
73
- controls[controls_count].height = height;
70
+ controls[controls_count].point .set (x, y);
71
+ controls[controls_count].size .set (width, height);
74
72
controls[controls_count].data = data;
75
73
controls_count++;
76
74
}
77
75
78
76
void Touch::idle () {
79
- uint16_t i;
80
- int16_t _x, _y;
81
-
82
77
if (!enabled) return ;
83
78
84
79
// Return if Touch::idle is called within the same millisecond
85
80
const millis_t now = millis ();
86
81
if (now <= next_touch_ms) return ;
87
82
next_touch_ms = now;
88
83
89
- if (get_point (&_x, &_y)) {
84
+ xy_int_t got_point;
85
+ if (get_point (got_point)) {
90
86
#if HAS_RESUME_CONTINUE
91
87
// UI is waiting for a click anywhere?
92
88
if (wait_for_user) {
@@ -110,19 +106,21 @@ void Touch::idle() {
110
106
if (time_to_hold == 0 ) time_to_hold = now + MINIMUM_HOLD_TIME;
111
107
if (PENDING (now, time_to_hold)) return ;
112
108
113
- if (x != 0 && y != 0 ) {
109
+ if (bool (point) ) {
114
110
if (current_control) {
115
- if (WITHIN (x, current_control->x - FREE_MOVE_RANGE, current_control->x + current_control->width + FREE_MOVE_RANGE) && WITHIN (y, current_control->y - FREE_MOVE_RANGE, current_control->y + current_control->height + FREE_MOVE_RANGE)) {
116
- LIMIT (x, current_control->x , current_control->x + current_control->width );
117
- LIMIT (y, current_control->y , current_control->y + current_control->height );
111
+ if ( WITHIN (point.x , current_control->x - FREE_MOVE_RANGE, current_control->x + current_control->width + FREE_MOVE_RANGE)
112
+ && WITHIN (point.y , current_control->y - FREE_MOVE_RANGE, current_control->y + current_control->height + FREE_MOVE_RANGE)
113
+ ) {
114
+ LIMIT (point.x , current_control->x , current_control->x + current_control->width );
115
+ LIMIT (point.y , current_control->y , current_control->y + current_control->height );
118
116
touch (current_control);
119
117
}
120
118
else
121
119
current_control = nullptr ;
122
120
}
123
121
else {
124
- for (i = 0 ; i < controls_count; i++) {
125
- if ((WITHIN (x, controls[i].x , controls[i].x + controls[i].width ) && WITHIN (y, controls[i].y , controls[i].y + controls[i].height )) || (TERN (TOUCH_SCREEN_CALIBRATION, controls[i].type == CALIBRATE, false ))) {
122
+ for (uint16_t i = 0 ; i < controls_count; i++) {
123
+ if ((WITHIN (point. x , controls[i].x , controls[i].x + controls[i].width ) && WITHIN (point. y , controls[i].y , controls[i].y + controls[i].height )) || (TERN (TOUCH_SCREEN_CALIBRATION, controls[i].type == CALIBRATE, false ))) {
126
124
touch_control_type = controls[i].type ;
127
125
touch (&controls[i]);
128
126
break ;
@@ -133,11 +131,10 @@ void Touch::idle() {
133
131
if (!current_control)
134
132
touch_time = now;
135
133
}
136
- x = _x;
137
- y = _y;
134
+ point = got_point;
138
135
}
139
136
else {
140
- x = y = 0 ;
137
+ point. reset () ;
141
138
current_control = nullptr ;
142
139
touch_time = 0 ;
143
140
touch_control_type = NONE;
@@ -150,7 +147,7 @@ void Touch::touch(touch_control_t *control) {
150
147
switch (control->type ) {
151
148
#if ENABLED(TOUCH_SCREEN_CALIBRATION)
152
149
case CALIBRATE:
153
- if (touch_calibration.handleTouch (x, y )) ui.refresh ();
150
+ if (touch_calibration.handleTouch (point )) ui.refresh ();
154
151
break ;
155
152
#endif
156
153
@@ -263,28 +260,26 @@ void Touch::hold(touch_control_t *control, millis_t delay) {
263
260
ui.refresh ();
264
261
}
265
262
266
- bool Touch::get_point (int16_t *x, int16_t *y) {
267
- #if ENABLED(TFT_TOUCH_DEVICE_XPT2046)
268
- #if ENABLED(TOUCH_SCREEN_CALIBRATION)
269
- const bool is_touched = (touch_calibration.calibration .orientation == TOUCH_PORTRAIT ? io.getRawPoint (y, x) : io.getRawPoint (x, y));
270
- if (is_touched && touch_calibration.calibration .orientation != TOUCH_ORIENTATION_NONE) {
271
- *x = int16_t ((int32_t (*x) * touch_calibration.calibration .x ) >> 16 ) + touch_calibration.calibration .offset_x ;
272
- *y = int16_t ((int32_t (*y) * touch_calibration.calibration .y ) >> 16 ) + touch_calibration.calibration .offset_y ;
273
- }
274
- #else
275
- const bool is_touched = (TOUCH_ORIENTATION == TOUCH_PORTRAIT ? io.getRawPoint (y, x) : io.getRawPoint (x, y));
276
- *x = uint16_t ((uint32_t (*x) * TOUCH_CALIBRATION_X) >> 16 ) + TOUCH_OFFSET_X;
277
- *y = uint16_t ((uint32_t (*y) * TOUCH_CALIBRATION_Y) >> 16 ) + TOUCH_OFFSET_Y;
263
+ bool Touch::get_point (xy_int_t &point) {
264
+ bool is_touched = false ;
265
+ #if ANY(TFT_TOUCH_DEVICE_XPT2046, TFT_TOUCH_DEVICE_GT911)
266
+ is_touched = (TOUCH_ORIENTATION_NONE != _TOUCH_ORIENTATION)
267
+ && (TOUCH_PORTRAIT == _TOUCH_ORIENTATION
268
+ ? io.getRawPoint (&point.y , &point.x )
269
+ : io.getRawPoint (&point.x , &point.y ));
270
+ #if ENABLED(TFT_TOUCH_DEVICE_XPT2046)
271
+ point.x = uint16_t ((uint32_t (point.x ) * _TOUCH_CALIBRATION_X) >> 16 ) + _TOUCH_OFFSET_X;
272
+ point.y = uint16_t ((uint32_t (point.y ) * _TOUCH_CALIBRATION_Y) >> 16 ) + _TOUCH_OFFSET_Y;
278
273
#endif
279
- #elif ENABLED(TFT_TOUCH_DEVICE_GT911)
280
- const bool is_touched = (TOUCH_ORIENTATION == TOUCH_PORTRAIT ? io.getPoint (y, x) : io.getPoint (x, y));
281
274
#endif
275
+
282
276
#if HAS_TOUCH_SLEEP
283
277
if (is_touched)
284
278
wakeUp ();
285
279
else if (!isSleeping () && ELAPSED (millis (), next_sleep_ms) && ui.on_status_screen ())
286
280
sleepTimeout ();
287
281
#endif
282
+
288
283
return is_touched;
289
284
}
290
285
0 commit comments