From d8724bb546eddf3cb4d1a161a35a117e3dcb7636 Mon Sep 17 00:00:00 2001
From: Roxy-3D <Roxanne.Rae.Neufeld@gmail.com>
Date: Tue, 28 Mar 2017 16:40:34 -0500
Subject: [PATCH] Get G29's P1 (Automated Probing) working again.

Incorrect optimizations of data types and ternary operators caused some
issues.
---
 Marlin/UBL_G29.cpp  | 29 +++++++++++++----------------
 Marlin/ultralcd.cpp | 32 +++++++++++++++++++++-----------
 2 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/Marlin/UBL_G29.cpp b/Marlin/UBL_G29.cpp
index 4ad1e2d71e..f806bb6cd3 100644
--- a/Marlin/UBL_G29.cpp
+++ b/Marlin/UBL_G29.cpp
@@ -33,7 +33,7 @@
   #include "planner.h"
   #include "ultralcd.h"
 
-  #include <avr/io.h>
+  #include <math.h>
 
   void lcd_babystep_z();
   void lcd_return_to_status();
@@ -300,7 +300,7 @@
 
   int ubl_eeprom_start = -1;
   bool ubl_has_control_of_lcd_panel = false;
-  volatile uint8_t ubl_encoderDiff = 0; // Volatile because it's changed by Temperature ISR button update
+  volatile int8_t ubl_encoderDiff = 0; // Volatile because it's changed by Temperature ISR button update
 
   // The simple parameter flags and values are 'static' so parameter parsing can be in a support routine.
   static int g29_verbose_level = 0, phase_value = -1, repetition_cnt = 1,
@@ -496,7 +496,7 @@
           SERIAL_ECHO_START;
           SERIAL_ECHOLNPGM("Checking G29 has control of LCD Panel:");
           wait_for_user = true;
