From 4f1eadf41f1e2d79f7c62c60a1ead6ae34b63be5 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Tue, 12 Sep 2017 15:02:17 -0500
Subject: [PATCH] FILAMENT_WIDTH_SENSOR feature

---
 Marlin/src/Marlin.cpp                         | 16 -------
 Marlin/src/Marlin.h                           |  9 ----
 .../sensor/M406.h => feature/filwidth.cpp}    | 21 ++++++---
 .../sensor/M404.h => feature/filwidth.h}      | 25 +++++------
 .../M405.h => feature/filwidth/M404-M407.cpp} | 43 ++++++++++++++++++-
 Marlin/src/gcode/gcode.cpp                    | 17 ++------
 Marlin/src/gcode/sensor/M407.h                | 29 -------------
 Marlin/src/lcd/ultralcd.cpp                   |  4 --
 Marlin/src/module/planner.cpp                 |  4 ++
 Marlin/src/module/temperature.cpp             |  4 ++
 10 files changed, 81 insertions(+), 91 deletions(-)
 rename Marlin/src/{gcode/sensor/M406.h => feature/filwidth.cpp} (53%)
 rename Marlin/src/{gcode/sensor/M404.h => feature/filwidth.h} (53%)
 rename Marlin/src/gcode/{sensor/M405.h => feature/filwidth/M404-M407.cpp} (64%)
 delete mode 100644 Marlin/src/gcode/sensor/M407.h

diff --git a/Marlin/src/Marlin.cpp b/Marlin/src/Marlin.cpp
index 51de309d3e..3cb9136314 100644
--- a/Marlin/src/Marlin.cpp
+++ b/Marlin/src/Marlin.cpp
@@ -186,15 +186,6 @@ static millis_t stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL
   ;
 #endif
 
-#if ENABLED(FILAMENT_WIDTH_SENSOR)
-  bool filament_sensor = false;                                 // M405 turns on filament sensor control. M406 turns it off.
-  float filament_width_nominal = DEFAULT_NOMINAL_FILAMENT_DIA,  // Nominal filament width. Change with M404.
-        filament_width_meas = DEFAULT_MEASURED_FILAMENT_DIA;    // Measured filament diameter
-  uint8_t meas_delay_cm = MEASUREMENT_DELAY_CM,                 // Distance delay setting
-          measurement_delay[MAX_MEASUREMENT_DELAY + 1];         // Ring buffer to delayed measurement. Store extruder factor after subtracting 100
-  int8_t filwidth_delay_index[2] = { 0, -1 };                   // Indexes into ring buffer
-#endif
-
 #if ENABLED(FILAMENT_RUNOUT_SENSOR)
   static bool filament_ran_out = false;
 #endif
