From a143dcf80d3f28eebf9926975510a2d0402acdc3 Mon Sep 17 00:00:00 2001 From: DashyFox Date: Fri, 16 Feb 2024 09:28:09 +0300 Subject: [PATCH] add packToOutClass func --- IR_Decoder.cpp | 101 ++++++++++++++++++++++++++++--------------------- IR_Decoder.h | 2 + 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/IR_Decoder.cpp b/IR_Decoder.cpp index 3d7cff9..ab92032 100644 --- a/IR_Decoder.cpp +++ b/IR_Decoder.cpp @@ -1,7 +1,7 @@ #include "IR_Decoder.h" #include "IR_Encoder.h" -// #define IRDEBUG_INFO +#define IRDEBUG_INFO #define checkAddr(h, l) (\ ((uint16_t)((dataBuffer[h] << 8) | dataBuffer[l]) == addrSelf) || \ @@ -79,7 +79,7 @@ void IR_Decoder::start_RX() { void IR_Decoder::listen() { if (isRecive && ((micros() - prevRise) > IR_timeout)) { - isRecive = false; + isRecive = false; start_RX(); } } @@ -97,7 +97,7 @@ void IR_Decoder::tick() { if (currentFront.time - prevRise > IR_timeout * 2 /* && !isRecive */) { // первый preambFrontCounter = preambFronts - 1U; - + if (!currentFront.dir) { isRecive = true; } @@ -326,10 +326,13 @@ void IR_Decoder::writeToBuffer(bool bit) { i_syncBit++; } ////////////////////// Проверка наличия битов синхранизации ////////////////////// - isWrongPack = (err_syncBit >= syncBits); - #ifdef IRDEBUG_INFO - if (isWrongPack) Serial.print("****************"); - #endif + if (isWrongPack = (err_syncBit >= syncBits)) { + start_RX(); + #ifdef IRDEBUG_INFO + Serial.print("****************"); + #endif + }; + }//**************************************************************************************************// // Serial.print(bit); @@ -348,54 +351,38 @@ void IR_Decoder::writeToBuffer(bool bit) { if (i_dataBuffer == ((dataBuffer[0] & IR_MASK_MSG_INFO) * bitPerByte)) { Serial.print(" <-"); } } #endif + if ((i_dataBuffer >= (8 * msgBytes)) && !isCrcCorrect) { uint16_t crcValue; switch ((dataBuffer[0] >> 5) & IR_MASK_MSG_TYPE) { case IR_MSG_ACCEPT: - if (i_dataBuffer >= ((msgBytes + addrBytes + crcBytes) * bitPerByte)) { - constexpr uint8_t dataSize = msgBytes + addrBytes; - isCrcCorrect = crcCheck(dataSize, crcValue); - if (isCrcCorrect && checkAddr(1, 2)) { - gotAccept._set(dataBuffer, msgBytes + addrBytes + crcBytes, crcValue, errors, riseSyncTime); - gotAccept._isAvaliable = true; - } - } + packToOutClass( + ((msgBytes + addrBytes + crcBytes) * bitPerByte), // endBitOffset + (msgBytes + addrBytes), // bytesToCheck + 1, // addressForCheckOffset + &gotAccept // objFine + ); break; case IR_MSG_REQUEST: - if (i_dataBuffer >= ((msgBytes + addrBytes + addrBytes + crcBytes) * bitPerByte)) { - constexpr uint8_t dataSize = msgBytes + addrBytes + addrBytes; - isCrcCorrect = crcCheck(dataSize, crcValue); - if (isCrcCorrect && checkAddr(3, 4)) { - gotRequest._isAvaliable = true; - gotRequest._set(dataBuffer, msgBytes + addrBytes + addrBytes + crcBytes, crcValue, errors, riseSyncTime); - } - } + packToOutClass( + ((msgBytes + addrBytes + addrBytes + crcBytes) * bitPerByte), // endBitOffset + (msgBytes + addrBytes + addrBytes), // bytesToCheck + 3, // addressForCheckOffset + &gotRequest // objFine + ); break; case IR_MSG_DATA_ACCEPT: case IR_MSG_DATA_NOACCEPT: - if (i_dataBuffer >= ((dataBuffer[0] & IR_MASK_MSG_INFO) + crcBytes) * bitPerByte) { - #ifdef IRDEBUG_INFO - Serial.print(" IN "); - #endif - const uint8_t dataSize = (dataBuffer[0] & IR_MASK_MSG_INFO); - isCrcCorrect = crcCheck(dataSize, crcValue); - if (isCrcCorrect && checkAddr(3, 4)) { - #ifdef IRDEBUG_INFO - Serial.println(" OK "); - #endif - gotData._isAvaliable = true; - gotData._set(dataBuffer, (dataSize)+crcBytes, crcValue, errors, riseSyncTime); - } else { - gotRawData._isAvaliable = true; - gotRawData._set(dataBuffer, (dataSize)+crcBytes, crcValue, errors, riseSyncTime); - #ifdef IRDEBUG_INFO - Serial.println(" NOT OK "); - #endif - } - } + packToOutClass( + (((dataBuffer[0] & IR_MASK_MSG_INFO) + crcBytes) * bitPerByte), // endBitOffset + (dataBuffer[0] & IR_MASK_MSG_INFO), // bytesToCheck + 3, // addressForCheckOffset + &gotData, // objFine + &gotRawData // objWrong + ); break; default: @@ -405,6 +392,34 @@ void IR_Decoder::writeToBuffer(bool bit) { ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } +void IR_Decoder::packToOutClass(uint8_t endBitOffset, uint8_t bytesToCheck, uint8_t addressForCheckOffset, InputData* objFine, InputData* objWrong = nullptr) { + uint16_t crcValue; + InputData* objResult = nullptr; + if (i_dataBuffer >= endBitOffset) { + #ifdef IRDEBUG_INFO + Serial.print(" IN "); + #endif + isCrcCorrect = crcCheck(bytesToCheck, crcValue); + if (isCrcCorrect && checkAddr(addressForCheckOffset, addressForCheckOffset + 1)) { + #ifdef IRDEBUG_INFO + Serial.println(" OK "); + #endif + objResult = objFine; + } else { + objResult = objWrong; + #ifdef IRDEBUG_INFO + Serial.println(" NOT OK "); + #endif + } + if (objWrong != nullptr) { + objResult->_isAvaliable = true; + objResult->_set(dataBuffer, bytesToCheck + crcBytes, crcValue, errors, riseSyncTime); + } + isRecive = false; + start_RX(); + } +} + bool IR_Decoder::crcCheck(uint8_t len, crc_t& crc) { diff --git a/IR_Decoder.h b/IR_Decoder.h index 364c38c..7b96fec 100644 --- a/IR_Decoder.h +++ b/IR_Decoder.h @@ -257,11 +257,13 @@ private: /// @brief Запиь бита в буффер, а так же проверка битов синхранизации и их фильтрация /// @param Бит данных void writeToBuffer(bool); + void packToOutClass(uint8_t endBitOffset, uint8_t bytesToCheck, uint8_t addressForCheckOffset, InputData* objFine, InputData* objWrong = nullptr); //////////////////////////////////////////////////////////////////////// /// @brief Установка и сброс начальных значений и флагов в готовность к приёму данных void start_RX(); + /// @brief Целочисленное деление с округлением вверх /// @param val Значение /// @param divider Делитель