improved error response of ToolManager
authorLogxen <logxen@hotmail.com>
Tue, 4 Mar 2014 21:58:05 +0000 (13:58 -0800)
committerLogxen <logxen@hotmail.com>
Tue, 4 Mar 2014 21:58:05 +0000 (13:58 -0800)
src/modules/tools/toolmanager/ToolManager.cpp
src/modules/tools/toolmanager/ToolManager.h

index dc58b35..71e4267 100644 (file)
@@ -13,14 +13,23 @@ using namespace std;
 #include "ToolManager.h"
 #include "Conveyor.h"
 
+#define return_error_on_unhandled_gcode_checksum    CHECKSUM("return_error_on_unhandled_gcode")
+
 ToolManager::ToolManager(){
     active_tool = 0;
 }
 
 void ToolManager::on_module_loaded(){
+    this->on_config_reload(this);
+
+    this->register_for_event(ON_CONFIG_RELOAD);
     this->register_for_event(ON_GCODE_RECEIVED);
 }
 
+void ToolManager::on_config_reload(void *argument){
+    return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool();
+}
+
 void ToolManager::on_gcode_received(void *argument){
     Gcode *gcode = static_cast<Gcode*>(argument);
 
@@ -33,8 +42,11 @@ void ToolManager::on_gcode_received(void *argument){
         gcode->mark_as_taken();
         if(new_tool >= (int)this->tools.size() || new_tool < 0){
             // invalid tool
-            // TODO: report invalid selection
-            gcode->stream->printf(";;T%d invalid tool\r\n", new_tool);
+            if( return_error_on_unhandled_gcode ) {
+                char buf[32]; // should be big enough for any status
+                int n= snprintf(buf, sizeof(buf), "T%d invalid tool ", new_tool);
+                gcode->txt_after_ok.append(buf, n);
+            }
         } else {
             if(new_tool != this->active_tool){
                 void *returned_data;
index ddc9589..04b2539 100644 (file)
@@ -19,11 +19,13 @@ class ToolManager : public Module {
 
         void on_module_loaded();
         void on_gcode_received(void*);
+        void on_config_reload(void*);
         void add_tool(Tool* tool_to_add);
 
         vector<Tool*> tools;
 
         int active_tool;
+        bool return_error_on_unhandled_gcode;
 };