-          while (wait_for_user) {
+          while (!ubl_lcd_clicked()) {
             safe_delay(250);
             SERIAL_ECHO((int)ubl_encoderDiff);
             ubl_encoderDiff = 0;
@@ -1310,7 +1310,7 @@
 
 	  if (far_flag) {                                    // If doing the far_flag action, we want to be as far as possible
             for (k = 0; k < UBL_MESH_NUM_X_POINTS; k++) {    // from the starting point and from any other probed points.  We
-              for (l = 0; j < UBL_MESH_NUM_Y_POINTS; l++) {  // want the next point spread out and filling in any blank spaces
+              for (l = 0; l < UBL_MESH_NUM_Y_POINTS; l++) {  // want the next point spread out and filling in any blank spaces
                 if ( !isnan(z_values[k][l])) {               // in the mesh.   So we add in some of the distance to every probed 
                   distance += (i-k)*(i-k)*MESH_X_DIST*.05;   // point we can find.
                   distance += (j-l)*(j-l)*MESH_Y_DIST*.05;
@@ -1366,15 +1366,12 @@
 
       do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);    // Move the nozzle to where we are going to edit
       do_blocking_move_to_xy(xProbe, yProbe);
-      float new_z = z_values[location.x_index][location.y_index] + 0.001;
-
-      round_off = (int32_t)(new_z * 1000.0 + 2.5); // we chop off the last digits just to be clean. We are rounding to the
-      round_off -= (round_off % 5L); // closest 0 or 5 at the 3rd decimal place.
+      float new_z = z_values[location.x_index][location.y_index];
+      
+      round_off = (int32_t)(new_z * 1000.0);    // we chop off the last digits just to be clean. We are rounding to the
       new_z = float(round_off) / 1000.0;
 
-      //SERIAL_ECHOPGM("Mesh Point Currently At:  ");
-      //SERIAL_PROTOCOL_F(new_z, 6);
-      //SERIAL_EOL;
+      ubl_has_control_of_lcd_panel = true;
 
       lcd_implementation_clear();
       lcd_mesh_edit_setup(new_z);
@@ -1383,20 +1380,20 @@
       do {
         new_z = lcd_mesh_edit();
         idle();
-      } while (wait_for_user);
+      } while (!ubl_lcd_clicked());
 
       lcd_return_to_status();
 
-      ubl_has_control_of_lcd_panel++; // There is a race condition for the Encoder Wheel getting clicked.
-                                      // It could get detected in lcd_mesh_edit (actually _lcd_mesh_fine_tune)
-                                      // or here.
+      ubl_has_control_of_lcd_panel = true; // There is a race condition for the Encoder Wheel getting clicked.
+                                           // It could get detected in lcd_mesh_edit (actually _lcd_mesh_fine_tune)
+                                           // or here.
 
       const millis_t nxt = millis() + 1500UL;
       while (ubl_lcd_clicked()) { // debounce and watch for abort
         idle();
         if (ELAPSED(millis(), nxt)) {
           lcd_return_to_status();
-          SERIAL_PROTOCOLLNPGM("\nFine Tuning of Mesh Stopped.");
+//        SERIAL_PROTOCOLLNPGM("\nFine Tuning of Mesh Stopped.");
           do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
           lcd_setstatus("Mesh Editing Stopped", true);
 
diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp
index 08c82dc288..38b772c2c1 100755
--- a/Marlin/ultralcd.cpp
+++ b/Marlin/ultralcd.cpp
@@ -125,7 +125,7 @@ uint16_t max_display_update_time = 0;
 
   #if ENABLED(AUTO_BED_LEVELING_UBL)
     extern bool ubl_has_control_of_lcd_panel;
-    extern uint8_t ubl_encoderDiff;
+    extern int8_t ubl_encoderDiff;
   #endif
 
   #if HAS_POWER_SWITCH
@@ -859,21 +859,23 @@ void kill_screen(const char* lcd_msg) {
     static int ubl_encoderPosition = 0;
 
     static void _lcd_mesh_fine_tune(const char* msg) {
-      static millis_t next_click = 0;
+//    static millis_t next_click = 0;             // We are going to accelerate the number speed when the wheel
+//                                                // turns fast.   But that isn't implemented yet
       int16_t last_digit;
       int32_t rounded;
 
       defer_return_to_status = true;
       if (ubl_encoderDiff) {
-        // If moving the Encoder wheel very slowly, move by just 1 position
-        ubl_encoderPosition = ELAPSED(millis(), next_click)
-          ? ubl_encoderDiff > 0 ? 1 : -1
-          : ubl_encoderDiff * 2;
+        if ( ubl_encoderDiff > 0 ) 
+          ubl_encoderPosition = 1;
+        else {
+          ubl_encoderPosition = -1;
+        }
 
         ubl_encoderDiff = 0;
-        next_click = millis() + 200L;
+//      next_click = millis();
 
-        mesh_edit_accumulator += float((int32_t)ubl_encoderPosition) * .005 / 2.0;
+        mesh_edit_accumulator += ( (float) (ubl_encoderPosition)) * .005 / 2.0 ;
         mesh_edit_value = mesh_edit_accumulator;
         encoderPosition = 0;
         lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
@@ -881,7 +883,6 @@ void kill_screen(const char* lcd_msg) {
         rounded = (int32_t)(mesh_edit_value * 1000.0);
         last_digit = rounded % 5L; //10L;
         rounded -= last_digit;
-        last_digit = rounded % 5L; //10L;
         mesh_edit_value = float(rounded) / 1000.0;
       }
 
@@ -890,19 +891,28 @@ void kill_screen(const char* lcd_msg) {
     }
 
 
+    void _lcd_mesh_edit_NOP() {
+      defer_return_to_status = true;
+    }
+
+
     void _lcd_mesh_edit() {
       _lcd_mesh_fine_tune(PSTR("Mesh Editor: "));
       defer_return_to_status = true;
     }
 
     float lcd_mesh_edit() {
-      lcd_goto_screen(_lcd_mesh_edit);
+      lcd_goto_screen(_lcd_mesh_edit_NOP);
+      _lcd_mesh_fine_tune(PSTR("Mesh Editor: "));
+      defer_return_to_status = true;
       return mesh_edit_value;
     }
 
     void lcd_mesh_edit_setup(float initial) {
       mesh_edit_value = mesh_edit_accumulator = initial;
-      lcd_goto_screen(_lcd_mesh_edit);
+      lcd_goto_screen(_lcd_mesh_edit_NOP);
+      mesh_edit_value = mesh_edit_accumulator = initial;
+      defer_return_to_status = true; 
     }
 
     void _lcd_z_offset_edit() {
-- 
GitLab