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].pos .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,24 @@ 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->pos .x - FREE_MOVE_RANGE, current_control->pos .x + current_control->size .x + FREE_MOVE_RANGE)
112
+ && WITHIN (point.y , current_control->pos .y - FREE_MOVE_RANGE, current_control->pos .y + current_control->size .y + FREE_MOVE_RANGE)
113
+ ) {
114
+ LIMIT (point.x , current_control->pos .x , current_control->pos .x + current_control->size .x );
115
+ LIMIT (point.y , current_control->pos .y , current_control->pos .y + current_control->size .y );
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 (TERN0 (TOUCH_SCREEN_CALIBRATION, controls[i].type == CALIBRATE)
124
+ || ( WITHIN (point.x , controls[i].pos .x , controls[i].pos .x + controls[i].size .x )
125
+ && WITHIN (point.y , controls[i].pos .y , controls[i].pos .y + controls[i].size .y ))
126
+ ) {
126
127
touch_control_type = controls[i].type ;
127
128
touch (&controls[i]);
128
129
break ;
@@ -133,11 +134,10 @@ void Touch::idle() {
133
134
if (!current_control)
134
135
touch_time = now;
135
136
}
136
- x = _x;
137
- y = _y;
137
+ point = got_point;
138
138
}
139
139
else {
140
- x = y = 0 ;
140
+ point. reset () ;
141
141
current_control = nullptr ;
142
142
touch_time = 0 ;
143
143
touch_control_type = NONE;
@@ -150,7 +150,7 @@ void Touch::touch(touch_control_t *control) {
150
150
switch (control->type ) {
151
151
#if ENABLED(TOUCH_SCREEN_CALIBRATION)
152
152
case CALIBRATE:
153
- if (touch_calibration.handleTouch (x, y )) ui.refresh ();
153
+ if (touch_calibration.handleTouch (point )) ui.refresh ();
154
154
break ;
155
155
#endif
156
156
@@ -177,7 +177,7 @@ void Touch::touch(touch_control_t *control) {
177
177
ui.encoderPosition = ui.encoderPosition + LCD_HEIGHT < (uint32_t )screen_items ? ui.encoderPosition + LCD_HEIGHT : screen_items;
178
178
ui.refresh ();
179
179
break ;
180
- case SLIDER: hold (control); ui.encoderPosition = (x - control->x ) * control->data / control->width ; break ;
180
+ case SLIDER: hold (control); ui.encoderPosition = (point. x - control->pos . x ) * control->data / control->size . x ; break ;
181
181
case INCREASE: hold (control, repeat_delay - 5 ); TERN (AUTO_BED_LEVELING_UBL, ui.external_control ? bedlevel.encoder_diff ++ : ui.encoderPosition ++, ui.encoderPosition ++); break ;
182
182
case DECREASE: hold (control, repeat_delay - 5 ); TERN (AUTO_BED_LEVELING_UBL, ui.external_control ? bedlevel.encoder_diff -- : ui.encoderPosition --, ui.encoderPosition --); break ;
183
183
case HEATER:
@@ -263,28 +263,26 @@ void Touch::hold(touch_control_t *control, millis_t delay) {
263
263
ui.refresh ();
264
264
}
265
265
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;
266
+ bool Touch::get_point (xy_int_t &point) {
267
+ bool is_touched = false ;
268
+ #if ANY(TFT_TOUCH_DEVICE_XPT2046, TFT_TOUCH_DEVICE_GT911)
269
+ is_touched = (TOUCH_ORIENTATION_NONE != _TOUCH_ORIENTATION)
270
+ && (TOUCH_PORTRAIT == _TOUCH_ORIENTATION
271
+ ? io.getRawPoint (&point.y , &point.x )
272
+ : io.getRawPoint (&point.x , &point.y ));
273
+ #if ENABLED(TFT_TOUCH_DEVICE_XPT2046)
274
+ point.x = uint16_t ((uint32_t (point.x ) * _TOUCH_CALIBRATION_X) >> 16 ) + _TOUCH_OFFSET_X;
275
+ point.y = uint16_t ((uint32_t (point.y ) * _TOUCH_CALIBRATION_Y) >> 16 ) + _TOUCH_OFFSET_Y;
278
276
#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
277
#endif
278
+
282
279
#if HAS_TOUCH_SLEEP
283
280
if (is_touched)
284
281
wakeUp ();
285
282
else if (!isSleeping () && ELAPSED (millis (), next_sleep_ms) && ui.on_status_screen ())
286
283
sleepTimeout ();
287
284
#endif
285
+
288
286
return is_touched;
289
287
}
290
288
0 commit comments