add packToOutClass func

This commit is contained in:
DashyFox 2024-02-16 09:28:09 +03:00
parent 4a8195e561
commit a143dcf80d
2 changed files with 60 additions and 43 deletions

View File

@ -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) || \
@ -326,10 +326,13 @@ void IR_Decoder::writeToBuffer(bool bit) {
i_syncBit++;
}
////////////////////// Проверка наличия битов синхранизации //////////////////////
isWrongPack = (err_syncBit >= syncBits);
if (isWrongPack = (err_syncBit >= syncBits)) {
start_RX();
#ifdef IRDEBUG_INFO
if (isWrongPack) Serial.print("****************");
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) {

View File

@ -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 Делитель