reduce the memory usage of each temperatureswitch.
authorJim Morris <morris@wolfman.com>
Thu, 10 Dec 2015 05:36:22 +0000 (21:36 -0800)
committerJim Morris <morris@wolfman.com>
Thu, 10 Dec 2015 05:36:22 +0000 (21:36 -0800)
  no longer required to keep a vector of temperaturecontrollers

src/modules/tools/temperatureswitch/TemperatureSwitch.cpp
src/modules/tools/temperatureswitch/TemperatureSwitch.h

index f614471..510da51 100755 (executable)
@@ -86,45 +86,24 @@ TemperatureSwitch* TemperatureSwitch::load_config(uint16_t modcs)
 
     if(designator == 0) return nullptr; // no designator then not valid
 
-    // create a new temperature switch module
-    TemperatureSwitch *ts= new TemperatureSwitch();
-
-    //__debugbreak();
-
-    // make a list of temperature controls with matching designators with the same first letter
-    // the list is added t the controllers vector given below
-    {
-        std::vector<struct pad_temperature> controllers;
-        bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
-        if (ok) {
-            for (auto &c : controllers) {
-                if (c.designator[0] == designator) {
-                    ts->temp_controllers.push_back(c.id);
-                }
-            }
-        }
-    }
-
-    // if we don't have any matching controllers, then not valid
-    if (ts->temp_controllers.empty()) {
-        THEKERNEL->streams->printf("WARNING TEMPERATURESWITCH: no controllers matching: %c\n", designator);
-        delete ts;
-        return nullptr;
-    }
-
     // load settings from config file
-    s = THEKERNEL->config->value(temperatureswitch_checksum, modcs, temperatureswitch_switch_checksum)->by_default("")->as_string();
-    if(s.empty()) {
+    string switchname = THEKERNEL->config->value(temperatureswitch_checksum, modcs, temperatureswitch_switch_checksum)->by_default("")->as_string();
+    if(switchname.empty()) {
         // handle old configs where this was called type @DEPRECATED
-        s = THEKERNEL->config->value(temperatureswitch_checksum, modcs, temperatureswitch_type_checksum)->by_default("")->as_string();
-        if(s.empty()) {
+        switchname = THEKERNEL->config->value(temperatureswitch_checksum, modcs, temperatureswitch_type_checksum)->by_default("")->as_string();
+        if(switchname.empty()) {
             // no switch specified so invalid entry
             THEKERNEL->streams->printf("WARNING TEMPERATURESWITCH: no switch specified\n");
-            delete this;
             return nullptr;
         }
     }
 
+    // create a new temperature switch module
+    TemperatureSwitch *ts= new TemperatureSwitch();
+
+    // save designator
+    ts->designator= designator;
+
     // if we should turn the switch on or off when trigger is hit
     ts->inverted = THEKERNEL->config->value(temperatureswitch_checksum, modcs, temperatureswitch_inverted_checksum)->by_default(false)->as_bool();
 
@@ -138,7 +117,7 @@ TemperatureSwitch* TemperatureSwitch::load_config(uint16_t modcs)
     // the mcode used to arm the switch
     ts->arm_mcode = THEKERNEL->config->value(temperatureswitch_checksum, modcs, temperatureswitch_arm_command_checksum)->by_default(0)->as_number();
 
-    ts->temperatureswitch_switch_cs= get_checksum(s); // checksum of the switch to use
+    ts->temperatureswitch_switch_cs= get_checksum(switchname); // checksum of the switch to use
 
     ts->temperatureswitch_threshold_temp = THEKERNEL->config->value(temperatureswitch_checksum, modcs, temperatureswitch_threshold_temp_checksum)->by_default(50.0f)->as_number();
 
@@ -217,18 +196,19 @@ void TemperatureSwitch::set_state(STATE state)
 // Get the highest temperature from the set of temperature controllers
 float TemperatureSwitch::get_highest_temperature()
 {
-    struct pad_temperature temp;
     float high_temp = 0.0;
 
-    for (auto controller : temp_controllers) {
-        bool ok = PublicData::get_value(temperature_control_checksum, current_temperature_checksum, controller, &temp);
-        if (ok) {
+    std::vector<struct pad_temperature> controllers;
+    bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
+    if (ok) {
+        for (auto &c : controllers) {
             // check if this controller's temp is the highest and save it if so
-            if (temp.current_temperature > high_temp) {
-                high_temp = temp.current_temperature;
+            if (c.designator[0] == this->designator && c.current_temperature > high_temp) {
+                high_temp = c.current_temperature;
             }
         }
     }
+
     return high_temp;
 }
 
index 99c8286..1c45c90 100755 (executable)
@@ -47,9 +47,6 @@ class TemperatureSwitch : public Module
         // temperature has changed state
         void set_state(STATE state);
 
-        // the set of temperature controllers that match the required designator prefix
-        vector<uint16_t> temp_controllers;
-
         // temperatureswitch.hotend.threshold_temp
         float temperatureswitch_threshold_temp;
 
@@ -74,6 +71,7 @@ class TemperatureSwitch : public Module
         uint16_t arm_mcode;
 
         struct {
+            char designator:8;
             bool inverted:1;
             bool armed:1;
             TRIGGER_TYPE trigger:2;