From f8eb28786af813b57b2e4cc1ca12a16383145290 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 20 May 2024 13:19:41 +0200 Subject: [PATCH] pro-mini doesn't support map, find protocol based on each protocol's value - type safe --- gateway/include/Protocol.h | 13 +++---------- gateway/include/Protocol_1.h | 7 +++++-- gateway/include/Protocol_2.h | 5 +++-- gateway/include/Protocol_Doorbell.h | 25 ++++++++++--------------- gateway/include/devices.h | 2 +- gateway/src/gateway.cpp | 18 +++++++++++++----- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/gateway/include/Protocol.h b/gateway/include/Protocol.h index 056adc8..c923a0a 100644 --- a/gateway/include/Protocol.h +++ b/gateway/include/Protocol.h @@ -2,19 +2,14 @@ #include #include -using namespace std; - class Protocol { protected: unsigned int no; public: - static unordered_map 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 Protocol::mapProtocols; -Protocol fallbackProtocol{ 0 }; +} fallbackProtocol{ 0 }; diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h index 112104d..f759c79 100644 --- a/gateway/include/Protocol_1.h +++ b/gateway/include/Protocol_1.h @@ -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; \ No newline at end of file diff --git a/gateway/include/Protocol_2.h b/gateway/include/Protocol_2.h index 9e780f2..024e2ec 100644 --- a/gateway/include/Protocol_2.h +++ b/gateway/include/Protocol_2.h @@ -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; \ No newline at end of file +}; +constexpr Protocol_2 protocol2; diff --git a/gateway/include/Protocol_Doorbell.h b/gateway/include/Protocol_Doorbell.h index 59285cf..bd4230f 100644 --- a/gateway/include/Protocol_Doorbell.h +++ b/gateway/include/Protocol_Doorbell.h @@ -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; diff --git a/gateway/include/devices.h b/gateway/include/devices.h index 4353af7..e30158c 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -109,7 +109,7 @@ Command* commands[] = { }).asDevice(gatewayDevice).build(), Builder