mirror of
https://github.com/Show-maket/IR-protocol.git
synced 2025-06-27 20:59:37 +00:00
merge from TX and RX
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
#include "IR_Decoder.h"
|
#include "IR_Decoder.h"
|
||||||
#include "IR_Encoder.h"
|
#include "IR_Encoder.h"
|
||||||
|
|
||||||
#define checkAddr(h, l) (\
|
#define checkAddr(h, l) (\
|
||||||
((uint16_t)((dataBuffer[h] << 8) | dataBuffer[l]) == addrSelf) || \
|
((uint16_t)((dataBuffer[h] << 8) | dataBuffer[l]) == addrSelf) || \
|
||||||
((uint16_t)((dataBuffer[h] << 8) | dataBuffer[l]) >= IR_Broadcast)\
|
((uint16_t)((dataBuffer[h] << 8) | dataBuffer[l]) >= IR_Broadcast)\
|
||||||
@ -42,7 +42,7 @@ void IR_Decoder::writeToBuffer(bool bit) {
|
|||||||
isRawAvaliable = true;
|
isRawAvaliable = true;
|
||||||
isMsgAvaliable = crcCheck(dataSize);
|
isMsgAvaliable = crcCheck(dataSize);
|
||||||
if (isMsgAvaliable && checkAddr(1, 2)) {
|
if (isMsgAvaliable && checkAddr(1, 2)) {
|
||||||
gotAccept._set(dataBuffer, msgBytes + addrBytes + crcBytes, crcValue, errorCounter);
|
gotAccept._set(dataBuffer, msgBytes + addrBytes + crcBytes, crcValue, errorCounter, riseSyncTime);
|
||||||
gotAccept._isAvaliable = true;
|
gotAccept._isAvaliable = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ void IR_Decoder::writeToBuffer(bool bit) {
|
|||||||
isMsgAvaliable = (crcCheck(dataSize));
|
isMsgAvaliable = (crcCheck(dataSize));
|
||||||
if (isMsgAvaliable && checkAddr(3, 4)) {
|
if (isMsgAvaliable && checkAddr(3, 4)) {
|
||||||
gotRequest._isAvaliable = true;
|
gotRequest._isAvaliable = true;
|
||||||
gotRequest._set(dataBuffer, msgBytes + addrBytes + addrBytes + crcBytes, crcValue, errorCounter);
|
gotRequest._set(dataBuffer, msgBytes + addrBytes + addrBytes + crcBytes, crcValue, errorCounter, riseSyncTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -64,14 +64,17 @@ void IR_Decoder::writeToBuffer(bool bit) {
|
|||||||
case IR_MSG_DATA_ACCEPT:
|
case IR_MSG_DATA_ACCEPT:
|
||||||
case IR_MSG_DATA_NOACCEPT:
|
case IR_MSG_DATA_NOACCEPT:
|
||||||
if (bufBitPos >= ((bitPerByte + syncBits) * ((rawBuffer[0] & IR_MASK_MSG_INFO) + crcBytes)) - syncBits) {
|
if (bufBitPos >= ((bitPerByte + syncBits) * ((rawBuffer[0] & IR_MASK_MSG_INFO) + crcBytes)) - syncBits) {
|
||||||
const uint8_t dataSize = (rawBuffer[0] & IR_MASK_MSG_INFO);
|
const uint8_t dataSize = (rawBuffer[0] & IR_MASK_MSG_INFO);
|
||||||
isRawAvaliable = true;
|
isRawAvaliable = true;
|
||||||
isMsgAvaliable = crcCheck(dataSize);
|
isMsgAvaliable = crcCheck(dataSize);
|
||||||
if (isMsgAvaliable && checkAddr(3, 4)
|
if (isMsgAvaliable && checkAddr(3, 4)
|
||||||
) {
|
) {
|
||||||
gotData._isAvaliable = true;
|
gotData._isAvaliable = true;
|
||||||
gotData._set(dataBuffer, (dataSize)+crcBytes, crcValue, errorCounter);
|
gotData._set(dataBuffer, (dataSize)+crcBytes, crcValue, errorCounter, riseSyncTime);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
gotRawData._isAvaliable = true;
|
||||||
|
gotRawData._set(dataBuffer, (dataSize)+crcBytes, crcValue, errorCounter, riseSyncTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -168,6 +171,11 @@ uint16_t IR_Decoder::ceil_div(uint16_t val, uint16_t divider) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////// isr ///////////////////////////////////////////
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
void IR_Decoder::isr() { // в прерывании вызываем isr()
|
void IR_Decoder::isr() { // в прерывании вызываем isr()
|
||||||
if (isPairSending) return;
|
if (isPairSending) return;
|
||||||
|
|
||||||
@ -175,21 +183,21 @@ void IR_Decoder::isr() { // в прерывании вызываем isr()
|
|||||||
isPreamb = true;
|
isPreamb = true;
|
||||||
frontCounter = preambFronts - 1U;
|
frontCounter = preambFronts - 1U;
|
||||||
errorCounter = 0;
|
errorCounter = 0;
|
||||||
riseSyncTime = bitTime;
|
riseSyncTime = bitTime /* 1100 */;
|
||||||
start_RX();
|
start_RX();
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
if (frontCounter > 0) { // в преамбуле
|
if (frontCounter > 0) { // в преамбуле
|
||||||
uint32_t risePeriod = micros() - prevRise;
|
uint32_t risePeriod = micros() - prevRise;
|
||||||
if ((PIND >> 2) & 1 && risePeriod < IR_timeout) { // __/``` ↑ и мы в внутри пакета
|
if ((PIND >> 2) & 1 && risePeriod < IR_timeout) { // __/``` ↑ и мы в внутри пакета
|
||||||
|
|
||||||
if (freeFrec) { riseSyncTime = (riseSyncTime + risePeriod / 2) / 2; } // tuner
|
|
||||||
|
|
||||||
if (risePeriod < riseTimeMin << 1) { // fix рваной единицы
|
if (risePeriod < riseTimeMin << 1) { // fix рваной единицы
|
||||||
frontCounter += 2;
|
frontCounter += 2;
|
||||||
errorCounter++;
|
errorCounter++;
|
||||||
|
} else {
|
||||||
|
if (freeFrec) { riseSyncTime = (riseSyncTime + risePeriod / 2) / 2; } // tuner
|
||||||
}
|
}
|
||||||
} else { riseSyncTime = bitTime; } // сброс тюнера
|
} else { /* riseSyncTime = bitTime; */ } // сброс тюнера
|
||||||
frontCounter--;
|
frontCounter--;
|
||||||
} else {
|
} else {
|
||||||
if (isPreamb) {// первый фронт после
|
if (isPreamb) {// первый фронт после
|
||||||
@ -233,7 +241,7 @@ void IR_Decoder::isr() { // в прерывании вызываем isr()
|
|||||||
lowCount = ceil_div(lowTime, riseTime); // предполагаемое колличество LOW битов
|
lowCount = ceil_div(lowTime, riseTime); // предполагаемое колличество LOW битов
|
||||||
allCount = ceil_div(risePeriod, riseTime); // предполагаемое колличество всего битов
|
allCount = ceil_div(risePeriod, riseTime); // предполагаемое колличество всего битов
|
||||||
|
|
||||||
if (highCount == 0 && highTime > riseTime / 3) { // fix короткой единицы (?)после пропуска нулей(?)
|
if (highCount == 0 && highTime > riseTime / 4) { // fix короткой единицы (?)после пропуска нулей(?)
|
||||||
highCount++;
|
highCount++;
|
||||||
errorCounter++;
|
errorCounter++;
|
||||||
#ifdef IRDEBUG
|
#ifdef IRDEBUG
|
||||||
|
50
IR_Decoder.h
50
IR_Decoder.h
@ -4,12 +4,12 @@
|
|||||||
//#define IRDEBUG
|
//#define IRDEBUG
|
||||||
|
|
||||||
#ifdef IRDEBUG
|
#ifdef IRDEBUG
|
||||||
#define wrHigh 3 // Запись HIGH инициирована // green
|
#define wrHigh A3 // Запись HIGH инициирована // green
|
||||||
#define wrLow 4 // Запись LOW инициирована // blue
|
#define wrLow A3 // Запись LOW инициирована // blue
|
||||||
#define writeOp 6 // Операция записи, 1 пульс для 0 и 2 для 1 // orange
|
#define writeOp 13 // Операция записи, 1 пульс для 0 и 2 для 1 // orange
|
||||||
// Исправленные ошибки // purle
|
// Исправленные ошибки // purle
|
||||||
// 1 пульс: fix
|
// 1 пульс: fix
|
||||||
#define errOut 5
|
#define errOut A3
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -39,7 +39,7 @@ public:
|
|||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
class InputData {
|
class InputData : protected IR_FOX {
|
||||||
friend IR_Decoder;
|
friend IR_Decoder;
|
||||||
protected:
|
protected:
|
||||||
bool _isAvaliable = false;
|
bool _isAvaliable = false;
|
||||||
@ -51,11 +51,13 @@ public:
|
|||||||
uint16_t _crcPackVal;
|
uint16_t _crcPackVal;
|
||||||
uint16_t _crcCalcVal;
|
uint16_t _crcCalcVal;
|
||||||
uint16_t _errCount;
|
uint16_t _errCount;
|
||||||
|
uint16_t _bitPeriod;
|
||||||
|
|
||||||
void _set(uint8_t* ptr, uint8_t len, uint16_t crc, uint16_t err) {
|
void _set(uint8_t* ptr, uint8_t len, uint16_t crc, uint16_t err, uint16_t rTime) {
|
||||||
_crcCalcVal = crc;
|
_crcCalcVal = crc;
|
||||||
_dataRawSize = len;
|
_dataRawSize = len;
|
||||||
_errCount = err;
|
_errCount = err;
|
||||||
|
_bitPeriod = rTime;
|
||||||
if (_data != nullptr) { delete _data; _data = nullptr; }
|
if (_data != nullptr) { delete _data; _data = nullptr; }
|
||||||
_data = new uint8_t[len];
|
_data = new uint8_t[len];
|
||||||
for (uint8_t i = 0; i < len; i++) { _data[i] = ptr[i]; }
|
for (uint8_t i = 0; i < len; i++) { _data[i] = ptr[i]; }
|
||||||
@ -74,6 +76,7 @@ public:
|
|||||||
uint16_t errorCount() { return _errCount; };
|
uint16_t errorCount() { return _errCount; };
|
||||||
uint16_t crcIN() { return _crcPackVal; };
|
uint16_t crcIN() { return _crcPackVal; };
|
||||||
uint16_t crcCALC() { return _crcCalcVal; };
|
uint16_t crcCALC() { return _crcCalcVal; };
|
||||||
|
uint16_t tunerTime() { return _bitPeriod; };
|
||||||
void resetAvaliable() { _isAvaliable = false; };
|
void resetAvaliable() { _isAvaliable = false; };
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -88,12 +91,21 @@ public:
|
|||||||
uint8_t dataRawSize() { return _dataRawSize; };
|
uint8_t dataRawSize() { return _dataRawSize; };
|
||||||
uint8_t* dataRaw() { return _data; };
|
uint8_t* dataRaw() { return _data; };
|
||||||
bool isNeedAccept() { return ((_msgType >> 5) & IR_MASK_MSG_TYPE) == IR_MSG_DATA_ACCEPT; };
|
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);
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
void ini() override {
|
void ini() override {
|
||||||
_addrFrom = (_data[1] << 8) | _data[2];
|
_addrFrom = (_data[1] << 8) | _data[2];
|
||||||
_addrTo = (_data[3] << 8) | _data[4];
|
_addrTo = (_data[3] << 8) | _data[4];
|
||||||
_crcPackVal = (_data[_dataRawSize - 2] << 8) | _data[_dataRawSize - 1];
|
_crcPackVal = (_data[_dataRawSize - 2] << 8) | _data[_dataRawSize - 1];
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class RawData : public Data {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -120,6 +132,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
Data gotData;
|
Data gotData;
|
||||||
|
RawData gotRawData;
|
||||||
Accept gotAccept;
|
Accept gotAccept;
|
||||||
Request gotRequest;
|
Request gotRequest;
|
||||||
|
|
||||||
@ -176,6 +189,31 @@ private:
|
|||||||
//TODO: Сделать функцию медианы
|
//TODO: Сделать функцию медианы
|
||||||
void medi(uint16_t);
|
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
|
#ifdef IRDEBUG
|
||||||
inline void errPulse(uint8_t pin, uint8_t count);
|
inline void errPulse(uint8_t pin, uint8_t count);
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
#include "IR_Encoder.h"
|
#include "IR_Encoder.h"
|
||||||
#include "IR_Decoder.h"
|
#include "IR_Decoder.h"
|
||||||
|
|
||||||
#define halfPeriod ((carrierPeriod / 2) - 5) //13 //8 // 14 при работе с регистрами, 14 -6 = 8 для digitalWrite()
|
|
||||||
const auto _x_ = halfPeriod;
|
|
||||||
|
|
||||||
IR_Encoder::IR_Encoder(uint16_t addr, uint8_t pin, IR_Decoder* decPair) : ir_out(pin), addrSelf(addr), decoder(decPair) {};
|
IR_Encoder::IR_Encoder(uint16_t addr, uint8_t pin, IR_Decoder* decPair) : ir_out(pin), addrSelf(addr), decoder(decPair) {};
|
||||||
IR_Encoder::~IR_Encoder() {};
|
IR_Encoder::~IR_Encoder() {};
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ public:
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void sendData(uint16_t addrTo, T& data, bool needAccept = false);
|
void sendData(uint16_t addrTo, T& data, bool needAccept = false);
|
||||||
void sendACK(uint16_t addrTo, uint8_t addInfo, bool forAll = false);
|
void sendACK(uint16_t addrTo, uint8_t addInfo = 0, bool forAll = false);
|
||||||
void sendRequest(uint16_t addrTo, uint8_t addInfo = 0);
|
void sendRequest(uint16_t addrTo, uint8_t addInfo = 0);
|
||||||
|
|
||||||
|
|
||||||
|
46
IR_config.h
46
IR_config.h
@ -63,6 +63,12 @@ msg type:
|
|||||||
#define bytePerPack 3 // колличество байтов в пакете
|
#define bytePerPack 3 // колличество байтов в пакете
|
||||||
#define freeFrec true
|
#define freeFrec true
|
||||||
|
|
||||||
|
#define carrierTune 8
|
||||||
|
// 8 для gyverCore
|
||||||
|
// 4~5 для arduino nano
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#define bitPerByte 8U // Колличество бит в байте
|
#define bitPerByte 8U // Колличество бит в байте
|
||||||
@ -86,13 +92,17 @@ typedef uint16_t crc_t;
|
|||||||
#define carrierFrec 38000U // частота несущей
|
#define carrierFrec 38000U // частота несущей
|
||||||
#define carrierPeriod (1000000U/carrierFrec) // период несущей в us
|
#define carrierPeriod (1000000U/carrierFrec) // период несущей в us
|
||||||
|
|
||||||
|
|
||||||
|
#define halfPeriod ((carrierPeriod / 2) - carrierTune) //13 //8 // 14 при работе с регистрами, 14 -6 = 8 для digitalWrite()
|
||||||
|
const auto _x_ = halfPeriod;
|
||||||
|
|
||||||
// В процессе работы значения будут отклонятся в соответствии с предыдущим битом
|
// В процессе работы значения будут отклонятся в соответствии с предыдущим битом
|
||||||
#define bitActiveTakts 25U // длительность активной части бита в тактах
|
#define bitActiveTakts 30U // длительность активной части бита в тактах
|
||||||
#define bitPauseTakts 5U // длительность промежутков в тактах
|
#define bitPauseTakts 7U // длительность промежутков в тактах
|
||||||
|
|
||||||
#define bitTakts (bitActiveTakts+bitPauseTakts*2U) // Общая длительность бита в тактах
|
#define bitTakts (bitActiveTakts+bitPauseTakts*2U) // Общая длительность бита в тактах
|
||||||
#define bitTime (bitTakts*carrierPeriod) // Общая длительность бита
|
#define bitTime (bitTakts*carrierPeriod) // Общая длительность бита
|
||||||
//const auto viewValue = bitTime;
|
const auto viewValue = bitTime;
|
||||||
#define tolerance 300U
|
#define tolerance 300U
|
||||||
|
|
||||||
class IR_FOX {
|
class IR_FOX {
|
||||||
@ -113,4 +123,34 @@ public:
|
|||||||
}
|
}
|
||||||
return crc;
|
return crc;
|
||||||
}
|
}
|
||||||
|
String printBytes(uint8_t* d ,uint8_t s , uint8_t mode = 10) {
|
||||||
|
String str = "";
|
||||||
|
uint8_t control = bitPerByte;
|
||||||
|
uint8_t* _data = d;
|
||||||
|
switch (mode) {
|
||||||
|
case 2:
|
||||||
|
for (size_t i = 0; i < s * 8; i++) {
|
||||||
|
if (i == control) {
|
||||||
|
str += " ";
|
||||||
|
control += bitPerByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
str += _data[(i / 8)] >> (7 - (i % 8)) & 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
for (size_t i = 0; i < s; i++) {
|
||||||
|
str += _data[i];
|
||||||
|
str += " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
str += " ";
|
||||||
|
return str;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user