temperaturecontrol: allow setting background tool without activating
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / TemperatureControl.cpp
index ba343f9..61bafaa 100644 (file)
@@ -77,7 +77,6 @@ TemperatureControl::TemperatureControl(uint16_t name, int index)
     sensor= nullptr;
     readonly= false;
     tick= 0;
-    single= false;
 }
 
 TemperatureControl::~TemperatureControl()
@@ -98,6 +97,7 @@ 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);
@@ -117,6 +117,12 @@ void TemperatureControl::on_halt(void *arg)
     }
 }
 
+void TemperatureControl::on_idle(void *arg)
+{
+    sensor->on_idle();
+}
+
+
 void TemperatureControl::on_main_loop(void *argument)
 {
     if (this->temp_violated) {
@@ -299,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);
 
@@ -320,23 +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;
+           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(!single) {
+           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);
-                    this->active = (active_tool_name == this->name_checksum);
-                }
-            }
-
-            if(this->active) {
+               // 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');