From 0b80841c38dcd8259821f72dc40c194e67042c5d Mon Sep 17 00:00:00 2001
From: Victor Oliveira <rhapsodyv@gmail.com>
Date: Fri, 16 Oct 2020 18:19:48 -0300
Subject: [PATCH] Fix Color UI external_control, wait_for_release (#19771)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
---
 Marlin/src/lcd/tft/touch.cpp | 21 ++++++++++++---------
 Marlin/src/lcd/tft/touch.h   |  8 +++++++-
 Marlin/src/lcd/ultralcd.cpp  |  4 ----
 Marlin/src/module/probe.cpp  |  2 +-
 4 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/Marlin/src/lcd/tft/touch.cpp b/Marlin/src/lcd/tft/touch.cpp
index 80c65f074a..3ecf481ed1 100644
--- a/Marlin/src/lcd/tft/touch.cpp
+++ b/Marlin/src/lcd/tft/touch.cpp
@@ -40,7 +40,7 @@ int16_t Touch::x, Touch::y;
 touch_control_t Touch::controls[];
 touch_control_t *Touch::current_control;
 uint16_t Touch::controls_count;
-millis_t Touch::now = 0;
+millis_t Touch::last_touch_ms = 0;
 millis_t Touch::time_to_hold;
 millis_t Touch::repeat_delay;
 millis_t Touch::touch_time;
@@ -79,8 +79,10 @@ void Touch::idle() {
 
   if (!enabled) return;
 
-  if (now == millis()) return;
-  now = millis();
+  // Return if Touch::idle is called within the same millisecond
+  const millis_t now = millis();
+  if (last_touch_ms == now) return;
+  last_touch_ms = now;
 
   if (get_point(&_x, &_y)) {
     #if HAS_RESUME_CONTINUE
@@ -88,24 +90,25 @@ void Touch::idle() {
       if (wait_for_user) {
         touch_control_type = CLICK;
         ui.lcd_clicked = true;
+        if (ui.external_control) wait_for_user = false;
         return;
       }
     #endif
 
     #if LCD_TIMEOUT_TO_STATUS
-      ui.return_to_status_ms = now + LCD_TIMEOUT_TO_STATUS;
+      ui.return_to_status_ms = last_touch_ms + LCD_TIMEOUT_TO_STATUS;
     #endif
 
     if (touch_time) {
       #if ENABLED(TOUCH_SCREEN_CALIBRATION)
-        if (touch_control_type == NONE && ELAPSED(now, touch_time + TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS) && ui.on_status_screen())
+        if (touch_control_type == NONE && ELAPSED(last_touch_ms, touch_time + TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS) && ui.on_status_screen())
           ui.goto_screen(touch_screen_calibration);
       #endif
       return;
     }
 
-    if (time_to_hold == 0) time_to_hold = now + MINIMUM_HOLD_TIME;
-    if (PENDING(now, time_to_hold)) return;
+    if (time_to_hold == 0) time_to_hold = last_touch_ms + MINIMUM_HOLD_TIME;
+    if (PENDING(last_touch_ms, time_to_hold)) return;
 
     if (x != 0 && y != 0) {
       if (current_control) {
@@ -131,7 +134,7 @@ void Touch::idle() {
       }
 
       if (current_control == NULL)
-        touch_time = now;
+        touch_time = last_touch_ms;
     }
     x = _x;
     y = _y;
@@ -284,7 +287,7 @@ void Touch::hold(touch_control_t *control, millis_t delay) {
   current_control = control;
   if (delay) {
     repeat_delay = delay > MIN_REPEAT_DELAY ? delay : MIN_REPEAT_DELAY;
-    time_to_hold = now + repeat_delay;
+    time_to_hold = last_touch_ms + repeat_delay;
   }
   ui.refresh();
 }
diff --git a/Marlin/src/lcd/tft/touch.h b/Marlin/src/lcd/tft/touch.h
index 7d8f222918..b2c7a21ba1 100644
--- a/Marlin/src/lcd/tft/touch.h
+++ b/Marlin/src/lcd/tft/touch.h
@@ -164,7 +164,13 @@ class Touch {
     static void reset() { controls_count = 0; touch_time = -1; current_control = NULL; }
     static void clear() { controls_count = 0; }
     static void idle();
-    static bool is_clicked() { return touch_control_type == CLICK; }
+    static bool is_clicked() {
+      if (touch_control_type == CLICK) {
+        touch_control_type = NONE;
+        return true;
+      }
+      return false;
+    }
     static void disable() { enabled = false; }
     static void enable() { enabled = true; }
 
diff --git a/Marlin/src/lcd/ultralcd.cpp b/Marlin/src/lcd/ultralcd.cpp
index da9c3e2456..25f5afc71d 100644
--- a/Marlin/src/lcd/ultralcd.cpp
+++ b/Marlin/src/lcd/ultralcd.cpp
@@ -1478,10 +1478,6 @@ void MarlinUI::update() {
     set_status_P(msg, -1);
   }
 
-  #if ENABLED(SDSUPPORT)
-    extern bool wait_for_user, wait_for_heatup;
-  #endif
-
   void MarlinUI::abort_print() {
     #if ENABLED(SDSUPPORT)
       wait_for_heatup = wait_for_user = false;
diff --git a/Marlin/src/module/probe.cpp b/Marlin/src/module/probe.cpp
index defc22b1fe..27ceb989c0 100644
--- a/Marlin/src/module/probe.cpp
+++ b/Marlin/src/module/probe.cpp
@@ -38,7 +38,7 @@
 #include "../gcode/gcode.h"
 #include "../lcd/ultralcd.h"
 
-#include "../MarlinCore.h" // for stop(), disable_e_steppers, wait_for_user
+#include "../MarlinCore.h" // for stop(), disable_e_steppers
 
 #if HAS_LEVELING
   #include "../feature/bedlevel/bedlevel.h"