use constexpr and dedicated protocol class
This commit is contained in:
parent
f8eb28786a
commit
45e0ea3261
@ -2,22 +2,34 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <RCSwitch.h>
|
#include <RCSwitch.h>
|
||||||
|
|
||||||
class Protocol {
|
class ProtocolNo {
|
||||||
protected:
|
protected:
|
||||||
unsigned int no;
|
unsigned int no;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
constexpr Protocol(unsigned int protocol) : no(protocol) {}
|
constexpr ProtocolNo(unsigned int protocol) : no(protocol) {}
|
||||||
|
|
||||||
constexpr operator unsigned int() const { return no; }
|
constexpr operator unsigned int() const { return no; }
|
||||||
|
ProtocolNo& operator=(unsigned int p){ no = p; return *this;}
|
||||||
|
};
|
||||||
|
constexpr ProtocolNo NO_PROTOCOL{ 0 };
|
||||||
|
constexpr ProtocolNo PROTOCOL_1{ 1 };
|
||||||
|
constexpr ProtocolNo PROTOCOL_2{ 2 };
|
||||||
|
constexpr ProtocolNo PROTOCOL_13{ 13 };
|
||||||
|
|
||||||
Protocol& setProtocol(unsigned int p) {
|
class Protocol {
|
||||||
|
protected:
|
||||||
|
ProtocolNo no;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Protocol(ProtocolNo protocol) : no(protocol) {}
|
||||||
|
|
||||||
|
Protocol& setProtocol(ProtocolNo p) {
|
||||||
no = p;
|
no = p;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) {
|
virtual void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) {
|
||||||
unsigned int protocol = rcSwitch["protocol"];
|
ProtocolNo protocol = rcSwitch["protocol"];
|
||||||
rcDevice.setProtocol(protocol);
|
rcDevice.setProtocol(protocol);
|
||||||
rcDevice.send(rcSwitch["value"]);
|
rcDevice.send(rcSwitch["value"]);
|
||||||
}
|
}
|
||||||
@ -27,4 +39,4 @@ public:
|
|||||||
rcSwitch["protocol"] = no;
|
rcSwitch["protocol"] = no;
|
||||||
rcSwitch["value"] = value;
|
rcSwitch["value"] = value;
|
||||||
}
|
}
|
||||||
} fallbackProtocol{ 0 };
|
} fallbackProtocol{ NO_PROTOCOL };
|
||||||
|
|||||||
@ -5,10 +5,10 @@
|
|||||||
class Protocol_1 : public Protocol {
|
class Protocol_1 : public Protocol {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
constexpr Protocol_1() : Protocol(1) {}
|
Protocol_1() : Protocol(PROTOCOL_1) {}
|
||||||
|
|
||||||
void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override {
|
void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override {
|
||||||
unsigned int protocol = rcSwitch["protocol"];
|
ProtocolNo protocol = rcSwitch["protocol"];
|
||||||
rcDevice.setProtocol(protocol);
|
rcDevice.setProtocol(protocol);
|
||||||
const char* group = rcSwitch["group"];
|
const char* group = rcSwitch["group"];
|
||||||
int channel = rcSwitch["channel"];
|
int channel = rcSwitch["channel"];
|
||||||
@ -33,5 +33,4 @@ public:
|
|||||||
return std::string{ uId };
|
return std::string{ uId };
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
};
|
} protocol1;
|
||||||
constexpr Protocol_1 protocol1;
|
|
||||||
@ -5,7 +5,7 @@
|
|||||||
class Protocol_2 : public Protocol {
|
class Protocol_2 : public Protocol {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
constexpr Protocol_2() : Protocol(2) {}
|
Protocol_2() : Protocol(PROTOCOL_2) {}
|
||||||
|
|
||||||
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
|
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
|
||||||
switch (value) {
|
switch (value) {
|
||||||
@ -32,5 +32,4 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
} protocol2;
|
||||||
constexpr Protocol_2 protocol2;
|
|
||||||
|
|||||||
@ -8,7 +8,7 @@
|
|||||||
class Protocol_Doorbell : public Protocol {
|
class Protocol_Doorbell : public Protocol {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
constexpr Protocol_Doorbell() : Protocol(16) {}
|
Protocol_Doorbell() : Protocol(PROTOCOL_13) {}
|
||||||
|
|
||||||
static void ring(const char* value) {
|
static void ring(const char* value) {
|
||||||
preamble();
|
preamble();
|
||||||
@ -55,5 +55,4 @@ private:
|
|||||||
}
|
}
|
||||||
interrupts();
|
interrupts();
|
||||||
}
|
}
|
||||||
};
|
} doorbell;
|
||||||
constexpr Protocol_Doorbell doorbell;
|
|
||||||
|
|||||||
@ -31,13 +31,13 @@ void setup() {
|
|||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Protocol& findProtocol(unsigned int protocol) {
|
Protocol& findProtocol(unsigned int protocol) {
|
||||||
switch (protocol) {
|
switch (protocol) {
|
||||||
case protocol1:
|
case PROTOCOL_1:
|
||||||
return protocol1;
|
return protocol1;
|
||||||
case protocol2:
|
case PROTOCOL_2:
|
||||||
return protocol2;
|
return protocol2;
|
||||||
case doorbell:
|
case PROTOCOL_13:
|
||||||
return doorbell;
|
return doorbell;
|
||||||
default:
|
default:
|
||||||
return fallbackProtocol.setProtocol(protocol);
|
return fallbackProtocol.setProtocol(protocol);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user