From 96432374655ea48d90335273c2fcd0bd7146d0de Mon Sep 17 00:00:00 2001 From: DashyFox Date: Tue, 27 Feb 2024 11:38:23 +0300 Subject: [PATCH] add Accept logic --- IR_Decoder.h | 25 +++++++++++++++++++------ IR_DecoderRaw.h | 2 +- IR_Encoder.cpp | 14 +++++++------- IR_Encoder.h | 4 ++-- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/IR_Decoder.h b/IR_Decoder.h index 1370d12..58f23b1 100644 --- a/IR_Decoder.h +++ b/IR_Decoder.h @@ -1,16 +1,23 @@ #pragma once #include "IR_DecoderRaw.h" #include "PacketTypes.h" +#include "IR_Encoder.h" class IR_Decoder : public IR_DecoderRaw { + uint32_t acceptSendTimer; + bool isWaitingAcceptSend; + uint16_t addrAcceptSendTo; + + + public: + PacketTypes::Data gotData; PacketTypes::DataBack gotBackData; PacketTypes::Accept gotAccept; PacketTypes::Request gotRequest; - IR_Decoder(const uint8_t isrPin, uint16_t addr, IR_Encoder* encPair = nullptr) : IR_DecoderRaw(isrPin, addr, encPair) { - } + IR_Decoder(const uint8_t isrPin, uint16_t addr, IR_Encoder* encPair = nullptr) : IR_DecoderRaw(isrPin, addr, encPair) {} void tick() { IR_DecoderRaw::tick(); @@ -18,10 +25,9 @@ public: #ifdef IRDEBUG_INFO Serial.println("PARSING RAW DATA"); #endif - bool isNeenAccept = false; + isWaitingAcceptSend = false; switch (packInfo.buffer[0] >> 5 & IR_MASK_MSG_TYPE) { case IR_MSG_DATA_ACCEPT: - isNeenAccept = true; case IR_MSG_DATA_NOACCEPT: gotData.set(&packInfo, id); break; @@ -39,8 +45,15 @@ public: default: break; } - - + if (gotData.isAvailable && (gotData.getMsgType() == IR_MSG_DATA_ACCEPT)) { + acceptSendTimer = millis(); + addrAcceptSendTo = gotData.getAddrFrom(); + if(addrAcceptSendTo && addrAcceptSendTo < IR_Broadcast) isWaitingAcceptSend = true; + } + } + if (isWaitingAcceptSend && millis() - acceptSendTimer > 75) { + encoder->sendAccept(addrAcceptSendTo); + isWaitingAcceptSend = false; } } }; diff --git a/IR_DecoderRaw.h b/IR_DecoderRaw.h index 93a25a6..e0a24e3 100644 --- a/IR_DecoderRaw.h +++ b/IR_DecoderRaw.h @@ -45,12 +45,12 @@ public: protected: PackInfo packInfo; uint16_t id; + IR_Encoder* encoder; // Указатель на парный передатчик private: ErrorsStruct errors; bool isAvailable = false; uint16_t packSize; uint16_t crcValue; - IR_Encoder* encoder; // Указатель на парный передатчик volatile uint16_t isPairSending = 0; // Флаг передачи парного передатчика volatile bool isRecive = false; // Флаг приёма volatile bool isPreamb = false; // флаг начальной последовости diff --git a/IR_Encoder.cpp b/IR_Encoder.cpp index 1737913..204a927 100644 --- a/IR_Encoder.cpp +++ b/IR_Encoder.cpp @@ -72,17 +72,17 @@ void IR_Encoder::sendData(uint16_t addrTo, uint8_t* data, uint8_t len, bool need rawSend(sendBuffer, packSize); } -void IR_Encoder::sendAccept(uint16_t addrTo, uint8_t addInfo) { +void IR_Encoder::sendAccept(uint16_t addrTo) { memset(sendBuffer, 0x00, dataByteSizeMax); sendBuffer[0] = IR_MSG_ACCEPT << 5; - sendBuffer[0] |= addInfo & IR_MASK_MSG_INFO; + sendBuffer[0] |= msgBytes + addrBytes + crcBytes & IR_MASK_MSG_INFO; // размер пакета // addr_self sendBuffer[1] = id >> 8 & 0xFF; sendBuffer[2] = id & 0xFF; - Serial.print("\nRAW Accept to "); - Serial.println(addrTo); + // Serial.print("\nRAW Accept to "); + // Serial.println(addrTo); // data crc @@ -92,10 +92,10 @@ void IR_Encoder::sendAccept(uint16_t addrTo, uint8_t addInfo) { rawSend(sendBuffer, msgBytes + addrBytes + crcBytes); } -void IR_Encoder::sendRequest(uint16_t addrTo, uint8_t addInfo) { +void IR_Encoder::sendRequest(uint16_t addrTo) { memset(sendBuffer, 0x00, dataByteSizeMax); sendBuffer[0] = IR_MSG_REQUEST << 5; - sendBuffer[0] |= addInfo & IR_MASK_MSG_INFO; + sendBuffer[0] |= msgBytes + addrBytes + addrBytes + crcBytes & IR_MASK_MSG_INFO; // addr_self sendBuffer[1] = id >> 8 & 0xFF; @@ -127,7 +127,7 @@ void IR_Encoder::_sendBack(bool isAdressed, uint16_t addrTo, uint8_t* data, uint uint8_t packSize = msgBytes + addrBytes + (isAdressed ? addrBytes : 0) + min(1, len) + crcBytes; uint8_t msgType = - ((isAdressed ? IR_MSG_BACK_TO : IR_MSG_BACK)<<5) | ((packSize) & (IR_MASK_MSG_INFO >> 1)); + ((isAdressed ? IR_MSG_BACK_TO : IR_MSG_BACK) << 5) | ((packSize) & (IR_MASK_MSG_INFO >> 1)); // формирование массива // msg_type diff --git a/IR_Encoder.h b/IR_Encoder.h index 65462f2..a3eab06 100644 --- a/IR_Encoder.h +++ b/IR_Encoder.h @@ -45,8 +45,8 @@ public: void IR_Encoder::setBlindDecoders(IR_DecoderRaw* decoders [], uint8_t count); void rawSend(uint8_t* ptr, uint8_t len); void sendData(uint16_t addrTo, uint8_t* data, uint8_t len, bool needAccept = false); - void sendAccept(uint16_t addrTo, uint8_t addInfo = 0); - void sendRequest(uint16_t addrTo, uint8_t addInfo = 0); + void sendAccept(uint16_t addrTo); + void sendRequest(uint16_t addrTo); void sendBack(uint8_t* data = nullptr, uint8_t len = 0); void sendBackTo(uint16_t addrTo, uint8_t* data = nullptr, uint8_t len = 0); void isr();