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.
#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();
}
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); }
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; }
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
}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;
}
// 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();
}
}
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;
}
// 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()){
// 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);
}
}
}
// 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;
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;
}
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_temperature> controllers;
+ 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;
}
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()
{
}
}
+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
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{
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;
}
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");
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");