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