From ee4c4f7f40a181dd679a154e75ea853ba2b9bd0e Mon Sep 17 00:00:00 2001 From: thisiskeithb <13375512+thisiskeithb@users.noreply.github.com> Date: Wed, 8 Feb 2023 23:23:46 -0800 Subject: [PATCH 1/5] Fix TOUCH_SCREEN_CALIBRATION Invalid First Point Avoid registering first touch point as top-left corner when initiating touch calibration since user is touching TFT --- Marlin/src/lcd/tft_io/touch_calibration.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Marlin/src/lcd/tft_io/touch_calibration.cpp b/Marlin/src/lcd/tft_io/touch_calibration.cpp index 44ebc73d71a2..5f45a9506df2 100644 --- a/Marlin/src/lcd/tft_io/touch_calibration.cpp +++ b/Marlin/src/lcd/tft_io/touch_calibration.cpp @@ -92,6 +92,13 @@ void TouchCalibration::validate_calibration() { bool TouchCalibration::handleTouch(uint16_t x, uint16_t y) { static millis_t next_button_update_ms = 0; const millis_t now = millis(); + + // Avoid registering first touch point as top-left corner when initiating touch calibration since user is touching TFT + if (next_button_update_ms == 0) { + next_button_update_ms = now + BUTTON_DELAY_MENU; + return true; + } + if (PENDING(now, next_button_update_ms)) return false; next_button_update_ms = now + BUTTON_DELAY_MENU; From 0e74176fb5e15367cefd6cb58f5b428b11c1aced Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 23 Mar 2023 16:39:36 -0500 Subject: [PATCH 2/5] ignore first touch after calibration_start --- Marlin/src/lcd/tft_io/touch_calibration.cpp | 18 +++++++++--------- Marlin/src/lcd/tft_io/touch_calibration.h | 6 ++++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Marlin/src/lcd/tft_io/touch_calibration.cpp b/Marlin/src/lcd/tft_io/touch_calibration.cpp index 5f45a9506df2..3cc35a7fdfca 100644 --- a/Marlin/src/lcd/tft_io/touch_calibration.cpp +++ b/Marlin/src/lcd/tft_io/touch_calibration.cpp @@ -41,6 +41,7 @@ touch_calibration_t TouchCalibration::calibration; calibrationState TouchCalibration::calibration_state = CALIBRATION_NONE; touch_calibration_point_t TouchCalibration::calibration_points[4]; uint8_t TouchCalibration::failed_count; +millis_t TouchCalibration::next_button_update_ms; // = 0; void TouchCalibration::validate_calibration() { #define VALIDATE_PRECISION(XY, A, B) validate_precision_##XY(CALIBRATION_##A, CALIBRATION_##B) @@ -89,19 +90,18 @@ void TouchCalibration::validate_calibration() { } } -bool TouchCalibration::handleTouch(uint16_t x, uint16_t y) { - static millis_t next_button_update_ms = 0; - const millis_t now = millis(); +bool TouchCalibration::handleTouch(const uint16_t x, const uint16_t y) { + const millis_t now = millis(), next_ms = next_button_update_ms; - // Avoid registering first touch point as top-left corner when initiating touch calibration since user is touching TFT - if (next_button_update_ms == 0) { - next_button_update_ms = now + BUTTON_DELAY_MENU; + if (next_ms && PENDING(now, next_ms)) return false; + next_button_update_ms = now + BUTTON_DELAY_MENU; + + // Ignore the first touch because it was probably the one that entered the screen + if (!next_ms) { + next_button_update_ms += 500; return true; } - if (PENDING(now, next_button_update_ms)) return false; - next_button_update_ms = now + BUTTON_DELAY_MENU; - if (calibration_state < CALIBRATION_SUCCESS) { calibration_points[calibration_state].raw_x = x; calibration_points[calibration_state].raw_y = y; diff --git a/Marlin/src/lcd/tft_io/touch_calibration.h b/Marlin/src/lcd/tft_io/touch_calibration.h index abd566770007..73d3d10f1486 100644 --- a/Marlin/src/lcd/tft_io/touch_calibration.h +++ b/Marlin/src/lcd/tft_io/touch_calibration.h @@ -57,6 +57,7 @@ class TouchCalibration { public: static calibrationState calibration_state; static touch_calibration_point_t calibration_points[4]; + static millis_t next_button_update_ms; static bool validate_precision(int32_t a, int32_t b) { return (a > b ? (100 * b) / a : (100 * a) / b) > TOUCH_SCREEN_CALIBRATION_PRECISION; } static bool validate_precision_x(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_x, calibration_points[b].raw_x); } @@ -64,11 +65,12 @@ class TouchCalibration { static void validate_calibration(); static touch_calibration_t calibration; - static uint8_t failed_count; + static uint8_t failed_count; static void calibration_reset() { calibration = { TOUCH_CALIBRATION_X, TOUCH_CALIBRATION_Y, TOUCH_OFFSET_X, TOUCH_OFFSET_Y, TOUCH_ORIENTATION }; } static bool need_calibration() { return !calibration.offset_x && !calibration.offset_y && !calibration.x && !calibration.y; } static calibrationState calibration_start() { + next_button_update_ms = 0; calibration = { 0, 0, 0, 0, TOUCH_ORIENTATION_NONE }; calibration_state = CALIBRATION_TOP_LEFT; calibration_points[CALIBRATION_TOP_LEFT].x = 30; @@ -89,7 +91,7 @@ class TouchCalibration { return !need_calibration(); } - static bool handleTouch(uint16_t x, uint16_t y); + static bool handleTouch(const uint16_t x, const uint16_t y); }; extern TouchCalibration touch_calibration; From f877b75a2448db72b5719736e7a50a0d2d7d9164 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 23 Mar 2023 17:30:36 -0500 Subject: [PATCH 3/5] cleanup --- Marlin/src/lcd/tft/touch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/lcd/tft/touch.cpp b/Marlin/src/lcd/tft/touch.cpp index b73f67e74414..9482c85a4706 100644 --- a/Marlin/src/lcd/tft/touch.cpp +++ b/Marlin/src/lcd/tft/touch.cpp @@ -152,7 +152,7 @@ void Touch::touch(touch_control_t *control) { case CALIBRATE: if (touch_calibration.handleTouch(x, y)) ui.refresh(); break; - #endif // TOUCH_SCREEN_CALIBRATION + #endif case MENU_SCREEN: ui.goto_screen((screenFunc_t)control->data); break; case BACK: ui.goto_previous_screen(); break; From c28028ddb37380d069973c62549eb76c5fb41fb3 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 23 Mar 2023 17:52:09 -0500 Subject: [PATCH 4/5] alternative touch delay --- .../lcd/extui/mks_ui/tft_lvgl_configuration.cpp | 3 ++- Marlin/src/lcd/tft_io/touch_calibration.cpp | 14 ++++---------- Marlin/src/lcd/tft_io/touch_calibration.h | 4 ++-- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index 2248ef334ffd..08db5ae7ad56 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -492,6 +492,7 @@ void lv_encoder_pin_init() { } #if 1 // HAS_ENCODER_ACTION + void lv_update_encoder() { static uint32_t encoder_time1; uint32_t tmpTime, diffTime = 0; @@ -552,7 +553,7 @@ void lv_encoder_pin_init() { #endif // HAS_ENCODER_WHEEL - } // next_button_update_ms + } // encoder_time1 } #endif // HAS_ENCODER_ACTION diff --git a/Marlin/src/lcd/tft_io/touch_calibration.cpp b/Marlin/src/lcd/tft_io/touch_calibration.cpp index 3cc35a7fdfca..2a638fd89e75 100644 --- a/Marlin/src/lcd/tft_io/touch_calibration.cpp +++ b/Marlin/src/lcd/tft_io/touch_calibration.cpp @@ -41,7 +41,7 @@ touch_calibration_t TouchCalibration::calibration; calibrationState TouchCalibration::calibration_state = CALIBRATION_NONE; touch_calibration_point_t TouchCalibration::calibration_points[4]; uint8_t TouchCalibration::failed_count; -millis_t TouchCalibration::next_button_update_ms; // = 0; +millis_t TouchCalibration::next_touch_update_ms; // = 0; void TouchCalibration::validate_calibration() { #define VALIDATE_PRECISION(XY, A, B) validate_precision_##XY(CALIBRATION_##A, CALIBRATION_##B) @@ -91,16 +91,10 @@ void TouchCalibration::validate_calibration() { } bool TouchCalibration::handleTouch(const uint16_t x, const uint16_t y) { - const millis_t now = millis(), next_ms = next_button_update_ms; + const millis_t now = millis(); - if (next_ms && PENDING(now, next_ms)) return false; - next_button_update_ms = now + BUTTON_DELAY_MENU; - - // Ignore the first touch because it was probably the one that entered the screen - if (!next_ms) { - next_button_update_ms += 500; - return true; - } + if (next_touch_update_ms && PENDING(now, next_touch_update_ms)) return false; + next_touch_update_ms = now + BUTTON_DELAY_MENU; if (calibration_state < CALIBRATION_SUCCESS) { calibration_points[calibration_state].raw_x = x; diff --git a/Marlin/src/lcd/tft_io/touch_calibration.h b/Marlin/src/lcd/tft_io/touch_calibration.h index 73d3d10f1486..030b4977db51 100644 --- a/Marlin/src/lcd/tft_io/touch_calibration.h +++ b/Marlin/src/lcd/tft_io/touch_calibration.h @@ -57,7 +57,7 @@ class TouchCalibration { public: static calibrationState calibration_state; static touch_calibration_point_t calibration_points[4]; - static millis_t next_button_update_ms; + static millis_t next_touch_update_ms; static bool validate_precision(int32_t a, int32_t b) { return (a > b ? (100 * b) / a : (100 * a) / b) > TOUCH_SCREEN_CALIBRATION_PRECISION; } static bool validate_precision_x(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_x, calibration_points[b].raw_x); } @@ -70,7 +70,7 @@ class TouchCalibration { static bool need_calibration() { return !calibration.offset_x && !calibration.offset_y && !calibration.x && !calibration.y; } static calibrationState calibration_start() { - next_button_update_ms = 0; + next_touch_update_ms = millis() + 750UL; calibration = { 0, 0, 0, 0, TOUCH_ORIENTATION_NONE }; calibration_state = CALIBRATION_TOP_LEFT; calibration_points[CALIBRATION_TOP_LEFT].x = 30; From fd76e4726b1b84c78d64cbf4f2f9fb4820554fc4 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 23 Mar 2023 23:37:22 -0500 Subject: [PATCH 5/5] tweak --- Marlin/src/lcd/touch/touch_buttons.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/lcd/touch/touch_buttons.cpp b/Marlin/src/lcd/touch/touch_buttons.cpp index d641dd3b1c92..08554de0d3ca 100644 --- a/Marlin/src/lcd/touch/touch_buttons.cpp +++ b/Marlin/src/lcd/touch/touch_buttons.cpp @@ -91,7 +91,7 @@ uint8_t TouchButtons::read_buttons() { y = uint16_t((uint32_t(y) * TOUCH_CALIBRATION_Y) >> 16) + TOUCH_OFFSET_Y; #endif #elif ENABLED(TFT_TOUCH_DEVICE_GT911) - bool is_touched = (TOUCH_ORIENTATION == TOUCH_PORTRAIT ? touchIO.getPoint(&y, &x) : touchIO.getPoint(&x, &y)); + const bool is_touched = (TOUCH_ORIENTATION == TOUCH_PORTRAIT ? touchIO.getPoint(&y, &x) : touchIO.getPoint(&x, &y)); if (!is_touched) return 0; #endif