From 091f94a6bfa7447d9651147fe63c3445d764b4f0 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Sat, 13 May 2017 00:43:12 -0500
Subject: [PATCH] Fix up M421 and some comments

---
 Marlin/Marlin_main.cpp | 114 +++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 72 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 70d5f36e6d..01151bc968 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -170,6 +170,8 @@
  * M302 - Allow cold extrudes, or set the minimum extrude S<temperature>. (Requires PREVENT_COLD_EXTRUSION)
  * M303 - PID relay autotune S<temperature> sets the target temperature. Default 150C. (Requires PIDTEMP)
  * M304 - Set bed PID parameters P I and D. (Requires PIDTEMPBED)
+ * M350 - Set microstepping mode. (Requires digital microstepping pins.)
+ * M351 - Toggle MS1 MS2 pins directly. (Requires digital microstepping pins.)
  * M355 - Turn the Case Light on/off and set its brightness. (Requires CASE_LIGHT_PIN)
  * M380 - Activate solenoid on active extruder. (Requires EXT_SOLENOID)
  * M381 - Disable all solenoids. (Requires EXT_SOLENOID)
@@ -194,6 +196,7 @@
  * M666 - Set delta endstop adjustment. (Requires DELTA)
  * M605 - Set dual x-carriage movement mode: "M605 S<mode> [X<x_offset>] [R<temp_offset>]". (Requires DUAL_X_CARRIAGE)
  * M851 - Set Z probe's Z offset in current units. (Negative = below the nozzle.)
+ * M900 - Get and/or Set advance K factor and WH/D ratio. (Requires LIN_ADVANCE)
  * M906 - Set or get motor current in milliamps using axis codes X, Y, Z, E. Report values if no axis codes given. (Requires HAVE_TMC2130)
  * M907 - Set digital trimpot motor current using axis codes. (Requires a board with digital trimpots)
  * M908 - Control digital trimpot directly. (Requires DAC_STEPPER_CURRENT or DIGIPOTSS_PIN)
@@ -203,8 +206,6 @@
  * M912 - Clear stepper driver overtemperature pre-warn condition flag. (Requires HAVE_TMC2130)
  * M913 - Set HYBRID_THRESHOLD speed. (Requires HYBRID_THRESHOLD)
  * M914 - Set SENSORLESS_HOMING sensitivity. (Requires SENSORLESS_HOMING)
- * M350 - Set microstepping mode. (Requires digital microstepping pins.)
- * M351 - Toggle MS1 MS2 pins directly. (Requires digital microstepping pins.)
  *
  * M360 - SCARA calibration: Move to cal-position ThetaA (0 deg calibration)
  * M361 - SCARA calibration: Move to cal-position ThetaB (90 deg calibration - steps per degree)
