change the temperature public data access to pass in the pad_temp for the result...
authorJim Morris <morris@wolfman.com>
Sat, 12 Sep 2015 04:13:25 +0000 (21:13 -0700)
committerJim Morris <morris@wolfman.com>
Sat, 12 Sep 2015 04:13:25 +0000 (21:13 -0700)
Allo publicdata get_value to pass in the recipient for the data, rather than the callee having to store that data, currently used for polling temperature controls.

src/libs/PublicData.cpp
src/libs/PublicData.h
src/libs/PublicDataRequest.h
src/modules/tools/temperaturecontrol/TemperatureControl.cpp
src/modules/tools/temperaturecontrol/TemperatureControl.h
src/modules/tools/temperatureswitch/TemperatureSwitch.cpp
src/modules/utils/panel/screens/PrepareScreen.cpp
src/modules/utils/panel/screens/WatchScreen.cpp
src/modules/utils/panel/screens/WatchScreen.h
src/modules/utils/player/Player.cpp
src/modules/utils/simpleshell/SimpleShell.cpp

index ec927e0..53cd152 100644 (file)
@@ -2,10 +2,14 @@
 #include "PublicData.h"
 #include "PublicDataRequest.h"
 
-bool PublicData::get_value(uint16_t csa, uint16_t csb, uint16_t csc, void **data) {
+bool PublicData::get_value(uint16_t csa, uint16_t csb, uint16_t csc, void *data) {
     PublicDataRequest pdr(csa, csb, csc);
+    pdr.set_data_ptr(data); // the caller may have put a placeholder for the returned data here
     THEKERNEL->call_event(ON_GET_PUBLIC_DATA, &pdr );
-    *data= pdr.get_data_ptr();
+    if(pdr.has_returned_data()) {
+        // the callee set the returned data pointer
+        *(void**)data= pdr.get_data_ptr();
+    }
     return pdr.is_taken();
 }
 
index fe52e10..911b7a3 100644 (file)
 
 class PublicData {
     public:
-        static bool get_value(uint16_t csa, void **data) { return get_value(csa, 0, 0, data); }
-        static bool get_value(uint16_t csa, uint16_t csb, void **data) { return get_value(csa, csb, 0, data); }
-        static bool get_value(uint16_t cs[3], void **data) { return get_value(cs[0], cs[1], cs[2], data); };
-        static bool get_value(uint16_t csa, uint16_t csb, uint16_t csc, void **data);
+        static bool get_value(uint16_t csa, void *data) { return get_value(csa, 0, 0, data); }
+        static bool get_value(uint16_t csa, uint16_t csb, void *data) { return get_value(csa, csb, 0, data); }
+        static bool get_value(uint16_t cs[3], void *data) { return get_value(cs[0], cs[1], cs[2], data); };
+        static bool get_value(uint16_t csa, uint16_t csb, uint16_t csc, void *data);
 
         static bool set_value(uint16_t csa, void *data) { return set_value(csa, 0, 0, data); }
         static bool set_value(uint16_t csa, uint16_t csb, void *data) { return set_value(csa, csb, 0, data); }
index 128e26a..5ff7efd 100644 (file)
@@ -10,9 +10,9 @@
 
 class PublicDataRequest {
     public:
-        PublicDataRequest(uint16_t addrcs1){ target[0]= addrcs1; target[1]= 0; target[2]= 0; data_taken= false; data= NULL; }
-        PublicDataRequest(uint16_t addrcs1, uint16_t addrcs2){ target[0]= addrcs1; target[1]= addrcs2; target[2]= 0; data_taken= false; data= NULL; }
-        PublicDataRequest(uint16_t addrcs1, uint16_t addrcs2, uint16_t addrcs3){ target[0]= addrcs1; target[1]= addrcs2; target[2]= addrcs3; data_taken= false; data= NULL; }
+        PublicDataRequest(uint16_t addrcs1){ target[0]= addrcs1; target[1]= 0; target[2]= 0; data_taken= false; data= NULL; returned_data= true; }
+        PublicDataRequest(uint16_t addrcs1, uint16_t addrcs2){ target[0]= addrcs1; target[1]= addrcs2; target[2]= 0; data_taken= false; data= NULL; returned_data= true; }
+        PublicDataRequest(uint16_t addrcs1, uint16_t addrcs2, uint16_t addrcs3){ target[0]= addrcs1; target[1]= addrcs2; target[2]= addrcs3; data_taken= false; data= NULL; returned_data= true; }
 
         virtual ~PublicDataRequest() { data= NULL; }
 
@@ -22,14 +22,18 @@ class PublicDataRequest {
 
         bool is_taken() const { return this->data_taken; }
         void set_taken() { this->data_taken= true; }
-
+        void clear_returned_data() {this->returned_data= false; }
+        bool has_returned_data() const { return this->returned_data; }
         void set_data_ptr(void *d) { this->data= d; }
         void* get_data_ptr(void) const { return this->data; }
 
     private:
         uint16_t target[3];
         void* data;
-        bool data_taken;
+        struct {
+            bool data_taken:1;
+            bool returned_data:1;
+        };
 };
 
 #endif
index 8078cfe..40bb805 100644 (file)
@@ -356,25 +356,20 @@ void TemperatureControl::on_get_public_data(void *argument)
 
     }else if(!pdr->second_element_is(poll_controls_checksum)) {
         // polling for all temperature controls
-        // add our data to the list
-        std::vector<struct pad_temperature*> *v;
-        void *data= pdr->get_data_ptr();
-        if(data == nullptr) {
-            // first one allocates a vector for the return data
-            v= new std::vector<struct pad_temperature*> ;
-            pdr->set_data_ptr(v);
-
-        }else{
-            v= static_cast<std::vector<pad_temperature*>*>(data);
-        }
+        // add our data to the list which is passed in via the data_ptr
+
+        std::vector<struct pad_temperature> *v= static_cast<std::vector<pad_temperature>*>(pdr->get_data_ptr());
 
+        struct pad_temperature t;
         // setup data
-        this->public_data_return.current_temperature = this->get_temperature();
-        this->public_data_return.target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature;
-        this->public_data_return.pwm = this->o;
-        this->public_data_return.designator= this->designator;
-        this->public_data_return.id= this->name_checksum;
-        v->push_back(&this->public_data_return);
+        t.current_temperature = this->get_temperature();
+        t.target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature;
+        t.pwm = this->o;
+        t.designator= this->designator;
+        t.id= this->name_checksum;
+        v->push_back(t);
+        pdr->set_taken();
+        pdr->clear_returned_data();
         return;
     }
 
@@ -383,13 +378,14 @@ void TemperatureControl::on_get_public_data(void *argument)
 
     // ok this is targeted at us, so send back the requested data
     if(pdr->third_element_is(current_temperature_checksum)) {
-        this->public_data_return.current_temperature = this->get_temperature();
-        this->public_data_return.target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature;
-        this->public_data_return.pwm = this->o;
-        this->public_data_return.designator= this->designator;
-        this->public_data_return.id= this->name_checksum;
-        pdr->set_data_ptr(&this->public_data_return);
+        struct pad_temperature *t= static_cast<pad_temperature*>(pdr->get_data_ptr());
+        t->current_temperature = this->get_temperature();
+        t->target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature;
+        t->pwm = this->o;
+        t->designator= this->designator;
+        t->id= this->name_checksum;
         pdr->set_taken();
+        pdr->clear_returned_data();
     }
 
 }
index 4760c3e..aa5ef4a 100644 (file)
@@ -64,7 +64,6 @@ class TemperatureControl : public Module {
         uint16_t set_m_code;
         uint16_t set_and_wait_m_code;
         uint16_t get_m_code;
-        struct pad_temperature public_data_return;
 
         std::string designator;
 
index aa6b643..b3485c0 100755 (executable)
@@ -70,7 +70,6 @@ bool TemperatureSwitch::load_config(uint16_t modcs)
     }
 
     // create a temperature control and load settings
-
     char designator= 0;
     string s= THEKERNEL->config->value(temperatureswitch_checksum, modcs, designator_checksum)->by_default("")->as_string();
     if(s.empty()){
@@ -86,13 +85,14 @@ bool TemperatureSwitch::load_config(uint16_t modcs)
     // create a new temperature switch module
     TemperatureSwitch *ts= new TemperatureSwitch();
 
-    // see what its designator is and add to list of it the one we specified
-    std::vector<struct pad_temperature*> *controllers;
-    bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, (void**)&controllers);
+    // 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);
+        for (auto &c : controllers) {
+            if (c.designator[0] == designator) {
+                ts->temp_controllers.push_back(c.id);
             }
         }
     }
