Browse Source

Various UBL cleanups and bug fixes

Scott Lahteine 7 years ago
parent
commit
9217e4b8ec

+ 107
- 142
Marlin/G26_Mesh_Validation_Tool.cpp View File

35
   #include "temperature.h"
35
   #include "temperature.h"
36
   #include "UBL.h"
36
   #include "UBL.h"
37
   #include "ultralcd.h"
37
   #include "ultralcd.h"
38
-//#include <avr/pgmspace.h>
39
 
38
 
40
   #define EXTRUSION_MULTIPLIER 1.0    // This is too much clutter for the main Configuration.h file  But
39
   #define EXTRUSION_MULTIPLIER 1.0    // This is too much clutter for the main Configuration.h file  But
41
   #define RETRACTION_MULTIPLIER 1.0   // some user have expressed an interest in being able to customize
40
   #define RETRACTION_MULTIPLIER 1.0   // some user have expressed an interest in being able to customize
177
 
176
 
178
   /**
177
   /**
179
    * G26: Mesh Validation Pattern generation.
178
    * G26: Mesh Validation Pattern generation.
180
-   * 
179
+   *
181
    * Used to interactively edit UBL's Mesh by placing the
180
    * Used to interactively edit UBL's Mesh by placing the
182
    * nozzle in a problem area and doing a G29 P4 R command.
181
    * nozzle in a problem area and doing a G29 P4 R command.
183
    */
182
    */
234
     move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], ooze_amount);
233
     move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], ooze_amount);
235
 
234
 
236
     ubl_has_control_of_lcd_panel = true; // Take control of the LCD Panel!
235
     ubl_has_control_of_lcd_panel = true; // Take control of the LCD Panel!
237
-//  debug_current_and_destination((char*)"Starting G26 Mesh Validation Pattern.");
236
+    //debug_current_and_destination((char*)"Starting G26 Mesh Validation Pattern.");
238
 
237
 
239
     /**
238
     /**
240
      * Declare and generate a sin() & cos() table to be used during the circle drawing.  This will lighten
239
      * Declare and generate a sin() & cos() table to be used during the circle drawing.  This will lighten
250
 
249
 
251
       if (ubl_lcd_clicked()) {              // Check if the user wants to stop the Mesh Validation
250
       if (ubl_lcd_clicked()) {              // Check if the user wants to stop the Mesh Validation
252
         #if ENABLED(ULTRA_LCD)
251
         #if ENABLED(ULTRA_LCD)
253
-          lcd_setstatuspgm(PSTR("Mesh Validation Stopped."), (uint8_t) 99);
252
+          lcd_setstatuspgm(PSTR("Mesh Validation Stopped."), 99);
254
           lcd_quick_feedback();
253
           lcd_quick_feedback();
255
         #endif
254
         #endif
256
         while (!ubl_lcd_clicked()) {         // Wait until the user is done pressing the
255
         while (!ubl_lcd_clicked()) {         // Wait until the user is done pressing the
257
           idle();                            // Encoder Wheel if that is why we are leaving
256
           idle();                            // Encoder Wheel if that is why we are leaving
258
-          lcd_setstatuspgm(PSTR(" "), (uint8_t) 99);
257
+          lcd_reset_alert_level();
258
+          lcd_setstatuspgm(PSTR(""));
259
         }
259
         }
260
-        while ( ubl_lcd_clicked()) {         // Wait until the user is done pressing the
260
+        while (ubl_lcd_clicked()) {          // Wait until the user is done pressing the
261
           idle();                            // Encoder Wheel if that is why we are leaving
261
           idle();                            // Encoder Wheel if that is why we are leaving
262
-          lcd_setstatuspgm(PSTR("Unpress Wheel "), (uint8_t) 99);
262
+          lcd_setstatuspgm(PSTR("Unpress Wheel"), 99);
263
         }
263
         }
264
         goto LEAVE;
264
         goto LEAVE;
265
       }
265
       }
276
         // Let's do a couple of quick sanity checks.  We can pull this code out later if we never see it catch a problem
276
         // Let's do a couple of quick sanity checks.  We can pull this code out later if we never see it catch a problem
277
         #ifdef DELTA
277
         #ifdef DELTA
278
           if (HYPOT2(circle_x, circle_y) > sq(DELTA_PRINTABLE_RADIUS)) {
278
           if (HYPOT2(circle_x, circle_y) > sq(DELTA_PRINTABLE_RADIUS)) {
279
-            SERIAL_PROTOCOLLNPGM("?Error: Attempt to print outside of DELTA_PRINTABLE_RADIUS.");
279
+            SERIAL_ERROR_START;
280
+            SERIAL_ERRORLNPGM("Attempt to print outside of DELTA_PRINTABLE_RADIUS.");
280
             goto LEAVE;
281
             goto LEAVE;
281
           }
282
           }
282
         #endif
283
         #endif
283
 
284
 
284
-        if (circle_x < X_MIN_POS || circle_x > X_MAX_POS || circle_y < Y_MIN_POS || circle_y > Y_MAX_POS) {
285
-          SERIAL_PROTOCOLLNPGM("?Error: Attempt to print off the bed.");
285
+        // TODO: Change this to use `position_is_reachable`
286
+        if (circle_x < (X_MIN_POS) || circle_x > (X_MAX_POS) || circle_y < (Y_MIN_POS) || circle_y > (Y_MAX_POS)) {
287
+          SERIAL_ERROR_START;
288
+          SERIAL_ERRORLNPGM("Attempt to print off the bed.");
286
           goto LEAVE;
289
           goto LEAVE;
287
         }
290
         }
288
 
291
 
290
         yi = location.y_index;
293
         yi = location.y_index;
291
 
294
 
292
         if (g26_debug_flag) {
295
         if (g26_debug_flag) {
293
-          SERIAL_ECHOPGM("   Doing circle at: (xi=");
294
-          SERIAL_ECHO(xi);
295
-          SERIAL_ECHOPGM(", yi=");
296
-          SERIAL_ECHO(yi);
297
-          SERIAL_ECHOLNPGM(")");
296
+          SERIAL_ECHOPAIR("   Doing circle at: (xi=", xi);
297
+          SERIAL_ECHOPAIR(", yi=", yi);
298
+          SERIAL_CHAR(')');
299
+          SERIAL_EOL;
298
         }
300
         }
299
 
301
 
300
         start_angle = 0.0;    // assume it is going to be a full circle
302
         start_angle = 0.0;    // assume it is going to be a full circle
344
             ye = constrain(ye, Y_MIN_POS + 1, Y_MAX_POS - 1);
346
             ye = constrain(ye, Y_MIN_POS + 1, Y_MAX_POS - 1);
345
           #endif
347
           #endif
346
 
348
 
347
-//          if (g26_debug_flag) {
348
-//            char ccc, *cptr, seg_msg[50], seg_num[10];
349
-//            strcpy(seg_msg, "   segment: ");
350
-//            strcpy(seg_num, "    \n");
351
-//            cptr = (char*) "01234567890ABCDEF????????";
352
-//            ccc = cptr[tmp_div_30];
353
-//            seg_num[1] = ccc;
354
-//            strcat(seg_msg, seg_num);
355
-//            debug_current_and_destination(seg_msg);
356
-//          }
349
+          //if (g26_debug_flag) {
350
+          //  char ccc, *cptr, seg_msg[50], seg_num[10];
351
+          //  strcpy(seg_msg, "   segment: ");
352
+          //  strcpy(seg_num, "    \n");
353
+          //  cptr = (char*) "01234567890ABCDEF????????";
354
+          //  ccc = cptr[tmp_div_30];
355
+          //  seg_num[1] = ccc;
356
+          //  strcat(seg_msg, seg_num);
357
+          //  debug_current_and_destination(seg_msg);
358
+          //}
357
 
359
 
358
           print_line_from_here_to_there(x, y, layer_height, xe, ye, layer_height);
360
           print_line_from_here_to_there(x, y, layer_height, xe, ye, layer_height);
359
 
361
 
360
         }
362
         }
361
-//      lcd_init_counter++;
362
-//      if (lcd_init_counter > 10) {
363
-//        lcd_init_counter = 0;
364
-//        lcd_init(); // Some people's LCD Displays are locking up.  This might help them
365
-//        ubl_has_control_of_lcd_panel = true;     // Make sure UBL still is controlling the LCD Panel
366
-//      }
367
-
368
-    // If the end point of the line is closer to the nozzle, we are going to
369
-//      debug_current_and_destination((char*)"Looking for lines to connect.");
363
+        //lcd_init_counter++;
364
+        //if (lcd_init_counter > 10) {
365
+        //  lcd_init_counter = 0;
366
+        //  lcd_init(); // Some people's LCD Displays are locking up.  This might help them
367
+        //  ubl_has_control_of_lcd_panel = true;     // Make sure UBL still is controlling the LCD Panel
368
+        //}
369
+
370
+        //debug_current_and_destination((char*)"Looking for lines to connect.");
370
         look_for_lines_to_connect();
371
         look_for_lines_to_connect();
371
-//      debug_current_and_destination((char*)"Done with line connect.");
372
+        //debug_current_and_destination((char*)"Done with line connect.");
372
       }
373
       }
373
 
374
 
374
-//    debug_current_and_destination((char*)"Done with current circle.");
375
-
376
-    // If the end point of the line is closer to the nozzle, we are going to
377
-
375
+      //debug_current_and_destination((char*)"Done with current circle.");
378
     }
376
     }
379
     while (location.x_index >= 0 && location.y_index >= 0);
377
     while (location.x_index >= 0 && location.y_index >= 0);
380
 
378
 
381
     LEAVE:
379
     LEAVE:
382
-    lcd_setstatuspgm(PSTR("Leaving G26 "), (uint8_t) 99);
380
+    lcd_reset_alert_level();
381
+    lcd_setstatuspgm(PSTR("Leaving G26"));
383
 
382
 
384
     retract_filament();
383
     retract_filament();
385
-    destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES;                             // Raise the nozzle
384
+    destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES;
386
 
385
 
387
-//  debug_current_and_destination((char*)"ready to do Z-Raise.");
388
-    move_to( destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0);   // Raise the nozzle
389
-//  debug_current_and_destination((char*)"done doing Z-Raise.");
386
+    //debug_current_and_destination((char*)"ready to do Z-Raise.");
387
+    move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0); // Raise the nozzle
388
+    //debug_current_and_destination((char*)"done doing Z-Raise.");
390
 
389
 
391
-    destination[X_AXIS] = x_pos;                                                  // Move back to the starting position
390
+    destination[X_AXIS] = x_pos;                                               // Move back to the starting position
392
     destination[Y_AXIS] = y_pos;
391
     destination[Y_AXIS] = y_pos;
393
-    destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES;                             // Keep the nozzle where it is
392
+    //destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES;                        // Keep the nozzle where it is
394
 
393
 
395
     move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0); // Move back to the starting position
394
     move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0); // Move back to the starting position
396
-//  debug_current_and_destination((char*)"done doing X/Y move.");
395
+    //debug_current_and_destination((char*)"done doing X/Y move.");
397
 
396
 
398
     ubl_has_control_of_lcd_panel = false;     // Give back control of the LCD Panel!
397
     ubl_has_control_of_lcd_panel = false;     // Give back control of the LCD Panel!
399
 
398
 
481
               ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
480
               ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
482
 
481
 
483
               if (g26_debug_flag) {
482
               if (g26_debug_flag) {
484
-                SERIAL_ECHOPGM(" Connecting with horizontal line (sx=");
485
-                SERIAL_ECHO(sx);
486
-                SERIAL_ECHOPGM(", sy=");
487
-                SERIAL_ECHO(sy);
488
-                SERIAL_ECHOPGM(") -> (ex=");
489
-                SERIAL_ECHO(ex);
490
-                SERIAL_ECHOPGM(", ey=");
491
-                SERIAL_ECHO(ey);
492
-                SERIAL_ECHOLNPGM(")");
493
-//              debug_current_and_destination((char*)"Connecting horizontal line.");
483
+                SERIAL_ECHOPAIR(" Connecting with horizontal line (sx=", sx);
484
+                SERIAL_ECHOPAIR(", sy=", sy);
485
+                SERIAL_ECHOPAIR(") -> (ex=", ex);
486
+                SERIAL_ECHOPAIR(", ey=", ey);
487
+                SERIAL_CHAR(')');
488
+                SERIAL_EOL;
489
+                //debug_current_and_destination((char*)"Connecting horizontal line.");
494
               }
490
               }
495
 
491
 
496
               print_line_from_here_to_there(sx, sy, layer_height, ex, ey, layer_height);
492
               print_line_from_here_to_there(sx, sy, layer_height, ex, ey, layer_height);
521
                 ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
517
                 ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
522
 
518
 
523
                 if (g26_debug_flag) {
519
                 if (g26_debug_flag) {
524
-                  SERIAL_ECHOPGM(" Connecting with vertical line (sx=");
525
-                  SERIAL_ECHO(sx);
526
-                  SERIAL_ECHOPGM(", sy=");
527
-                  SERIAL_ECHO(sy);
528
-                  SERIAL_ECHOPGM(") -> (ex=");
529
-                  SERIAL_ECHO(ex);
530
-                  SERIAL_ECHOPGM(", ey=");
531
-                  SERIAL_ECHO(ey);
532
-                  SERIAL_ECHOLNPGM(")");
520
+                  SERIAL_ECHOPAIR(" Connecting with vertical line (sx=", sx);
521
+                  SERIAL_ECHOPAIR(", sy=", sy);
522
+                  SERIAL_ECHOPAIR(") -> (ex=", ex);
523
+                  SERIAL_ECHOPAIR(", ey=", ey);
524
+                  SERIAL_CHAR(')');
525
+                  SERIAL_EOL;
533
                   debug_current_and_destination((char*)"Connecting vertical line.");
526
                   debug_current_and_destination((char*)"Connecting vertical line.");
534
                 }
527
                 }
535
                 print_line_from_here_to_there(sx, sy, layer_height, ex, ey, layer_height);
528
                 print_line_from_here_to_there(sx, sy, layer_height, ex, ey, layer_height);
548
 
541
 
549
     bool has_xy_component = (x != current_position[X_AXIS] || y != current_position[Y_AXIS]); // Check if X or Y is involved in the movement.
542
     bool has_xy_component = (x != current_position[X_AXIS] || y != current_position[Y_AXIS]); // Check if X or Y is involved in the movement.
550
 
543
 
551
-//  if (g26_debug_flag) {
552
-//    SERIAL_ECHOPAIR("in move_to()  has_xy_component:", (int)has_xy_component);
553
-//    SERIAL_EOL;
554
-//  }
544
+    //if (g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to()  has_xy_component:", (int)has_xy_component);
555
 
545
 
556
     if (z != last_z) {
546
     if (z != last_z) {
557
-//    if (g26_debug_flag) {
558
-//      SERIAL_ECHOPAIR("in move_to()  changing Z to ", (int)z);
559
-//      SERIAL_EOL;
560
-//    }
547
+      //if (g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to()  changing Z to ", (int)z);
561
 
548
 
562
       last_z = z;
549
       last_z = z;
563
       feed_value = planner.max_feedrate_mm_s[Z_AXIS]/(3.0);  // Base the feed rate off of the configured Z_AXIS feed rate
550
       feed_value = planner.max_feedrate_mm_s[Z_AXIS]/(3.0);  // Base the feed rate off of the configured Z_AXIS feed rate
572
       stepper.synchronize();
559
       stepper.synchronize();
573
       set_destination_to_current();
560
       set_destination_to_current();
574
 
561
 
575
-//    if (g26_debug_flag)
576
-//      debug_current_and_destination((char*)" in move_to() done with Z move");
562
+      //if (g26_debug_flag) debug_current_and_destination((char*)" in move_to() done with Z move");
577
     }
563
     }
578
 
564
 
579
     // Check if X or Y is involved in the movement.
565
     // Check if X or Y is involved in the movement.
580
     // Yes: a 'normal' movement. No: a retract() or un_retract()
566
     // Yes: a 'normal' movement. No: a retract() or un_retract()
581
     feed_value = has_xy_component ? PLANNER_XY_FEEDRATE() / 10.0 : planner.max_feedrate_mm_s[E_AXIS] / 1.5;
567
     feed_value = has_xy_component ? PLANNER_XY_FEEDRATE() / 10.0 : planner.max_feedrate_mm_s[E_AXIS] / 1.5;
582
 
568
 
583
-    if (g26_debug_flag) {
584
-      SERIAL_ECHOPAIR("in move_to() feed_value for XY:", feed_value);
585
-      SERIAL_EOL;
586
-    }
569
+    if (g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to() feed_value for XY:", feed_value);
587
 
570
 
588
     destination[X_AXIS] = x;
571
     destination[X_AXIS] = x;
589
     destination[Y_AXIS] = y;
572
     destination[Y_AXIS] = y;
590
     destination[E_AXIS] += e_delta;
573
     destination[E_AXIS] += e_delta;
591
 
574
 
592
-//  if (g26_debug_flag)
593
-//    debug_current_and_destination((char*)" in move_to() doing last move");
575
+    //if (g26_debug_flag) debug_current_and_destination((char*)" in move_to() doing last move");
594
 
576
 
595
     ubl_line_to_destination(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feed_value, 0);
577
     ubl_line_to_destination(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feed_value, 0);
596
 
578
 
597
-//  if (g26_debug_flag)
598
-//    debug_current_and_destination((char*)" in move_to() after last move");
579
+    //if (g26_debug_flag) debug_current_and_destination((char*)" in move_to() after last move");
599
 
580
 
600
     stepper.synchronize();
581
     stepper.synchronize();
601
     set_destination_to_current();
582
     set_destination_to_current();
605
   void retract_filament() {
586
   void retract_filament() {
606
     if (!g26_retracted) { // Only retract if we are not already retracted!
587
     if (!g26_retracted) { // Only retract if we are not already retracted!
607
       g26_retracted = true;
588
       g26_retracted = true;
608
-//    if (g26_debug_flag) SERIAL_ECHOLNPGM(" Decided to do retract.");
589
+      //if (g26_debug_flag) SERIAL_ECHOLNPGM(" Decided to do retract.");
609
       move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], -1.0 * retraction_multiplier);
590
       move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], -1.0 * retraction_multiplier);
610
-//    if (g26_debug_flag) SERIAL_ECHOLNPGM(" Retraction done.");
591
+      //if (g26_debug_flag) SERIAL_ECHOLNPGM(" Retraction done.");
611
     }
592
     }
612
   }
593
   }
613
 
594
 
615
     if (g26_retracted) { // Only un-retract if we are retracted.
596
     if (g26_retracted) { // Only un-retract if we are retracted.
616
       move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 1.2 * retraction_multiplier);
597
       move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 1.2 * retraction_multiplier);
617
       g26_retracted = false;
598
       g26_retracted = false;
618
-//    if (g26_debug_flag) SERIAL_ECHOLNPGM(" unretract done.");
599
+      //if (g26_debug_flag) SERIAL_ECHOLNPGM(" unretract done.");
619
     }
600
     }
620
   }
601
   }
621
 
602
 
635
    * cases where the optimization comes into play.
616
    * cases where the optimization comes into play.
636
    */