@@ -667,13 +658,6 @@ static bool pin_is_protected(const int8_t pin) {
   #include "gcode/probe/M401_M402.h"
 #endif
 
-#if ENABLED(FILAMENT_WIDTH_SENSOR)
-  #include "gcode/sensor/M404.h"
-  #include "gcode/sensor/M405.h"
-  #include "gcode/sensor/M406.h"
-  #include "gcode/sensor/M407.h"
-#endif
-
 void quickstop_stepper() {
   stepper.quick_stop();
   stepper.synchronize();
diff --git a/Marlin/src/Marlin.h b/Marlin/src/Marlin.h
index 9e69068cfd..7695f03e5b 100644
--- a/Marlin/src/Marlin.h
+++ b/Marlin/src/Marlin.h
@@ -211,15 +211,6 @@ extern volatile bool wait_for_heatup;
   extern uint8_t baricuda_valve_pressure, baricuda_e_to_p_pressure;
 #endif
 
-#if ENABLED(FILAMENT_WIDTH_SENSOR)
-  extern bool filament_sensor;         // Flag that filament sensor readings should control extrusion
-  extern float filament_width_nominal, // Theoretical filament diameter i.e., 3.00 or 1.75
-               filament_width_meas;    // Measured filament diameter
-  extern uint8_t meas_delay_cm,        // Delay distance
-                 measurement_delay[];  // Ring buffer to delay measurement
-  extern int8_t filwidth_delay_index[2]; // Ring buffer indexes. Used by planner, temperature, and main code
-#endif
-
 #if ENABLED(ADVANCED_PAUSE_FEATURE)
   extern AdvancedPauseMenuResponse advanced_pause_menu_response;
 #endif
diff --git a/Marlin/src/gcode/sensor/M406.h b/Marlin/src/feature/filwidth.cpp
similarity index 53%
rename from Marlin/src/gcode/sensor/M406.h
rename to Marlin/src/feature/filwidth.cpp
index 0074d8eb58..e43a9ccd7d 100644
--- a/Marlin/src/gcode/sensor/M406.h
+++ b/Marlin/src/feature/filwidth.cpp
@@ -20,10 +20,17 @@
  *
  */
 
-/**
- * M406: Turn off filament sensor for control
- */
-void gcode_M406() {
-  filament_sensor = false;
-  calculate_volumetric_multipliers();   // Restore correct 'volumetric_multiplier' value
-}
+#include "../inc/MarlinConfig.h"
+
+#if ENABLED(FILAMENT_WIDTH_SENSOR)
+
+#include "filwidth.h"
+
+bool filament_sensor = false;                                 // M405/M406 turns filament sensor control ON/OFF.
+float filament_width_nominal = DEFAULT_NOMINAL_FILAMENT_DIA,  // Nominal filament width. Change with M404.
+      filament_width_meas = DEFAULT_MEASURED_FILAMENT_DIA;    // Measured filament diameter
+uint8_t meas_delay_cm = MEASUREMENT_DELAY_CM,                 // Distance delay setting
+        measurement_delay[MAX_MEASUREMENT_DELAY + 1];         // Ring buffer to delayed measurement. Store extruder factor after subtracting 100
+int8_t filwidth_delay_index[2] = { 0, -1 };                   // Indexes into ring buffer
+
+#endif // FILAMENT_WIDTH_SENSOR
diff --git a/Marlin/src/gcode/sensor/M404.h b/Marlin/src/feature/filwidth.h
similarity index 53%
rename from Marlin/src/gcode/sensor/M404.h
rename to Marlin/src/feature/filwidth.h
index f94cdc2dcc..4368059390 100644
--- a/Marlin/src/gcode/sensor/M404.h
+++ b/Marlin/src/feature/filwidth.h
@@ -20,15 +20,16 @@
  *
  */
 
-/**
- * M404: Display or set (in current units) the nominal filament width (3mm, 1.75mm ) W<3.0>
- */
-void gcode_M404() {
-  if (parser.seen('W')) {
-    filament_width_nominal = parser.value_linear_units();
-  }
-  else {
-    SERIAL_PROTOCOLPGM("Filament dia (nominal mm):");
-    SERIAL_PROTOCOLLN(filament_width_nominal);
-  }
-}
+#ifndef __FILWIDTH_H__
+#define __FILWIDTH_H__
+
+#include "../inc/MarlinConfig.h"
+
+extern bool filament_sensor;                                  // M405/M406 turns filament sensor control ON/OFF.
+extern float filament_width_nominal,                          // Nominal filament width. Change with M404.
+             filament_width_meas;                             // Measured filament diameter
+extern uint8_t meas_delay_cm,                                 // Distance delay setting
+               measurement_delay[MAX_MEASUREMENT_DELAY + 1];  // Ring buffer to delayed measurement. Store extruder factor after subtracting 100
+extern int8_t filwidth_delay_index[2];                        // Indexes into ring buffer
+
+#endif // __FILWIDTH_H__
diff --git a/Marlin/src/gcode/sensor/M405.h b/Marlin/src/gcode/feature/filwidth/M404-M407.cpp
similarity index 64%
rename from Marlin/src/gcode/sensor/M405.h
rename to Marlin/src/gcode/feature/filwidth/M404-M407.cpp
index bd1ed80880..42ac9b622a 100644
--- a/Marlin/src/gcode/sensor/M405.h
+++ b/Marlin/src/gcode/feature/filwidth/M404-M407.cpp
@@ -20,10 +20,33 @@
  *
  */
 
+#include "../../../inc/MarlinConfig.h"
+
+#if ENABLED(FILAMENT_WIDTH_SENSOR)
+
+#include "../../../feature/filwidth.h"
+#include "../../../module/planner.h"
+#include "../../../module/temperature.h"
+#include "../../../Marlin.h"
+#include "../../gcode.h"
+
+/**
+ * M404: Display or set (in current units) the nominal filament width (3mm, 1.75mm ) W<3.0>
+ */
+void GcodeSuite::M404() {
+  if (parser.seen('W')) {
+    filament_width_nominal = parser.value_linear_units();
+  }
+  else {
+    SERIAL_PROTOCOLPGM("Filament dia (nominal mm):");
+    SERIAL_PROTOCOLLN(filament_width_nominal);
+  }
+}
+
 /**
  * M405: Turn on filament sensor for control
  */
-void gcode_M405() {
+void GcodeSuite::M405() {
   // This is technically a linear measurement, but since it's quantized to centimeters and is a different
   // unit than everything else, it uses parser.value_byte() instead of parser.value_linear_units().
   if (parser.seen('D')) {
@@ -47,3 +70,21 @@ void gcode_M405() {
   //SERIAL_PROTOCOLPGM("Extrusion ratio(%):");
   //SERIAL_PROTOCOL(planner.flow_percentage[active_extruder]);
 }
+
+/**
+ * M406: Turn off filament sensor for control
+ */
+void GcodeSuite::M406() {
+  filament_sensor = false;
+  calculate_volumetric_multipliers();   // Restore correct 'volumetric_multiplier' value
+}
+
+/**
+ * M407: Get measured filament diameter on serial output
+ */
+void GcodeSuite::M407() {
+  SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
+  SERIAL_PROTOCOLLN(filament_width_meas);
+}
+
+#endif // FILAMENT_WIDTH_SENSOR
diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp
index 3d92736aee..530a689468 100644
--- a/Marlin/src/gcode/gcode.cpp
+++ b/Marlin/src/gcode/gcode.cpp
@@ -102,11 +102,6 @@ void GcodeSuite::get_destination_from_command() {
 //
 // Placeholders for non-migrated codes
 //
-extern void gcode_G0_G1(
-  #if IS_SCARA
-    bool fast_move=false
-  #endif
-);
 extern void gcode_G2_G3(bool clockwise);
 extern void gcode_G4();
 extern void gcode_G5();
@@ -216,10 +211,6 @@ extern void gcode_M381();
 extern void gcode_M400();
 extern void gcode_M401();
 extern void gcode_M402();
-extern void gcode_M404();
-extern void gcode_M405();
-extern void gcode_M406();
-extern void gcode_M407();
 extern void gcode_M410();
 extern void gcode_M428();
 extern void gcode_M500();
@@ -871,16 +862,16 @@ void GcodeSuite::process_next_command() {
 
       #if ENABLED(FILAMENT_WIDTH_SENSOR)
         case 404:  // M404: Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width
-          gcode_M404();
+          M404();
           break;
         case 405:  // M405: Turn on filament sensor for control
-          gcode_M405();
+          M405();
           break;
         case 406:  // M406: Turn off filament sensor for control
-          gcode_M406();
+          M406();
           break;
         case 407:   // M407: Display measured filament diameter
-          gcode_M407();
+          M407();
           break;
       #endif // FILAMENT_WIDTH_SENSOR
 
diff --git a/Marlin/src/gcode/sensor/M407.h b/Marlin/src/gcode/sensor/M407.h
deleted file mode 100644
index a68bc89632..0000000000
--- a/Marlin/src/gcode/sensor/M407.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/**
- * Marlin 3D Printer Firmware
- * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
- *
- * Based on Sprinter and grbl.
- * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-/**
- * M407: Get measured filament diameter on serial output
- */
-void gcode_M407() {
-  SERIAL_PROTOCOLPGM("Filament dia (measured mm):");
-  SERIAL_PROTOCOLLN(filament_width_meas);
-}
diff --git a/Marlin/src/lcd/ultralcd.cpp b/Marlin/src/lcd/ultralcd.cpp
index 49ce24d5b8..e4bbf00448 100644
--- a/Marlin/src/lcd/ultralcd.cpp
+++ b/Marlin/src/lcd/ultralcd.cpp
@@ -47,10 +47,6 @@
   #include "../feature/filwidth.h"
 #endif
 
-#if HAS_BED_PROBE
-  #include "../module/probe.h"
-#endif
-
 #if ENABLED(BLTOUCH)
   #include "../module/endstops.h"
 #endif
diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp
index c37fe01bb9..d17a321aa3 100644
--- a/Marlin/src/module/planner.cpp
+++ b/Marlin/src/module/planner.cpp
@@ -72,6 +72,10 @@
   #include "../feature/bedlevel/bedlevel.h"
 #endif
 
+#if ENABLED(FILAMENT_WIDTH_SENSOR)
+  #include "../feature/filwidth.h"
+#endif
+
 Planner planner;
 
   // public:
diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp
index 8944e581bf..57eb529f2a 100644
--- a/Marlin/src/module/temperature.cpp
+++ b/Marlin/src/module/temperature.cpp
@@ -45,6 +45,10 @@
 
 #include "printcounter.h"
 
+#if ENABLED(FILAMENT_WIDTH_SENSOR)
+  #include "../feature/filwidth.h"
+#endif
+
 #ifdef K1 // Defined in Configuration.h in the PID settings
   #define K2 (1.0-K1)
 #endif
-- 
GitLab