mirror of
https://github.com/Show-maket/IR-protocol.git
synced 2025-06-28 05:09:40 +00:00
Compare commits
25 Commits
341ff3a470
...
STM32
Author | SHA1 | Date | |
---|---|---|---|
bc9563fbb5 | |||
021e1e290d | |||
89d14919c9 | |||
403b8e6850 | |||
d0c3138c52 | |||
2d839d3ff8 | |||
6ba8fdffe4 | |||
aa0b478229 | |||
277985f79a | |||
444b84c313 | |||
2db1ef7805 | |||
1353ab6f75 | |||
d1cb167aaf | |||
30ad816c2a | |||
ecfb3b5f98 | |||
70a22463ef | |||
71f58a4992 | |||
b6b9d2c820 | |||
98a21f5e56 | |||
591727546e | |||
79bb804bb4 | |||
0471b8cc89 | |||
90c41cfe2b | |||
1ecc33e9c4 | |||
7ef8158a00 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
.vscode/*
|
.vscode/*
|
||||||
bin/*
|
bin/*
|
||||||
!.vscode/launch.json
|
!.vscode/launch.json
|
||||||
log/*
|
log/*
|
||||||
|
/.vscode
|
||||||
|
4
.vscode/arduino.json
vendored
4
.vscode/arduino.json
vendored
@ -1,7 +1,5 @@
|
|||||||
{
|
{
|
||||||
"board": "STMicroelectronics:stm32:GenF4",
|
"board": "STMicroelectronics:stm32:GenF4",
|
||||||
"port": "COM17",
|
"port": "COM17",
|
||||||
"prebuild": "if exist bin rd /s /q bin",
|
"prebuild": "if exist bin rd /s /q bin"
|
||||||
"sketch": "IR-protocol.ino",
|
|
||||||
"configuration": "clock=25MHz,pnum=MAKET_F401RETX,upload_method=swdMethod,xserial=generic,usb=CDCgen,xusb=FS,opt=osstd,dbg=none,rtlib=nano"
|
|
||||||
}
|
}
|
180
IR-protocol.ino
180
IR-protocol.ino
@ -23,18 +23,6 @@
|
|||||||
|
|
||||||
#define LoopOut PC13
|
#define LoopOut PC13
|
||||||
|
|
||||||
#define dec_ISR(n) \
|
|
||||||
void dec_##n##_ISR() { dec##n.isr(); }
|
|
||||||
#define dec_Ini(n) \
|
|
||||||
IR_Decoder dec##n(dec##n##_PIN, n); \
|
|
||||||
dec_ISR(n)
|
|
||||||
|
|
||||||
#define decPinMode(n) pinMode(dec##n##_PIN, INPUT_PULLUP);
|
|
||||||
#define decAttach(n) attachInterrupt(dec##n##_PIN, dec_##n##_ISR, CHANGE);
|
|
||||||
#define decSetup(n) decPinMode(n); decAttach(n);
|
|
||||||
#define decTick(n) dec##n.tick();
|
|
||||||
#define decStat(n) rx_flag |= status/* Simple */(dec##n);
|
|
||||||
|
|
||||||
//////////////// Ini /////////////////
|
//////////////// Ini /////////////////
|
||||||
|
|
||||||
#define INFO "IR_FOX TEST"
|
#define INFO "IR_FOX TEST"
|
||||||
@ -63,20 +51,47 @@ void EncoderISR()
|
|||||||
|
|
||||||
//-------------------------------------------------------------------
|
//-------------------------------------------------------------------
|
||||||
|
|
||||||
dec_Ini(0);
|
IR_Decoder dec0(dec0_PIN, 0);
|
||||||
dec_Ini(1);
|
void dec_0_ISR() { dec0.isr(); }
|
||||||
dec_Ini(2);
|
|
||||||
dec_Ini(3);
|
IR_Decoder dec1(dec1_PIN, 1);
|
||||||
dec_Ini(4);
|
void dec_1_ISR() { dec1.isr(); }
|
||||||
dec_Ini(5);
|
|
||||||
dec_Ini(6);
|
IR_Decoder dec2(dec2_PIN, 2);
|
||||||
dec_Ini(7);
|
void dec_2_ISR() { dec2.isr(); }
|
||||||
// dec_Ini(8);
|
|
||||||
// dec_Ini(9);
|
IR_Decoder dec3(dec3_PIN, 3);
|
||||||
// dec_Ini(10);
|
void dec_3_ISR() { dec3.isr(); }
|
||||||
// dec_Ini(11);
|
|
||||||
// dec_Ini(12);
|
IR_Decoder dec4(dec4_PIN, 4);
|
||||||
// dec_Ini(13);
|
void dec_4_ISR() { dec4.isr(); }
|
||||||
|
|
||||||
|
IR_Decoder dec5(dec5_PIN, 5);
|
||||||
|
void dec_5_ISR() { dec5.isr(); }
|
||||||
|
|
||||||
|
IR_Decoder dec6(dec6_PIN, 6);
|
||||||
|
void dec_6_ISR() { dec6.isr(); }
|
||||||
|
|
||||||
|
IR_Decoder dec7(dec7_PIN, 7);
|
||||||
|
void dec_7_ISR() { dec7.isr(); }
|
||||||
|
|
||||||
|
// IR_Decoder dec8(dec8_PIN, 8);
|
||||||
|
// void dec_8_ISR() { dec8.isr(); }
|
||||||
|
|
||||||
|
// IR_Decoder dec9(dec9_PIN, 9);
|
||||||
|
// void dec_9_ISR() { dec9.isr(); }
|
||||||
|
|
||||||
|
// IR_Decoder dec10(dec10_PIN, 10);
|
||||||
|
// void dec_10_ISR() { dec10.isr(); }
|
||||||
|
|
||||||
|
// IR_Decoder dec11(dec11_PIN, 11);
|
||||||
|
// void dec_11_ISR() { dec11.isr(); }
|
||||||
|
|
||||||
|
// IR_Decoder dec12(dec12_PIN, 12);
|
||||||
|
// void dec_12_ISR() { dec12.isr(); }
|
||||||
|
|
||||||
|
// IR_Decoder dec13(dec13_PIN, 13);
|
||||||
|
// void dec_13_ISR() { dec13.isr(); }
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////
|
||||||
uint8_t data0[] = {};
|
uint8_t data0[] = {};
|
||||||
@ -88,6 +103,7 @@ uint8_t data4[] = {42, 127, 137, 255};
|
|||||||
uint32_t loopTimer;
|
uint32_t loopTimer;
|
||||||
uint8_t sig = 0;
|
uint8_t sig = 0;
|
||||||
uint16_t targetAddr = IR_Broadcast;
|
uint16_t targetAddr = IR_Broadcast;
|
||||||
|
|
||||||
Timer t1(500, millis, []()
|
Timer t1(500, millis, []()
|
||||||
{
|
{
|
||||||
// Serial.println( digitalPinToBitMask(enc0.getPin()), BIN);
|
// Serial.println( digitalPinToBitMask(enc0.getPin()), BIN);
|
||||||
@ -181,6 +197,7 @@ Timer t1(500, millis, []()
|
|||||||
// encBackward.sendData(IR_Broadcast, data2);
|
// encBackward.sendData(IR_Broadcast, data2);
|
||||||
// encTree.sendData(IR_Broadcast, rawData3);
|
// encTree.sendData(IR_Broadcast, rawData3);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Timer t2(50, millis, []()
|
// Timer t2(50, millis, []()
|
||||||
// { digitalToggle(LED_BUILTIN); });
|
// { digitalToggle(LED_BUILTIN); });
|
||||||
|
|
||||||
@ -190,8 +207,6 @@ HardwareTimer IR_Timer(TIM3);
|
|||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
// MicrosTimer.setOve
|
|
||||||
|
|
||||||
IR_Timer.setOverflow(carrierFrec * 2, HERTZ_FORMAT);
|
IR_Timer.setOverflow(carrierFrec * 2, HERTZ_FORMAT);
|
||||||
IR_Timer.attachInterrupt(1, EncoderISR);
|
IR_Timer.attachInterrupt(1, EncoderISR);
|
||||||
NVIC_SetPriority(IRQn_Type::TIM3_IRQn, 0);
|
NVIC_SetPriority(IRQn_Type::TIM3_IRQn, 0);
|
||||||
@ -201,79 +216,59 @@ void setup()
|
|||||||
Serial.println(F(INFO));
|
Serial.println(F(INFO));
|
||||||
|
|
||||||
pinMode(LoopOut, OUTPUT);
|
pinMode(LoopOut, OUTPUT);
|
||||||
// pinMode(SignalDetectLed, OUTPUT);
|
|
||||||
|
|
||||||
pinMode(dec0_PIN, INPUT_PULLUP);
|
pinMode(dec0_PIN, INPUT_PULLUP);
|
||||||
// pinMode(dec2_PIN, INPUT_PULLUP);
|
pinMode(dec1_PIN, INPUT_PULLUP);
|
||||||
|
pinMode(dec2_PIN, INPUT_PULLUP);
|
||||||
|
pinMode(dec3_PIN, INPUT_PULLUP);
|
||||||
|
pinMode(dec4_PIN, INPUT_PULLUP);
|
||||||
|
pinMode(dec5_PIN, INPUT_PULLUP);
|
||||||
|
pinMode(dec6_PIN, INPUT_PULLUP);
|
||||||
|
pinMode(dec7_PIN, INPUT_PULLUP);
|
||||||
|
// pinMode(dec8_PIN, INPUT_PULLUP);
|
||||||
|
// pinMode(dec9_PIN, INPUT_PULLUP);
|
||||||
|
// pinMode(dec10_PIN, INPUT_PULLUP);
|
||||||
|
// pinMode(dec11_PIN, INPUT_PULLUP);
|
||||||
|
// pinMode(dec12_PIN, INPUT_PULLUP);
|
||||||
|
// pinMode(dec13_PIN, INPUT_PULLUP);
|
||||||
|
|
||||||
static IR_DecoderRaw *blind[]{
|
attachInterrupt(dec0_PIN, dec_0_ISR, CHANGE);
|
||||||
&dec0,
|
attachInterrupt(dec1_PIN, dec_1_ISR, CHANGE);
|
||||||
&dec1,
|
attachInterrupt(dec2_PIN, dec_2_ISR, CHANGE);
|
||||||
&dec2,
|
attachInterrupt(dec3_PIN, dec_3_ISR, CHANGE);
|
||||||
&dec3,
|
attachInterrupt(dec4_PIN, dec_4_ISR, CHANGE);
|
||||||
&dec4,
|
attachInterrupt(dec5_PIN, dec_5_ISR, CHANGE);
|
||||||
&dec5,
|
attachInterrupt(dec6_PIN, dec_6_ISR, CHANGE);
|
||||||
&dec6,
|
attachInterrupt(dec7_PIN, dec_7_ISR, CHANGE);
|
||||||
&dec7,
|
// attachInterrupt(dec8_PIN, dec_8_ISR, CHANGE);
|
||||||
// &dec8,
|
// attachInterrupt(dec9_PIN, dec_9_ISR, CHANGE);
|
||||||
// &dec9,
|
// attachInterrupt(dec10_PIN, dec_10_ISR, CHANGE);
|
||||||
// &dec10,
|
// attachInterrupt(dec11_PIN, dec_11_ISR, CHANGE);
|
||||||
// &dec11,
|
// attachInterrupt(dec12_PIN, dec_12_ISR, CHANGE);
|
||||||
// &dec12,
|
// attachInterrupt(dec13_PIN, dec_13_ISR, CHANGE);
|
||||||
// &dec13,
|
|
||||||
};
|
|
||||||
|
|
||||||
enc0.setBlindDecoders(blind, sizeof(blind) / sizeof(IR_DecoderRaw *));
|
|
||||||
// enc1.setBlindDecoders(blind, sizeof(blind) / sizeof(IR_DecoderRaw *));
|
|
||||||
// enc2.setBlindDecoders(blind, sizeof(blind) / sizeof(IR_DecoderRaw *));
|
|
||||||
// enc3.setBlindDecoders(blind, sizeof(blind) / sizeof(IR_DecoderRaw *));
|
|
||||||
// enc10.setBlindDecoders(blind, sizeof(blind) / sizeof(IR_DecoderRaw *));
|
|
||||||
|
|
||||||
decSetup(0);
|
|
||||||
decSetup(1);
|
|
||||||
decSetup(2);
|
|
||||||
decSetup(3);
|
|
||||||
decSetup(4);
|
|
||||||
decSetup(5);
|
|
||||||
decSetup(6);
|
|
||||||
decSetup(7);
|
|
||||||
// decSetup(8);
|
|
||||||
// decSetup(9);
|
|
||||||
// decSetup(10);
|
|
||||||
// decSetup(11);
|
|
||||||
// decSetup(12);
|
|
||||||
// decSetup(13);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
digitalToggle(LoopOut);
|
digitalToggle(LoopOut);
|
||||||
Timer::tick();
|
Timer::tick();
|
||||||
|
|
||||||
IR_Decoder::tick();
|
IR_Decoder::tick();
|
||||||
|
|
||||||
bool rx_flag;
|
bool rx_flag = false;
|
||||||
decStat(0);
|
rx_flag |= status(dec0);
|
||||||
decStat(1);
|
rx_flag |= status(dec1);
|
||||||
decStat(2);
|
rx_flag |= status(dec2);
|
||||||
decStat(3);
|
rx_flag |= status(dec3);
|
||||||
decStat(4);
|
rx_flag |= status(dec4);
|
||||||
decStat(5);
|
rx_flag |= status(dec5);
|
||||||
decStat(6);
|
rx_flag |= status(dec6);
|
||||||
decStat(7);
|
rx_flag |= status(dec7);
|
||||||
// decStat(8);
|
// rx_flag |= status(dec8);
|
||||||
// decStat(9);
|
// rx_flag |= status(dec9);
|
||||||
// decStat(10);
|
// rx_flag |= status(dec10);
|
||||||
// decStat(11);
|
// rx_flag |= status(dec11);
|
||||||
// decStat(12);
|
// rx_flag |= status(dec12);
|
||||||
// decStat(13);
|
// rx_flag |= status(dec13);
|
||||||
|
|
||||||
// status(decForward);
|
|
||||||
// status(decBackward);
|
|
||||||
|
|
||||||
// Serial.println(micros() - loopTimer);
|
|
||||||
// loopTimer = micros();
|
|
||||||
// delayMicroseconds(120*5);
|
|
||||||
|
|
||||||
if (Serial.available())
|
if (Serial.available())
|
||||||
{
|
{
|
||||||
@ -289,13 +284,14 @@ void loop()
|
|||||||
case 102:
|
case 102:
|
||||||
targetAddr = 777;
|
targetAddr = 777;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
sig = in;
|
sig = in;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Timer statusSimpleDelay;
|
Timer statusSimpleDelay;
|
||||||
bool statusSimple(IR_Decoder &dec)
|
bool statusSimple(IR_Decoder &dec)
|
||||||
{
|
{
|
||||||
@ -326,7 +322,7 @@ bool status(IR_Decoder &dec)
|
|||||||
if (dec.gotData.available())
|
if (dec.gotData.available())
|
||||||
{
|
{
|
||||||
detectSignal();
|
detectSignal();
|
||||||
Serial.println(micros());
|
// Serial.println(micros());
|
||||||
String str;
|
String str;
|
||||||
if (/* dec.gotData.getDataPrt()[1] */ 1)
|
if (/* dec.gotData.getDataPrt()[1] */ 1)
|
||||||
{
|
{
|
||||||
|
@ -23,12 +23,14 @@ void IR_Decoder::enable()
|
|||||||
{
|
{
|
||||||
dec_list.push_back(this);
|
dec_list.push_back(this);
|
||||||
}
|
}
|
||||||
|
pinMode(pin, INPUT_PULLUP);
|
||||||
attachInterrupt(pin, (*this)(), CHANGE);
|
attachInterrupt(pin, (*this)(), CHANGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IR_Decoder::disable()
|
void IR_Decoder::disable()
|
||||||
{
|
{
|
||||||
detachInterrupt(pin);
|
detachInterrupt(pin);
|
||||||
|
pinMode(pin, INPUT);
|
||||||
auto &dec_list = get_dec_list();
|
auto &dec_list = get_dec_list();
|
||||||
auto it = std::find(dec_list.begin(), dec_list.end(), this);
|
auto it = std::find(dec_list.begin(), dec_list.end(), this);
|
||||||
if (it != dec_list.end())
|
if (it != dec_list.end())
|
||||||
@ -59,6 +61,7 @@ void IR_Decoder::tick()
|
|||||||
void IR_Decoder::_tick()
|
void IR_Decoder::_tick()
|
||||||
{
|
{
|
||||||
IR_DecoderRaw::tick();
|
IR_DecoderRaw::tick();
|
||||||
|
|
||||||
if (availableRaw())
|
if (availableRaw())
|
||||||
{
|
{
|
||||||
#ifdef IRDEBUG_INFO
|
#ifdef IRDEBUG_INFO
|
||||||
@ -95,9 +98,13 @@ void IR_Decoder::_tick()
|
|||||||
}
|
}
|
||||||
gotRaw.set(&packInfo, id);
|
gotRaw.set(&packInfo, id);
|
||||||
}
|
}
|
||||||
if (isWaitingAcceptSend && millis() - acceptSendTimer > 75)
|
if (isWaitingAcceptSend && millis() - acceptSendTimer > acceptDelay)
|
||||||
{
|
{
|
||||||
encoder->sendAccept(addrAcceptSendTo, acceptCustomByte);
|
encoder->sendAccept(addrAcceptSendTo, acceptCustomByte);
|
||||||
isWaitingAcceptSend = false;
|
isWaitingAcceptSend = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IR_Decoder::isReceive(uint8_t type) {
|
||||||
|
return (msgTypeReceive & 0b11111000) && ((msgTypeReceive & IR_MASK_MSG_TYPE) == type);
|
||||||
|
}
|
||||||
|
@ -14,7 +14,7 @@ private:
|
|||||||
bool isWaitingAcceptSend;
|
bool isWaitingAcceptSend;
|
||||||
uint16_t addrAcceptSendTo;
|
uint16_t addrAcceptSendTo;
|
||||||
|
|
||||||
uint16_t acceptDelay = 75;
|
uint16_t acceptDelay = IR_ResponseDelay;
|
||||||
uint8_t acceptCustomByte;
|
uint8_t acceptCustomByte;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -31,6 +31,8 @@ public:
|
|||||||
|
|
||||||
void enable();
|
void enable();
|
||||||
void disable();
|
void disable();
|
||||||
|
|
||||||
|
bool isReceive(uint8_t type);
|
||||||
|
|
||||||
~IR_Decoder();
|
~IR_Decoder();
|
||||||
|
|
||||||
|
@ -112,16 +112,37 @@ void IR_DecoderRaw::firstRX()
|
|||||||
|
|
||||||
void IR_DecoderRaw::listenStart()
|
void IR_DecoderRaw::listenStart()
|
||||||
{
|
{
|
||||||
if (isRecive && ((micros() - prevRise) > IR_timeout * 2))
|
if (isReciveRaw && ((micros() - prevRise) > IR_timeout * 2))
|
||||||
{
|
{
|
||||||
// Serial.print("\nlis>");
|
// Serial.print("\nlis>");
|
||||||
isRecive = false;
|
isReciveRaw = false;
|
||||||
firstRX();
|
firstRX();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ---- быстрая проверка конца пакета ---------------------------------
|
||||||
|
inline void IR_DecoderRaw::checkTimeout()
|
||||||
|
{
|
||||||
|
if (!isRecive) return; // уже не принимаем – нечего проверять
|
||||||
|
|
||||||
|
if (micros() - lastEdgeTime > IR_timeout * 2U)
|
||||||
|
{
|
||||||
|
isRecive = false; // приём завершён
|
||||||
|
msgTypeReceive = 0;
|
||||||
|
// firstRX(); // подготовка к новому пакету
|
||||||
|
lastEdgeTime = micros(); // защита от повторного срабатывания
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====================================================================
|
||||||
|
|
||||||
void IR_DecoderRaw::tick()
|
void IR_DecoderRaw::tick()
|
||||||
{
|
{
|
||||||
|
// FrontStorage *currentFrontPtr;
|
||||||
|
// noInterrupts();
|
||||||
|
// currentFrontPtr = subBuffer.pop();
|
||||||
|
// interrupts();
|
||||||
|
|
||||||
FrontStorage currentFront;
|
FrontStorage currentFront;
|
||||||
noInterrupts();
|
noInterrupts();
|
||||||
listenStart();
|
listenStart();
|
||||||
@ -130,12 +151,24 @@ void IR_DecoderRaw::tick()
|
|||||||
if (currentFrontPtr == nullptr)
|
if (currentFrontPtr == nullptr)
|
||||||
{
|
{
|
||||||
isSubBufferOverflow = false;
|
isSubBufferOverflow = false;
|
||||||
|
checkTimeout(); // <--- новое место проверки
|
||||||
interrupts();
|
interrupts();
|
||||||
return;
|
return;
|
||||||
} // Если данных нет - ничего не делаем
|
} // Если данных нет - ничего не делаем
|
||||||
currentFront = *currentFrontPtr;
|
currentFront = *currentFrontPtr;
|
||||||
interrupts();
|
interrupts();
|
||||||
|
|
||||||
|
// ---------- буфер пуст: фронтов нет, проверяем тайм-аут ----------
|
||||||
|
// if (currentFrontPtr == nullptr)
|
||||||
|
// {
|
||||||
|
// isSubBufferOverflow = false;
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // ---------- есть фронт: продолжаем обработку ----------
|
||||||
|
// FrontStorage currentFront = *currentFrontPtr;
|
||||||
|
lastEdgeTime = currentFront.time; // запоминаем любой фронт
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
if (currentFront.dir)
|
if (currentFront.dir)
|
||||||
@ -197,7 +230,7 @@ void IR_DecoderRaw::tick()
|
|||||||
digitalWrite(errOut, currentFront.dir);
|
digitalWrite(errOut, currentFront.dir);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (currentFront.time > prevRise && currentFront.time - prevRise > IR_timeout * 2 && !isRecive)
|
if (currentFront.time > prevRise && currentFront.time - prevRise > IR_timeout * 2 && !isReciveRaw)
|
||||||
{ // первый
|
{ // первый
|
||||||
#ifdef IRDEBUG
|
#ifdef IRDEBUG
|
||||||
errPulse(up, 50);
|
errPulse(up, 50);
|
||||||
@ -209,6 +242,7 @@ void IR_DecoderRaw::tick()
|
|||||||
isPreamb = true;
|
isPreamb = true;
|
||||||
|
|
||||||
isRecive = true;
|
isRecive = true;
|
||||||
|
isReciveRaw = true;
|
||||||
isWrongPack = false;
|
isWrongPack = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -441,6 +475,8 @@ void IR_DecoderRaw::writeToBuffer(bool bit)
|
|||||||
if (isBufferOverflow || isPreamb || isWrongPack)
|
if (isBufferOverflow || isPreamb || isWrongPack)
|
||||||
{
|
{
|
||||||
isRecive = false;
|
isRecive = false;
|
||||||
|
isReciveRaw = false;
|
||||||
|
msgTypeReceive = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -564,6 +600,8 @@ void IR_DecoderRaw::writeToBuffer(bool bit)
|
|||||||
packInfo.rTime = riseSyncTime;
|
packInfo.rTime = riseSyncTime;
|
||||||
|
|
||||||
isRecive = false;
|
isRecive = false;
|
||||||
|
isReciveRaw = false;
|
||||||
|
msgTypeReceive = 0;
|
||||||
isAvailable = crcCheck(packSize - crcBytes, crcValue);
|
isAvailable = crcCheck(packSize - crcBytes, crcValue);
|
||||||
|
|
||||||
#ifdef BRUTEFORCE_CHECK
|
#ifdef BRUTEFORCE_CHECK
|
||||||
@ -594,6 +632,12 @@ void IR_DecoderRaw::writeToBuffer(bool bit)
|
|||||||
OUT_BRUTEFORCE:;
|
OUT_BRUTEFORCE:;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (packSize && (i_dataBuffer == 8)) {
|
||||||
|
msgTypeReceive = (dataBuffer[0]>>5) | 0b11111000;
|
||||||
|
// SerialUSB.println(msgTypeReceive & IR_MASK_MSG_TYPE);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
#define riseTimeMin (riseTime - riseTolerance)
|
#define riseTimeMin (riseTime - riseTolerance)
|
||||||
#define aroundRise(t) (riseTimeMin < t && t < riseTimeMax)
|
#define aroundRise(t) (riseTimeMin < t && t < riseTimeMax)
|
||||||
#define IR_timeout (riseTimeMax * (8 + syncBits + 1)) // us // таймаут в 8 data + 3 sync + 1
|
#define IR_timeout (riseTimeMax * (8 + syncBits + 1)) // us // таймаут в 8 data + 3 sync + 1
|
||||||
|
constexpr uint16_t IR_ResponseDelay = ((uint16_t)(((bitTime+riseTolerance) * (8 + syncBits + 1))*2.7735))/1000;
|
||||||
|
|
||||||
class IR_Encoder;
|
class IR_Encoder;
|
||||||
class IR_DecoderRaw : virtual public IR_FOX
|
class IR_DecoderRaw : virtual public IR_FOX
|
||||||
@ -30,9 +31,10 @@ class IR_DecoderRaw : virtual public IR_FOX
|
|||||||
friend IR_Encoder;
|
friend IR_Encoder;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
PackInfo packInfo;
|
PackInfo packInfo;
|
||||||
IR_Encoder *encoder; // Указатель на парный передатчик
|
uint8_t msgTypeReceive = 0;
|
||||||
bool availableRaw();
|
IR_Encoder *encoder; // Указатель на парный передатчик
|
||||||
|
bool availableRaw();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
@ -47,7 +49,7 @@ public:
|
|||||||
|
|
||||||
inline bool isOverflow() { return isBufferOverflow; }; // Буффер переполнился
|
inline bool isOverflow() { return isBufferOverflow; }; // Буффер переполнился
|
||||||
bool isSubOverflow();
|
bool isSubOverflow();
|
||||||
inline bool isReciving() { return isBufferOverflow; }; // Возвращает true, если происходит приём пакета
|
volatile inline bool isReciving() { return isRecive; }; // Возвращает true, если происходит приём пакета
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
private:
|
private:
|
||||||
@ -64,6 +66,8 @@ private:
|
|||||||
|
|
||||||
uint16_t riseSyncTime = bitTime; // Подстраиваемое время бита в мкс
|
uint16_t riseSyncTime = bitTime; // Подстраиваемое время бита в мкс
|
||||||
|
|
||||||
|
volatile uint32_t lastEdgeTime = 0; // время последнего фронта
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
volatile uint32_t currentSubBufferIndex; // Счетчик текущей позиции во вспомогательном буфере фронтов/спадов
|
volatile uint32_t currentSubBufferIndex; // Счетчик текущей позиции во вспомогательном буфере фронтов/спадов
|
||||||
|
|
||||||
@ -99,7 +103,9 @@ private:
|
|||||||
int16_t bufBitPos = 0; // Позиция для записи бита в буффер
|
int16_t bufBitPos = 0; // Позиция для записи бита в буффер
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void listenStart(); // @brief Слушатель для работы isReciving()
|
bool isReciveRaw;
|
||||||
|
void listenStart();
|
||||||
|
void checkTimeout(); //
|
||||||
|
|
||||||
/// @brief Проверка CRC. Проверяет len байт со значением crc, пришедшим в пакете
|
/// @brief Проверка CRC. Проверяет len байт со значением crc, пришедшим в пакете
|
||||||
/// @param len Длина в байтах проверяемых данных
|
/// @param len Длина в байтах проверяемых данных
|
||||||
|
116
IR_Encoder.cpp
116
IR_Encoder.cpp
@ -5,11 +5,8 @@
|
|||||||
#define ISR_Out 10
|
#define ISR_Out 10
|
||||||
#define TestOut 13
|
#define TestOut 13
|
||||||
|
|
||||||
std::list<IR_Encoder *> &IR_Encoder::get_enc_list() // определение функции
|
IR_Encoder *IR_Encoder::head = nullptr;
|
||||||
{
|
IR_Encoder *IR_Encoder::last = nullptr;
|
||||||
static std::list<IR_Encoder *> dec_list; // статическая локальная переменная
|
|
||||||
return dec_list; // возвращается ссылка на переменную
|
|
||||||
}
|
|
||||||
|
|
||||||
IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair, bool autoHandle)
|
IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair, bool autoHandle)
|
||||||
{
|
{
|
||||||
@ -29,31 +26,92 @@ IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair, bool
|
|||||||
{
|
{
|
||||||
decPair->encoder = this;
|
decPair->encoder = this;
|
||||||
}
|
}
|
||||||
pinMode(pin, OUTPUT);
|
|
||||||
|
|
||||||
if (autoHandle)
|
if (autoHandle)
|
||||||
{
|
{
|
||||||
get_enc_list().push_back(this);
|
if (IR_Encoder::head == nullptr)
|
||||||
|
{
|
||||||
|
IR_Encoder::head = this;
|
||||||
|
}
|
||||||
|
if (last != nullptr)
|
||||||
|
{
|
||||||
|
last->next = this;
|
||||||
|
}
|
||||||
|
last = this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
HardwareTimer* IR_Encoder::IR_Timer = nullptr;
|
||||||
|
|
||||||
|
inline HardwareTimer* IR_Encoder::get_IR_Timer(){return IR_Encoder::IR_Timer;}
|
||||||
|
|
||||||
|
void IR_Encoder::begin(HardwareTimer* timer, uint8_t channel, IRQn_Type IRQn, uint8_t priority, void(*isrCallback)()){
|
||||||
|
IR_Timer = timer;
|
||||||
|
if(IR_Timer == nullptr) return;
|
||||||
|
IR_Timer->setOverflow(carrierFrec * 2, HERTZ_FORMAT);
|
||||||
|
IR_Timer->attachInterrupt(channel, (isrCallback == nullptr ? IR_Encoder::isr : isrCallback));
|
||||||
|
NVIC_SetPriority(IRQn, priority);
|
||||||
|
IR_Timer->resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void IR_Encoder::enable()
|
void IR_Encoder::enable()
|
||||||
{
|
{
|
||||||
auto &enc_list = get_enc_list();
|
bool exist = false;
|
||||||
if (std::find(enc_list.begin(), enc_list.end(), this) == enc_list.end())
|
IR_Encoder *current = IR_Encoder::head;
|
||||||
|
while (current != nullptr)
|
||||||
{
|
{
|
||||||
enc_list.push_back(this);
|
exist = (current == this);
|
||||||
|
if (exist) break;
|
||||||
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
if (!exist)
|
||||||
|
{
|
||||||
|
if (IR_Encoder::head == nullptr)
|
||||||
|
{
|
||||||
|
IR_Encoder::head = this;
|
||||||
|
last = this;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
last->next = this;
|
||||||
|
last = this;
|
||||||
|
}
|
||||||
|
this->next = nullptr; // Указываем, что следующий за этим элементом — nullptr
|
||||||
|
}
|
||||||
|
pinMode(pin, OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IR_Encoder::disable()
|
void IR_Encoder::disable()
|
||||||
{
|
{
|
||||||
auto &enc_list = get_enc_list();
|
IR_Encoder *current = IR_Encoder::head;
|
||||||
auto it = std::find(enc_list.begin(), enc_list.end(), this);
|
IR_Encoder *prev = nullptr;
|
||||||
if (it != enc_list.end())
|
|
||||||
|
while (current != nullptr)
|
||||||
{
|
{
|
||||||
enc_list.erase(it);
|
if (current == this) break;
|
||||||
|
prev = current;
|
||||||
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (current != nullptr) // Элемент найден в списке
|
||||||
|
{
|
||||||
|
if (prev != nullptr)
|
||||||
|
{
|
||||||
|
prev->next = current->next; // Убираем текущий элемент из списка
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
IR_Encoder::head = current->next; // Удаляемый элемент был первым
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current == last)
|
||||||
|
{
|
||||||
|
last = prev; // Если удаляется последний элемент, обновляем last
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pinMode(pin, INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IR_Encoder::setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count)
|
void IR_Encoder::setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count)
|
||||||
@ -66,17 +124,18 @@ void IR_Encoder::setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count)
|
|||||||
blindDecoders = decoders;
|
blindDecoders = decoders;
|
||||||
}
|
}
|
||||||
|
|
||||||
IR_Encoder::~IR_Encoder()
|
IR_Encoder::~IR_Encoder(){};
|
||||||
{
|
|
||||||
get_enc_list().remove(this);
|
|
||||||
};
|
|
||||||
|
|
||||||
void IR_Encoder::sendData(uint16_t addrTo, uint8_t dataByte, bool needAccept)
|
void IR_Encoder::sendData(uint16_t addrTo, uint8_t dataByte, bool needAccept)
|
||||||
{
|
{
|
||||||
sendData(addrTo, &dataByte, 1, needAccept);
|
sendData(addrTo, &dataByte, 1, needAccept);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IR_Encoder::sendData(uint16_t addrTo, uint8_t *data, uint8_t len, bool needAccept)
|
void IR_Encoder::sendData(uint16_t addrTo, uint8_t *data, uint8_t len, bool needAccept){
|
||||||
|
sendDataFULL(id, addrTo, data, len, needAccept);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IR_Encoder::sendDataFULL(uint16_t addrFrom, uint16_t addrTo, uint8_t *data, uint8_t len, bool needAccept)
|
||||||
{
|
{
|
||||||
if (len > bytePerPack)
|
if (len > bytePerPack)
|
||||||
{
|
{
|
||||||
@ -93,8 +152,8 @@ void IR_Encoder::sendData(uint16_t addrTo, uint8_t *data, uint8_t len, bool need
|
|||||||
sendBuffer[0] = msgType;
|
sendBuffer[0] = msgType;
|
||||||
|
|
||||||
// addr_self
|
// addr_self
|
||||||
sendBuffer[1] = id >> 8 & 0xFF;
|
sendBuffer[1] = addrFrom >> 8 & 0xFF;
|
||||||
sendBuffer[2] = id & 0xFF;
|
sendBuffer[2] = addrFrom & 0xFF;
|
||||||
|
|
||||||
// addr_to
|
// addr_to
|
||||||
sendBuffer[3] = addrTo >> 8 & 0xFF;
|
sendBuffer[3] = addrTo >> 8 & 0xFF;
|
||||||
@ -225,6 +284,10 @@ void IR_Encoder::setDecoder_isSending()
|
|||||||
for (uint8_t i = 0; i < decodersCount; i++)
|
for (uint8_t i = 0; i < decodersCount; i++)
|
||||||
{
|
{
|
||||||
blindDecoders[i]->isPairSending ^= id;
|
blindDecoders[i]->isPairSending ^= id;
|
||||||
|
// Serial.print("setDecoder_isSending() id = ");
|
||||||
|
// Serial.print(id);
|
||||||
|
// Serial.print(" isPairSending = ");
|
||||||
|
// Serial.println(blindDecoders[i]->isPairSending);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -236,7 +299,7 @@ void IR_Encoder::rawSend(uint8_t *ptr, uint8_t len)
|
|||||||
// TODO: Обработка повторной отправки
|
// TODO: Обработка повторной отправки
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Serial.println("START");
|
||||||
setDecoder_isSending();
|
setDecoder_isSending();
|
||||||
|
|
||||||
// noInterrupts();
|
// noInterrupts();
|
||||||
@ -261,10 +324,11 @@ void IR_Encoder::rawSend(uint8_t *ptr, uint8_t len)
|
|||||||
|
|
||||||
void IR_Encoder::isr()
|
void IR_Encoder::isr()
|
||||||
{
|
{
|
||||||
// Serial.println(get_enc_list().size());
|
IR_Encoder *current = IR_Encoder::head;
|
||||||
for (const auto &element : get_enc_list())
|
while (current != nullptr)
|
||||||
{
|
{
|
||||||
element->_isr();
|
current->_isr();
|
||||||
|
current = current->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -292,7 +356,9 @@ void IR_Encoder::_isr()
|
|||||||
// сброс счетчиков
|
// сброс счетчиков
|
||||||
// ...
|
// ...
|
||||||
isSending = false;
|
isSending = false;
|
||||||
|
// Serial.println("STOP");
|
||||||
setDecoder_isSending();
|
setDecoder_isSending();
|
||||||
|
// Serial.println();
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
16
IR_Encoder.h
16
IR_Encoder.h
@ -7,10 +7,13 @@ class IR_DecoderRaw;
|
|||||||
class IR_Encoder : public IR_FOX
|
class IR_Encoder : public IR_FOX
|
||||||
{
|
{
|
||||||
friend IR_DecoderRaw;
|
friend IR_DecoderRaw;
|
||||||
|
static IR_Encoder *head;
|
||||||
|
static IR_Encoder *last;
|
||||||
|
IR_Encoder *next;
|
||||||
public:
|
public:
|
||||||
|
static HardwareTimer* IR_Timer;
|
||||||
private:
|
private:
|
||||||
uint16_t id; /// @brief Адрес передатчика
|
// uint16_t id; /// @brief Адрес передатчика
|
||||||
public:
|
public:
|
||||||
/// @brief Класс передатчика
|
/// @brief Класс передатчика
|
||||||
/// @param addr Адрес передатчика
|
/// @param addr Адрес передатчика
|
||||||
@ -18,6 +21,8 @@ public:
|
|||||||
/// @param decPair Приёмник, для которого отключается приём в момент передачи передатчиком
|
/// @param decPair Приёмник, для которого отключается приём в момент передачи передатчиком
|
||||||
IR_Encoder(uint8_t pin, uint16_t addr = 0, IR_DecoderRaw *decPair = nullptr, bool autoHandle = true);
|
IR_Encoder(uint8_t pin, uint16_t addr = 0, IR_DecoderRaw *decPair = nullptr, bool autoHandle = true);
|
||||||
static void isr();
|
static void isr();
|
||||||
|
static void begin(HardwareTimer* timer, uint8_t channel, IRQn_Type IRQn, uint8_t priority, void(*isrCallback)() = nullptr);
|
||||||
|
static HardwareTimer* get_IR_Timer();
|
||||||
|
|
||||||
void enable();
|
void enable();
|
||||||
void disable();
|
void disable();
|
||||||
@ -27,6 +32,8 @@ public:
|
|||||||
|
|
||||||
void sendData(uint16_t addrTo, uint8_t dataByte, bool needAccept = false);
|
void sendData(uint16_t addrTo, uint8_t dataByte, bool needAccept = false);
|
||||||
void sendData(uint16_t addrTo, uint8_t *data = nullptr, uint8_t len = 0, bool needAccept = false);
|
void sendData(uint16_t addrTo, uint8_t *data = nullptr, uint8_t len = 0, bool needAccept = false);
|
||||||
|
void sendDataFULL(uint16_t addrFrom, uint16_t addrTo, uint8_t *data = nullptr, uint8_t len = 0, bool needAccept = false);
|
||||||
|
|
||||||
|
|
||||||
void sendAccept(uint16_t addrTo, uint8_t customByte = 0);
|
void sendAccept(uint16_t addrTo, uint8_t customByte = 0);
|
||||||
void sendRequest(uint16_t addrTo);
|
void sendRequest(uint16_t addrTo);
|
||||||
@ -39,10 +46,9 @@ public:
|
|||||||
~IR_Encoder();
|
~IR_Encoder();
|
||||||
volatile bool ir_out_virtual;
|
volatile bool ir_out_virtual;
|
||||||
|
|
||||||
private:
|
|
||||||
static std::list<IR_Encoder*>& get_enc_list();
|
|
||||||
void _sendBack(bool isAdressed, uint16_t addrTo, uint8_t *data, uint8_t len);
|
|
||||||
void _isr();
|
void _isr();
|
||||||
|
private:
|
||||||
|
void _sendBack(bool isAdressed, uint16_t addrTo, uint8_t *data, uint8_t len);
|
||||||
|
|
||||||
void setDecoder_isSending();
|
void setDecoder_isSending();
|
||||||
void sendByte(uint8_t byte, bool *prev, bool LOW_FIRST);
|
void sendByte(uint8_t byte, bool *prev, bool LOW_FIRST);
|
||||||
|
@ -4,7 +4,6 @@ void IR_FOX::setPin(uint8_t pin){
|
|||||||
this->pin = pin;
|
this->pin = pin;
|
||||||
port = digitalPinToPort(pin);
|
port = digitalPinToPort(pin);
|
||||||
mask = digitalPinToBitMask(pin);
|
mask = digitalPinToBitMask(pin);
|
||||||
pinMode(pin, INPUT_PULLUP);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IR_FOX::checkAddressRuleApply(uint16_t address, uint16_t id, bool &flag)
|
void IR_FOX::checkAddressRuleApply(uint16_t address, uint16_t id, bool &flag)
|
||||||
|
14
IR_config.h
14
IR_config.h
@ -15,7 +15,7 @@
|
|||||||
IR_MSG_ACCEPT с адреса 0 воспринимается всеми устройствами
|
IR_MSG_ACCEPT с адреса 0 воспринимается всеми устройствами
|
||||||
*/
|
*/
|
||||||
//**** Контрольные точки ******
|
//**** Контрольные точки ******
|
||||||
#define IR_MAX_ADDR_CPU 64999
|
#define IR_MAX_ADDR_CPU 63999
|
||||||
#define IR_MIN_ADDR_CPU 32000
|
#define IR_MIN_ADDR_CPU 32000
|
||||||
|
|
||||||
// //***** Группы машинок ********
|
// //***** Группы машинок ********
|
||||||
@ -28,11 +28,11 @@
|
|||||||
|
|
||||||
//********* Машинки ***********
|
//********* Машинки ***********
|
||||||
#define IR_MAX_CAR 31999
|
#define IR_MAX_CAR 31999
|
||||||
#define IR_MIN_CAR 100
|
#define IR_MIN_CAR 1
|
||||||
|
|
||||||
//***** Пульты управления *****
|
//***** Пульты управления *****
|
||||||
#define IR_MAX_CONTROLLER 99
|
#define IR_MAX_CONTROLLER 64999
|
||||||
#define IR_MIN_CONTROLLER 0
|
#define IR_MIN_CONTROLLER 64000
|
||||||
/*
|
/*
|
||||||
|
|
||||||
/```````````````````````````````````````````````` data pack `````````````````````````````````````````````\
|
/```````````````````````````````````````````````` data pack `````````````````````````````````````````````\
|
||||||
@ -57,9 +57,9 @@ msg type:
|
|||||||
#define IR_MSG_BACK 0U // | 000...... | = Задний сигнал машинки
|
#define IR_MSG_BACK 0U // | 000...... | = Задний сигнал машинки
|
||||||
#define IR_MSG_ACCEPT 1U // | 001..... | = подтверждение
|
#define IR_MSG_ACCEPT 1U // | 001..... | = подтверждение
|
||||||
#define IR_MSG_REQUEST 2U // | 010..... | = запрос
|
#define IR_MSG_REQUEST 2U // | 010..... | = запрос
|
||||||
#define IR_MSG_ 3U // | 011..... | = ??
|
// #define IR_MSG_ 3U // | 011..... | = ??
|
||||||
#define IR_MSG_BACK_TO 4U // | 100..... | = Задний сигнал машинки c адресацией
|
#define IR_MSG_BACK_TO 4U // | 100..... | = Задний сигнал машинки c адресацией
|
||||||
#define IR_MSG_ 5U // | 101..... | = ??
|
// #define IR_MSG_ 5U // | 101..... | = ??
|
||||||
#define IR_MSG_DATA_NOACCEPT 6U // | 110..... | = данные, не требующие подтверждения
|
#define IR_MSG_DATA_NOACCEPT 6U // | 110..... | = данные, не требующие подтверждения
|
||||||
#define IR_MSG_DATA_ACCEPT 7U // | 111..... | = данные требующие подтверждения
|
#define IR_MSG_DATA_ACCEPT 7U // | 111..... | = данные требующие подтверждения
|
||||||
; /* // ----------
|
; /* // ----------
|
||||||
@ -122,7 +122,7 @@ typedef uint16_t crc_t;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef subBufferSize
|
#ifndef subBufferSize
|
||||||
#define subBufferSize 50 // Буфер для складирования фронтов, пока их не обработают (передатчик)
|
#define subBufferSize 250 // Буфер для складирования фронтов, пока их не обработают (передатчик)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define preambPulse 3
|
#define preambPulse 3
|
||||||
|
Reference in New Issue
Block a user