617
    */
637
   void print_line_from_here_to_there( float sx, float sy, float sz, float ex, float ey, float ez) {
618
   void print_line_from_here_to_there( float sx, float sy, float sz, float ex, float ey, float ez) {
638
-    float dx, dy, dx_s, dy_s, dx_e, dy_e, dist_start, dist_end, Line_Length;
639
-
640
-    dx_s = current_position[X_AXIS] - sx;   // find our distance from the start of the actual line segment
641
-    dy_s = current_position[Y_AXIS] - sy;
642
-    dist_start = HYPOT2(dx_s, dy_s);        // We don't need to do a sqrt(), we can compare the distance^2
643
-                                            // to save computation time
644
-    dx_e = current_position[X_AXIS] - ex;   // find our distance from the end of the actual line segment
645
-    dy_e = current_position[Y_AXIS] - ey;
646
-    dist_end = HYPOT2(dx_e, dy_e);
647
-
648
-    dx = ex - sx;
649
-    dy = ey - sy;
650
-    Line_Length = HYPOT(dx, dy);
619
+    const float dx_s = current_position[X_AXIS] - sx,   // find our distance from the start of the actual line segment
620
+                dy_s = current_position[Y_AXIS] - sy,
621
+                dist_start = HYPOT2(dx_s, dy_s),        // We don't need to do a sqrt(), we can compare the distance^2
622
+                                                        // to save computation time
623
+                dx_e = current_position[X_AXIS] - ex,   // find our distance from the end of the actual line segment
624
+                dy_e = current_position[Y_AXIS] - ey,
625
+                dist_end = HYPOT2(dx_e, dy_e),
626
+
627
+                dx = ex - sx,
628
+                dy = ey - sy,
629
+                line_length = HYPOT(dx, dy);
651
 
630
 
652
     // If the end point of the line is closer to the nozzle, we are going to
631
     // If the end point of the line is closer to the nozzle, we are going to
653
     // flip the direction of this line.   We will print it from the end to the start.
632
     // flip the direction of this line.   We will print it from the end to the start.
654
     // On very small lines we don't do the optimization because it just isn't worth it.
633
     // On very small lines we don't do the optimization because it just isn't worth it.
655
     //
634
     //
656
-    if (dist_end < dist_start && (SIZE_OF_INTERSECTION_CIRCLES) < abs(Line_Length)) {
657
-//    if (g26_debug_flag)
658
-//      SERIAL_ECHOLNPGM("  Reversing start and end of print_line_from_here_to_there()");
635
+    if (dist_end < dist_start && (SIZE_OF_INTERSECTION_CIRCLES) < abs(line_length)) {
636
+      //if (g26_debug_flag) SERIAL_ECHOLNPGM("  Reversing start and end of print_line_from_here_to_there()");
659
       print_line_from_here_to_there(ex, ey, ez, sx, sy, sz);
637
       print_line_from_here_to_there(ex, ey, ez, sx, sy, sz);
660
       return;
638
       return;
661
     }
639
     }
664
 
642
 
665
     if (dist_start > 2.0) {
643
     if (dist_start > 2.0) {
666
       retract_filament();
644
       retract_filament();
667
-//    if (g26_debug_flag)
668
-//      SERIAL_ECHOLNPGM("  filament retracted.");
645
+      //if (g26_debug_flag) SERIAL_ECHOLNPGM("  filament retracted.");
669
     }
646
     }
670
-    // If the end point of the line is closer to the nozzle, we are going to
671
     move_to(sx, sy, sz, 0.0); // Get to the starting point with no extrusion
647
     move_to(sx, sy, sz, 0.0); // Get to the starting point with no extrusion
672
 
648
 
673
-    // If the end point of the line is closer to the nozzle, we are going to
674
-
675
-    float e_pos_delta = Line_Length * g26_e_axis_feedrate * extrusion_multiplier;
649
+    const float e_pos_delta = line_length * g26_e_axis_feedrate * extrusion_multiplier;
676
 
650
 
677
     un_retract_filament();
651
     un_retract_filament();
678
 
652
 
679
-    // If the end point of the line is closer to the nozzle, we are going to
680
-//  if (g26_debug_flag) {
681
-//    SERIAL_ECHOLNPGM("  doing printing move.");
682
-//    debug_current_and_destination((char*)"doing final move_to() inside print_line_from_here_to_there()");
683
-//  }
653
+    //if (g26_debug_flag) {
654
+    //  SERIAL_ECHOLNPGM("  doing printing move.");
655
+    //  debug_current_and_destination((char*)"doing final move_to() inside print_line_from_here_to_there()");
656
+    //}
684
     move_to(ex, ey, ez, e_pos_delta);  // Get to the ending point with an appropriate amount of extrusion
657
     move_to(ex, ey, ez, e_pos_delta);  // Get to the ending point with an appropriate amount of extrusion
685
-
686
-    // If the end point of the line is closer to the nozzle, we are going to
687
   }
658
   }
688
 
659
 
