diff --git a/IR-Protocol.ino b/IR-Protocol.ino index d812e06..bc499f5 100644 --- a/IR-Protocol.ino +++ b/IR-Protocol.ino @@ -1,4 +1,4 @@ -#include "IR_DecoderRaw.h" +#include "IR_Decoder.h" #include "IR_Encoder.h" #include "TimerStatic.h" #include "MemoryCheck.h" @@ -19,8 +19,8 @@ //////////////// Var ///////////////// -IR_DecoderRaw decForward(2, 555); -IR_DecoderRaw decBackward(3, 777); +IR_Decoder decForward(2, 555); +IR_Decoder decBackward(3, 777); IR_Encoder encForward(42, encForward_PIN, &decBackward); // IR_Encoder encBackward(321, encBackward_PIN); @@ -226,10 +226,8 @@ void loop() { //test -void status(IR_DecoderRaw& dec) { - if (dec.available()) { - Serial.println("LOOP RAW DATA"); - } +void status(IR_Decoder& dec) { + // IR_DecoderRaw::AnyData* infoArr [] = { &dec.gotData, &dec.gotBackData }; diff --git a/IR_Decoder.h b/IR_Decoder.h index 11095d6..4859f02 100644 --- a/IR_Decoder.h +++ b/IR_Decoder.h @@ -1,33 +1,45 @@ #pragma once -#include "IR_config.h" - - // class IR_Decoder - - - - - - - - - - - - - - - - - - - - - - - +#include "IR_DecoderRaw.h" +#include "PacketTypes.h" +class IR_Decoder : public IR_DecoderRaw { +public: + IR_Decoder(const uint8_t isrPin, uint16_t addr, IR_Encoder* encPair = nullptr) : + IR_DecoderRaw(isrPin, addr, encPair), + gotData(Data(&packInfo)) { + } + + Data gotData; + + void tick() { + IR_DecoderRaw::tick(); + if (available()) { + Serial.println("PARSING RAW DATA"); + + switch (packInfo.buffer[0] & IR_MASK_MSG_TYPE) { + case IR_MSG_DATA_NOACCEPT: + + break; + case IR_MSG_DATA_ACCEPT: + break; + case IR_MSG_BACK: + break; + case IR_MSG_BACK_TO: + break; + case IR_MSG_REQUEST: + break; + case IR_MSG_ACCEPT: + break; + + default: + break; + } + + } + } +}; diff --git a/IR_DecoderRaw.cpp b/IR_DecoderRaw.cpp index 51c38fe..5849f22 100644 --- a/IR_DecoderRaw.cpp +++ b/IR_DecoderRaw.cpp @@ -349,14 +349,21 @@ void IR_DecoderRaw::writeToBuffer(bool bit) { if (!isAvailable && isData) { if (i_dataBuffer == 8 * msgBytes) {// Ппервый байт - packSize = (dataBuffer[0] & IR_MASK_MSG_INFO) + crcBytes; + packSize = dataBuffer[0] & IR_MASK_MSG_INFO; Serial.print(" ["); Serial.print(packSize); Serial.print("] "); } if (packSize && (i_dataBuffer == packSize*bitPerByte)) { // Конец - isAvailable = crcCheck(packSize - crcBytes, crcValue); Serial.print(" END DATA "); + + packInfo.buffer = dataBuffer; + packInfo.crc = crcValue; + packInfo.err = errors; + packInfo.packSize = packSize; + packInfo.rTime = riseSyncTime; + isRecive = false; + isAvailable = crcCheck(packSize - crcBytes, crcValue); } } diff --git a/IR_DecoderRaw.h b/IR_DecoderRaw.h index 56c6989..515d9fe 100644 --- a/IR_DecoderRaw.h +++ b/IR_DecoderRaw.h @@ -23,7 +23,7 @@ class IR_Encoder; -class IR_DecoderRaw : private IR_FOX { +class IR_DecoderRaw : public IR_FOX { friend IR_Encoder; public: const uint8_t isrPin; // Пин прерывания @@ -42,8 +42,10 @@ public: bool isReciving() { return isBufferOverflow; }; // Возвращает true, если происходит приём пакета ////////////////////////////////////////////////////////////////////////// - +protected: + PackInfo packInfo; private: + ErrorsStruct errors; uint16_t id; bool isAvailable = false; uint16_t packSize; diff --git a/IR_Encoder.cpp b/IR_Encoder.cpp index 661e633..0848086 100644 --- a/IR_Encoder.cpp +++ b/IR_Encoder.cpp @@ -39,7 +39,7 @@ void IR_Encoder::sendData(uint16_t addrTo, uint8_t* data, uint8_t len, bool need 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); + ((needAccept ? IR_MSG_DATA_ACCEPT : IR_MSG_DATA_NOACCEPT) << 5) | (packSize & IR_MASK_MSG_INFO); // формирование массива // msg_type @@ -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 = - (IR_MSG_BACK << 5) | (isAdressed << 4U) | ((packSize - crcBytes) & (IR_MASK_MSG_INFO >> 1)); + (IR_MSG_BACK << 5) | (isAdressed << 4U) | ((packSize) & (IR_MASK_MSG_INFO >> 1)); // формирование массива // msg_type diff --git a/IR_config.h b/IR_config.h index 97ee536..19fafa4 100644 --- a/IR_config.h +++ b/IR_config.h @@ -146,9 +146,9 @@ public: }; struct ErrorsStruct { - uint8_t lowSignal; - uint8_t highSignal; - uint8_t other; + uint8_t lowSignal = 0; + uint8_t highSignal= 0; + uint8_t other= 0; void reset() { lowSignal = 0; @@ -160,11 +160,11 @@ public: }; struct PackInfo { - uint8_t* ptr; - uint8_t packSize; - uint16_t crc; + uint8_t* buffer = nullptr; + uint8_t packSize = 0; + uint16_t crc= 0; ErrorsStruct err; - uint16_t rTime; + uint16_t rTime = 0; }; protected: diff --git a/PacketTypes.h b/PacketTypes.h new file mode 100644 index 0000000..b75693a --- /dev/null +++ b/PacketTypes.h @@ -0,0 +1,59 @@ +#pragma once +#include "IR_config.h" + +class IOffsets { +public: + uint8_t msgOffset; + uint8_t addressFromOffset; + uint8_t addressToOffset; + uint8_t DataOffset; +}; + +class IPackInfo { +public: + IR_FOX::PackInfo* packInfo; +}; + +class IBaseEmptyPack : virtual public IOffsets, virtual public IPackInfo {}; + +class IEmptyPack : virtual protected IBaseEmptyPack { + bool isAvailable; + +public: + IEmptyPack(IR_FOX::PackInfo* _packInfo) { packInfo = _packInfo; } + virtual bool available() { if (isAvailable) { isAvailable = false; return true; } else { return false; } }; + virtual uint8_t getMsgInfo() { return packInfo->buffer[0] & IR_MASK_MSG_INFO; }; + virtual uint8_t getMsgType() { return (packInfo->buffer[0] >> 5) & IR_MASK_MSG_TYPE; }; + virtual uint8_t getMsgRAW() { return packInfo->buffer[0]; }; + virtual uint16_t getErrorCount() { return packInfo->err.all(); }; + virtual uint8_t getErrorLowSignal() { return packInfo->err.lowSignal; }; + virtual uint8_t getErrorHighSignal() { return packInfo->err.highSignal; }; + virtual uint8_t getErrorOther() { return packInfo->err.other; }; + virtual uint16_t getTunerTime() { return packInfo->rTime; }; +}; + +class IHasAddresFrom : virtual protected IBaseEmptyPack { + virtual uint16_t getAddrFrom() { return packInfo->buffer[addressFromOffset] | packInfo->buffer[addressFromOffset + 1]; }; +}; + +class IHasAddresTo : virtual protected IBaseEmptyPack { + virtual uint16_t getAddrTo() { return packInfo->buffer[addressToOffset] | packInfo->buffer[addressToOffset + 1]; }; +}; + +class IHasAddresData : virtual protected IBaseEmptyPack { + virtual uint8_t getDataSize() { return /* packInfo->buffer[packInfo->packSize-2] */0; };//TODO: + virtual uint8_t* getDataPrt() { return packInfo->buffer + DataOffset; }; + virtual uint8_t getDataRawSize() { return packInfo->packSize; }; + virtual uint8_t* getDataRawPtr() { return packInfo->buffer; }; +}; + + + +class Data : + virtual public IEmptyPack, + virtual public IHasAddresFrom { +public: + Data(IR_FOX::PackInfo* packInfo) : IEmptyPack(packInfo) { + msgOffset = 0; + } +}; \ No newline at end of file