use enum for protocol
This commit is contained in:
parent
45e0ea3261
commit
f668beeca2
@ -2,19 +2,12 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <RCSwitch.h>
|
#include <RCSwitch.h>
|
||||||
|
|
||||||
class ProtocolNo {
|
enum ProtocolNo : unsigned int {
|
||||||
protected:
|
NO_PROTOCOL = 0,
|
||||||
unsigned int no;
|
PROTOCOL_1 = 1,
|
||||||
|
PROTOCOL_2 = 2,
|
||||||
public:
|
PROTOCOL_13 = 13
|
||||||
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 };
|
|
||||||
|
|
||||||
class Protocol {
|
class Protocol {
|
||||||
protected:
|
protected:
|
||||||
@ -23,8 +16,8 @@ protected:
|
|||||||
public:
|
public:
|
||||||
Protocol(ProtocolNo protocol) : no(protocol) {}
|
Protocol(ProtocolNo protocol) : no(protocol) {}
|
||||||
|
|
||||||
Protocol& setProtocol(ProtocolNo p) {
|
Protocol& setProtocol(unsigned int p) {
|
||||||
no = p;
|
no = static_cast<ProtocolNo>(p);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@ class Protocol_Doorbell : public Protocol {
|
|||||||
public:
|
public:
|
||||||
Protocol_Doorbell() : Protocol(PROTOCOL_13) {}
|
Protocol_Doorbell() : Protocol(PROTOCOL_13) {}
|
||||||
|
|
||||||
static void ring(const char* value) {
|
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);
|
||||||
@ -19,27 +19,27 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void transmitBit(uint8_t value) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void transmitHigh() {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void transmitLow() {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void preamble() {
|
void preamble() {
|
||||||
noInterrupts();
|
noInterrupts();
|
||||||
for (int i = 0; i < 370; i++) {
|
for (int i = 0; i < 370; i++) {
|
||||||
transmitBit(HIGH);
|
transmitBit(HIGH);
|
||||||
@ -48,7 +48,7 @@ private:
|
|||||||
interrupts();
|
interrupts();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void code(const char* value) {
|
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();
|
||||||
|
|||||||
@ -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) Protocol_Doorbell::ring("00000000110100101000100");
|
if (strcmp("PRESS", msg) == 0) doorbell.ring("00000000110100101000100");
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.asDevice(
|
.asDevice(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user