IR-protocol/IR_Decoder.h

283 lines
9.8 KiB
C++

#pragma once
#include "IR_config.h"
//#define IRDEBUG
#ifdef IRDEBUG
#define wrHigh A3 // Запись HIGH инициирована // green
#define wrLow A3 // Запись LOW инициирована // blue
#define writeOp 13 // Операция записи, 1 пульс для 0 и 2 для 1 // orange
// Исправленные ошибки // purle
// 1 пульс: fix
#define errOut A3
#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 IR_timeout ((riseTimeMax * 8) + syncBits +1) // us // таймаут в 8 data + 3 sync + 1
#define subBufferSize 15 //Буфер для складирования фронтов, пока их не обработают
class IR_Encoder;
class IR_Decoder : private IR_FOX {
friend IR_Encoder;
public:
uint16_t addrSelf;
IR_Decoder(const uint8_t isrPin, uint16_t addr, IR_Encoder* encPair = nullptr);
~IR_Decoder();
// @brief Для прерывания
void isr();
void tick();
// @return Буффер переполнился
bool isOverflow() { return isBufferOverflow; };
bool isReciving() { return isRecive; };
// @brief Слушатель для работы isReciving()
void listen();
//////////////////////////////////////////////////////////////////////////
class InputData : protected IR_FOX {
friend IR_Decoder;
protected:
bool _isAvaliable = false;
uint8_t _msgType = 0;
uint16_t _addrFrom = 0;
uint16_t _addrTo = 0;
uint8_t* _data = nullptr;
uint8_t _dataRawSize = 0;
uint16_t _crcPackVal = 0;
uint16_t _crcCalcVal = 0;
uint16_t _errCount = 0;
uint16_t _bitPeriod = 0;
void _set(uint8_t* ptr, uint8_t len, uint16_t crc, uint16_t err, uint16_t rTime) {
_crcCalcVal = crc;
_dataRawSize = len;
_errCount = err;
_bitPeriod = rTime;
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; };
uint16_t tunerTime() { return _bitPeriod; };
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; };
String printRawData(uint8_t mode = 10) {
return printBytes(dataRaw(), dataRawSize(), mode);
}
String printData(uint8_t mode = 10) {
return printBytes(data(), dataSize(), mode);
}
~Data() {};
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 RawData : public Data {
// };
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];
}
};
class RawTune {
friend IR_Decoder;
private:
bool _isAvaliable = false;
uint16_t _errCount = 0;
uint16_t _tune = 0;
public:
bool avaliable() { return _isAvaliable; };
uint16_t getTune() { return _tune; };
uint16_t errorCount() { return _errCount; };
void resetAvaliable() { _isAvaliable = false; };
private:
void _set(uint16_t val) {
_tune = val;
_isAvaliable = true;
}
};
Data gotData;
Data gotRawData;
// RawData gotRawData;
Accept gotAccept;
Request gotRequest;
RawTune gotTune;
private:
const uint8_t isrPin;
IR_Encoder* encoder;
bool isPairSending = false;
bool IsPairSendLOW = false;
volatile bool isRecive = 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
////////////////////////////////////////////////////////////////////////
void noFunc();
volatile uint8_t currentSubBufferIndex; // Счетчик текущей позиции во вспомогательном буфере фронтов/спадов
// Структура для хранения времени и направления фронта/спада
struct FrontStorage {
volatile uint32_t time; // Время
volatile bool dir; // Направление (true = ↑; false = ↓)
volatile FrontStorage* next; // Указатель на следующий связанный фронт/спад, или nullptr если конец
// Операторо присвоения
FrontStorage& operator= (FrontStorage& val) {
this->next = val.next;
this->time = val.time;
this->dir = val.dir;
return *this;
}
};
volatile FrontStorage* lastFront = nullptr; // Указатель последнего фронта/спада
volatile FrontStorage* firstUnHandledFront = nullptr; // Указатель первого необработанного фронта/спада
volatile FrontStorage subBuffer[subBufferSize]; // вспомогательный буфер для хранения необработанных фронтов/спадов
////////////////////////////////////////////////////////////////////////
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);
////////////////////////////////////////////////////////////////////////
bool isData = true; // Флаг относится ли бит к данным, или битам синхронизации
uint16_t i_dataBuffer; // Счётчик буфера данных
uint8_t nextControlBit = bitPerByte; // Метка для смены флага isData
uint8_t i_syncBit; // Счётчик битов синхронизации
void writeToBuffer(bool);
////////////////////////////////////////////////////////////////////////
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);
// class Medi {
// public:
// uint16_t* arr;
// uint8_t size;
// uint8_t center;
// Medi(uint8_t _size) : size(_size - 1) {
// arr = new uint16_t[size] { 0 };
// center = size / 2;
// };
// void add(uint16_t newVal) {
// _add(newVal, center);
// }
// void _add(uint16_t newVal, int8_t pos, bool f) {
// if (pos < 0 || pos > size) return;
// if (newVal < arr[pos]) _add(newVal, pos-1, f);
// if (newVal > arr[pos]) _add(newVal, pos+1, f);
// }
// ~Medi() { delete arr; }
// };
#ifdef IRDEBUG
inline void errPulse(uint8_t pin, uint8_t count);
inline void infoPulse(uint8_t pin, uint8_t count);
#endif
};