@@ -7141,7 +7142,7 @@ inline void gcode_M82() { axis_relative_modes[E_AXIS] = false; }
 inline void gcode_M83() { axis_relative_modes[E_AXIS] = true; }
 
 /**
- * M18, M84: Disable all stepper motors
+ * M18, M84: Disable stepper motors
  */
 inline void gcode_M18_M84() {
   if (code_seen('S')) {
@@ -8166,7 +8167,7 @@ inline void gcode_M303() {
   }
 
   /**
-   * M364: SCARA calibration: Move to cal-position PSIC (90 deg to Theta calibration position)
+   * M364: SCARA calibration: Move to cal-position PsiC (90 deg to Theta calibration position)
    */
   inline bool gcode_M364() {
     SERIAL_ECHOLNPGM(" Cal: Theta-Psi 90");
@@ -8409,39 +8410,33 @@ void quickstop_stepper() {
 #endif
 
 #if ENABLED(MESH_BED_LEVELING)
+
   /**
    * M421: Set a single Mesh Bed Leveling Z coordinate
-   * Use either 'M421 X<linear> Y<linear> Z<linear>' or 'M421 I<xindex> J<yindex> Z<linear>'
+   *
+   * Usage:
+   *   M421 X<linear> Y<linear> Z<linear>
+   *   M421 X<linear> Y<linear> Q<offset>
+   *   M421 I<xindex> J<yindex> Z<linear>
+   *   M421 I<xindex> J<yindex> Q<offset>
    */
   inline void gcode_M421() {
+    const bool hasX = code_seen('X'), hasI = code_seen('I');
+    const int8_t ix = hasI ? code_value_byte() : hasX ? mbl.probe_index_x(RAW_X_POSITION(code_value_linear_units())) : -1;
+    const bool hasY = code_seen('Y'), hasJ = code_seen('J');
+    const int8_t iy = hasJ ? code_value_byte() : hasY ? mbl.probe_index_y(RAW_Y_POSITION(code_value_linear_units())) : -1;
+    const bool hasZ = code_seen('Z'), hasQ = code_seen('Q');
 
-    const bool hasX = code_seen('X'), hasI = !hasX && code_seen('I');
-    const int8_t px = hasX || hasI ? mbl.probe_index_x(code_value_linear_units()) : 0;
-    const bool hasY = code_seen('Y'), hasJ = !hasY && code_seen('J');
-    const int8_t py = hasY || hasJ ? mbl.probe_index_y(code_value_linear_units()) : 0;
-    const bool hasZ = code_seen('Z');
-    const float z = hasZ ? code_value_linear_units() : 0;
-
-    if (hasX && hasY && hasZ) {
-      if (px >= 0 && py >= 0)
-        mbl.set_z(px, py, z);
-      else {
-        SERIAL_ERROR_START;
-        SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY);
-      }
-    }
-    else if (hasI && hasJ && hasZ) {
-      if (WITHIN(px, 0, GRID_MAX_POINTS_X - 1) && WITHIN(py, 0, GRID_MAX_POINTS_Y - 1))
-        mbl.set_z(px, py, z);
-      else {
-        SERIAL_ERROR_START;
-        SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY);
-      }
-    }
-    else {
+    if (int(hasI && hasJ) + int(hasX && hasY) != 1 || hasZ == hasQ) {
       SERIAL_ERROR_START;
       SERIAL_ERRORLNPGM(MSG_ERR_M421_PARAMETERS);
     }
+    else if (ix < 0 || iy < 0) {
+      SERIAL_ERROR_START;
+      SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY);
+    }
+    else
+      mbl.set_z(ix, iy, code_value_linear_units() + (hasQ ? mbl.z_values[ix][iy] : 0));
   }
 
 #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
@@ -8454,38 +8449,26 @@ void quickstop_stepper() {
    *   M421 I<xindex> J<yindex> Q<offset>
    */
   inline void gcode_M421() {
-
     const bool hasI = code_seen('I');
-    const int8_t px = hasI ? code_value_int() : 0;
+    const int8_t ix = hasI ? code_value_byte() : -1;
     const bool hasJ = code_seen('J');
-    const int8_t py = hasJ ? code_value_int() : 0;
-    const bool hasZ = code_seen('Z'), hasQ = !hasZ && code_seen('Q');
-    const float z = hasZ || hasQ ? code_value_linear_units() : 0;
+    const int8_t iy = hasJ ? code_value_byte() : -1;
+    const bool hasZ = code_seen('Z'), hasQ = code_seen('Q');
 
-    if (!hasI || !hasJ || (hasQ && hasZ) || (!hasQ && !hasZ)) {
+    if (!hasI || !hasJ || hasZ == hasQ) {
       SERIAL_ERROR_START;
       SERIAL_ERRORLNPGM(MSG_ERR_M421_PARAMETERS);
-      return;
     }
-
-    if (WITHIN(px, 0, GRID_MAX_POINTS_X - 1) && WITHIN(py, 0, GRID_MAX_POINTS_Y - 1)) {
-      if (hasZ) { // doing an absolute mesh value
-        z_values[px][py] = z;
-        #if ENABLED(ABL_BILINEAR_SUBDIVISION)
-          bed_level_virt_interpolate();
-        #endif
-      } 
-      else { // doing an offset of a mesh value
-        z_values[px][py] += z;
-        #if ENABLED(ABL_BILINEAR_SUBDIVISION)
-          bed_level_virt_interpolate();
-        #endif
-      }
-    }
-    else { // bad indexes were specified for the mesh point
+    else if (!WITHIN(ix, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(iy, 0, GRID_MAX_POINTS_Y - 1)) {
       SERIAL_ERROR_START;
       SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY);
     }
+    else {
+      z_values[ix][iy] = code_value_linear_units() + (hasQ ? z_values[ix][iy] : 0);
+      #if ENABLED(ABL_BILINEAR_SUBDIVISION)
+        bed_level_virt_interpolate();
+      #endif
+    }
   }
 
 #elif ENABLED(AUTO_BED_LEVELING_UBL)
@@ -8499,37 +8482,24 @@ void quickstop_stepper() {
    *   M421 C Z<linear>
    *   M421 C Q<offset>
    */
-
   inline void gcode_M421() {
-
-    // Get the closest position for 'C', if needed
     const mesh_index_pair location = find_closest_mesh_point_of_type(REAL, current_position[X_AXIS], current_position[Y_AXIS], USE_NOZZLE_AS_REFERENCE, NULL, false);
-
     const bool hasC = code_seen('C'), hasI = code_seen('I');
-    const int8_t px = hasC ? location.x_index : hasI ? code_value_int() : 0;
-
+    const int8_t ix = hasI ? code_value_byte() : hasC ? location.x_index : -1;
     const bool hasJ = code_seen('J');
-    const int8_t py = hasC ? location.y_index : hasJ ? code_value_int() : 0;
+    const int8_t iy = hasJ ? code_value_byte() : hasC ? location.y_index : -1;
+    const bool hasZ = code_seen('Z'), hasQ = code_seen('Q');
 
-    const bool hasZ = code_seen('Z'), hasQ = !hasZ && code_seen('Q');
-    const float z = hasZ || hasQ ? code_value_linear_units() : 0;
-
-    if ( ((hasI && hasJ) == hasC) || (hasQ && hasZ) || (!hasQ && !hasZ)) {
+    if (int(hasC) + int(hasI && hasJ) != 1 || hasZ == hasQ) {
       SERIAL_ERROR_START;
       SERIAL_ERRORLNPGM(MSG_ERR_M421_PARAMETERS);
-      return;
     }
-
-    if (WITHIN(px, 0, GRID_MAX_POINTS_X - 1) && WITHIN(py, 0, GRID_MAX_POINTS_Y - 1)) {
-      if (hasZ) // doing an absolute mesh value
-        ubl.z_values[px][py] = z;
-      else // doing an offset of a mesh value
-        ubl.z_values[px][py] += z;
-    }
-    else { // bad indexes were specified for the mesh point
+    else if (!WITHIN(ix, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(iy, 0, GRID_MAX_POINTS_Y - 1)) {
       SERIAL_ERROR_START;
       SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY);
     }
+    else
+      ubl.z_values[ix][iy] = code_value_linear_units() + (hasQ ? ubl.z_values[ix][iy] : 0);
   }
 
 #endif // AUTO_BED_LEVELING_UBL