rc-gateway/gateway/include/Protocol_Doorbell.h

61 lines
1.4 KiB
C++

#pragma once
#include "Protocol.h"
#include "RC.h"
#define BIT_LENGTH 40
#define BIT_LENGTH_3 BIT_LENGTH*3
#define TX_DELAY 620
#define PORT_BIT PB3
class Protocol_Doorbell : public Protocol {
public:
Protocol_Doorbell() : Protocol(16) {
}
void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override {
pinMode(SEND_PIN, OUTPUT);
preamble();
_delay_us(TX_DELAY);
for (int i = 0; i < 7; i++) {
code("00000000110100101000100");
_delay_us(TX_DELAY);
}
pinMode(SEND_PIN, INPUT);
}
private:
void transmitHigh() {
PORTB |= _BV(PORT_BIT); // HIGH
_delay_us(BIT_LENGTH_3);
PORTB &= ~_BV(PORT_BIT); // LOW
_delay_us(BIT_LENGTH);
}
void transmitLow() {
PORTB |= _BV(PORT_BIT); // HIGH
_delay_us(BIT_LENGTH);
PORTB &= ~_BV(PORT_BIT); // LOW
_delay_us(BIT_LENGTH_3);
}
void preamble() {
noInterrupts();
for (int i = 0; i < 370; i++) {
PORTB |= _BV(PORT_BIT); // HIGH
_delay_us(BIT_LENGTH);
PORTB &= ~_BV(PORT_BIT); // LOW
_delay_us(BIT_LENGTH);
}
interrupts();
}
void code(const char* value) {
noInterrupts();
for (const char* p = value; *p; p++) {
*p == '1' ? transmitHigh() : transmitLow();
}
interrupts();
}
};