689
   /**
660
   /**
820
     return UBL_OK;
791
     return UBL_OK;
821
   }
792
   }
822
 
793
 
794
+  bool exit_from_g26() {
795
+    //strcpy(lcd_status_message, "Leaving G26"); // We can't do lcd_setstatus() without having it continue;
796
+    lcd_reset_alert_level();
797
+    lcd_setstatuspgm(PSTR("Leaving G26"));
798
+    while (ubl_lcd_clicked()) idle();
799
+    return UBL_ERR;
800
+  }
801
+
823
   /**
802
   /**
824
    * Turn on the bed and nozzle heat and
803
    * Turn on the bed and nozzle heat and
825
    * wait for them to get up to temperature.
804
    * wait for them to get up to temperature.
828
     #if HAS_TEMP_BED
807
     #if HAS_TEMP_BED
829
       #if ENABLED(ULTRA_LCD)
808
       #if ENABLED(ULTRA_LCD)
830
         if (bed_temp > 25) {
809
         if (bed_temp > 25) {
831
-          lcd_setstatuspgm(PSTR("G26 Heating Bed."), (uint8_t) 99);
810
+          lcd_setstatuspgm(PSTR("G26 Heating Bed."), 99);
832
           lcd_quick_feedback();
811
           lcd_quick_feedback();
833
       #endif
812
       #endif
834
           ubl_has_control_of_lcd_panel = true;
813
           ubl_has_control_of_lcd_panel = true;
835
           thermalManager.setTargetBed(bed_temp);
814
           thermalManager.setTargetBed(bed_temp);
836
           while (abs(thermalManager.degBed() - bed_temp) > 3) {
815
           while (abs(thermalManager.degBed() - bed_temp) > 3) {
837
-            if (ubl_lcd_clicked()) {
838
-              strcpy(lcd_status_message, "Leaving G26");      // We can't do lcd_setstatus() without having it continue;
839
-              lcd_setstatuspgm(PSTR("Leaving G26"), (uint8_t) 99);      // Now we do it right.
840
-              while (ubl_lcd_clicked())                       // Debounce Encoder Wheel 
841
-                idle();
842
-              return UBL_ERR;
843
-            }
816
+            if (ubl_lcd_clicked()) return exit_from_g26();
844
             idle();
817
             idle();
845
           }
818
           }
846
       #if ENABLED(ULTRA_LCD)
819
       #if ENABLED(ULTRA_LCD)
847
         }
820
         }
848
-        lcd_setstatuspgm(PSTR("G26 Heating Nozzle."), (uint8_t) 99);
821
+        lcd_setstatuspgm(PSTR("G26 Heating Nozzle."), 99);
849
         lcd_quick_feedback();
822
         lcd_quick_feedback();
850
       #endif
823
       #endif
851
     #endif
824
     #endif
853
     // Start heating the nozzle and wait for it to reach temperature.
826
     // Start heating the nozzle and wait for it to reach temperature.
854
     thermalManager.setTargetHotend(hotend_temp, 0);
827
     thermalManager.setTargetHotend(hotend_temp, 0);
855
     while (abs(thermalManager.degHotend(0) - hotend_temp) > 3) {
828
     while (abs(thermalManager.degHotend(0) - hotend_temp) > 3) {
856
-      if (ubl_lcd_clicked()) {
857
-        strcpy(lcd_status_message, "Leaving G26");          // We can't do lcd_setstatuspgm() without having it continue;
858
-        lcd_setstatuspgm(PSTR("Leaving G26"), (uint8_t) 99);          // Now we do it right.
859
-        while (ubl_lcd_clicked())                           // Debounce Encoder Wheel 
860
-          idle();
861
-        return UBL_ERR;
862
-      }
829
+      if (ubl_lcd_clicked()) return exit_from_g26();
863
       idle();
830
       idle();
864
     }
831
     }
865
 
832
 
866
     #if ENABLED(ULTRA_LCD)
833
     #if ENABLED(ULTRA_LCD)
867
-      lcd_setstatuspgm(PSTR(""), (uint8_t) 99);
834
+      lcd_reset_alert_level();
835
+      lcd_setstatuspgm(PSTR(""));
868
       lcd_quick_feedback();
836
       lcd_quick_feedback();
869
     #endif
837
     #endif
870
     return UBL_OK;
838
     return UBL_OK;
877
     float Total_Prime = 0.0;
845
     float Total_Prime = 0.0;
878
 
846
 
879
     if (prime_flag == -1) {  // The user wants to control how much filament gets purged
847
     if (prime_flag == -1) {  // The user wants to control how much filament gets purged
880
-      lcd_setstatuspgm(PSTR("User-Controlled Prime"), (uint8_t) 99);
848
+      lcd_setstatuspgm(PSTR("User-Controlled Prime"), 99);
881
       chirp_at_user();
849
       chirp_at_user();
882
 
850
 
883
       set_destination_to_current();
851
       set_destination_to_current();
894
         #endif
862
         #endif
895
         ubl_line_to_destination(
863
         ubl_line_to_destination(
896
           destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS],
864
           destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS],
897
-          //planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0, 0xFFFF, 0xFFFF);
898
           planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0
865
           planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0
899
         );
866
         );
900
 
867
 
906
         idle();
873
         idle();
907
       }
874
       }
908
 
875
 
909
-      strcpy(lcd_status_message, "Done Priming"); // We can't do lcd_setstatuspgm() without having it continue;
910
-                                                  // So...  We cheat to get a message up.
911
-      while (ubl_lcd_clicked())                   // Debounce Encoder Wheel 
912
-        idle();
876
+      while (ubl_lcd_clicked()) idle();           // Debounce Encoder Wheel
913
 
877
 
914
       #if ENABLED(ULTRA_LCD)
878
       #if ENABLED(ULTRA_LCD)
915
-        lcd_setstatuspgm(PSTR("Done Priming"), (uint8_t) 99); 
879
+        strcpy_P(lcd_status_message, PSTR("Done Priming")); // We can't do lcd_setstatuspgm() without having it continue;
880
+                                                            // So...  We cheat to get a message up.
881
+        lcd_setstatuspgm(PSTR("Done Priming"), 99);
916
         lcd_quick_feedback();
882
         lcd_quick_feedback();
917
       #endif
883
       #endif
918
     }
884
     }
919
     else {
885
     else {
920
       #if ENABLED(ULTRA_LCD)
886
       #if ENABLED(ULTRA_LCD)
921
-        lcd_setstatuspgm(PSTR("Fixed Length Prime."), (uint8_t) 99);
887
+        lcd_setstatuspgm(PSTR("Fixed Length Prime."), 99);
922
         lcd_quick_feedback();
888
         lcd_quick_feedback();
923
       #endif
889
       #endif
924
       set_destination_to_current();
890
       set_destination_to_current();
925
       destination[E_AXIS] += prime_length;
891
       destination[E_AXIS] += prime_length;
926
       ubl_line_to_destination(
892
       ubl_line_to_destination(
927
         destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS],
893
         destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS],
928
-        //planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0, 0xFFFF, 0xFFFF);
929
         planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0
894
         planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0
930
       );
895
       );
931
       stepper.synchronize();
896
       stepper.synchronize();

+ 10
- 20
Marlin/M100_Free_Mem_Chk.cpp View File

76
       // We want to start and end the dump on a nice 16 byte boundry even though
76
       // We want to start and end the dump on a nice 16 byte boundry even though
77
       // the values we are using are not 16 byte aligned.
77
       // the values we are using are not 16 byte aligned.
78
       //
78
       //
79
-      SERIAL_ECHOPGM("\nbss_end : ");
80
-      prt_hex_word((unsigned int) ptr);
81
-      ptr = (char*)((unsigned long) ptr & 0xfff0);
79
+      SERIAL_ECHOPAIR("\nbss_end : ", hex_word((uint16_t)ptr));
80
+      ptr = (char*)((uint32_t)ptr & 0xfff0);
82
       sp = top_of_stack();
81
       sp = top_of_stack();
83
-      SERIAL_ECHOPGM("\nStack Pointer : ");
84
-      prt_hex_word((unsigned int) sp);
85
-      SERIAL_EOL;
86
-      sp = (char*)((unsigned long) sp | 0x000f);
82
+      SERIAL_ECHOLNPAIR("\nStack Pointer : ", hex_word((uint16_t)sp));
83
+      sp = (char*)((uint32_t)sp | 0x000f);
87
       n = sp - ptr;
84
       n = sp - ptr;
88
       //
85
       //
89
       // This is the main loop of the Dump command.
86
       // This is the main loop of the Dump command.
90
       //
87
       //
91
       while (ptr < sp) {
88
       while (ptr < sp) {
92
-        prt_hex_word((unsigned int) ptr); // Print the address
89
+        print_hex_word((uint16_t)ptr); // Print the address
93
         SERIAL_CHAR(':');
90
         SERIAL_CHAR(':');
94
         for (i = 0; i < 16; i++) {      // and 16 data bytes
91
         for (i = 0; i < 16; i++) {      // and 16 data bytes
95
-          prt_hex_byte(*(ptr + i));
92
+          print_hex_byte(*(ptr + i));
96
           SERIAL_CHAR(' ');
93
           SERIAL_CHAR(' ');
97
         }
94
         }
98
         SERIAL_CHAR('|');         // now show where non 0xE5's are
95
         SERIAL_CHAR('|');         // now show where non 0xE5's are
99
-        for (i = 0; i < 16; i++) {
100
-          if (*(ptr + i) == (char)0xe5)
101
-            SERIAL_CHAR(' ');
102
-          else
103
-            SERIAL_CHAR('?');
104
-        }
96
+        for (i = 0; i < 16; i++)
97
+          SERIAL_CHAR((*(ptr + i) == (char)0xe5) ? ' ' : '?');
105
         SERIAL_EOL;
98
         SERIAL_EOL;
106
         ptr += 16;
99
         ptr += 16;
107
       }
100
       }
127
         j = how_many_E5s_are_here(ptr + i);
120
         j = how_many_E5s_are_here(ptr + i);
128
         if (j > 8) {
121
         if (j > 8) {
129
           SERIAL_ECHOPAIR("Found ", j);
122
           SERIAL_ECHOPAIR("Found ", j);
130
-          SERIAL_ECHOPGM(" bytes free at 0x");
131
-          prt_hex_word((int) ptr + i);
132
-          SERIAL_EOL;
123
+          SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)(ptr + i)));
133
           i += j;
124
           i += j;
134
           block_cnt++;
125
           block_cnt++;
135
         }
126
         }
164
       j = n / (x + 1);
155
       j = n / (x + 1);
165
       for (i = 1; i <= x; i++) {
156
       for (i = 1; i <= x; i++) {
166
         *(ptr + (i * j)) = i;
157
         *(ptr + (i * j)) = i;
167
-        SERIAL_ECHOPGM("\nCorrupting address: 0x");
168
-        prt_hex_word((unsigned int)(ptr + (i * j)));
158
+        SERIAL_ECHOPAIR("\nCorrupting address: 0x", hex_word((uint16_t)(ptr + i * j)));
169
       }
159
       }
170
       SERIAL_ECHOLNPGM("\n");
160
       SERIAL_ECHOLNPGM("\n");
171
       return;
161
       return;

+ 4
- 1
Marlin/Marlin.h View File

363
 #endif
363
 #endif
364
 
364
 
365
 #if ENABLED(HOST_KEEPALIVE_FEATURE)
365
 #if ENABLED(HOST_KEEPALIVE_FEATURE)
366
-  extern uint8_t host_keepalive_interval;
366
+  extern MarlinBusyState busy_state;
367
+  #define KEEPALIVE_STATE(n) do{ busy_state = n; }while(0)
368
+#else
369
+  #define KEEPALIVE_STATE(n) NOOP
367
 #endif
370
 #endif
368
 
371
 
369
 #if FAN_COUNT > 0
372
 #if FAN_COUNT > 0

+ 22
- 22
Marlin/Marlin_main.cpp View File

655
   static MarlinBusyState busy_state = NOT_BUSY;
655
   static MarlinBusyState busy_state = NOT_BUSY;
656
   static millis_t next_busy_signal_ms = 0;
656
   static millis_t next_busy_signal_ms = 0;
657
   uint8_t host_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
657
   uint8_t host_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
658
-  #define KEEPALIVE_STATE(n) do{ busy_state = n; }while(0)
659
 #else
658
 #else
660
-  #define host_keepalive() ;
661
-  #define KEEPALIVE_STATE(n) ;
662
-#endif // HOST_KEEPALIVE_FEATURE
659
+  #define host_keepalive() NOOP
660
+#endif
663
 
661
 
664
 #define DEFINE_PGM_READ_ANY(type, reader)       \
662
 #define DEFINE_PGM_READ_ANY(type, reader)       \
665
   static inline type pgm_read_any(const type *p)  \
663
   static inline type pgm_read_any(const type *p)  \
1031
   // send "wait" to indicate Marlin is still waiting.
1029
   // send "wait" to indicate Marlin is still waiting.
1032
   #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
1030
   #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
1033
     static millis_t last_command_time = 0;
1031
     static millis_t last_command_time = 0;
1034
-    millis_t ms = millis();
1032
+    const millis_t ms = millis();
1035
     if (commands_in_queue == 0 && !MYSERIAL.available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
1033
     if (commands_in_queue == 0 && !MYSERIAL.available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
1036
       SERIAL_ECHOLNPGM(MSG_WAIT);
1034
       SERIAL_ECHOLNPGM(MSG_WAIT);
1037
       last_command_time = ms;
1035
       last_command_time = ms;
4710
 
4708
 
4711
     #endif
4709
     #endif
4712
 
4710
 
4713
-    wait_for_user = true;
4714
     KEEPALIVE_STATE(PAUSED_FOR_USER);
4711
     KEEPALIVE_STATE(PAUSED_FOR_USER);
4712
+    wait_for_user = true;
4715
 
4713
 
4716
     stepper.synchronize();
4714
     stepper.synchronize();
4717
     refresh_cmd_timeout();
4715
     refresh_cmd_timeout();
5050
       if (first_pin > NUM_DIGITAL_PINS - 1) return;
5048
       if (first_pin > NUM_DIGITAL_PINS - 1) return;
5051
     }
5049
     }
5052
 
5050
 
5053
-    bool ignore_protection = code_seen('I') ? code_value_bool() : false;
5051
+    const bool ignore_protection = code_seen('I') ? code_value_bool() : false;
5054
 
5052
 
5055
     // Watch until click, M108, or reset
5053
     // Watch until click, M108, or reset
5056
     if (code_seen('W') && code_value_bool()) { // watch digital pins
5054
     if (code_seen('W') && code_value_bool()) { // watch digital pins
6324
 
6322
 
6325
     #if DISABLED(SDSUPPORT)
6323
     #if DISABLED(SDSUPPORT)
6326
       // Wait for lcd click or M108
6324
       // Wait for lcd click or M108
6327
-      wait_for_user = true;
6328
       KEEPALIVE_STATE(PAUSED_FOR_USER);
6325
       KEEPALIVE_STATE(PAUSED_FOR_USER);
6326
+      wait_for_user = true;
6329
       while (wait_for_user) idle();
6327
       while (wait_for_user) idle();
6330
       KEEPALIVE_STATE(IN_HANDLER);
6328
       KEEPALIVE_STATE(IN_HANDLER);
6331
 
6329
 
7591
     disable_e_steppers();
7589
     disable_e_steppers();
7592
     safe_delay(100);
7590
     safe_delay(100);
7593
 
7591
 
7594
-    millis_t nozzle_timeout = millis() + (millis_t)(FILAMENT_CHANGE_NOZZLE_TIMEOUT) * 1000L;
7592
+    const millis_t nozzle_timeout = millis() + (millis_t)(FILAMENT_CHANGE_NOZZLE_TIMEOUT) * 1000UL;
7595
     bool nozzle_timed_out = false;
7593
     bool nozzle_timed_out = false;
7596
     float temps[4];
7594
     float temps[4];
7597
 
7595
 
7606
 
7604
 
7607
     HOTEND_LOOP() temps[e] = thermalManager.target_temperature[e]; // Save nozzle temps
7605
     HOTEND_LOOP() temps[e] = thermalManager.target_temperature[e]; // Save nozzle temps
7608
 
7606
 
7607
+    KEEPALIVE_STATE(PAUSED_FOR_USER);
7609
     wait_for_user = true;    // LCD click or M108 will clear this
7608
     wait_for_user = true;    // LCD click or M108 will clear this
7610
     while (wait_for_user) {
7609
     while (wait_for_user) {
7611
-      millis_t current_ms = millis();
7610
+
7612
       if (nozzle_timed_out)
7611
       if (nozzle_timed_out)
7613
         lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_CLICK_TO_HEAT_NOZZLE);
7612
         lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_CLICK_TO_HEAT_NOZZLE);
7614
 
7613
 
7616
         filament_change_beep();
7615
         filament_change_beep();
7617
       #endif
7616
       #endif
7618
 
7617
 
7619
-      if (current_ms >= nozzle_timeout) {
7620
-        if (!nozzle_timed_out) {
7621
-          nozzle_timed_out = true; // on nozzle timeout remember the nozzles need to be reheated
7622
-          HOTEND_LOOP() thermalManager.setTargetHotend(0, e); // Turn off all the nozzles
7623
-          lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_CLICK_TO_HEAT_NOZZLE);
7624
-        }
7618
+      if (!nozzle_timed_out && ELAPSED(millis(), nozzle_timeout)) {
7619
+        nozzle_timed_out = true; // on nozzle timeout remember the nozzles need to be reheated
7620
+        HOTEND_LOOP() thermalManager.setTargetHotend(0, e); // Turn off all the nozzles
7621
+        lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_CLICK_TO_HEAT_NOZZLE);
7625
       }
7622
       }
7626
       idle(true);
7623
       idle(true);
7627
     }
7624
     }
7625
+    KEEPALIVE_STATE(IN_HANDLER);
7628
 
7626
 
7629
     if (nozzle_timed_out)      // Turn nozzles back on if they were turned off
7627
     if (nozzle_timed_out)      // Turn nozzles back on if they were turned off
7630
       HOTEND_LOOP() thermalManager.setTargetHotend(temps[e], e);
7628
       HOTEND_LOOP() thermalManager.setTargetHotend(temps[e], e);
7652
       filament_change_beep(true);
7650
       filament_change_beep(true);
7653
     #endif
7651
     #endif
7654
 
7652
 
7653
+    KEEPALIVE_STATE(PAUSED_FOR_USER);
7655
     wait_for_user = true;    // LCD click or M108 will clear this
7654
     wait_for_user = true;    // LCD click or M108 will clear this
7656
     while (wait_for_user && nozzle_timed_out) {
7655
     while (wait_for_user && nozzle_timed_out) {
7657
       #if HAS_BUZZER
7656
       #if HAS_BUZZER
7659
       #endif
7658
       #endif
7660
       idle(true);
7659
       idle(true);
7661
     }
7660
     }
7661
+    KEEPALIVE_STATE(IN_HANDLER);
7662
 
7662
 
7663
     // Show "load" message
7663
     // Show "load" message
7664
     lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_LOAD);
7664
     lcd_filament_change_show_message(FILAMENT_CHANGE_MESSAGE_LOAD);
10137
 #if HAS_CONTROLLERFAN
10137
 #if HAS_CONTROLLERFAN
10138
 
10138
 
10139
   void controllerFan() {
10139
   void controllerFan() {
10140
-    static millis_t lastMotorOn = 0; // Last time a motor was turned on
10141
-    static millis_t nextMotorCheck = 0; // Last time the state was checked
10142
-    millis_t ms = millis();
10140
+    static millis_t lastMotorOn = 0, // Last time a motor was turned on
10141
+                    nextMotorCheck = 0; // Last time the state was checked
10142
+    const millis_t ms = millis();
10143
     if (ELAPSED(ms, nextMotorCheck)) {
10143
     if (ELAPSED(ms, nextMotorCheck)) {
10144
       nextMotorCheck = ms + 2500UL; // Not a time critical function, so only check every 2.5s
10144
       nextMotorCheck = ms + 2500UL; // Not a time critical function, so only check every 2.5s
10145
       if (X_ENABLE_READ == X_ENABLE_ON || Y_ENABLE_READ == Y_ENABLE_ON || Z_ENABLE_READ == Z_ENABLE_ON || thermalManager.soft_pwm_bed > 0
10145
       if (X_ENABLE_READ == X_ENABLE_ON || Y_ENABLE_READ == Y_ENABLE_ON || Z_ENABLE_READ == Z_ENABLE_ON || thermalManager.soft_pwm_bed > 0
10472
 
10472
 
10473
   if (commands_in_queue < BUFSIZE) get_available_commands();
10473
   if (commands_in_queue < BUFSIZE) get_available_commands();
10474
 
10474
 
10475
-  millis_t ms = millis();
10475
+  const millis_t ms = millis();
10476
 
10476
 
10477
   if (max_inactive_time && ELAPSED(ms, previous_cmd_ms + max_inactive_time)) {
10477
   if (max_inactive_time && ELAPSED(ms, previous_cmd_ms + max_inactive_time)) {
10478
     SERIAL_ERROR_START;
10478
     SERIAL_ERROR_START;
10686
 
10686
 
10687
   thermalManager.disable_all_heaters();
10687
   thermalManager.disable_all_heaters();
10688
   disable_all_steppers();
10688
   disable_all_steppers();
10689
-            
10689
+
10690
   #if ENABLED(ULTRA_LCD)
10690
   #if ENABLED(ULTRA_LCD)
10691
     kill_screen(lcd_msg);
10691
     kill_screen(lcd_msg);
10692
   #else
10692
   #else
10695
 
10695
 
10696
   _delay_ms(250); // Wait a short time
10696
   _delay_ms(250); // Wait a short time
10697
   cli(); // Stop interrupts
10697
   cli(); // Stop interrupts
10698
-            
10698
+
10699
   _delay_ms(250); //Wait to ensure all interrupts routines stopped
10699
   _delay_ms(250); //Wait to ensure all interrupts routines stopped
10700
   thermalManager.disable_all_heaters(); //turn off heaters again
10700
   thermalManager.disable_all_heaters(); //turn off heaters again
10701
 
10701
 

+ 68
- 104
Marlin/UBL_Bed_Leveling.cpp View File

38
   void bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { SBI(bits[y], x); }
38
   void bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { SBI(bits[y], x); }
39
   bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { return TEST(bits[y], x); }
39
   bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { return TEST(bits[y], x); }
40
 
40
 
41
+  static void serial_echo_xy(const uint16_t x, const uint16_t y) {
42
+    SERIAL_CHAR('(');
43
+    SERIAL_ECHO(x);
44
+    SERIAL_CHAR(',');
45
+    SERIAL_ECHO(y);
46
+    SERIAL_CHAR(')');
47
+    safe_delay(10);
48
+  }
49
+
50
+  static void serial_echo_10x_spaces() {
51
+    for (uint8_t i = UBL_MESH_NUM_X_POINTS - 1; --i;) {
52
+      SERIAL_ECHOPGM("          ");
53
+      #if TX_BUFFER_SIZE > 0
54
+        MYSERIAL.flushTX();
55
+      #endif
56
+      safe_delay(10);
57
+    }
58
+  }
59
+
41
   /**
60
   /**
42
    * These variables used to be declared inside the unified_bed_leveling class. We are going to
61
    * These variables used to be declared inside the unified_bed_leveling class. We are going to
43
    * still declare them within the .cpp file for bed leveling. But there is only one instance of
62
    * still declare them within the .cpp file for bed leveling. But there is only one instance of
105
     }
124
     }
106
 
125
 
107
     j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
126
     j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
108
-    eeprom_read_block((void *)&z_values , (void *)j, sizeof(z_values));
127
+    eeprom_read_block((void *)&z_values, (void *)j, sizeof(z_values));
109
 
128
 
110
-    SERIAL_PROTOCOLPGM("Mesh loaded from slot ");
111
-    SERIAL_PROTOCOL(m);
112
-    SERIAL_PROTOCOLPGM("  at offset 0x");
113
-    prt_hex_word(j);
114
-    SERIAL_EOL;
129
+    SERIAL_PROTOCOLPAIR("Mesh loaded from slot ", m);
130
+    SERIAL_PROTOCOLLNPAIR(" at offset 0x", hex_word(j));
115
   }
131
   }
116
 
132
 
117
   void unified_bed_leveling::store_mesh(const int16_t m) {
133
   void unified_bed_leveling::store_mesh(const int16_t m) {
132
     j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
148
     j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
133
     eeprom_write_block((const void *)&z_values, (void *)j, sizeof(z_values));
149
     eeprom_write_block((const void *)&z_values, (void *)j, sizeof(z_values));
134
 
150
 
135
-    SERIAL_PROTOCOLPGM("Mesh saved in slot ");
136
-    SERIAL_PROTOCOL(m);
137
-    SERIAL_PROTOCOLPGM("  at offset 0x");
138
-    prt_hex_word(j);
139
-    SERIAL_EOL;
151
+    SERIAL_PROTOCOLPAIR("Mesh saved in slot ", m);
152
+    SERIAL_PROTOCOLLNPAIR(" at offset 0x", hex_word(j));
140
   }
153
   }
141
 
154
 
142
   void unified_bed_leveling::reset() {
155
   void unified_bed_leveling::reset() {
151
   }
164
   }
152
 
165
 
153
   void unified_bed_leveling::invalidate() {
166
   void unified_bed_leveling::invalidate() {
154
-    prt_hex_word((unsigned int)this);
167
+    print_hex_word((uint16_t)this);
155
     SERIAL_EOL;
168
     SERIAL_EOL;
156
 
169
 
157
     state.active = false;
170
     state.active = false;
162
   }
175
   }
163
 
176
 
164
   void unified_bed_leveling::display_map(const int map_type) {
177
   void unified_bed_leveling::display_map(const int map_type) {
165
-    float f, current_xi, current_yi;
166
-    int8_t i, j;
167
-    UNUSED(map_type);
168
 
178
 
169
-    if (map_type==0) {
170
-      SERIAL_PROTOCOLLNPGM("\nBed Topography Report:\n");
179
+    const bool map0 = map_type == 0;
171
 
180
 
172
-      SERIAL_ECHOPAIR("(", 0);
173
-      SERIAL_ECHOPAIR(", ", UBL_MESH_NUM_Y_POINTS - 1);
174
-      SERIAL_ECHOPGM(")    ");
181
+    if (map0) {
182
+      SERIAL_PROTOCOLLNPGM("\nBed Topography Report:\n");
183
+      serial_echo_xy(0, UBL_MESH_NUM_Y_POINTS - 1);
184
+      SERIAL_ECHOPGM("    ");
175
     }
185
     }
176
 
186
 
177
-    current_xi = ubl.get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0);
178
-    current_yi = ubl.get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
179
-
180
-    if (map_type==0) {
181
-      for (i = 0; i < UBL_MESH_NUM_X_POINTS - 1; i++) {
182
-        SERIAL_ECHOPGM("            ");
183
-        #if TX_BUFFER_SIZE>0
184
-          MYSERIAL.flushTX();
185
-        #endif
186
-        safe_delay(15);
187
-      }
188
-      
189
-      SERIAL_ECHOPAIR("(", UBL_MESH_NUM_X_POINTS - 1);
190
-      SERIAL_ECHOPAIR(",", UBL_MESH_NUM_Y_POINTS - 1);
191
-      SERIAL_ECHOLNPGM(")");
192
-
193
-      SERIAL_ECHOPAIR("(", UBL_MESH_MIN_X);
194
-      SERIAL_ECHOPAIR(",", UBL_MESH_MAX_Y);
195
-      SERIAL_CHAR(')');
196
-      safe_delay(15);
197
-
198
-      for (i = 0; i < UBL_MESH_NUM_X_POINTS - 1; i++) {
199
-        SERIAL_ECHOPGM("            ");
200
-        #if TX_BUFFER_SIZE>0
201
-          MYSERIAL.flushTX();
202
-        #endif
203
-        safe_delay(15);
204
-      }
205
-
206
-      SERIAL_ECHOPAIR("(", UBL_MESH_MAX_X);
207
-      SERIAL_ECHOPAIR(",", UBL_MESH_MAX_Y);
208
-      SERIAL_ECHOLNPGM(")");
209
-      safe_delay(15);
187
+    if (map0) {
188
+      serial_echo_10x_spaces();
189
+      serial_echo_xy(UBL_MESH_NUM_X_POINTS - 1, UBL_MESH_NUM_Y_POINTS - 1);
190
+      SERIAL_EOL;
191
+      serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MIN_Y);
192
+      serial_echo_10x_spaces();
193
+      serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MAX_Y);
194
+      SERIAL_EOL;
210
     }
195
     }
211
 
196
 
212
-    for (j = UBL_MESH_NUM_Y_POINTS - 1; j >= 0; j--) {
213
-      for (i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
214
-        f = z_values[i][j];
197
+    const float current_xi = ubl.get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0),
198
+                current_yi = ubl.get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
199
+
200
+    for (uint8_t j = UBL_MESH_NUM_Y_POINTS - 1; j >= 0; j--) {
201
+      for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
202
+        const bool is_current = i == current_xi && j == current_yi;
215
 
203
 
216
         // is the nozzle here?  if so, mark the number
204
         // is the nozzle here?  if so, mark the number
217
-        if (map_type==0) 
218
-          SERIAL_CHAR(i == current_xi && j == current_yi ? '[' : ' ');
219
-
220
-        if (isnan(f))
221
-          if (map_type==0) {
222
-            SERIAL_PROTOCOLPGM("    .    ");
223
-          } else 
224
-            SERIAL_PROTOCOLPGM("NAN");
205
+        if (map0)
206
+          SERIAL_CHAR(is_current ? '[' : ' ');
207
+
208
+        const float f = z_values[i][j];
209
+        if (isnan(f)) {
210
+          serialprintPGM(map0 ? PSTR("    .    ") : PSTR("NAN"));
211
+        }
225
         else {
212
         else {
226
           // if we don't do this, the columns won't line up nicely
213
           // if we don't do this, the columns won't line up nicely
227
-          if (f>=0.0 && map_type==0) SERIAL_CHAR(' ');
214
+          if (f >= 0.0 && map0) SERIAL_CHAR(' ');
228
           SERIAL_PROTOCOL_F(f, 3);
215
           SERIAL_PROTOCOL_F(f, 3);
229
           idle();
216
           idle();
230
         }
217
         }
231
-        if (map_type!=0 && i<UBL_MESH_NUM_X_POINTS-1) 
232
-         SERIAL_PROTOCOLPGM(",");
218
+        if (!map0 && i < UBL_MESH_NUM_X_POINTS - 1)
219
+         SERIAL_CHAR(',');
233
 
220
 
234
-        #if TX_BUFFER_SIZE>0
221
+        #if TX_BUFFER_SIZE > 0
235
           MYSERIAL.flushTX();
222
           MYSERIAL.flushTX();
236
         #endif
223
         #endif
237
         safe_delay(15);
224
         safe_delay(15);
238
-        if (map_type==0) {
239
-          if (i == current_xi && j == current_yi) // is the nozzle here? if so, finish marking the number
240
-            SERIAL_CHAR(']');
241
-          else
242
-            SERIAL_PROTOCOL("  ");
225
+        if (map0) {
226
+          SERIAL_CHAR(is_current ? ']' : ' ');
243
           SERIAL_CHAR(' ');
227
           SERIAL_CHAR(' ');
244
         }
228
         }
245
       }
229
       }
246
       SERIAL_EOL;
230
       SERIAL_EOL;
247
-      if (j && map_type==0) { // we want the (0,0) up tight against the block of numbers
231
+      if (j && map0) { // we want the (0,0) up tight against the block of numbers
248
         SERIAL_CHAR(' ');
232
         SERIAL_CHAR(' ');
249
         SERIAL_EOL;
233
         SERIAL_EOL;
250
       }
234
       }
251
     }
235
     }
252
 
236
 
253
-    if (map_type==0) {
254
-      SERIAL_ECHOPAIR("(", int(UBL_MESH_MIN_X));
255
-      SERIAL_ECHOPAIR(",", int(UBL_MESH_MIN_Y));
256
-      SERIAL_ECHOPGM(")    ");
257
-
258
-      for (i = 0; i < UBL_MESH_NUM_X_POINTS - 1; i++)  {
259
-        SERIAL_ECHOPGM("            ");
260
-        #if TX_BUFFER_SIZE>0
261
-          MYSERIAL.flushTX();
262
-        #endif
263
-        safe_delay(15);
264
-      }
265
-      SERIAL_ECHOPAIR("(", int(UBL_MESH_MAX_X));
266
-      SERIAL_ECHOPAIR(",", int(UBL_MESH_MIN_Y));
267
-      SERIAL_CHAR(')');
237
+    if (map0) {
238
+      serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MIN_Y);
239
+      SERIAL_ECHOPGM("    ");
240
+      serial_echo_10x_spaces();
241
+      serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MIN_Y);
242
+      SERIAL_EOL;
243
+      serial_echo_xy(0, 0);
244
+      SERIAL_ECHOPGM("       ");
245
+      serial_echo_10x_spaces();
246
+      serial_echo_xy(UBL_MESH_NUM_X_POINTS - 1, 0);
268
       SERIAL_EOL;
247
       SERIAL_EOL;
269
-
270
-      SERIAL_ECHOPAIR("(", 0);
271
-      SERIAL_ECHOPAIR(",", 0);
272
-      SERIAL_ECHOPGM(")       ");
273
-
274
-      for (i = 0; i < UBL_MESH_NUM_X_POINTS - 1; i++) {
275
-        SERIAL_ECHOPGM("            ");
276
-        #if TX_BUFFER_SIZE>0
277
-          MYSERIAL.flushTX();
278
-        #endif
279
-        safe_delay(15);
280
-      }
281
-      SERIAL_ECHOPAIR("(", UBL_MESH_NUM_X_POINTS-1);
282
-      SERIAL_ECHOPAIR(",", 0);
283
-      SERIAL_ECHOLNPGM(")");
284
     }
248
     }
285
   }
249
   }
286
 
250
 

+ 203
- 207
Marlin/UBL_G29.cpp View File

158
    *                    only done between probe points. You will need to press and hold the switch until the
158
    *                    only done between probe points. You will need to press and hold the switch until the
159
    *                    Phase 1 command can detect it.)
159
    *                    Phase 1 command can detect it.)
160
    *
160
    *
161
-   *   P2    Phase 2    Probe areas of the Mesh that can not be automatically handled. Phase 2 respects an H
161
+   *   P2    Phase 2    Probe areas of the Mesh that can't be automatically handled. Phase 2 respects an H
162
    *                    parameter to control the height between Mesh points. The default height for movement
162
    *                    parameter to control the height between Mesh points. The default height for movement
163
    *                    between Mesh points is 5mm. A smaller number can be used to make this part of the
163
    *                    between Mesh points is 5mm. A smaller number can be used to make this part of the
164
    *                    calibration less time consuming. You will be running the nozzle down until it just barely
164
    *                    calibration less time consuming. You will be running the nozzle down until it just barely
303
   volatile int8_t ubl_encoderDiff = 0; // Volatile because it's changed by Temperature ISR button update
303
   volatile int8_t ubl_encoderDiff = 0; // Volatile because it's changed by Temperature ISR button update
304
 
304
 
305
   // The simple parameter flags and values are 'static' so parameter parsing can be in a support routine.
305
   // The simple parameter flags and values are 'static' so parameter parsing can be in a support routine.
306
-  static int g29_verbose_level = 0, phase_value = -1, repetition_cnt = 1,
307
-             storage_slot = 0, map_type = 0, test_pattern = 0, unlevel_value = -1;
308
-  static bool repeat_flag = UBL_OK, c_flag = false, x_flag = UBL_OK, y_flag = UBL_OK, statistics_flag = UBL_OK, business_card_mode = false;
309
-  static float x_pos = 0.0, y_pos = 0.0, height_value = 5.0, measured_z, card_thickness = 0.0, constant = 0.0;
306
+  static int g29_verbose_level, phase_value = -1, repetition_cnt,
307
+             storage_slot = 0, map_type; //unlevel_value = -1;
308
+  static bool repeat_flag, c_flag, x_flag, y_flag;
309
+  static float x_pos, y_pos, measured_z, card_thickness = 0.0, ubl_constant = 0.0;
310
 
310
 
311
   #if ENABLED(ULTRA_LCD)
311
   #if ENABLED(ULTRA_LCD)
312
     void lcd_setstatus(const char* message, bool persist);
312
     void lcd_setstatus(const char* message, bool persist);
313
   #endif
313
   #endif
314
 
314
 
315
   void gcode_G29() {
315
   void gcode_G29() {
316
-    float Z1, Z2, Z3;
317
-
318
-    g29_verbose_level = 0;  // These may change, but let's get some reasonable values into them.
319
-    repeat_flag       = UBL_OK;
320
-    repetition_cnt    = 1;
321
-    c_flag            = false;
322
-
323
     SERIAL_PROTOCOLLNPAIR("ubl_eeprom_start=", ubl_eeprom_start);
316
     SERIAL_PROTOCOLLNPAIR("ubl_eeprom_start=", ubl_eeprom_start);
324
-
325
     if (ubl_eeprom_start < 0) {
317
     if (ubl_eeprom_start < 0) {
326
       SERIAL_PROTOCOLLNPGM("?You need to enable your EEPROM and initialize it");
318
       SERIAL_PROTOCOLLNPGM("?You need to enable your EEPROM and initialize it");
327
       SERIAL_PROTOCOLLNPGM("with M502, M500, M501 in that order.\n");
319
       SERIAL_PROTOCOLLNPGM("with M502, M500, M501 in that order.\n");
350
 
342
 
351
     if (code_seen('Q')) {
343
     if (code_seen('Q')) {
352
 
344
 
353
-      if (code_has_value()) test_pattern = code_value_int();
354
-
355
-      if (test_pattern < 0 || test_pattern > 4) {
356
-        SERIAL_PROTOCOLLNPGM("Invalid test_pattern value. (0-4)\n");
345
+      const int test_pattern = code_has_value() ? code_value_int() : -1;
346
+      if (test_pattern < 0 || test_pattern > 2) {
347
+        SERIAL_PROTOCOLLNPGM("Invalid test_pattern value. (0-2)\n");
357
         return;
348
         return;
358
       }
349
       }
359
       SERIAL_PROTOCOLLNPGM("Loading test_pattern values.\n");
350
       SERIAL_PROTOCOLLNPGM("Loading test_pattern values.\n");
360
       switch (test_pattern) {
351
       switch (test_pattern) {
361
         case 0:
352
         case 0:
362
-          for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++) {         // Create a bowl shape. This is
363
-            for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++) {       // similar to what a user would see with
364
-              Z1 = 0.5 * (UBL_MESH_NUM_X_POINTS) - x;                   // a poorly calibrated Delta.
365
-              Z2 = 0.5 * (UBL_MESH_NUM_Y_POINTS) - y;
366
-              z_values[x][y] += 2.0 * HYPOT(Z1, Z2);
353
+          for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++) {   // Create a bowl shape - similar to
354
+            for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++) { // a poorly calibrated Delta.
355
+              const float p1 = 0.5 * (UBL_MESH_NUM_X_POINTS) - x,
356
+                          p2 = 0.5 * (UBL_MESH_NUM_Y_POINTS) - y;
357
+              z_values[x][y] += 2.0 * HYPOT(p1, p2);
367
             }
358
             }
368
           }
359
           }
369
-        break;
360
+          break;
370
         case 1:
361
         case 1:
371
-          for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++) {  // Create a diagonal line several Mesh
372
-            z_values[x][x] += 9.999;                             // cells thick that is raised
373
-            if (x < UBL_MESH_NUM_Y_POINTS - 1)
374
-              z_values[x][x + 1] += 9.999;                       // We want the altered line several mesh points thick
375
-            if (x > 0)
376
-              z_values[x][x - 1] += 9.999;                       // We want the altered line several mesh points thick
362
+          for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++) {  // Create a diagonal line several Mesh cells thick that is raised
363
+            z_values[x][x] += 9.999;
364
+            z_values[x][x + (x < UBL_MESH_NUM_Y_POINTS - 1) ? 1 : -1] += 9.999; // We want the altered line several mesh points thick
377
           }
365
           }
378
           break;
366
           break;
379
         case 2:
367
         case 2:
380
-          // Allow the user to specify the height because 10mm is
381
-          // a little bit extreme in some cases.
368
+          // Allow the user to specify the height because 10mm is a little extreme in some cases.
382
           for (uint8_t x = (UBL_MESH_NUM_X_POINTS) / 3; x < 2 * (UBL_MESH_NUM_X_POINTS) / 3; x++)   // Create a rectangular raised area in
369
           for (uint8_t x = (UBL_MESH_NUM_X_POINTS) / 3; x < 2 * (UBL_MESH_NUM_X_POINTS) / 3; x++)   // Create a rectangular raised area in
383
             for (uint8_t y = (UBL_MESH_NUM_Y_POINTS) / 3; y < 2 * (UBL_MESH_NUM_Y_POINTS) / 3; y++) // the center of the bed
370
             for (uint8_t y = (UBL_MESH_NUM_Y_POINTS) / 3; y < 2 * (UBL_MESH_NUM_Y_POINTS) / 3; y++) // the center of the bed
384
-              z_values[x][y] += code_seen('C') ? constant : 9.99;
385
-          break;
386
-        case 3:
371
+              z_values[x][y] += code_seen('C') ? ubl_constant : 9.99;
387
           break;
372
           break;
388
       }
373
       }
389
     }
374
     }
390
 
375
 
391
-/*
376
+    /*
392
     if (code_seen('U')) {
377
     if (code_seen('U')) {
393
       unlevel_value = code_value_int();
378
       unlevel_value = code_value_int();
394
-//    if (unlevel_value < 0 || unlevel_value > 7) {
395
-//      SERIAL_PROTOCOLLNPGM("Invalid Unlevel value. (0-4)\n");
396
-//      return;
397
-//    }
379
+      //if (unlevel_value < 0 || unlevel_value > 7) {
380
+      //  SERIAL_PROTOCOLLNPGM("Invalid Unlevel value. (0-4)\n");
381
+      //  return;
382
+      //}
398
     }
383
     }
399
-*/
384
+    //*/
400
 