@@ -206,13 +206,12 @@ void TemperatureSwitch::on_second_tick(void *argument)
 // Get the highest temperature from the set of temperature controllers
 float TemperatureSwitch::get_highest_temperature()
 {
-    void *returned_temp;
+    struct pad_temperature temp;
     float high_temp = 0.0;
 
     for (auto controller : temp_controllers) {
-        bool temp_ok = PublicData::get_value(temperature_control_checksum, controller, current_temperature_checksum, &returned_temp);
-        if (temp_ok) {
-            struct pad_temperature temp =  *static_cast<struct pad_temperature *>(returned_temp);
+        bool ok = PublicData::get_value(temperature_control_checksum, controller, current_temperature_checksum, &temp);
+        if (ok) {
             // 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;
index 1905ffa..8a1543f 100644 (file)
@@ -101,11 +101,10 @@ void PrepareScreen::cooldown()
 
 static float getTargetTemperature(uint16_t heater_cs)
 {
-    void *returned_data;
-    bool ok = PublicData::get_value( temperature_control_checksum, heater_cs, current_temperature_checksum, &returned_data );
+    struct pad_temperature temp;
+    bool ok = PublicData::get_value( temperature_control_checksum, heater_cs, current_temperature_checksum, &temp );
 
     if (ok) {
-        struct pad_temperature temp =  *static_cast<struct pad_temperature *>(returned_data);
         return temp.target_temperature;
     }
 
index a4f8782..05b26c6 100644 (file)
@@ -127,15 +127,15 @@ void WatchScreen::on_refresh()
         bool bed_on= false, hotend_on= false, is_hot= false;
         uint8_t heon=0, hemsk= 0x01; // bit set for which hotend is on bit0: hotend1, bit1: hotend2 etc
         temp_controllers.clear();
-        std::vector<struct pad_temperature*> *controllers;
-        bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, (void**)&controllers);
+        std::vector<struct pad_temperaturecontrollers;
+        bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
         if (ok) {
-            for (auto &c : *controllers) {
-                temp_controllers.push_back(c->id);
-                if(c->current_temperature > 50) is_hot= true; // anything is hot
-                if(c->designator.front() == 'B' && c->target_temperature > 0) bed_on= true;   // bed on/off
-                if(c->designator.front() == 'T') { // a hotend by convention
-                    if(c->target_temperature > 0){
+            for (auto &c : controllers) {
+                temp_controllers.push_back(c.id);
+                if(c.current_temperature > 50) is_hot= true; // anything is hot
+                if(c.designator.front() == 'B' && c.target_temperature > 0) bed_on= true;   // bed on/off
+                if(c.designator.front() == 'T') { // a hotend by convention
+                    if(c.target_temperature > 0){
                         hotend_on= true;// hotend on/off (anyone)
                         heon |= hemsk;
                     }
@@ -178,18 +178,6 @@ void WatchScreen::on_main_loop()
     PanelScreen::on_main_loop(); // in case any queued commands left
 }
 
-void *WatchScreen::getTemperatures(uint16_t heater_cs)
-{
-    void *returned_data;
-    bool ok = PublicData::get_value( temperature_control_checksum, heater_cs, current_temperature_checksum, &returned_data );
-
-    if (ok) {
-        return returned_data;
-    }
-
-    return nullptr;
-}
-
 // fetch the data we are displaying
 void WatchScreen::get_current_status()
 {
@@ -249,6 +237,13 @@ void WatchScreen::get_sd_play_info()
     }
 }
 
+static struct pad_temperature getTemperatures(uint16_t heater_cs)
+{
+    struct pad_temperature temp;
+    PublicData::get_value( temperature_control_checksum, heater_cs, current_temperature_checksum, &temp );
+    return temp;
+}
+
 void WatchScreen::display_menu_line(uint16_t line)
 {
     // in menu mode
@@ -270,12 +265,11 @@ void WatchScreen::display_menu_line(uint16_t line)
                 for (size_t i = 0; i < 2; ++i) {
                     size_t o= i+(n*2);
                     if(o>tm.size()-1) break;
-                    struct pad_temperature *temp= static_cast<struct pad_temperature *>(getTemperatures(tm[o]));
-                    if(temp == nullptr) continue;
-                    int t= std::min(999, (int)roundf(temp->current_temperature));
-                    int tt= roundf(temp->target_temperature);
+                    struct pad_temperature temp= getTemperatures(tm[o]);
+                    int t= std::min(999, (int)roundf(temp.current_temperature));
+                    int tt= roundf(temp.target_temperature);
                     THEPANEL->lcd->setCursor(off, 0); // col, row
-                    off += THEPANEL->lcd->printf("%s:%03d/%03d ", temp->designator.substr(0, 2).c_str(), t, tt);
+                    off += THEPANEL->lcd->printf("%s:%03d/%03d ", temp.designator.substr(0, 2).c_str(), t, tt);
                 }
 
             }else{
index 5f3e4de..32787ba 100644 (file)
@@ -30,7 +30,7 @@ private:
     void get_sd_play_info();
     const char *get_status();
     const char *get_network();
-    void *getTemperatures(uint16_t heater_cs);
+
     std::vector<uint16_t> temp_controllers;
 
     uint32_t update_counts;
index 10cb071..677a818 100644 (file)
@@ -603,11 +603,11 @@ void Player::resume_command(string parameters, StreamOutput *stream )
             }
 
             for(auto& h : this->saved_temperatures) {
-                void *p;
-                if(PublicData::get_value( temperature_control_checksum, h.first, current_temperature_checksum, &p )) {
-                    struct pad_temperature *temp= static_cast<struct pad_temperature *>(p);
-                    if(timeup) stream->printf("%s:%3.1f /%3.1f @%d ", temp->designator.c_str(), temp->current_temperature, ((temp->target_temperature == -1) ? 0.0 : temp->target_temperature), temp->pwm);
-                    wait= wait || (temp->current_temperature < h.second);
+                struct pad_temperature temp;
+                if(PublicData::get_value( temperature_control_checksum, h.first, current_temperature_checksum, &temp )) {
+                    if(timeup)
+                        stream->printf("%s:%3.1f /%3.1f @%d ", temp.designator.c_str(), temp.current_temperature, ((temp.target_temperature == -1) ? 0.0 : temp.target_temperature), temp.pwm);
+                    wait= wait || (temp.current_temperature < h.second);
                 }
             }
             if(timeup) stream->printf("\n");
index 7e66473..af14bfc 100644 (file)
@@ -560,25 +560,24 @@ void SimpleShell::break_command( string parameters, StreamOutput *stream)
 void SimpleShell::get_command( string parameters, StreamOutput *stream)
 {
     string what = shift_parameter( parameters );
-    void *returned_data;
 
     if (what == "temp") {
+        struct pad_temperature temp;
         string type = shift_parameter( parameters );
-        bool ok = PublicData::get_value( temperature_control_checksum, get_checksum(type), current_temperature_checksum, &returned_data );
+        bool ok = PublicData::get_value( temperature_control_checksum, get_checksum(type), current_temperature_checksum, &temp );
 
         if (ok) {
-            struct pad_temperature temp =  *static_cast<struct pad_temperature *>(returned_data);
             stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
         } else {
             stream->printf("%s is not a known temperature device\r\n", type.c_str());
         }
 
     } else if (what == "pos") {
-        bool ok = PublicData::get_value( robot_checksum, current_position_checksum, &returned_data );
+        float *pos;
+        bool ok = PublicData::get_value( robot_checksum, current_position_checksum, &pos );
 
         if (ok) {
-            float *pos = static_cast<float *>(returned_data);
-            stream->printf("Position X: %f, Y: %f, Z: %f\r\n", pos[0], pos[1], pos[2]);
+             stream->printf("Position X: %f, Y: %f, Z: %f\r\n", pos[0], pos[1], pos[2]);
 
         } else {
             stream->printf("get pos command failed\r\n");