diff --git a/IR_Encoder.cpp b/IR_Encoder.cpp index 1847465..3d96b21 100644 --- a/IR_Encoder.cpp +++ b/IR_Encoder.cpp @@ -11,15 +11,15 @@ IR_Encoder::IR_Encoder(uint16_t addr, uint8_t pin, IR_Decoder* decPair = nullptr signal = noSignal; isSending = false; #if disablePairDec - if(decPair != nullptr){ - blindDecoders = new IR_Decoder*[1]{decPair}; + if (decPair != nullptr) { + blindDecoders = new IR_Decoder * [1] {decPair}; decodersCount = 1; } #endif }; -void IR_Encoder::setBlindDecoders(IR_Decoder* decoders[], uint8_t count){ +void IR_Encoder::setBlindDecoders(IR_Decoder* decoders [], uint8_t count) { #if disablePairDec - if(blindDecoders != nullptr) delete[] blindDecoders; + if (blindDecoders != nullptr) delete [] blindDecoders; #endif decodersCount = count; blindDecoders = decoders; @@ -31,10 +31,40 @@ IR_Encoder::~IR_Encoder() { }; void IR_Encoder::sendData(uint16_t addrTo, uint8_t* data, uint8_t len, bool needAccept = false) { + constexpr uint8_t dataStart = msgBytes + addrBytes + addrBytes; + memset(sendBuffer, 0x00, dataByteSizeMax); uint8_t packSize = msgBytes + addrBytes + addrBytes + len + crcBytes; uint8_t msgType = ((needAccept ? IR_MSG_DATA_ACCEPT : IR_MSG_DATA_NOACCEPT) << 5) | ((packSize - crcBytes) & IR_MASK_MSG_INFO); - _sendData(addrTo, data, len, msgType); + + // формирование массива + // msg_type + sendBuffer[0] = msgType; + + // addr_self + sendBuffer[1] = id >> 8 & 0xFF; + sendBuffer[2] = id & 0xFF; + + // addr_to + sendBuffer[3] = addrTo >> 8 & 0xFF; + sendBuffer[4] = addrTo & 0xFF; + + for (uint16_t i = dataStart; i < dataStart + len; i++) { + sendBuffer[i] = ((uint8_t*)data)[i - dataStart]; + } + + // data crc + sendBuffer[packSize - crcBytes] = crc8(sendBuffer, 0, packSize - crcBytes, poly1) & 0xFF; + sendBuffer[packSize - crcBytes + 1] = crc8(sendBuffer, 0, packSize - crcBytes + 1, poly2) & 0xFF; + + if (decPair != nullptr) { + decPair->isWaitingAccept = ((msgType >> 5) & IR_MASK_MSG_TYPE == IR_MSG_DATA_ACCEPT); + if (decPair->isWaitingAccept) { + decPair->addrWaitingFrom = addrTo; + } + } + // отправка + rawSend(sendBuffer, packSize); } void IR_Encoder::sendACK(uint16_t addrTo, uint8_t addInfo, bool forAll = false) { @@ -75,42 +105,7 @@ void IR_Encoder::sendRequest(uint16_t addrTo, uint8_t addInfo) { rawSend(sendBuffer, msgBytes + addrBytes + addrBytes + crcBytes); } -void IR_Encoder::_sendData(uint16_t addrTo, uint8_t* data, uint8_t len, uint8_t msgType) { - memset(sendBuffer, 0x00, dataByteSizeMax); - uint8_t packSize = msgBytes + addrBytes + addrBytes + len + crcBytes; - uint8_t dataStart = msgBytes + addrBytes + addrBytes; - - // формирование массива - // msg_type - sendBuffer[0] = msgType; - - // addr_self - sendBuffer[1] = id >> 8 & 0xFF; - sendBuffer[2] = id & 0xFF; - - // addr_to - sendBuffer[3] = addrTo >> 8 & 0xFF; - sendBuffer[4] = addrTo & 0xFF; - - for (uint16_t i = dataStart; i < dataStart + len; i++) { - sendBuffer[i] = ((uint8_t*)data)[i - dataStart]; - } - - // data crc - sendBuffer[packSize - crcBytes] = crc8(sendBuffer, 0, packSize - crcBytes, poly1) & 0xFF; - sendBuffer[packSize - crcBytes + 1] = crc8(sendBuffer, 0, packSize - crcBytes + 1, poly2) & 0xFF; - - if (decPair != nullptr) { - decPair->isWaitingAccept = ((msgType >> 5) & IR_MASK_MSG_TYPE == IR_MSG_DATA_ACCEPT); - if (decPair->isWaitingAccept) { - decPair->addrWaitingFrom = addrTo; - } - } - // отправка - rawSend(sendBuffer, packSize); - -} void IR_Encoder::setDecoder_isSending() { if (decodersCount) { diff --git a/IR_Encoder.h b/IR_Encoder.h index 066e981..e4949d5 100644 --- a/IR_Encoder.h +++ b/IR_Encoder.h @@ -44,8 +44,8 @@ public: } void IR_Encoder::setBlindDecoders(IR_Decoder* decoders [], uint8_t count); - template - void sendData(uint16_t addrTo, T& data, bool needAccept = false); + // template + // void sendData(uint16_t addrTo, T& data, bool needAccept = false); void sendData(uint16_t addrTo, uint8_t* data, uint8_t len, bool needAccept = false); void sendACK(uint16_t addrTo, uint8_t addInfo = 0, bool forAll = false); void sendRequest(uint16_t addrTo, uint8_t addInfo = 0); @@ -113,14 +113,14 @@ private: //////////////////////////////////////////////////////////////////////////////////////////////// -template -void IR_Encoder::sendData(uint16_t addrTo, T& data, bool needAccept = false) { // TODO: переделать логику LOW_FIRST - uint8_t len = sizeof(T); - uint8_t packSize = msgBytes + addrBytes + addrBytes + len + crcBytes; +// template +// void IR_Encoder::sendData(uint16_t addrTo, T& data, bool needAccept = false) { // TODO: переделать логику LOW_FIRST +// uint8_t len = sizeof(T); +// uint8_t packSize = msgBytes + addrBytes + addrBytes + len + crcBytes; - uint8_t msgType = - ((needAccept ? IR_MSG_DATA_ACCEPT : IR_MSG_DATA_NOACCEPT) << 5) | ((packSize - crcBytes) & IR_MASK_MSG_INFO); +// uint8_t msgType = +// ((needAccept ? IR_MSG_DATA_ACCEPT : IR_MSG_DATA_NOACCEPT) << 5) | ((packSize - crcBytes) & IR_MASK_MSG_INFO); - _sendData(addrTo, data, len, msgType); -} +// _sendData(addrTo, data, len, msgType); +// }