385
 
401
     if (code_seen('P')) {
386
     if (code_seen('P')) {
402
       phase_value = code_value_int();
387
       phase_value = code_value_int();
430
                             code_seen('O') || code_seen('M'), code_seen('E'), code_seen('U'));
415
                             code_seen('O') || code_seen('M'), code_seen('E'), code_seen('U'));
431
           break;
416
           break;
432
         //
417
         //
433
-        // Manually Probe Mesh in areas that can not be reached by the probe
418
+        // Manually Probe Mesh in areas that can't be reached by the probe
434
         //
419
         //
435
-        case 2:
420
+        case 2: {
436
           SERIAL_PROTOCOLLNPGM("Manually probing unreachable mesh locations.\n");
421
           SERIAL_PROTOCOLLNPGM("Manually probing unreachable mesh locations.\n");
437
           do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
422
           do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
438
           if (!x_flag && !y_flag) {      // use a good default location for the path
423
           if (!x_flag && !y_flag) {      // use a good default location for the path
451
             y_pos = current_position[Y_AXIS];
436
             y_pos = current_position[Y_AXIS];
452
           }
437
           }
453
 
438
 
454
-          height_value = code_seen('H') && code_has_value() ? code_value_float() : Z_CLEARANCE_BETWEEN_PROBES;
439
+          const float height = code_seen('H') && code_has_value() ? code_value_float() : Z_CLEARANCE_BETWEEN_PROBES;
455
 
440
 
456
-          if ((business_card_mode = code_seen('B'))) {
457
-            card_thickness = code_has_value() ? code_value_float() : measure_business_card_thickness(height_value);
441
+          if (code_seen('B')) {
442
+            card_thickness = code_has_value() ? code_value_float() : measure_business_card_thickness(height);
458
 
443
 
459
             if (fabs(card_thickness) > 1.5) {
444
             if (fabs(card_thickness) > 1.5) {
460
-              SERIAL_PROTOCOLLNPGM("?Error in Business Card measurment.\n");
445
+              SERIAL_PROTOCOLLNPGM("?Error in Business Card measurement.\n");
461
               return;
446
               return;
462
             }
447
             }
463
           }
448
           }
464
-          manually_probe_remaining_mesh(x_pos, y_pos, height_value, card_thickness, code_seen('O') || code_seen('M'));
465
-          break;
449
+          manually_probe_remaining_mesh(x_pos, y_pos, height, card_thickness, code_seen('O') || code_seen('M'));
450
+
451
+        } break;
452
+
466
         //
