mirror of
https://github.com/Show-maket/IR-protocol.git
synced 2025-06-28 05:09:40 +00:00
v1.00
This commit is contained in:
186
IR_Decoder.h
Normal file
186
IR_Decoder.h
Normal file
@ -0,0 +1,186 @@
|
||||
#pragma once
|
||||
#include "IR_config.h"
|
||||
|
||||
//#define IRDEBUG
|
||||
|
||||
#ifdef IRDEBUG
|
||||
#define wrHigh 3 // Запись HIGH инициирована // green
|
||||
#define wrLow 4 // Запись LOW инициирована // blue
|
||||
#define writeOp 6 // Операция записи, 1 пульс для 0 и 2 для 1 // orange
|
||||
// Исправленные ошибки // purle
|
||||
// 1 пульс: fix
|
||||
#define errOut 5
|
||||
#endif
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define riseTime riseSyncTime //* bitTime */ 893U // TODO: Должно высчитываться медианой
|
||||
#define riseTolerance tolerance /* 250U */ // погрешность
|
||||
#define riseTimeMax (riseTime + riseTolerance)
|
||||
#define riseTimeMin (riseTime - riseTolerance)
|
||||
#define aroundRise(t) (riseTimeMin < t && t < riseTimeMax)
|
||||
#define timeout ((riseTimeMax * 8) + syncBits +1) // us // таймаут в 8 data + 3 sync + 1
|
||||
|
||||
class IR_Encoder;
|
||||
class IR_Decoder : private IR_FOX {
|
||||
friend IR_Encoder;
|
||||
public:
|
||||
uint16_t addrSelf;
|
||||
|
||||
IR_Decoder(uint16_t addr, IR_Encoder* encPair = nullptr);
|
||||
~IR_Decoder();
|
||||
|
||||
// @brief Для прерывания
|
||||
void isr();
|
||||
|
||||
|
||||
// @return Буффер переполнился
|
||||
bool isOverflow() { return isBufferOverflow; };
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class InputData {
|
||||
friend IR_Decoder;
|
||||
protected:
|
||||
bool _isAvaliable = false;
|
||||
uint8_t _msgType;
|
||||
uint16_t _addrFrom = 0;
|
||||
uint16_t _addrTo = 0;
|
||||
uint8_t* _data = nullptr;
|
||||
uint8_t _dataRawSize = 0;
|
||||
uint16_t _crcPackVal;
|
||||
uint16_t _crcCalcVal;
|
||||
uint16_t _errCount;
|
||||
|
||||
void _set(uint8_t* ptr, uint8_t len, uint16_t crc, uint16_t err) {
|
||||
_crcCalcVal = crc;
|
||||
_dataRawSize = len;
|
||||
_errCount = err;
|
||||
if (_data != nullptr) { delete _data; _data = nullptr; }
|
||||
_data = new uint8_t[len];
|
||||
for (uint8_t i = 0; i < len; i++) { _data[i] = ptr[i]; }
|
||||
_msgType = _data[0];
|
||||
ini();
|
||||
_isAvaliable = true;
|
||||
}
|
||||
private:
|
||||
virtual void ini();
|
||||
|
||||
public:
|
||||
bool avaliable() { return _isAvaliable; };
|
||||
uint8_t msgInfo() { return _msgType & IR_MASK_MSG_INFO; };
|
||||
uint8_t msgType() { return (_msgType >> 5) & IR_MASK_MSG_TYPE; };
|
||||
uint8_t msgRAW() { return _msgType; };
|
||||
uint16_t errorCount() { return _errCount; };
|
||||
uint16_t crcIN() { return _crcPackVal; };
|
||||
uint16_t crcCALC() { return _crcCalcVal; };
|
||||
void resetAvaliable() { _isAvaliable = false; };
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Data : public InputData {
|
||||
public:
|
||||
uint16_t addrFrom() { return _addrFrom; };
|
||||
uint16_t addrTo() { return _addrTo; };
|
||||
uint8_t dataSize() { return _dataRawSize - (msgBytes + addrBytes + addrBytes + crcBytes); };
|
||||
uint8_t* data() { return &_data[msgBytes + addrBytes + addrBytes]; };
|
||||
uint8_t dataRawSize() { return _dataRawSize; };
|
||||
uint8_t* dataRaw() { return _data; };
|
||||
bool isNeedAccept() { return ((_msgType >> 5) & IR_MASK_MSG_TYPE) == IR_MSG_DATA_ACCEPT; };
|
||||
private:
|
||||
void ini() override {
|
||||
_addrFrom = (_data[1] << 8) | _data[2];
|
||||
_addrTo = (_data[3] << 8) | _data[4];
|
||||
_crcPackVal = (_data[_dataRawSize - 2] << 8) | _data[_dataRawSize - 1];
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class Accept : public InputData {
|
||||
public:
|
||||
uint16_t addrFrom() { return _addrFrom; };
|
||||
|
||||
private:
|
||||
void ini() override {
|
||||
_addrFrom = (_data[1] << 8) | _data[2];
|
||||
_crcPackVal = (_data[3] << 8) | _data[4];
|
||||
}
|
||||
};
|
||||
|
||||
class Request : public Accept {
|
||||
public:
|
||||
uint16_t addrTo() { return _addrTo; };
|
||||
private:
|
||||
void ini() override {
|
||||
_addrFrom = (_data[1] << 8) | _data[2];
|
||||
_addrTo = (_data[3] << 8) | _data[4];
|
||||
_crcPackVal = (_data[5] << 8) | _data[6];
|
||||
}
|
||||
};
|
||||
|
||||
Data gotData;
|
||||
Accept gotAccept;
|
||||
Request gotRequest;
|
||||
|
||||
|
||||
private:
|
||||
IR_Encoder* encoder;
|
||||
bool isPairSending = false;
|
||||
bool IsPairSendLOW = false;
|
||||
|
||||
bool isWaitingAccept = false;
|
||||
uint16_t addrWaitingFrom = 0;
|
||||
|
||||
|
||||
uint16_t addrFrom = 0;
|
||||
|
||||
uint16_t riseSyncTime = bitTime;
|
||||
|
||||
volatile bool isRawAvaliable = false;
|
||||
|
||||
volatile bool isMsgAvaliable = false;
|
||||
volatile bool isFilterBufferAvaliable = false;
|
||||
|
||||
volatile bool isBufferOverflow = false;
|
||||
|
||||
volatile bool isPreamb = false; // флаг начальной последовости
|
||||
bool HIGH_FIRST = true; // порядок приходящих битов
|
||||
|
||||
//Буффер
|
||||
const uint8_t bufferRawSize =
|
||||
((bufferBitSizeMax % 8 > 0) ?
|
||||
(bufferBitSizeMax / 8) + 1 :
|
||||
(bufferBitSizeMax / 8));
|
||||
const uint8_t bufferDataSize = dataByteSizeMax; // + crc
|
||||
uint8_t* rawBuffer = nullptr;
|
||||
uint8_t* dataBuffer = nullptr;
|
||||
|
||||
volatile uint32_t prevRise, prevFall, prevPrevFall, prevPrevRise;
|
||||
volatile uint16_t errorCounter = 0;
|
||||
volatile int8_t frontCounter = 0;
|
||||
volatile int16_t bufBitPos = 0;
|
||||
crc_t crcValue = 0;
|
||||
private:
|
||||
uint8_t* getDataBuffer(bool reset = false);
|
||||
bool crcCheck(uint8_t len);
|
||||
|
||||
inline void writeToBuffer(bool);
|
||||
inline void start_RX();
|
||||
void resetAvaliable();
|
||||
|
||||
uint16_t ceil_div(uint16_t, uint16_t);
|
||||
//uint16_t sma = 0;
|
||||
void SMA(uint16_t);
|
||||
|
||||
//TODO: Сделать функцию медианы
|
||||
void medi(uint16_t);
|
||||
|
||||
|
||||
#ifdef IRDEBUG
|
||||
inline void errPulse(uint8_t pin, uint8_t count);
|
||||
inline void infoPulse(uint8_t pin, uint8_t count);
|
||||
#endif
|
||||
|
||||
|
||||
};
|
Reference in New Issue
Block a user