Skip to content

Commit

Permalink
simulator: extend save and load to save Controller objects
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Sep 24, 2024
1 parent db6d81d commit ebf50fb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/sim/Controller_BL0942.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Shape.h"
#include "Junction.h"
#include "Text.h"
#include "../cJSON/cJSON.h"

#define BL0942_PACKET_LEN 23
#define BL0942_READ_COMMAND 0x58
Expand Down Expand Up @@ -78,4 +79,29 @@ class CControllerBase *CControllerBL0942::cloneController(class CShape *origOwne
return r;
}

void CControllerBL0942::saveTo(struct cJSON *j_obj) {
cJSON_AddStringToObject(j_obj, "voltage", this->txt_voltage->getText());
cJSON_AddStringToObject(j_obj, "current", this->txt_current->getText());
cJSON_AddStringToObject(j_obj, "power", this->txt_power->getText());
cJSON_AddStringToObject(j_obj, "frequency", this->txt_freq->getText());

}
void CControllerBL0942::loadFrom(struct cJSON *j_obj) {
cJSON *v = cJSON_GetObjectItemCaseSensitive(j_obj, "voltage");
if (v) {
this->txt_voltage->setText(v->valuestring);
}
cJSON *c = cJSON_GetObjectItemCaseSensitive(j_obj, "current");
if (c) {
this->txt_current->setText(c->valuestring);
}
cJSON *p = cJSON_GetObjectItemCaseSensitive(j_obj, "power");
if (p) {
this->txt_power->setText(p->valuestring);
}
cJSON *f = cJSON_GetObjectItemCaseSensitive(j_obj, "frequency");
if (f) {
this->txt_freq->setText(f->valuestring);
}
}
#endif
3 changes: 3 additions & 0 deletions src/sim/Controller_BL0942.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class CControllerBL0942 : public CControllerBase {
realVoltage = 220.0f;
realFreq = 60.0f;
}
virtual void saveTo(struct cJSON *j_obj);
virtual void loadFrom(struct cJSON *j_obj);

virtual void onDrawn();
virtual class CControllerBase *cloneController(class CShape *origOwner, class CShape *newOwner);
};
Expand Down
5 changes: 5 additions & 0 deletions src/sim/Controller_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class CControllerBase {
// for buttons
virtual class CJunction *findOtherJunctionIfPassable(class CJunction *ju) { return 0; }
virtual class CControllerBase *cloneController(class CShape *origOwner, class CShape *newOwner) { return 0; }
// save and load
virtual void saveTo(struct cJSON *j_obj) { }
virtual void loadFrom(struct cJSON *j_obj) { }


};


Expand Down
15 changes: 15 additions & 0 deletions src/sim/Controller_DHT11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Shape.h"
#include "Junction.h"
#include "Text.h"
#include "../cJSON/cJSON.h"

extern "C" bool SIM_ReadDHT11(int pin, byte *data) {
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
Expand Down Expand Up @@ -39,7 +40,21 @@ extern "C" bool SIM_ReadDHT11(int pin, byte *data) {
}
return true;
}
void CControllerDHT11::saveTo(struct cJSON *j_obj) {
cJSON_AddStringToObject(j_obj, "temperature", this->txt_temperature->getText());
cJSON_AddStringToObject(j_obj, "humidity", this->txt_humidity->getText());

}
void CControllerDHT11::loadFrom(struct cJSON *j_obj) {
cJSON *temp = cJSON_GetObjectItemCaseSensitive(j_obj, "temperature");
if (temp) {
this->txt_temperature->setText(temp->valuestring);
}
cJSON *hum = cJSON_GetObjectItemCaseSensitive(j_obj, "humidity");
if (hum) {
this->txt_humidity->setText(hum->valuestring);
}
}
void CControllerDHT11::onDrawn() {
if (txt_temperature->isBeingEdited() == false) {
realTemperature = txt_temperature->getFloat();
Expand Down
3 changes: 3 additions & 0 deletions src/sim/Controller_DHT11.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class CControllerDHT11 : public CControllerBase {
virtual void onDrawn();
virtual class CControllerBase *cloneController(class CShape *origOwner, class CShape *newOwner);

virtual void saveTo(struct cJSON *j_obj);
virtual void loadFrom(struct cJSON *j_obj);

float getTemperature() const {
return realTemperature;
}
Expand Down
9 changes: 9 additions & 0 deletions src/sim/SaveLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Simulation.h"
#include "PrefabManager.h"
#include "Text.h"
#include "Controller_Base.h"
#include "../cJSON/cJSON.h"

class CProject *CSaveLoad::loadProjectFile(const char *fname) {
Expand Down Expand Up @@ -93,6 +94,10 @@ class CSimulation *CSaveLoad::loadSimulationFromFile(const char *fname) {
if (jText != 0 && as_text != 0) {
as_text->setText(jText->valuestring);
}
class CControllerBase *cb = o->getController();
if (cb) {
cb->loadFrom(jObject);
}
}
}
cJSON_ArrayForEach(jWire, n_jWires)
Expand Down Expand Up @@ -130,6 +135,10 @@ void CSaveLoad::saveSimulationToFile(class CSimulation *simToSave, const char *f
if (as_text) {
cJSON_AddStringToObject(j_obj, "text", as_text->getText());
}
class CControllerBase *cb = obj->getController();
if (cb) {
cb->saveTo(j_obj);
}
}
cJSON *main_wires = cJSON_AddObjectToObject(main_sim, "wires");
for (int i = 0; i < simToSave->getWiresCount(); i++) {
Expand Down

0 comments on commit ebf50fb

Please sign in to comment.