453
         //
467
         // Populate invalid Mesh areas with a constant
454
         // Populate invalid Mesh areas with a constant
468
         //
455
         //
469
-        case 3:
470
-          height_value = 0.0; // Assume 0.0 until proven otherwise
471
-          if (code_seen('C')) height_value = constant;
456
+        case 3: {
457
+          const float height = code_seen('C') ? ubl_constant : 0.0;
472
           // If no repetition is specified, do the whole Mesh
458
           // If no repetition is specified, do the whole Mesh
473
           if (!repeat_flag) repetition_cnt = 9999;
459
           if (!repeat_flag) repetition_cnt = 9999;
474
           while (repetition_cnt--) {
460
           while (repetition_cnt--) {
475
             const mesh_index_pair location = find_closest_mesh_point_of_type(INVALID, x_pos, y_pos, 0, NULL, false); // The '0' says we want to use the nozzle's position
461
             const mesh_index_pair location = find_closest_mesh_point_of_type(INVALID, x_pos, y_pos, 0, NULL, false); // The '0' says we want to use the nozzle's position
476
             if (location.x_index < 0) break; // No more invalid Mesh Points to populate
462
             if (location.x_index < 0) break; // No more invalid Mesh Points to populate
477
-            z_values[location.x_index][location.y_index] = height_value;
463
+            z_values[location.x_index][location.y_index] = height;
478
           }
464
           }
479
-          break;
465
+        } break;
466
+
480
         //
467
         //
481
         // Fine Tune (Or Edit) the Mesh
468
         // Fine Tune (Or Edit) the Mesh
482
         //
469
         //
491
           break;
478
           break;
492
 
479
 
493
         case 10:
480
         case 10:
494
-          // Debug code... Pay no attention to this stuff
495
-          // it can be removed soon.
481
+          // [DEBUG] Pay no attention to this stuff. It can be removed soon.
496
           SERIAL_ECHO_START;
482
           SERIAL_ECHO_START;
497
           SERIAL_ECHOLNPGM("Checking G29 has control of LCD Panel:");
483
           SERIAL_ECHOLNPGM("Checking G29 has control of LCD Panel:");
498
-          wait_for_user = true;
484
+          KEEPALIVE_STATE(PAUSED_FOR_USER);
485
+          ubl_has_control_of_lcd_panel++;
499
           while (!ubl_lcd_clicked()) {
486
           while (!ubl_lcd_clicked()) {
500
             safe_delay(250);
487
             safe_delay(250);
501
-            SERIAL_ECHO((int)ubl_encoderDiff);
502
-            ubl_encoderDiff = 0;
503
-            SERIAL_EOL;
488
+            if (ubl_encoderDiff) {
489
+              SERIAL_ECHOLN((int)ubl_encoderDiff);
490
+              ubl_encoderDiff = 0;
491
+            }
492
+          }
493
+          SERIAL_ECHOLNPGM("G29 giving back control of LCD Panel.");
494
+          ubl_has_control_of_lcd_panel = false;
495
+          KEEPALIVE_STATE(IN_HANDLER);
496
+          break;
497
+
498
+        case 11:
499
+          // [DEBUG] wait_for_user code. Pay no attention to this stuff. It can be removed soon.
500
+          SERIAL_ECHO_START;
501
+          SERIAL_ECHOLNPGM("Checking G29 has control of LCD Panel:");
502
+          KEEPALIVE_STATE(PAUSED_FOR_USER);
503
+          wait_for_user = true;
504
+          while (wait_for_user) {
505
+            safe_delay(250);
506
+            if (ubl_encoderDiff) {
507
+              SERIAL_ECHOLN((int)ubl_encoderDiff);
508
+              ubl_encoderDiff = 0;
509
+            }
504
           }
510
           }
505
           SERIAL_ECHOLNPGM("G29 giving back control of LCD Panel.");
511
           SERIAL_ECHOLNPGM("G29 giving back control of LCD Panel.");
512
+          KEEPALIVE_STATE(IN_HANDLER);
506
           break;
513
           break;
507
       }
514
       }
508
     }
515
     }
509
 
516
 
510
     if (code_seen('T')) {
517
     if (code_seen('T')) {
511
-      Z1 = probe_pt(ubl_3_point_1_X, ubl_3_point_1_Y, false /*Stow Flag*/, g29_verbose_level) + zprobe_zoffset;
512
-      Z2 = probe_pt(ubl_3_point_2_X, ubl_3_point_2_Y, false /*Stow Flag*/, g29_verbose_level) + zprobe_zoffset;
513
-      Z3 = probe_pt(ubl_3_point_3_X, ubl_3_point_3_Y, true  /*Stow Flag*/, g29_verbose_level) + zprobe_zoffset;
518
+      float z1 = probe_pt(ubl_3_point_1_X, ubl_3_point_1_Y, false /*Stow Flag*/, g29_verbose_level) + zprobe_zoffset,
519
+            z2 = probe_pt(ubl_3_point_2_X, ubl_3_point_2_Y, false /*Stow Flag*/, g29_verbose_level) + zprobe_zoffset,
520
+            z3 = probe_pt(ubl_3_point_3_X, ubl_3_point_3_Y, true  /*Stow Flag*/, g29_verbose_level) + zprobe_zoffset;
514
 
521
 
515
-      //  We need to adjust Z1, Z2, Z3 by the Mesh Height at these points. Just because they are non-zero doesn't mean
522
+      //  We need to adjust z1, z2, z3 by the Mesh Height at these points. Just because they are non-zero doesn't mean
516
       //  the Mesh is tilted!  (We need to compensate each probe point by what the Mesh says that location's height is)
523
       //  the Mesh is tilted!  (We need to compensate each probe point by what the Mesh says that location's height is)
517
 
524
 
518
-      Z1 -= ubl.get_z_correction(ubl_3_point_1_X, ubl_3_point_1_Y);
519
-      Z2 -= ubl.get_z_correction(ubl_3_point_2_X, ubl_3_point_2_Y);
520
-      Z3 -= ubl.get_z_correction(ubl_3_point_3_X, ubl_3_point_3_Y);
525
+      z1 -= ubl.get_z_correction(ubl_3_point_1_X, ubl_3_point_1_Y);
526
+      z2 -= ubl.get_z_correction(ubl_3_point_2_X, ubl_3_point_2_Y);
527
+      z3 -= ubl.get_z_correction(ubl_3_point_3_X, ubl_3_point_3_Y);
521
 
528
 
522
       do_blocking_move_to_xy((X_MAX_POS - (X_MIN_POS)) / 2.0, (Y_MAX_POS - (Y_MIN_POS)) / 2.0);
529
       do_blocking_move_to_xy((X_MAX_POS - (X_MIN_POS)) / 2.0, (Y_MAX_POS - (Y_MIN_POS)) / 2.0);
523
-      tilt_mesh_based_on_3pts(Z1, Z2, Z3);
530
+      tilt_mesh_based_on_3pts(z1, z2, z3);
524
     }
531
     }
525
 
532
 
526
     //
533
     //
610
         save_ubl_active_state_and_disable();
617
         save_ubl_active_state_and_disable();
611
         //measured_z = probe_pt(x_pos + X_PROBE_OFFSET_FROM_EXTRUDER, y_pos + Y_PROBE_OFFSET_FROM_EXTRUDER, ProbeDeployAndStow, g29_verbose_level);
618
         //measured_z = probe_pt(x_pos + X_PROBE_OFFSET_FROM_EXTRUDER, y_pos + Y_PROBE_OFFSET_FROM_EXTRUDER, ProbeDeployAndStow, g29_verbose_level);
612
 
619
 
613
-        ubl_has_control_of_lcd_panel = true;// Grab the LCD Hardware
620
+        ubl_has_control_of_lcd_panel++;     // Grab the LCD Hardware
614
         measured_z = 1.5;
621
         measured_z = 1.5;
615
         do_blocking_move_to_z(measured_z);  // Get close to the bed, but leave some space so we don't damage anything
622
         do_blocking_move_to_z(measured_z);  // Get close to the bed, but leave some space so we don't damage anything
616
                                             // The user is not going to be locking in a new Z-Offset very often so
623
                                             // The user is not going to be locking in a new Z-Offset very often so
617
                                             // it won't be that painful to spin the Encoder Wheel for 1.5mm
624
                                             // it won't be that painful to spin the Encoder Wheel for 1.5mm
618
         lcd_implementation_clear();
625
         lcd_implementation_clear();
619
         lcd_z_offset_edit_setup(measured_z);
626
         lcd_z_offset_edit_setup(measured_z);
627
+
628
+        KEEPALIVE_STATE(PAUSED_FOR_USER);
629
+
620
         do {
630
         do {
621
           measured_z = lcd_z_offset_edit();
631
           measured_z = lcd_z_offset_edit();
622
           idle();
632
           idle();
628
                                           // or here. So, until we are done looking for a long Encoder Wheel Press,
638
                                           // or here. So, until we are done looking for a long Encoder Wheel Press,
629
                                           // we need to take control of the panel
639
                                           // we need to take control of the panel
630
 
640
 
641
+        KEEPALIVE_STATE(IN_HANDLER);
642
+
631
         lcd_return_to_status();
643
         lcd_return_to_status();
632
 
644
 
633
         const millis_t nxt = millis() + 1500UL;
645
         const millis_t nxt = millis() + 1500UL;
637
             SERIAL_PROTOCOLLNPGM("\nZ-Offset Adjustment Stopped.");
649
             SERIAL_PROTOCOLLNPGM("\nZ-Offset Adjustment Stopped.");
638
             do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
650
             do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
639
             lcd_setstatus("Z-Offset Stopped", true);
651
             lcd_setstatus("Z-Offset Stopped", true);
640
-            ubl_has_control_of_lcd_panel = false;
641
             restore_ubl_active_state_and_leave();
652
             restore_ubl_active_state_and_leave();
642
             goto LEAVE;
653
             goto LEAVE;
643
           }
654
           }
