pro-mini doesn't support map, find protocol based on each protocol's value - type safe
This commit is contained in:
parent
4096bfc83a
commit
f8eb28786a
@ -2,19 +2,14 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <RCSwitch.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Protocol {
|
||||
protected:
|
||||
unsigned int no;
|
||||
|
||||
public:
|
||||
static unordered_map<unsigned int, Protocol*> mapProtocols;
|
||||
constexpr Protocol(unsigned int protocol) : no(protocol) {}
|
||||
|
||||
Protocol(unsigned int protocol) {
|
||||
no = protocol;
|
||||
mapProtocols.insert({ no, this });
|
||||
}
|
||||
constexpr operator unsigned int() const { return no; }
|
||||
|
||||
Protocol& setProtocol(unsigned int p) {
|
||||
no = p;
|
||||
@ -32,6 +27,4 @@ public:
|
||||
rcSwitch["protocol"] = no;
|
||||
rcSwitch["value"] = value;
|
||||
}
|
||||
};
|
||||
unordered_map<unsigned int, Protocol*> Protocol::mapProtocols;
|
||||
Protocol fallbackProtocol{ 0 };
|
||||
} fallbackProtocol{ 0 };
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
class Protocol_1 : public Protocol {
|
||||
|
||||
public:
|
||||
Protocol_1() : Protocol(1) {}
|
||||
constexpr Protocol_1() : Protocol(1) {}
|
||||
|
||||
void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override {
|
||||
unsigned int protocol = rcSwitch["protocol"];
|
||||
@ -26,9 +26,12 @@ public:
|
||||
rcSwitch["raw_value"] = value;
|
||||
}
|
||||
|
||||
#if defined(ESP8266)
|
||||
static std::string buildId(const char* group, const unsigned char channel) {
|
||||
char uId[30];
|
||||
sprintf(uId, "%s_%d", group, channel);
|
||||
return std::string{ uId };
|
||||
}
|
||||
} protocol1;
|
||||
#endif
|
||||
};
|
||||
constexpr Protocol_1 protocol1;
|
||||
@ -5,7 +5,7 @@
|
||||
class Protocol_2 : public Protocol {
|
||||
|
||||
public:
|
||||
Protocol_2() : Protocol(2) {}
|
||||
constexpr Protocol_2() : Protocol(2) {}
|
||||
|
||||
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
|
||||
switch (value) {
|
||||
@ -32,4 +32,5 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
} protocol2;
|
||||
};
|
||||
constexpr Protocol_2 protocol2;
|
||||
|
||||
@ -8,44 +8,38 @@
|
||||
class Protocol_Doorbell : public Protocol {
|
||||
|
||||
public:
|
||||
Protocol_Doorbell() : Protocol(16) {}
|
||||
constexpr Protocol_Doorbell() : Protocol(16) {}
|
||||
|
||||
void ring() {
|
||||
static void ring(const char* value) {
|
||||
preamble();
|
||||
for (int i = 0; i < 7; i++) {
|
||||
delayMicroseconds(TX_DELAY);
|
||||
code("00000000110100101000100");
|
||||
code(value);
|
||||
}
|
||||
}
|
||||
|
||||
void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override {
|
||||
}
|
||||
|
||||
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
|
||||
}
|
||||
|
||||
private:
|
||||
void transmitBit(uint8_t value) {
|
||||
static void transmitBit(uint8_t value) {
|
||||
digitalWrite(SEND_PIN, value);
|
||||
delayMicroseconds(BIT_LENGTH);
|
||||
digitalWrite(SEND_PIN, LOW);
|
||||
}
|
||||
|
||||
void transmitHigh() {
|
||||
static void transmitHigh() {
|
||||
digitalWrite(SEND_PIN, HIGH);
|
||||
delayMicroseconds(BIT_LENGTH_3);
|
||||
digitalWrite(SEND_PIN, LOW);
|
||||
delayMicroseconds(BIT_LENGTH);
|
||||
}
|
||||
|
||||
void transmitLow() {
|
||||
static void transmitLow() {
|
||||
digitalWrite(SEND_PIN, HIGH);
|
||||
delayMicroseconds(BIT_LENGTH);
|
||||
digitalWrite(SEND_PIN, LOW);
|
||||
delayMicroseconds(BIT_LENGTH_3);
|
||||
}
|
||||
|
||||
void preamble() {
|
||||
static void preamble() {
|
||||
noInterrupts();
|
||||
for (int i = 0; i < 370; i++) {
|
||||
transmitBit(HIGH);
|
||||
@ -54,11 +48,12 @@ private:
|
||||
interrupts();
|
||||
}
|
||||
|
||||
void code(const char* value) {
|
||||
static void code(const char* value) {
|
||||
noInterrupts();
|
||||
for (const char* p = value; *p; p++) {
|
||||
*p == '1' ? transmitHigh() : transmitLow();
|
||||
}
|
||||
interrupts();
|
||||
}
|
||||
} doorbell;
|
||||
};
|
||||
constexpr Protocol_Doorbell doorbell;
|
||||
|
||||
@ -109,7 +109,7 @@ Command* commands[] = {
|
||||
}).asDevice(gatewayDevice).build(),
|
||||
Builder<Button>::instance(new Button{"Front door", "doorbell_front",
|
||||
[](const char* msg) {
|
||||
if (strcmp("PRESS", msg) == 0) doorbell.ring();
|
||||
if (strcmp("PRESS", msg) == 0) Protocol_Doorbell::ring("00000000110100101000100");
|
||||
}
|
||||
})
|
||||
.asDevice(
|
||||
|
||||
@ -31,9 +31,17 @@ void setup() {
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
Protocol& findProtocol(unsigned int protocol) {
|
||||
auto p = Protocol::mapProtocols[protocol];
|
||||
return p ? *p : fallbackProtocol.setProtocol(protocol);
|
||||
const Protocol& findProtocol(unsigned int protocol) {
|
||||
switch (protocol) {
|
||||
case protocol1:
|
||||
return protocol1;
|
||||
case protocol2:
|
||||
return protocol2;
|
||||
case doorbell:
|
||||
return doorbell;
|
||||
default:
|
||||
return fallbackProtocol.setProtocol(protocol);
|
||||
}
|
||||
}
|
||||
|
||||
void readRcSwitch() {
|
||||
@ -48,7 +56,7 @@ void readRcSwitch() {
|
||||
mySwitch.resetAvailable();
|
||||
|
||||
StaticJsonDocument<128> jsonDoc;
|
||||
Protocol& p = findProtocol(mySwitch.getReceivedProtocol());
|
||||
auto p = findProtocol(mySwitch.getReceivedProtocol());
|
||||
p.toJson(value, jsonDoc);
|
||||
if (!jsonDoc.isNull()) {
|
||||
serializeJson(jsonDoc, Serial);
|
||||
@ -75,7 +83,7 @@ void runJsonCommand(char* cmd) {
|
||||
if (err == DeserializationError::Ok) {
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||
Protocol& p = findProtocol(rcSwitch["protocol"]);
|
||||
auto p = findProtocol(rcSwitch["protocol"]);
|
||||
p.fromJson(rcSwitch, mySwitch);
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user