From 4937f9ada4b2441cc2d2f84e3acd7f60e35f23f8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Br=C3=A1zio?= <jbrazio@gmail.com>
Date: Thu, 14 Jul 2016 00:07:36 +0100
Subject: [PATCH] Minor G12 tweaks and point_t struct extension

---
 Marlin/Marlin_main.cpp | 13 ++++------
 Marlin/SanityCheck.h   |  4 +--
 Marlin/nozzle.h        | 55 +++++++++++++++++++++++++++++-------------
 Marlin/point_t.h       | 13 ++++++++++
 4 files changed, 58 insertions(+), 27 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 835b20fcd47..523f8cf3268 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -2717,18 +2717,15 @@ inline void gcode_G4() {
 
 #endif //FWRETRACT
 
-#if ENABLED(CLEAN_NOZZLE_FEATURE) && ENABLED(AUTO_BED_LEVELING_FEATURE)
+#if ENABLED(NOZZLE_CLEAN_FEATURE) && ENABLED(AUTO_BED_LEVELING_FEATURE)
   #include "nozzle.h"
 
   inline void gcode_G12() {
     // Don't allow nozzle cleaning without homing first
-    if (!axis_homed[X_AXIS] || !axis_homed[Y_AXIS] || !axis_homed[Z_AXIS]) {
-      axis_unhomed_error(true);
-      return;
-    }
+    if (axis_unhomed_error(true, true, true)) { return; }
 
     uint8_t const pattern = code_seen('P') ? code_value_ushort() : 0;
-    uint8_t const strokes = code_seen('S') ? code_value_ushort() : CLEAN_NOZZLE_STROKES;
+    uint8_t const strokes = code_seen('S') ? code_value_ushort() : NOZZLE_CLEAN_STROKES;
     uint8_t const objects = code_seen('T') ? code_value_ushort() : 3;
 
     Nozzle::clean(pattern, strokes, objects);
@@ -6796,11 +6793,11 @@ void process_next_command() {
           break;
       #endif // FWRETRACT
 
-      #if ENABLED(CLEAN_NOZZLE_FEATURE) && ENABLED(AUTO_BED_LEVELING_FEATURE)
+      #if ENABLED(NOZZLE_CLEAN_FEATURE) && HAS_BED_PROBE
         case 12:
           gcode_G12(); // G12: Clean Nozzle
           break;
-      #endif // CLEAN_NOZZLE_FEATURE
+      #endif // NOZZLE_CLEAN_FEATURE
 
       #if ENABLED(INCH_MODE_SUPPORT)
         case 20: //G20: Inch Mode
diff --git a/Marlin/SanityCheck.h b/Marlin/SanityCheck.h
index 81205006624..8334b12893a 100644
--- a/Marlin/SanityCheck.h
+++ b/Marlin/SanityCheck.h
@@ -649,8 +649,8 @@
 /**
  * Nozzle cleaning
  */
-#if ENABLED(CLEAN_NOZZLE_FEATURE) && DISABLED(AUTO_BED_LEVELING_FEATURE)
-  #error You must enable AUTO_BED_LEVELING_FEATURE for CLEAN_NOZZLE_FEATURE to work
+#if ENABLED(NOZZLE_CLEAN_FEATURE) && !HAS_BED_PROBE
+  #error Due to internal dependencies you must have a bed probe for NOZZLE_CLEAN_FEATURE to work
 #endif
 
 #endif //SANITYCHECK_H
diff --git a/Marlin/nozzle.h b/Marlin/nozzle.h
index 796d18b3499..02771a51648 100644
--- a/Marlin/nozzle.h
+++ b/Marlin/nozzle.h
@@ -20,8 +20,8 @@
  *
  */
 
-#ifndef __CLEAN_NOZZLE_H__
-#define __CLEAN_NOZZLE_H__
+#ifndef __NOZZLE_H__
+#define __NOZZLE_H__
 
 #include "Marlin.h"
 #include "point_t.h"
@@ -30,7 +30,7 @@
  * @brief Nozzle class
  *
  * @todo: Do not ignore the end.z value and allow XYZ movements
- * @todo: Currently this feature needs AUTO_BED_LEVELING_FEATURE to be active
+ * @todo: Currently this feature needs HAS_BED_PROBE to be active
  *  due to the do_blocking_move_to*() functions.
  */
 class Nozzle {
@@ -45,6 +45,17 @@ class Nozzle {
      */
     static void stroke(point_t const &start, point_t const &end, uint8_t const &strokes)
     __attribute__ ((optimize ("Os"))) {
+
+      #if ENABLED(NOZZLE_CLEAN_PARK)
+        // Store the current coords
+        point_t const initial = {
+          current_position[X_AXIS],
+          current_position[Y_AXIS],
+          current_position[Z_AXIS],
+          current_position[E_AXIS]
+        };
+      #endif
+
       // Move to the starting point
       do_blocking_move_to_xy(start.x, start.y);
       do_blocking_move_to_z(start.z);
@@ -54,6 +65,12 @@ class Nozzle {
         do_blocking_move_to_xy(end.x, end.y);
         do_blocking_move_to_xy(start.x, start.y);
       }
+
+      #if ENABLED(NOZZLE_CLEAN_PARK)
+        // Move the nozzle to the initial point
+        do_blocking_move_to_z(initial.z);
+        do_blocking_move_to_xy(initial.x, initial.y);
+      #endif
     }
 
     /**
@@ -74,13 +91,15 @@ class Nozzle {
       // Don't allow impossible triangles
       if (A <= 0.0f || P <= 0.0f ) return;
 
-      // Store the current coords
-      point_t const home = {
-        current_position[X_AXIS],
-        current_position[Y_AXIS],
-        current_position[Z_AXIS],
-        current_position[E_AXIS]
-      };
+      #if ENABLED(NOZZLE_CLEAN_PARK)
+        // Store the current coords
+        point_t const initial = {
+          current_position[X_AXIS],
+          current_position[Y_AXIS],
+          current_position[Z_AXIS],
+          current_position[E_AXIS]
+        };
+      #endif
 
       for (uint8_t j = 0; j < strokes; j++) {
         for (uint8_t i = 0; i < (objects << 1); i++) {
@@ -99,9 +118,11 @@ class Nozzle {
         }
       }
 
-      // Move to home/start position
-      do_blocking_move_to_z(home.z);
-      do_blocking_move_to_xy(home.x, home.y);
+      #if ENABLED(NOZZLE_CLEAN_PARK)
+        // Move the nozzle to the initial point
+        do_blocking_move_to_z(initial.z);
+        do_blocking_move_to_xy(initial.x, initial.y);
+      #endif
     }
 
   public:
@@ -118,14 +139,14 @@ class Nozzle {
       switch (pattern) {
         case 1:
           Nozzle::zigzag(
-            CLEAN_NOZZLE_START_PT,
-            CLEAN_NOZZLE_END_PT, strokes, objects);
+            NOZZLE_CLEAN_START_PT,
+            NOZZLE_CLEAN_END_PT, strokes, objects);
           break;
 
         default:
           Nozzle::stroke(
-            CLEAN_NOZZLE_START_PT,
-            CLEAN_NOZZLE_END_PT, strokes);
+            NOZZLE_CLEAN_START_PT,
+            NOZZLE_CLEAN_END_PT, strokes);
       }
     }
 };
diff --git a/Marlin/point_t.h b/Marlin/point_t.h
index 18dfe09c8f4..dbad668581d 100644
--- a/Marlin/point_t.h
+++ b/Marlin/point_t.h
@@ -28,6 +28,19 @@ struct point_t {
   float y;
   float z;
   float e;
+
+  point_t(float const x, float const y)
+    : point_t(x, y, NAN, NAN) {}
+
+  point_t(float const x, float const y, float const z)
+    : point_t(x, y, z, NAN) {}
+
+  point_t(float const x, float const y, float const z, float const e) {
+    this->x = x;
+    this->y = y;
+    this->z = z;
+    this->e = e;
+  }
 };
 
 #endif