702
       for (x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
713
       for (x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
703
         for (y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
714
         for (y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
704
           if (!isnan(z_values[x][y]))
715
           if (!isnan(z_values[x][y]))
705
-            z_values[x][y] -= mean + constant;
716
+            z_values[x][y] -= mean + ubl_constant;
706
   }
717
   }
707
 
718
 
708
   void shift_mesh_height() {
719
   void shift_mesh_height() {
709
     for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
720
     for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
710
       for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
721
       for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
711
         if (!isnan(z_values[x][y]))
722
         if (!isnan(z_values[x][y]))
712
-          z_values[x][y] += constant;
723
+          z_values[x][y] += ubl_constant;
713
   }
724
   }
714
 
725
 
715
   /**
726
   /**
728
         SERIAL_PROTOCOLLNPGM("\nMesh only partially populated.\n");
739
         SERIAL_PROTOCOLLNPGM("\nMesh only partially populated.\n");
729
         lcd_quick_feedback();
740
         lcd_quick_feedback();
730
         STOW_PROBE();
741
         STOW_PROBE();
731
-        while (ubl_lcd_clicked() ) {
732
-          idle();
733
-        }
742
+        while (ubl_lcd_clicked()) idle();
734
         ubl_has_control_of_lcd_panel = false;
743
         ubl_has_control_of_lcd_panel = false;
735
         restore_ubl_active_state_and_leave();
744
         restore_ubl_active_state_and_leave();
736
         safe_delay(50);  // Debounce the Encoder wheel
745
         safe_delay(50);  // Debounce the Encoder wheel
739
 
748
 
740
       location = find_closest_mesh_point_of_type(INVALID, lx, ly, 1, NULL, do_furthest );  // the '1' says we want the location to be relative to the probe
749
       location = find_closest_mesh_point_of_type(INVALID, lx, ly, 1, NULL, do_furthest );  // the '1' says we want the location to be relative to the probe
741
       if (location.x_index >= 0 && location.y_index >= 0) {
750
       if (location.x_index >= 0 && location.y_index >= 0) {
742
-        const float xProbe = ubl.map_x_index_to_bed_location(location.x_index),
743
-                    yProbe = ubl.map_y_index_to_bed_location(location.y_index);
744
-        if (xProbe < MIN_PROBE_X || xProbe > MAX_PROBE_X || yProbe < MIN_PROBE_Y || yProbe > MAX_PROBE_Y) {
745
-          SERIAL_PROTOCOLLNPGM("?Error: Attempt to probe off the bed.");
751
+
752
+        const float rawx = ubl.map_x_index_to_bed_location(location.x_index),
753
+                    rawy = ubl.map_y_index_to_bed_location(location.y_index);
754
+
755
+        // TODO: Change to use `position_is_reachable` (for SCARA-compatibility)
756
+        if (rawx < (MIN_PROBE_X) || rawx > (MAX_PROBE_X) || rawy < (MIN_PROBE_Y) || rawy > (MAX_PROBE_Y)) {
757
+          SERIAL_ERROR_START;
758
+          SERIAL_ERRORLNPGM("Attempt to probe off the bed.");
746
           ubl_has_control_of_lcd_panel = false;
759
           ubl_has_control_of_lcd_panel = false;
747
           goto LEAVE;
760
           goto LEAVE;
748
         }
761
         }
749
-        const float measured_z = probe_pt(xProbe, yProbe, stow_probe, g29_verbose_level);
762
+        const float measured_z = probe_pt(LOGICAL_X_POSITION(rawx), LOGICAL_Y_POSITION(rawy), stow_probe, g29_verbose_level);
750
         z_values[location.x_index][location.y_index] = measured_z + zprobe_zoffset;
763
         z_values[location.x_index][location.y_index] = measured_z + zprobe_zoffset;
751
       }
764
       }
752
 
765
 
831
   }
844
   }
832
 
845
 
833
   float use_encoder_wheel_to_measure_point() {
846
   float use_encoder_wheel_to_measure_point() {
847
+    KEEPALIVE_STATE(PAUSED_FOR_USER);
834
     while (!ubl_lcd_clicked()) {     // we need the loop to move the nozzle based on the encoder wheel here!
848
     while (!ubl_lcd_clicked()) {     // we need the loop to move the nozzle based on the encoder wheel here!
835
       idle();
849
       idle();
836
       if (ubl_encoderDiff) {
850
       if (ubl_encoderDiff) {
838
         ubl_encoderDiff = 0;
852
         ubl_encoderDiff = 0;
839
       }
853
       }
840
     }
854
     }
855
+    KEEPALIVE_STATE(IN_HANDLER);
841
     return current_position[Z_AXIS];
856
     return current_position[Z_AXIS];
842
   }
857
   }
843
 
858
 
844
-  float measure_business_card_thickness(const float &height_value) {
859
+  float measure_business_card_thickness(const float &in_height) {
845
 
860
 
846
     ubl_has_control_of_lcd_panel++;
861
     ubl_has_control_of_lcd_panel++;
847
     save_ubl_active_state_and_disable();   // we don't do bed level correction because we want the raw data when we probe
862
     save_ubl_active_state_and_disable();   // we don't do bed level correction because we want the raw data when we probe
848
 
863
 
849
     SERIAL_PROTOCOLLNPGM("Place Shim Under Nozzle and Perform Measurement.");
864
     SERIAL_PROTOCOLLNPGM("Place Shim Under Nozzle and Perform Measurement.");
850
-    do_blocking_move_to_z(height_value);
865
+    do_blocking_move_to_z(in_height);
851
     do_blocking_move_to_xy((float(X_MAX_POS) - float(X_MIN_POS)) / 2.0, (float(Y_MAX_POS) - float(Y_MIN_POS)) / 2.0);
866
     do_blocking_move_to_xy((float(X_MAX_POS) - float(X_MIN_POS)) / 2.0, (float(Y_MAX_POS) - float(Y_MIN_POS)) / 2.0);
852
       //, min( planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS])/2.0);
867
       //, min( planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS])/2.0);
853
 
868
 
854
-    const float Z1 = use_encoder_wheel_to_measure_point();
869
+    const float z1 = use_encoder_wheel_to_measure_point();
855
     do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
870
     do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
856
     ubl_has_control_of_lcd_panel = false;
871
     ubl_has_control_of_lcd_panel = false;
857
 
872
 
858
     SERIAL_PROTOCOLLNPGM("Remove Shim and Measure Bed Height.");
873
     SERIAL_PROTOCOLLNPGM("Remove Shim and Measure Bed Height.");
859
-    const float Z2 = use_encoder_wheel_to_measure_point();
874
+    const float z2 = use_encoder_wheel_to_measure_point();
860
     do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
875
     do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
861
 
876
 
862
     if (g29_verbose_level > 1) {
877
     if (g29_verbose_level > 1) {
863
       SERIAL_PROTOCOLPGM("Business Card is: ");
878
       SERIAL_PROTOCOLPGM("Business Card is: ");
864
-      SERIAL_PROTOCOL_F(abs(Z1 - Z2), 6);
879
+      SERIAL_PROTOCOL_F(abs(z1 - z2), 6);
865
       SERIAL_PROTOCOLLNPGM("mm thick.");
880
       SERIAL_PROTOCOLLNPGM("mm thick.");
866
     }
881
     }
867
     restore_ubl_active_state_and_leave();
882
     restore_ubl_active_state_and_leave();
868
-    return abs(Z1 - Z2);
883
+    return abs(z1 - z2);
869
   }
884
   }
870
 
885
 
871
   void manually_probe_remaining_mesh(const float &lx, const float &ly, const float &z_clearance, const float &card_thickness, const bool do_ubl_mesh_map) {
886
   void manually_probe_remaining_mesh(const float &lx, const float &ly, const float &z_clearance, const float &card_thickness, const bool do_ubl_mesh_map) {
881
       if (do_ubl_mesh_map) ubl.display_map(map_type);
896
       if (do_ubl_mesh_map) ubl.display_map(map_type);
882
 
897
 
883
       location = find_closest_mesh_point_of_type(INVALID, lx, ly, 0, NULL, false); // The '0' says we want to use the nozzle's position
898
       location = find_closest_mesh_point_of_type(INVALID, lx, ly, 0, NULL, false); // The '0' says we want to use the nozzle's position
884
-      // It doesn't matter if the probe can not reach the
885
-      // NAN location. This is a manual probe.
899
+      // It doesn't matter if the probe can't reach the NAN location. This is a manual probe.
886
       if (location.x_index < 0 && location.y_index < 0) continue;
900
       if (location.x_index < 0 && location.y_index < 0) continue;
887
 
901
 
888
-      const float xProbe = ubl.map_x_index_to_bed_location(location.x_index),
889
-                  yProbe = ubl.map_y_index_to_bed_location(location.y_index);
902
+      const float rawx = ubl.map_x_index_to_bed_location(location.x_index),
903
+                  rawy = ubl.map_y_index_to_bed_location(location.y_index);
890
 
904
 
891
-      // Modify to use if (position_is_reachable(pos[XYZ]))
892
-      if (xProbe < (X_MIN_POS) || xProbe > (X_MAX_POS) || yProbe < (Y_MIN_POS) || yProbe > (Y_MAX_POS)) {
893
-        SERIAL_PROTOCOLLNPGM("?Error: Attempt to probe off the bed.");
905
+      // TODO: Change to use `position_is_reachable` (for SCARA-compatibility)
906
+      if (rawx < (X_MIN_POS) || rawx > (X_MAX_POS) || rawy < (Y_MIN_POS) || rawy > (Y_MAX_POS)) {
907
+        SERIAL_ERROR_START;
908
+        SERIAL_ERRORLNPGM("Attempt to probe off the bed.");
894
         ubl_has_control_of_lcd_panel = false;
909
         ubl_has_control_of_lcd_panel = false;
895
         goto LEAVE;
910
         goto LEAVE;
896
       }
911
       }
897
 
912
 
898
-      const float dx = xProbe - last_x,
913
+      const float xProbe = LOGICAL_X_POSITION(rawx),
914
+                  yProbe = LOGICAL_Y_POSITION(rawy),
915
+                  dx = xProbe - last_x,
899
                   dy = yProbe - last_y;
916
                   dy = yProbe - last_y;
900
 
917
 
901
       if (HYPOT(dx, dy) < BIG_RAISE_NOT_NEEDED)
918
       if (HYPOT(dx, dy) < BIG_RAISE_NOT_NEEDED)
908
       last_x = xProbe;
925
       last_x = xProbe;
909
       last_y = yProbe;
926
       last_y = yProbe;
910
 
927
 
928
+      KEEPALIVE_STATE(PAUSED_FOR_USER);
911
       ubl_has_control_of_lcd_panel = true;
929
       ubl_has_control_of_lcd_panel = true;
912
-      while (!ubl_lcd_clicked) {     // we need the loop to move the nozzle based on the encoder wheel here!
930
+
931
+      while (!ubl_lcd_clicked()) {     // we need the loop to move the nozzle based on the encoder wheel here!
913
         idle();
932
         idle();
914
         if (ubl_encoderDiff) {
933
         if (ubl_encoderDiff) {
915
           do_blocking_move_to_z(current_position[Z_AXIS] + float(ubl_encoderDiff) / 100.0);
934
           do_blocking_move_to_z(current_position[Z_AXIS] + float(ubl_encoderDiff) / 100.0);
926
           lcd_quick_feedback();
945
           lcd_quick_feedback();
927
           while (ubl_lcd_clicked()) idle();
946
           while (ubl_lcd_clicked()) idle();
928
           ubl_has_control_of_lcd_panel = false;
947
           ubl_has_control_of_lcd_panel = false;
948
+          KEEPALIVE_STATE(IN_HANDLER);
929
           restore_ubl_active_state_and_leave();
949
           restore_ubl_active_state_and_leave();
930
           return;
950
           return;
931
         }
951
         }
933
 
953
 
934
       z_values[location.x_index][location.y_index] = current_position[Z_AXIS] - card_thickness;
954
       z_values[location.x_index][location.y_index] = current_position[Z_AXIS] - card_thickness;
935
       if (g29_verbose_level > 2) {
955
       if (g29_verbose_level > 2) {
936
-        SERIAL_PROTOCOL("Mesh Point Measured at: ");
956
+        SERIAL_PROTOCOLPGM("Mesh Point Measured at: ");
937
         SERIAL_PROTOCOL_F(z_values[location.x_index][location.y_index], 6);
957
         SERIAL_PROTOCOL_F(z_values[location.x_index][location.y_index], 6);
938
         SERIAL_EOL;
958
         SERIAL_EOL;
939
       }
959
       }
943
 
963
 
944
     LEAVE:
964
     LEAVE:
945
     restore_ubl_active_state_and_leave();
965
     restore_ubl_active_state_and_leave();
966
+    KEEPALIVE_STATE(IN_HANDLER);
946
     do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
967
     do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
947
     do_blocking_move_to_xy(lx, ly);
968
     do_blocking_move_to_xy(lx, ly);
948
   }
969
   }
949
 
970
 
950
   bool g29_parameter_parsing() {
971
   bool g29_parameter_parsing() {
951
-
952
     #if ENABLED(ULTRA_LCD)
972
     #if ENABLED(ULTRA_LCD)
953
       lcd_setstatus("Doing G29 UBL !", true);
973
       lcd_setstatus("Doing G29 UBL !", true);
954
       lcd_quick_feedback();
974
       lcd_quick_feedback();
955
     #endif
975
     #endif
956
 
976
 
957
-    x_pos = current_position[X_AXIS];
958
-    y_pos = current_position[Y_AXIS];
959
-    x_flag = y_flag = repeat_flag = false;
960
-    map_type = 0;
961
-    constant = 0.0;
962
-    repetition_cnt = 1;
963
-
964
-    if ((x_flag = code_seen('X'))) {
965
-      x_pos = code_value_float();
966
-      if (x_pos < X_MIN_POS || x_pos > X_MAX_POS) {
967
-        SERIAL_PROTOCOLLNPGM("Invalid X location specified.\n");
968
-        return UBL_ERR;
969
-      }
977
+    g29_verbose_level = code_seen('V') ? code_value_int() : 0;
978
+    if (g29_verbose_level < 0 || g29_verbose_level > 4) {
979
+      SERIAL_PROTOCOLLNPGM("Invalid Verbose Level specified. (0-4)\n");
980
+      return UBL_ERR;
970
     }
981
     }
971
 
982
 
972
-    if ((y_flag = code_seen('Y'))) {
973
-      y_pos = code_value_float();
974
-      if (y_pos < Y_MIN_POS || y_pos > Y_MAX_POS) {
975
-        SERIAL_PROTOCOLLNPGM("Invalid Y location specified.\n");
976
-        return UBL_ERR;
977
-      }
983
+    x_flag = code_seen('X') && code_has_value();
984
+    x_pos = x_flag ? code_value_float() : current_position[X_AXIS];
985
+    if (x_pos < LOGICAL_X_POSITION(X_MIN_POS) || x_pos > LOGICAL_X_POSITION(X_MAX_POS)) {
986
+      SERIAL_PROTOCOLLNPGM("Invalid X location specified.\n");
987
+      return UBL_ERR;
978
     }
988
     }
979
 
989
 
980
-    if (x_flag != y_flag) {
981
-      SERIAL_PROTOCOLLNPGM("Both X & Y locations must be specified.\n");
990
+    y_flag = code_seen('Y') && code_has_value();
991
+    y_pos = y_flag ? code_value_float() : current_position[Y_AXIS];
992
+    if (y_pos < LOGICAL_Y_POSITION(Y_MIN_POS) || y_pos > LOGICAL_Y_POSITION(Y_MAX_POS)) {
993
+      SERIAL_PROTOCOLLNPGM("Invalid Y location specified.\n");
982
       return UBL_ERR;
994
       return UBL_ERR;
983
     }
995
     }
984
 
996
 
985
-    g29_verbose_level = 0;
986
-    if (code_seen('V')) {
987
-      g29_verbose_level = code_value_int();
988
-      if (g29_verbose_level < 0 || g29_verbose_level > 4) {
989
-        SERIAL_PROTOCOLLNPGM("Invalid Verbose Level specified. (0-4)\n");
990
-        return UBL_ERR;
991
-      }
997
+    if (x_flag != y_flag) {
998
+      SERIAL_PROTOCOLLNPGM("Both X & Y locations must be specified.\n");
999
+      return UBL_ERR;
992
     }
1000
     }
993
 
1001
 
994
     if (code_seen('A')) {     // Activate the Unified Bed Leveling System
1002
     if (code_seen('A')) {     // Activate the Unified Bed Leveling System
997
       ubl.store_state();
1005
       ubl.store_state();
998
     }
1006
     }
999
 
1007
 
1000
-    if ((c_flag = code_seen('C') && code_has_value()))
1001
-      constant = code_value_float();
1008
+    c_flag = code_seen('C') && code_has_value();
1009
+    ubl_constant = c_flag ? code_value_float() : 0.0;
1002
 
1010
 
1003
     if (code_seen('D')) {     // Disable the Unified Bed Leveling System
1011
     if (code_seen('D')) {     // Disable the Unified Bed Leveling System
1004
       ubl.state.active = 0;
1012
       ubl.state.active = 0;
1018
       }
1026
       }
1019
     #endif
1027
     #endif
1020
 
1028
 
1021
-    if ((repeat_flag = code_seen('R'))) {
1022
-      repetition_cnt = code_has_value() ? code_value_int() : 9999;
1023
-      if (repetition_cnt < 1) {
1024
-        SERIAL_PROTOCOLLNPGM("Invalid Repetition count.\n");
1025
-        return UBL_ERR;
1026
-      }
1029
+    repeat_flag = code_seen('R');
1030
+    repetition_cnt = repeat_flag ? (code_has_value() ? code_value_int() : 9999) : 1;
1031
+    if (repetition_cnt < 1) {
1032
+      SERIAL_PROTOCOLLNPGM("Invalid Repetition count.\n");
1033
+      return UBL_ERR;
1027
     }
1034
     }
1028
 
1035
 
1029
-    if (code_seen('O')) {     // Check if a map type was specified
1030
-      map_type = code_value_int() ? code_has_value() : 0; 
1031
-      if ( map_type<0 || map_type>1) {
1032
-        SERIAL_PROTOCOLLNPGM("Invalid map type.\n");
1033
-        return UBL_ERR;
1034
-      }
1036
+    map_type = code_seen('O') && code_has_value() ? code_value_int() : 0;
1037
+    if (map_type < 0 || map_type > 1) {
1038
+      SERIAL_PROTOCOLLNPGM("Invalid map type.\n");
1039
+      return UBL_ERR;
1035
     }
1040
     }
1036
 
1041
 
1042
+    /*
1037
     if (code_seen('M')) {     // Check if a map type was specified
1043
     if (code_seen('M')) {     // Check if a map type was specified
1038
-      map_type = code_value_int() ? code_has_value() : 0; 
1039
-      if ( map_type<0 || map_type>1) {
1044
+      map_type = code_has_value() ? code_value_int() : 0; 
1045
+      if (map_type < 0 || map_type > 1) {
1040
         SERIAL_PROTOCOLLNPGM("Invalid map type.\n");
1046
         SERIAL_PROTOCOLLNPGM("Invalid map type.\n");
1041
         return UBL_ERR;
1047
         return UBL_ERR;
1042
       }
1048
       }
1043
     }
1049
     }
1050
+    //*/
1044
 
1051
 
1045
     return UBL_OK;
1052
     return UBL_OK;
1046
   }
1053
   }
