Consolidate common spindle functions
authorRandy C. Will <randall.will@gmail.com>
Sun, 19 Nov 2017 20:35:55 +0000 (12:35 -0800)
committerRandy C. Will <randall.will@gmail.com>
Sun, 19 Nov 2017 20:35:55 +0000 (12:35 -0800)
Doing the exact same event registration in every sub-module isn't very
DRY and could lead to inadvertent missed registrations in new
sub-modules.  Consolidating the registrations into `SpindleMaker`.

src/modules/tools/spindle/AnalogSpindleControl.cpp
src/modules/tools/spindle/ModbusSpindleControl.cpp
src/modules/tools/spindle/PWMSpindleControl.cpp
src/modules/tools/spindle/SpindleMaker.cpp

index c53e5c5..76f3668 100644 (file)
@@ -21,7 +21,6 @@
 #define spindle_pwm_pin_checksum            CHECKSUM("pwm_pin")
 #define spindle_pwm_period_checksum         CHECKSUM("pwm_period")
 #define spindle_switch_on_pin_checksum      CHECKSUM("switch_on_pin")
-#define spindle_ignore_on_halt_checksum     CHECKSUM("ignore_on_halt")
 
 void AnalogSpindleControl::on_module_loaded()
 {
@@ -60,11 +59,6 @@ void AnalogSpindleControl::on_module_loaded()
         switch_on = new Pin();
         switch_on->from_string(switch_on_pin)->as_output()->set(false);
     }
-    // register for events
-    register_for_event(ON_GCODE_RECEIVED);
-    if (!THEKERNEL->config->value(spindle_checksum, spindle_ignore_on_halt_checksum)->by_default(false)->as_bool()) {
-        register_for_event(ON_HALT);
-    }
 }
 
 void AnalogSpindleControl::turn_on() 
index ad300b3..42bbb51 100644 (file)
@@ -19,7 +19,6 @@
 #define spindle_rx_pin_checksum             CHECKSUM("rx_pin")
 #define spindle_tx_pin_checksum             CHECKSUM("tx_pin")
 #define spindle_dir_pin_checksum            CHECKSUM("dir_pin")
-#define spindle_ignore_on_halt_checksum     CHECKSUM("ignore_on_halt")
 
 void ModbusSpindleControl::on_module_loaded()
 {
@@ -49,11 +48,5 @@ void ModbusSpindleControl::on_module_loaded()
 
     // setup the Modbus interface
     modbus = new Modbus(tx_pin, rx_pin, dir_pin);
-
-    // register for events
-    register_for_event(ON_GCODE_RECEIVED);
-    if (!THEKERNEL->config->value(spindle_checksum, spindle_ignore_on_halt_checksum)->by_default(false)->as_bool()) {
-        register_for_event(ON_HALT);
-    }
 }
 
index 6ee4f24..84871fb 100644 (file)
@@ -11,7 +11,6 @@
 #include "Config.h"
 #include "checksumm.h"
 #include "ConfigValue.h"
-#include "Gcode.h"
 #include "StreamOutputPool.h"
 #include "SlowTicker.h"
 #include "Conveyor.h"
@@ -35,7 +34,6 @@
 #define spindle_control_I_checksum          CHECKSUM("control_I")
 #define spindle_control_D_checksum          CHECKSUM("control_D")
 #define spindle_control_smoothing_checksum  CHECKSUM("control_smoothing")
-#define spindle_ignore_on_halt_checksum     CHECKSUM("ignore_on_halt")
 
 #define UPDATE_FREQ 1000
 
@@ -108,12 +106,6 @@ void PWMSpindleControl::on_module_loaded()
     }
     
     THEKERNEL->slow_ticker->attach(UPDATE_FREQ, this, &PWMSpindleControl::on_update_speed);
-
-    // register for events
-    register_for_event(ON_GCODE_RECEIVED);
-    if (!THEKERNEL->config->value(spindle_checksum, spindle_ignore_on_halt_checksum)->by_default(false)->as_bool()) {
-        register_for_event(ON_HALT);
-    }
 }
 
 void PWMSpindleControl::on_pin_rise()
index b6a3a9c..d817df9 100644 (file)
 #include "ConfigValue.h"
 #include "StreamOutputPool.h"
 
-#define spindle_checksum            CHECKSUM("spindle")
-#define enable_checksum             CHECKSUM("enable")
-#define spindle_type_checksum       CHECKSUM("type")
-#define spindle_vfd_type_checksum   CHECKSUM("vfd_type")
+#define spindle_checksum                   CHECKSUM("spindle")
+#define enable_checksum                    CHECKSUM("enable")
+#define spindle_type_checksum              CHECKSUM("type")
+#define spindle_vfd_type_checksum          CHECKSUM("vfd_type")
+#define spindle_ignore_on_halt_checksum    CHECKSUM("ignore_on_halt")
 
 void SpindleMaker::load_spindle(){
 
@@ -55,6 +56,12 @@ void SpindleMaker::load_spindle(){
 
     // Add the spindle if we successfully initialized one
     if( spindle != NULL) {
+
+        spindle->register_for_event(ON_GCODE_RECEIVED);
+        if (!THEKERNEL->config->value(spindle_checksum, spindle_ignore_on_halt_checksum)->by_default(false)->as_bool()) {
+            spindle->register_for_event(ON_HALT);
+        }
+
         THEKERNEL->add_module( spindle );
     }