use pointers to fix virtual calls
This commit is contained in:
parent
0ebb9902df
commit
bdf87cab17
@ -10,6 +10,7 @@ public:
|
|||||||
Protocol(unsigned int protocol) {
|
Protocol(unsigned int protocol) {
|
||||||
this->protocol = protocol;
|
this->protocol = protocol;
|
||||||
}
|
}
|
||||||
|
virtual ~Protocol() {}
|
||||||
|
|
||||||
virtual void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) {
|
virtual void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) {
|
||||||
unsigned int protocol = rcSwitch["protocol"];
|
unsigned int protocol = rcSwitch["protocol"];
|
||||||
|
|||||||
@ -64,5 +64,5 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} protocol_1;
|
};
|
||||||
|
|
||||||
|
|||||||
@ -64,4 +64,4 @@ private:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} protocol_2;
|
};
|
||||||
@ -27,14 +27,20 @@ void setup() {
|
|||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
Protocol findProtocol(unsigned int protocol) {
|
void blink() {
|
||||||
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
Protocol* findProtocol(unsigned int protocol) {
|
||||||
switch (protocol) {
|
switch (protocol) {
|
||||||
case 1:
|
case 1:
|
||||||
return protocol_1;
|
return new Protocol_1();
|
||||||
case 2:
|
case 2:
|
||||||
return protocol_2;
|
return new Protocol_2();
|
||||||
default:
|
default:
|
||||||
return Protocol(protocol);
|
return new Protocol(protocol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +50,9 @@ void readRcSwitch() {
|
|||||||
mySwitch.resetAvailable();
|
mySwitch.resetAvailable();
|
||||||
|
|
||||||
StaticJsonDocument<200> jsonDoc;
|
StaticJsonDocument<200> jsonDoc;
|
||||||
findProtocol(mySwitch.getReceivedProtocol()).toJson(value, jsonDoc);
|
Protocol* p = findProtocol(mySwitch.getReceivedProtocol());
|
||||||
|
p->toJson(value, jsonDoc);
|
||||||
|
delete p;
|
||||||
if (!jsonDoc.isNull()) {
|
if (!jsonDoc.isNull()) {
|
||||||
serializeJson(jsonDoc, Serial);
|
serializeJson(jsonDoc, Serial);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
@ -52,12 +60,6 @@ void readRcSwitch() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void blink() {
|
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
void runJsonCommands(const char* cmd) {
|
void runJsonCommands(const char* cmd) {
|
||||||
String origCmd = String(cmd);
|
String origCmd = String(cmd);
|
||||||
StaticJsonDocument<512> jsonArray;
|
StaticJsonDocument<512> jsonArray;
|
||||||
@ -67,7 +69,9 @@ void runJsonCommands(const char* cmd) {
|
|||||||
for (JsonVariant jsonDoc : array) {
|
for (JsonVariant jsonDoc : array) {
|
||||||
if (jsonDoc.containsKey("rcSwitch")) {
|
if (jsonDoc.containsKey("rcSwitch")) {
|
||||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||||
findProtocol(rcSwitch["protocol"]).fromJson(rcSwitch, mySwitch);
|
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
||||||
|
p->fromJson(rcSwitch, mySwitch);
|
||||||
|
delete p;
|
||||||
serializeJson(jsonDoc, Serial);
|
serializeJson(jsonDoc, Serial);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
@ -91,8 +95,9 @@ void readCommand() {
|
|||||||
if (cmd.endsWith(",")) {
|
if (cmd.endsWith(",")) {
|
||||||
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
cmd = cmd.substring(0, cmd.lastIndexOf(','));
|
||||||
}
|
}
|
||||||
cmd = "[" + cmd + "]";
|
char buffer[cmd.length()+2];
|
||||||
runJsonCommands(cmd.c_str());
|
sprintf(buffer, "[%s]", cmd.c_str());
|
||||||
|
runJsonCommands(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user