1054
 
1061
 
1055
     SERIAL_PROTOCOL(str);
1062
     SERIAL_PROTOCOL(str);
1056
     SERIAL_PROTOCOL_F(f, 8);
1063
     SERIAL_PROTOCOL_F(f, 8);
1057
-    SERIAL_PROTOCOL("  ");
1064
+    SERIAL_PROTOCOLPGM("  ");
1058
     ptr = (char*)&f;
1065
     ptr = (char*)&f;
1059
-    for (uint8_t i = 0; i < 4; i++) {
1060
-      SERIAL_PROTOCOL("  ");
1061
-      prt_hex_byte(*ptr++);
1062
-    }
1063
-    SERIAL_PROTOCOL("  isnan()=");
1064
-    SERIAL_PROTOCOL(isnan(f));
1065
-    SERIAL_PROTOCOL("  isinf()=");
1066
-    SERIAL_PROTOCOL(isinf(f));
1066
+    for (uint8_t i = 0; i < 4; i++)
1067
+      SERIAL_PROTOCOLPAIR("  ", hex_byte(*ptr++));
1068
+    SERIAL_PROTOCOLPAIR("  isnan()=", isnan(f));
1069
+    SERIAL_PROTOCOLPAIR("  isinf()=", isinf(f));
1067
 
1070
 
1068
-    constexpr float g = INFINITY;
1069
-    if (f == -g)
1070
-      SERIAL_PROTOCOL("  Minus Infinity detected.");
1071
+    if (f == -INFINITY)
1072
+      SERIAL_PROTOCOLPGM("  Minus Infinity detected.");
1071
 
1073
 
1072
     SERIAL_EOL;
1074
     SERIAL_EOL;
1073
   }
1075
   }
1104
    */
1106
    */
1105
   void g29_what_command() {
1107
   void g29_what_command() {
1106
     const uint16_t k = E2END - ubl_eeprom_start;
1108
     const uint16_t k = E2END - ubl_eeprom_start;
1107
-    statistics_flag++;
1108
 
1109
 
1109
     SERIAL_PROTOCOLPGM("Unified Bed Leveling System Version 1.00 ");
1110
     SERIAL_PROTOCOLPGM("Unified Bed Leveling System Version 1.00 ");
1110
     if (ubl.state.active)  
1111
     if (ubl.state.active)  
1117
     if (ubl.state.eeprom_storage_slot == -1)
1118
     if (ubl.state.eeprom_storage_slot == -1)
1118
       SERIAL_PROTOCOLPGM("No Mesh Loaded.");
1119
       SERIAL_PROTOCOLPGM("No Mesh Loaded.");
1119
     else {
1120
     else {
1120
-      SERIAL_PROTOCOLPGM("Mesh: ");
1121
-      prt_hex_word(ubl.state.eeprom_storage_slot);
1121
+      SERIAL_PROTOCOLPAIR("Mesh ", ubl.state.eeprom_storage_slot);
1122
       SERIAL_PROTOCOLPGM(" Loaded.");
1122
       SERIAL_PROTOCOLPGM(" Loaded.");
1123
     }
1123
     }
1124
     SERIAL_EOL;
1124
     SERIAL_EOL;
1136
 
1136
 
1137
     SERIAL_PROTOCOLPGM("X-Axis Mesh Points at: ");
1137
     SERIAL_PROTOCOLPGM("X-Axis Mesh Points at: ");
1138
     for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
1138
     for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
1139
-      SERIAL_PROTOCOL_F( ubl.map_x_index_to_bed_location(i), 1);
1139
+      SERIAL_PROTOCOL_F(LOGICAL_X_POSITION(ubl.map_x_index_to_bed_location(i)), 1);
1140
       SERIAL_PROTOCOLPGM("  ");
1140
       SERIAL_PROTOCOLPGM("  ");
1141
       safe_delay(50);
1141
       safe_delay(50);
1142
     }
1142
     }
1144
 
1144
 
1145
     SERIAL_PROTOCOLPGM("Y-Axis Mesh Points at: ");
1145
     SERIAL_PROTOCOLPGM("Y-Axis Mesh Points at: ");
1146
     for (uint8_t i = 0; i < UBL_MESH_NUM_Y_POINTS; i++) {
1146
     for (uint8_t i = 0; i < UBL_MESH_NUM_Y_POINTS; i++) {
1147
-      SERIAL_PROTOCOL_F( ubl.map_y_index_to_bed_location(i), 1);
1147
+      SERIAL_PROTOCOL_F(LOGICAL_Y_POSITION(ubl.map_y_index_to_bed_location(i)), 1);
1148
       SERIAL_PROTOCOLPGM("  ");
1148
       SERIAL_PROTOCOLPGM("  ");
1149
       safe_delay(50);
1149
       safe_delay(50);
1150
     }
1150
     }
1162
     SERIAL_PROTOCOLLNPAIR("ubl_state_recursion_chk :", ubl_state_recursion_chk);
1162
     SERIAL_PROTOCOLLNPAIR("ubl_state_recursion_chk :", ubl_state_recursion_chk);
1163
     SERIAL_EOL;
1163
     SERIAL_EOL;
1164
     safe_delay(50);
1164
     safe_delay(50);
1165
-    SERIAL_PROTOCOLPGM("Free EEPROM space starts at: 0x");
1166
-    prt_hex_word(ubl_eeprom_start);
1167
-    SERIAL_EOL;
1165
+    SERIAL_PROTOCOLLNPAIR("Free EEPROM space starts at: 0x", hex_word(ubl_eeprom_start));
1168
 
1166
 
1169
-    SERIAL_PROTOCOLPGM("end of EEPROM              : ");
1170
-    prt_hex_word(E2END);
1171
-    SERIAL_EOL;
1167
+    SERIAL_PROTOCOLLNPAIR("end of EEPROM              : ", hex_word(E2END));
1172
     safe_delay(50);
1168
     safe_delay(50);
1173
 
1169
 
1174
     SERIAL_PROTOCOLLNPAIR("sizeof(ubl) :  ", (int)sizeof(ubl));
1170
     SERIAL_PROTOCOLLNPAIR("sizeof(ubl) :  ", (int)sizeof(ubl));
1177
     SERIAL_EOL;
1173
     SERIAL_EOL;
1178
     safe_delay(50);
1174
     safe_delay(50);
1179
 
1175
 
1180
-    SERIAL_PROTOCOLPGM("EEPROM free for UBL: 0x");
1181
-    prt_hex_word(k);
1182
-    SERIAL_EOL;
1176
+    SERIAL_PROTOCOLLNPAIR("EEPROM free for UBL: 0x", hex_word(k));
1183
     safe_delay(50);
1177
     safe_delay(50);
1184
 
1178
 
1185
-    SERIAL_PROTOCOLPGM("EEPROM can hold 0x");
1186
-    prt_hex_word(k / sizeof(z_values));
1179
+    SERIAL_PROTOCOLPAIR("EEPROM can hold ", k / sizeof(z_values));
1187
     SERIAL_PROTOCOLLNPGM(" meshes.\n");
1180
     SERIAL_PROTOCOLLNPGM(" meshes.\n");
1188
     safe_delay(50);
1181
     safe_delay(50);
1189
 
1182
 
1190
-    SERIAL_PROTOCOLPGM("sizeof(ubl.state) :");
1191
-    prt_hex_word(sizeof(ubl.state));
1183
+    SERIAL_PROTOCOLPAIR("sizeof(ubl.state) : ", (int)sizeof(ubl.state));
1192
 
1184
 
1193
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_X_POINTS  ", UBL_MESH_NUM_X_POINTS);
1185
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_X_POINTS  ", UBL_MESH_NUM_X_POINTS);
1194
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_Y_POINTS  ", UBL_MESH_NUM_Y_POINTS);
1186
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_Y_POINTS  ", UBL_MESH_NUM_Y_POINTS);
1222
     SERIAL_ECHOLNPGM("EEPROM Dump:");
1214
     SERIAL_ECHOLNPGM("EEPROM Dump:");
1223
     for (uint16_t i = 0; i < E2END + 1; i += 16) {
1215
     for (uint16_t i = 0; i < E2END + 1; i += 16) {
1224
       if (!(i & 0x3)) idle();
1216
       if (!(i & 0x3)) idle();
1225
-      prt_hex_word(i);
1217
+      print_hex_word(i);
1226
       SERIAL_ECHOPGM(": ");
1218
       SERIAL_ECHOPGM(": ");
1227
       for (uint16_t j = 0; j < 16; j++) {
1219
       for (uint16_t j = 0; j < 16; j++) {
1228
         kkkk = i + j;
1220
         kkkk = i + j;
1229
         eeprom_read_block(&cccc, (void *)kkkk, 1);
1221
         eeprom_read_block(&cccc, (void *)kkkk, 1);
1230
-        prt_hex_byte(cccc);
1222
+        print_hex_byte(cccc);
1231
         SERIAL_ECHO(' ');
1223
         SERIAL_ECHO(' ');
1232
       }
1224
       }
1233
       SERIAL_EOL;
1225
       SERIAL_EOL;
1259
     eeprom_read_block((void *)&tmp_z_values, (void *)j, sizeof(tmp_z_values));
1251
     eeprom_read_block((void *)&tmp_z_values, (void *)j, sizeof(tmp_z_values));
1260
 
1252
 
1261
     SERIAL_ECHOPAIR("Subtracting Mesh ", storage_slot);
1253
     SERIAL_ECHOPAIR("Subtracting Mesh ", storage_slot);
1262
-    SERIAL_PROTOCOLPGM(" loaded from EEPROM address ");   // Soon, we can remove the extra clutter of printing
1263
-    prt_hex_word(j);            // the address in the EEPROM where the Mesh is stored.
1264
-    SERIAL_EOL;
1254
+    SERIAL_PROTOCOLLNPAIR(" loaded from EEPROM address ", hex_word(j)); // Soon, we can remove the extra clutter of printing
1255
+                                                                        // the address in the EEPROM where the Mesh is stored.
1265
 
1256
 
1266
     for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
1257
     for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
1267
       for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
1258
       for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
1269
   }
1260
   }
1270
 
1261
 
1271
   mesh_index_pair find_closest_mesh_point_of_type(const MeshPointType type, const float &lx, const float &ly, const bool probe_as_reference, unsigned int bits[16], bool far_flag) {
1262
   mesh_index_pair find_closest_mesh_point_of_type(const MeshPointType type, const float &lx, const float &ly, const bool probe_as_reference, unsigned int bits[16], bool far_flag) {
1272
-    int i, j, k, l;
1273
     float distance, closest = far_flag ? -99999.99 : 99999.99;
1263
     float distance, closest = far_flag ? -99999.99 : 99999.99;
1274
     mesh_index_pair return_val;
1264
     mesh_index_pair return_val;
1275
 
1265
 
1282
     const float px = lx - (probe_as_reference ? X_PROBE_OFFSET_FROM_EXTRUDER : 0),
1272
     const float px = lx - (probe_as_reference ? X_PROBE_OFFSET_FROM_EXTRUDER : 0),
1283
                 py = ly - (probe_as_reference ? Y_PROBE_OFFSET_FROM_EXTRUDER : 0);
1273
                 py = ly - (probe_as_reference ? Y_PROBE_OFFSET_FROM_EXTRUDER : 0);
1284
 
1274
 
1285
-    for (i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
1286
-      for (j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
1275
+    for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
1276
+      for (uint8_t j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
1287
 
1277
 
1288
         if ( (type == INVALID && isnan(z_values[i][j]))  // Check to see if this location holds the right thing
1278
         if ( (type == INVALID && isnan(z_values[i][j]))  // Check to see if this location holds the right thing
1289
           || (type == REAL && !isnan(z_values[i][j]))
1279
           || (type == REAL && !isnan(z_values[i][j]))
1292
 
1282
 
1293
           // We only get here if we found a Mesh Point of the specified type
1283
           // We only get here if we found a Mesh Point of the specified type
1294
 
1284
 
1295
-          const float mx = LOGICAL_X_POSITION(ubl.map_x_index_to_bed_location(i)), // Check if we can probe this mesh location
1296
-                      my = LOGICAL_Y_POSITION(ubl.map_y_index_to_bed_location(j));
1285
+          const float rawx = ubl.map_x_index_to_bed_location(i), // Check if we can probe this mesh location
1286
+                      rawy = ubl.map_y_index_to_bed_location(j);
1297
 
1287
 
1298
-          // If we are using the probe as the reference there are some locations we can't get to.
1299
-          // We prune these out of the list and ignore them until the next Phase where we do the
1300
-          // manual nozzle probing.
1288
+          // If using the probe as the reference there are some unreachable locations.
1289
+          // Prune them from the list and ignore them till the next Phase (manual nozzle probing).
1301
 
1290
 
1302
           if (probe_as_reference &&
1291
           if (probe_as_reference &&
1303
-            (mx < (MIN_PROBE_X) || mx > (MAX_PROBE_X) || my < (MIN_PROBE_Y) || my > (MAX_PROBE_Y))
1292
+            (rawx < (MIN_PROBE_X) || rawx > (MAX_PROBE_X) || rawy < (MIN_PROBE_Y) || rawy > (MAX_PROBE_Y))
1304
           ) continue;
1293
           ) continue;
1305
 
1294
 
1306
-          // We can get to it. Let's see if it is the closest location to the nozzle.
1295
+          // Unreachable. Check if it's the closest location to the nozzle.
1307
           // Add in a weighting factor that considers the current location of the nozzle.
1296
           // Add in a weighting factor that considers the current location of the nozzle.
1308
 
1297
 
1298
+          const float mx = LOGICAL_X_POSITION(rawx), // Check if we can probe this mesh location
1299
+                      my = LOGICAL_Y_POSITION(rawy);
1300
+
1309
           distance = HYPOT(px - mx, py - my) + HYPOT(current_x - mx, current_y - my) * 0.1;
1301
           distance = HYPOT(px - mx, py - my) + HYPOT(current_x - mx, current_y - my) * 0.1;
1310
 
1302
 
1311
-	  if (far_flag) {                                    // If doing the far_flag action, we want to be as far as possible
1312
-            for (k = 0; k < UBL_MESH_NUM_X_POINTS; k++) {    // from the starting point and from any other probed points.  We
1313
-              for (l = 0; l < UBL_MESH_NUM_Y_POINTS; l++) {  // want the next point spread out and filling in any blank spaces
1314
-                if ( !isnan(z_values[k][l])) {               // in the mesh.   So we add in some of the distance to every probed 
1315
-                  distance += (i-k)*(i-k)*MESH_X_DIST*.05;   // point we can find.
1316
-                  distance += (j-l)*(j-l)*MESH_Y_DIST*.05;
1317
-		}
1303
+          if (far_flag) {                                           // If doing the far_flag action, we want to be as far as possible
1304
+            for (uint8_t k = 0; k < UBL_MESH_NUM_X_POINTS; k++) {   // from the starting point and from any other probed points.  We
1305
+              for (uint8_t l = 0; l < UBL_MESH_NUM_Y_POINTS; l++) { // want the next point spread out and filling in any blank spaces
1306
+                if (!isnan(z_values[k][l])) {                       // in the mesh. So we add in some of the distance to every probed
1307
+                  distance += sq(i - k) * (MESH_X_DIST) * .05       // point we can find.
1308
+                            + sq(j - l) * (MESH_Y_DIST) * .05;
1309
+                }
1318
               }
1310
               }
1319
-	    }
1320
-	  }
1311
+            }
1312
+          }
1321
 
1313
 
1322
-          if ( (!far_flag&&(distance < closest)) || (far_flag&&(distance > closest)) ) {  // if far_flag, look for furthest away point
1323
-            closest = distance;       // We found a closer location with
1314
+          if (far_flag == (distance > closest) && distance != closest) {  // if far_flag, look for farthest point
1315
+            closest = distance;       // We found a closer/farther location with
1324
             return_val.x_index = i;   // the specified type of mesh value.
1316
             return_val.x_index = i;   // the specified type of mesh value.
1325
             return_val.y_index = j;
1317
             return_val.y_index = j;
1326
             return_val.distance = closest;
1318
             return_val.distance = closest;
1327
           }
1319
           }
1328
         }
1320
         }
1329
-      }
1330
-    }
1321
+      } // for j
1322
+    } // for i
1323
+
1331
     return return_val;
1324
     return return_val;
1332
   }
