Merge branch 'protocol' into huzzah
This commit is contained in:
commit
795772909f
@ -2,27 +2,27 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <RCSwitch.h>
|
||||
|
||||
using namespace std;
|
||||
enum ProtocolNo : unsigned int {
|
||||
NO_PROTOCOL = 0,
|
||||
PROTOCOL_1 = 1,
|
||||
PROTOCOL_2 = 2,
|
||||
PROTOCOL_13 = 13
|
||||
};
|
||||
|
||||
class Protocol {
|
||||
protected:
|
||||
unsigned int no;
|
||||
ProtocolNo no;
|
||||
|
||||
public:
|
||||
static unordered_map<unsigned int, Protocol*> mapProtocols;
|
||||
|
||||
Protocol(unsigned int protocol) {
|
||||
no = protocol;
|
||||
mapProtocols.insert({ no, this });
|
||||
}
|
||||
Protocol(ProtocolNo protocol) : no(protocol) {}
|
||||
|
||||
Protocol& setProtocol(unsigned int p) {
|
||||
no = p;
|
||||
no = static_cast<ProtocolNo>(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"]);
|
||||
}
|
||||
@ -32,6 +32,4 @@ public:
|
||||
rcSwitch["protocol"] = no;
|
||||
rcSwitch["value"] = value;
|
||||
}
|
||||
};
|
||||
unordered_map<unsigned int, Protocol*> Protocol::mapProtocols;
|
||||
Protocol fallbackProtocol{ 0 };
|
||||
} fallbackProtocol{ NO_PROTOCOL };
|
||||
|
||||
@ -5,10 +5,10 @@
|
||||
class Protocol_1 : public Protocol {
|
||||
|
||||
public:
|
||||
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"];
|
||||
@ -26,9 +26,11 @@ 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
|
||||
} protocol1;
|
||||
@ -5,7 +5,7 @@
|
||||
class Protocol_2 : public Protocol {
|
||||
|
||||
public:
|
||||
Protocol_2() : Protocol(2) {}
|
||||
Protocol_2() : Protocol(PROTOCOL_2) {}
|
||||
|
||||
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
|
||||
switch (value) {
|
||||
@ -32,4 +32,4 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
} protocol2;
|
||||
} protocol2;
|
||||
|
||||
@ -8,22 +8,16 @@
|
||||
class Protocol_Doorbell : public Protocol {
|
||||
|
||||
public:
|
||||
Protocol_Doorbell() : Protocol(16) {}
|
||||
Protocol_Doorbell() : Protocol(PROTOCOL_13) {}
|
||||
|
||||
void ring() {
|
||||
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) {
|
||||
digitalWrite(SEND_PIN, value);
|
||||
|
||||
@ -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) 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);
|
||||
Protocol* findProtocol(unsigned int protocol) {
|
||||
switch (protocol) {
|
||||
case PROTOCOL_1:
|
||||
return &protocol1;
|
||||
case PROTOCOL_2:
|
||||
return &protocol2;
|
||||
case PROTOCOL_13:
|
||||
return &doorbell;
|
||||
default:
|
||||
return &fallbackProtocol.setProtocol(protocol);
|
||||
}
|
||||
}
|
||||
|
||||
void readRcSwitch() {
|
||||
@ -48,8 +56,8 @@ void readRcSwitch() {
|
||||
mySwitch.resetAvailable();
|
||||
|
||||
StaticJsonDocument<128> jsonDoc;
|
||||
Protocol& p = findProtocol(mySwitch.getReceivedProtocol());
|
||||
p.toJson(value, jsonDoc);
|
||||
auto p = findProtocol(mySwitch.getReceivedProtocol());
|
||||
p->toJson(value, jsonDoc);
|
||||
if (!jsonDoc.isNull()) {
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
@ -75,8 +83,8 @@ void runJsonCommand(char* cmd) {
|
||||
if (err == DeserializationError::Ok) {
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||
Protocol& p = findProtocol(rcSwitch["protocol"]);
|
||||
p.fromJson(rcSwitch, mySwitch);
|
||||
auto p = findProtocol(rcSwitch["protocol"]);
|
||||
p->fromJson(rcSwitch, mySwitch);
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
Board::publishResponse(jsonDoc);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user