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