1325
   }
1333
 
1326
 
1356
       bit_clear(not_done, location.x_index, location.y_index);  // Mark this location as 'adjusted' so we will find a
1349
       bit_clear(not_done, location.x_index, location.y_index);  // Mark this location as 'adjusted' so we will find a
1357
                                                                 // different location the next time through the loop
1350
                                                                 // different location the next time through the loop
1358
 
1351
 
1359
-      const float xProbe = ubl.map_x_index_to_bed_location(location.x_index),
1360
-                  yProbe = ubl.map_y_index_to_bed_location(location.y_index);
1361
-      if (xProbe < X_MIN_POS || xProbe > X_MAX_POS || yProbe < Y_MIN_POS || yProbe > Y_MAX_POS) { // In theory, we don't need this check.
1362
-        SERIAL_PROTOCOLLNPGM("?Error: Attempt to edit off the bed.");                             // This really can't happen, but for now,
1363
-        ubl_has_control_of_lcd_panel = false;                                                         // Let's do the check.
1352
+      const float rawx = ubl.map_x_index_to_bed_location(location.x_index),
1353
+                  rawy = ubl.map_y_index_to_bed_location(location.y_index);
1354
+
1355
+      // TODO: Change to use `position_is_reachable` (for SCARA-compatibility)
1356
+      if (rawx < (X_MIN_POS) || rawx > (X_MAX_POS) || rawy < (Y_MIN_POS) || rawy > (Y_MAX_POS)) { // In theory, we don't need this check.
1357
+        SERIAL_ERROR_START;
1358
+        SERIAL_ERRORLNPGM("Attempt to edit off the bed."); // This really can't happen, but do the check for now
1359
+        ubl_has_control_of_lcd_panel = false;
1364
         goto FINE_TUNE_EXIT;
1360
         goto FINE_TUNE_EXIT;
1365
       }
1361
       }
1366
 
1362
 
1367
       do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);    // Move the nozzle to where we are going to edit
1363
       do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);    // Move the nozzle to where we are going to edit
1368
-      do_blocking_move_to_xy(xProbe, yProbe);
1364
+      do_blocking_move_to_xy(LOGICAL_X_POSITION(rawx), LOGICAL_Y_POSITION(rawy));
1369
       float new_z = z_values[location.x_index][location.y_index];
1365
       float new_z = z_values[location.x_index][location.y_index];
1370
       
1366
       
1371
       round_off = (int32_t)(new_z * 1000.0);    // we chop off the last digits just to be clean. We are rounding to the
1367
       round_off = (int32_t)(new_z * 1000.0);    // we chop off the last digits just to be clean. We are rounding to the
1372
       new_z = float(round_off) / 1000.0;
1368
       new_z = float(round_off) / 1000.0;
1373
 
1369
 
1370
+      KEEPALIVE_STATE(PAUSED_FOR_USER);
1374
       ubl_has_control_of_lcd_panel = true;
1371
       ubl_has_control_of_lcd_panel = true;
1375
 
1372
 
1376
       lcd_implementation_clear();
1373
       lcd_implementation_clear();
1377
       lcd_mesh_edit_setup(new_z);
1374
       lcd_mesh_edit_setup(new_z);
1378
 
1375
 
1379
-      wait_for_user = true;
1380
       do {
1376
       do {
1381
         new_z = lcd_mesh_edit();
1377
         new_z = lcd_mesh_edit();
1382
         idle();
1378
         idle();
1393
         idle();
1389
         idle();
1394
         if (ELAPSED(millis(), nxt)) {
1390
         if (ELAPSED(millis(), nxt)) {
1395
           lcd_return_to_status();
1391
           lcd_return_to_status();
1396
-//        SERIAL_PROTOCOLLNPGM("\nFine Tuning of Mesh Stopped.");
1392
+          //SERIAL_PROTOCOLLNPGM("\nFine Tuning of Mesh Stopped.");
1397
           do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
1393
           do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
1398
           lcd_setstatus("Mesh Editing Stopped", true);
1394
           lcd_setstatus("Mesh Editing Stopped", true);
1399
 
1395
 
1400
           while (ubl_lcd_clicked()) idle();
1396
           while (ubl_lcd_clicked()) idle();
1401
 
1397
 
1402
-          ubl_has_control_of_lcd_panel = false;
1403
           goto FINE_TUNE_EXIT;
1398
           goto FINE_TUNE_EXIT;
1404
         }
1399
         }
1405
       }
1400
       }
1415
     FINE_TUNE_EXIT:
1410
     FINE_TUNE_EXIT:
1416
 
1411
 
1417
     ubl_has_control_of_lcd_panel = false;
1412
     ubl_has_control_of_lcd_panel = false;
1413
+    KEEPALIVE_STATE(IN_HANDLER);
1418
 
1414
 
1419
     if (do_ubl_mesh_map) ubl.display_map(map_type);
1415
     if (do_ubl_mesh_map) ubl.display_map(map_type);
1420
     restore_ubl_active_state_and_leave();
1416
     restore_ubl_active_state_and_leave();

+ 8
- 11
Marlin/configuration_store.cpp View File

1235
         SERIAL_ECHOPAIR("EEPROM can hold ", (int)((UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values)));
1235
         SERIAL_ECHOPAIR("EEPROM can hold ", (int)((UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values)));
1236
         SERIAL_ECHOLNPGM(" meshes.\n");
1236
         SERIAL_ECHOLNPGM(" meshes.\n");
1237
 
1237
 
1238
-        SERIAL_ECHOPAIR("\nUBL_MESH_NUM_X_POINTS  ", UBL_MESH_NUM_X_POINTS);
1239
-        SERIAL_ECHOPAIR("\nUBL_MESH_NUM_Y_POINTS  ", UBL_MESH_NUM_Y_POINTS);
1238
+        SERIAL_ECHOLNPGM("UBL_MESH_NUM_X_POINTS  " STRINGIFY(UBL_MESH_NUM_X_POINTS));
1239
+        SERIAL_ECHOLNPGM("UBL_MESH_NUM_Y_POINTS  " STRINGIFY(UBL_MESH_NUM_Y_POINTS));
1240
 
1240
 
1241
-        SERIAL_ECHOPAIR("\nUBL_MESH_MIN_X         ", UBL_MESH_MIN_X);
1242
-        SERIAL_ECHOPAIR("\nUBL_MESH_MIN_Y         ", UBL_MESH_MIN_Y);
1241
+        SERIAL_ECHOLNPGM("UBL_MESH_MIN_X         " STRINGIFY(UBL_MESH_MIN_X));
1242
+        SERIAL_ECHOLNPGM("UBL_MESH_MIN_Y         " STRINGIFY(UBL_MESH_MIN_Y));
1243
 
1243
 
1244
-        SERIAL_ECHOPAIR("\nUBL_MESH_MAX_X         ", UBL_MESH_MAX_X);
1245
-        SERIAL_ECHOPAIR("\nUBL_MESH_MAX_Y         ", UBL_MESH_MAX_Y);
1244
+        SERIAL_ECHOLNPGM("UBL_MESH_MAX_X         " STRINGIFY(UBL_MESH_MAX_X));
1245
+        SERIAL_ECHOLNPGM("UBL_MESH_MAX_Y         " STRINGIFY(UBL_MESH_MAX_Y));
1246
 
1246
 
1247
-        SERIAL_ECHOPGM("\nMESH_X_DIST        ");
1248
-        SERIAL_ECHO_F(MESH_X_DIST, 6);
1249
-        SERIAL_ECHOPGM("\nMESH_Y_DIST        ");
1250
-        SERIAL_ECHO_F(MESH_Y_DIST, 6);
1251
-        SERIAL_EOL;
1247
+        SERIAL_ECHOLNPGM("MESH_X_DIST        " STRINGIFY(MESH_X_DIST));
1248
+        SERIAL_ECHOLNPGM("MESH_Y_DIST        " STRINGIFY(MESH_Y_DIST));
1252
         SERIAL_EOL;
1249
         SERIAL_EOL;
1253
       }
1250
       }
1254
 
1251
 

+ 16
- 13
Marlin/hex_print_routines.cpp View File

26
 
26
 
27
 #include "hex_print_routines.h"
27
 #include "hex_print_routines.h"
28
 
28
 
29
-void prt_hex_nibble(uint8_t n) {
30
-  if (n <= 9) 
31
-    SERIAL_CHAR('0'+n);
32
-  else
33
-    SERIAL_CHAR('A' + n - 10);
34
-  delay(3);
35
-}
29
+static char _hex[5] = { 0 };
36
 
30
 
37
-void prt_hex_byte(uint8_t b) {
38
-  prt_hex_nibble((b & 0xF0) >> 4);
39
-  prt_hex_nibble(b & 0x0F);
31
+char* hex_byte(const uint8_t b) {
32
+  _hex[0] = hex_nybble(b >> 4);
33
+  _hex[1] = hex_nybble(b);
34
+  _hex[2] = '\0';
35
+  return _hex;
40
 }
36
 }
41
 
37
 
42
-void prt_hex_word(uint16_t w) {
43
-  prt_hex_byte((w & 0xFF00) >> 8);
44
-  prt_hex_byte(w & 0x0FF);
38
+char* hex_word(const uint16_t w) {
39
+  _hex[0] = hex_nybble(w >> 12);
40
+  _hex[1] = hex_nybble(w >> 8);
41
+  _hex[2] = hex_nybble(w >> 4);
42
+  _hex[3] = hex_nybble(w);
43
+  return _hex;
45
 }
44
 }
46
 
45
 
46
+void print_hex_nybble(const uint8_t n) { SERIAL_CHAR(hex_nybble(n));  }
47
+void print_hex_byte(const uint8_t b)   { SERIAL_ECHO(hex_byte(b)); }
48
+void print_hex_word(const uint16_t w)  { SERIAL_ECHO(hex_word(w)); }
49
+
47
 #endif // AUTO_BED_LEVELING_UBL || M100_FREE_MEMORY_WATCHER
50
 #endif // AUTO_BED_LEVELING_UBL || M100_FREE_MEMORY_WATCHER

+ 16
- 4
Marlin/hex_print_routines.h View File

23
 #ifndef HEX_PRINT_ROUTINES_H
23
 #ifndef HEX_PRINT_ROUTINES_H
24
 #define HEX_PRINT_ROUTINES_H
24
 #define HEX_PRINT_ROUTINES_H
25
 
25
 
26
+#include "MarlinConfig.h"
27
+
28
+#if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(M100_FREE_MEMORY_WATCHER)
29
+
26
 //
30
 //
27
-// 3 support routines to print hex numbers.  We can print a nibble, byte and word
31
+// Utility functions to create and print hex strings as nybble, byte, and word.
28
 //
32
 //
29
-void prt_hex_nibble(uint8_t n);
30
-void prt_hex_byte(uint8_t b);
31
-void prt_hex_word(uint16_t w);
32
 
33
 
34
+inline char hex_nybble(const uint8_t n) {
35
+  return (n & 0xF) + ((n & 0xF) < 10 ? '0' : 'A' - 10);
36
+}
37
+char* hex_byte(const uint8_t b);
38
+char* hex_word(const uint16_t w);
39
+
40
+void print_hex_nybble(const uint8_t n);
41
+void print_hex_byte(const uint8_t b);
42
+void print_hex_word(const uint16_t w);
43
+
44
+#endif // AUTO_BED_LEVELING_UBL || M100_FREE_MEMORY_WATCHER
33
 #endif // HEX_PRINT_ROUTINES_H
45
 #endif // HEX_PRINT_ROUTINES_H

+ 1
- 1
Marlin/stepper.cpp View File

1265
     #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_BABYSTEP
1265
     #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_BABYSTEP
1266
       uint32_t pulse_start;
1266
       uint32_t pulse_start;
1267
     #endif
1267
     #endif
1268
-    
1268
+
1269
     switch (axis) {
1269
     switch (axis) {
1270
 
1270
 
1271
       case X_AXIS:
1271
       case X_AXIS:

+ 4
- 24
Marlin/ultralcd.cpp View File

859
     static int ubl_encoderPosition = 0;
859
     static int ubl_encoderPosition = 0;
860
 
860
 
861
     static void _lcd_mesh_fine_tune(const char* msg) {
861
     static void _lcd_mesh_fine_tune(const char* msg) {
862
-//    static millis_t next_click = 0;             // We are going to accelerate the number speed when the wheel
863
-//                                                // turns fast.   But that isn't implemented yet
864
-      int16_t last_digit;
865
-      int32_t rounded;
866
-
867
       defer_return_to_status = true;
862
       defer_return_to_status = true;
868
       if (ubl_encoderDiff) {
863
       if (ubl_encoderDiff) {
869
-        if ( ubl_encoderDiff > 0 ) 
870
-          ubl_encoderPosition = 1;
871
-        else {
872
-          ubl_encoderPosition = -1;
873
-        }
874
-
864
+        ubl_encoderPosition = (ubl_encoderDiff > 0) ? 1 : -1;
875
         ubl_encoderDiff = 0;
865
         ubl_encoderDiff = 0;
876
-//      next_click = millis();
877
 
866
 
878
-        mesh_edit_accumulator += ( (float) (ubl_encoderPosition)) * .005 / 2.0 ;
867
+        mesh_edit_accumulator += float(ubl_encoderPosition) * 0.005 / 2.0;
879
         mesh_edit_value = mesh_edit_accumulator;
868
         mesh_edit_value = mesh_edit_accumulator;
880
         encoderPosition = 0;
869
         encoderPosition = 0;
881
         lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
870
         lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
882
 
871
 
883
-        rounded = (int32_t)(mesh_edit_value * 1000.0);
884
-        last_digit = rounded % 5L; //10L;
885
-        rounded -= last_digit;
886
-        mesh_edit_value = float(rounded) / 1000.0;
872
+        const int32_t rounded = (int32_t)(mesh_edit_value * 1000.0);
873
+        mesh_edit_value = float(rounded - (rounded % 5L)) / 1000.0;
887
       }
874
       }
888
 
875
 
889
       if (lcdDrawUpdate)
876
       if (lcdDrawUpdate)
890
         lcd_implementation_drawedit(msg, ftostr43sign(mesh_edit_value));
877
         lcd_implementation_drawedit(msg, ftostr43sign(mesh_edit_value));
891
     }
878
     }
892
 
879
 
893
-
894
     void _lcd_mesh_edit_NOP() {
880
     void _lcd_mesh_edit_NOP() {
895
       defer_return_to_status = true;
881
       defer_return_to_status = true;
896
     }
882
     }
897
 
883
 
898
-
899
     void _lcd_mesh_edit() {
884
     void _lcd_mesh_edit() {
900
       _lcd_mesh_fine_tune(PSTR("Mesh Editor: "));
885
       _lcd_mesh_fine_tune(PSTR("Mesh Editor: "));
901
-      defer_return_to_status = true;
902
     }
886
     }
903
 
887
 
904
     float lcd_mesh_edit() {
888
     float lcd_mesh_edit() {
905
       lcd_goto_screen(_lcd_mesh_edit_NOP);
889
       lcd_goto_screen(_lcd_mesh_edit_NOP);
906
       _lcd_mesh_fine_tune(PSTR("Mesh Editor: "));
890
       _lcd_mesh_fine_tune(PSTR("Mesh Editor: "));
907
-      defer_return_to_status = true;
908
       return mesh_edit_value;
891
       return mesh_edit_value;
909
     }
892
     }
910
 
893
 
911
     void lcd_mesh_edit_setup(float initial) {
894
     void lcd_mesh_edit_setup(float initial) {
912
       mesh_edit_value = mesh_edit_accumulator = initial;
895
       mesh_edit_value = mesh_edit_accumulator = initial;
913
       lcd_goto_screen(_lcd_mesh_edit_NOP);
896
       lcd_goto_screen(_lcd_mesh_edit_NOP);
914
-      mesh_edit_value = mesh_edit_accumulator = initial;
915
-      defer_return_to_status = true; 
916
     }
897
     }
917
 
898
 
918
     void _lcd_z_offset_edit() {
899
     void _lcd_z_offset_edit() {
919
       _lcd_mesh_fine_tune(PSTR("Z-Offset: "));
900
       _lcd_mesh_fine_tune(PSTR("Z-Offset: "));
920
-      defer_return_to_status = true;
921
     }
901
     }
922
 
902
 
923
     float lcd_z_offset_edit() {
903
     float lcd_z_offset_edit() {

Loading…
Cancel
Save