From 7c1afa3b87ec87f13643e6e4aff8fe2aed6f845e Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 18 Dec 2023 21:10:53 +0100 Subject: [PATCH] implement bell ringing using registries --- gateway/include/Protocol_Doorbell.h | 63 +++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/gateway/include/Protocol_Doorbell.h b/gateway/include/Protocol_Doorbell.h index faf1dc0..21609d3 100644 --- a/gateway/include/Protocol_Doorbell.h +++ b/gateway/include/Protocol_Doorbell.h @@ -1,7 +1,11 @@ #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 { @@ -10,18 +14,63 @@ public: } void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override { - // unsigned int protocol = rcSwitch["protocol"]; - // rcDevice.setProtocol(protocol); + pinMode(SEND_PIN, OUTPUT); preamble(); - delayMicroseconds(TX_DELAY); - send(); + _delay_us(TX_DELAY); + for (int i = 0; i < 7; i++) { + code("00000000110100101000100"); + _delay_us(TX_DELAY); + } + pinMode(SEND_PIN, INPUT); } private: - void preamble() { - digitalWrite(1, 1); + void transmitBit(uint8_t value) { + digitalWrite(SEND_PIN, value); + delayMicroseconds(BIT_LENGTH); + digitalWrite(SEND_PIN, LOW); } - void send() { + void transmitHigh() { + // digitalWrite(TX_PIN, HIGH); + // delayMicroseconds(BIT_LENGTH_3); + // digitalWrite(TX_PIN, LOW); + // delayMicroseconds(BIT_LENGTH); + // noInterrupts(); + PORTB |= _BV(PORT_BIT); // HIGH + _delay_us(BIT_LENGTH_3); + PORTB &= ~_BV(PORT_BIT); // LOW + _delay_us(BIT_LENGTH); + // interrupts(); + } + + void transmitLow() { + // digitalWrite(TX_PIN, HIGH); + // delayMicroseconds(BIT_LENGTH); + // digitalWrite(TX_PIN, LOW); + // delayMicroseconds(BIT_LENGTH_3); + // noInterrupts(); + PORTB |= _BV(PORT_BIT); // HIGH + _delay_us(BIT_LENGTH); + PORTB &= ~_BV(PORT_BIT); // LOW + _delay_us(BIT_LENGTH_3); + // interrupts(); + } + + void preamble() { + noInterrupts(); + for (int i = 0; i < 370; i++) { + transmitBit(HIGH); + transmitBit(LOW); + } + interrupts(); + } + + void code(const char* value) { + noInterrupts(); + for (const char* p = value; *p; p++) { + *p == '1' ? transmitHigh() : transmitLow(); + } + interrupts(); } };