temperaturecontrol: allow setting background tool without activating
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / TemperatureControl.cpp
index e0e8350..61bafaa 100644 (file)
@@ -97,13 +97,13 @@ void TemperatureControl::on_module_loaded()
     // Register for events
     this->register_for_event(ON_GCODE_RECEIVED);
     this->register_for_event(ON_GET_PUBLIC_DATA);
+    this->register_for_event(ON_IDLE);
 
     if(!this->readonly) {
         this->register_for_event(ON_SECOND_TICK);
         this->register_for_event(ON_MAIN_LOOP);
         this->register_for_event(ON_SET_PUBLIC_DATA);
         this->register_for_event(ON_HALT);
-        this->register_for_event(ON_IDLE);
     }
 }
 
@@ -305,11 +305,11 @@ void TemperatureControl::on_gcode_received(void *argument)
                     this->heater_pin.max_pwm(gcode->get_value('Y'));
 
             }else if(!gcode->has_letter('S')) {
-                gcode->stream->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g max pwm: %d O:%d\n", this->designator.c_str(), this->pool_index, this->p_factor, this->i_factor / this->PIDdt, this->d_factor * this->PIDdt, this->i_max, this->heater_pin.max_pwm(), o);
+                gcode->stream->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g Y(max pwm):%d O:%d\n", this->designator.c_str(), this->pool_index, this->p_factor, this->i_factor / this->PIDdt, this->d_factor * this->PIDdt, this->i_max, this->heater_pin.max_pwm(), o);
             }
 
         } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
-            gcode->stream->printf(";PID settings:\nM301 S%d P%1.4f I%1.4f D%1.4f X%1.4f Y%d\n", this->pool_index, this->p_factor, this->i_factor / this->PIDdt, this->d_factor * this->PIDdt, this->i_max, this->heater_pin.max_pwm());
+            gcode->stream->printf(";PID settings, i_max, max_pwm:\nM301 S%d P%1.4f I%1.4f D%1.4f X%1.4f Y%d\n", this->pool_index, this->p_factor, this->i_factor / this->PIDdt, this->d_factor * this->PIDdt, this->i_max, this->heater_pin.max_pwm());
 
             gcode->stream->printf(";Max temperature setting:\nM143 S%d P%1.4f\n", this->pool_index, this->max_temp);
 
@@ -326,21 +326,34 @@ void TemperatureControl::on_gcode_received(void *argument)
             }
 
         } else if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S')) {
-            // this only gets handled if it is not controlled by the tool manager or is active in the toolmanager
-            this->active = true;
-
-            // this is safe as old configs as well as single extruder configs the toolmanager will not be running so will return false
-            // this will also ignore anything that the tool manager is not controlling and return false, otherwise it returns the active tool
-            void *returned_data;
-            bool ok = PublicData::get_value( tool_manager_checksum, is_active_tool_checksum, this->name_checksum, &returned_data );
-            if (ok) {
-                uint16_t active_tool_name =  *static_cast<uint16_t *>(returned_data);
-                this->active = (active_tool_name == this->name_checksum);
-            }
-
-            if(this->active) {
+           int tool = gcode->has_letter('T') ? gcode->get_value('T') : -1;
+           // default on when no T, for unmanaged tools (e.g. heatbed)
+           bool am_tool = (tool == -1) ? true : false;
+
+           if (tool == -1) {
+                // this is safe as old configs as well as single extruder configs the toolmanager will not be running so will return false
+               // this will also ignore anything that the tool manager is not controlling and return false, otherwise it returns the active tool
+               void *returned_data;
+               bool ok = PublicData::get_value( tool_manager_checksum, is_active_tool_checksum, this->name_checksum, &returned_data );
+               if (ok) {
+                   uint16_t active_tool_name =  *static_cast<uint16_t *>(returned_data);
+                   am_tool = (active_tool_name == this->name_checksum);
+               }
+           } else {
+               // FIXME
+               // this is wrong because we can't get the names of any
+               // inactive tools to check that we are associated with
+               // one. Just assume one temperature control per
+               // tool...
+               am_tool = (tool == this->pool_index);
+           }
+
+            if(am_tool) {
                 // required so temp change happens in order
-                THEKERNEL->conveyor->wait_for_idle();
+               // Allow set without wait to be processed immediately
+               if( gcode->m == this->set_and_wait_m_code ) {
+                   THEKERNEL->conveyor->wait_for_idle();
+               }
 
                 float v = gcode->get_value('S');