fix forever loop

This commit is contained in:
DashyFox 2024-02-16 16:06:53 +03:00
parent a143dcf80d
commit a0ca86909d
2 changed files with 19 additions and 21 deletions

View File

@ -1,7 +1,7 @@
#include "IR_Decoder.h"
#include "IR_Encoder.h"
#define IRDEBUG_INFO
// #define IRDEBUG_INFO
#define checkAddr(h, l) (\
((uint16_t)((dataBuffer[h] << 8) | dataBuffer[l]) == addrSelf) || \
@ -12,7 +12,7 @@ IR_Decoder::IR_Decoder(const uint8_t isrPin, uint16_t addr, IR_Encoder* encPair
// rawBuffer = new uint8_t[bufferRawSize] { 0 };
dataBuffer = new uint8_t[dataByteSizeMax] { 0 };
prevRise = prevFall = prevPrevFall = prevPrevRise = 0;
start_RX();
// start_RX();
}
IR_Decoder::~IR_Decoder() {
@ -78,27 +78,33 @@ void IR_Decoder::start_RX() {
}
void IR_Decoder::listen() {
if (isRecive && ((micros() - prevRise) > IR_timeout)) {
if (isRecive && ((micros() - prevRise) > IR_timeout * 2)) {
isRecive = false;
start_RX();
}
}
void IR_Decoder::tick() {
listen();
if (firstUnHandledFront == nullptr) return; //Если данных нет - ничего не делаем
FrontStorage currentFront;
//найти следующий необработанный фронт/спад
noInterrupts();
currentFront = *((FrontStorage*)firstUnHandledFront);
listen();
if (firstUnHandledFront == nullptr) { interrupts(); return; } //Если данных нет - ничего не делаем
currentFront = *((FrontStorage*)firstUnHandledFront); //найти следующий необработанный фронт/спад
interrupts();
////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (currentFront.time - prevRise > IR_timeout * 2 /* && !isRecive */) { // первый
if (currentFront.time > prevRise && currentFront.time - prevRise > IR_timeout * 2 && !isRecive) { // первый
preambFrontCounter = preambFronts - 1U;
if (!currentFront.dir) {
#ifdef IRDEBUG_INFO
// Serial.print(" currentFront.time: "); Serial.print(currentFront.time);
// Serial.print(" currentFront.dir: "); Serial.print(currentFront.dir ? "UP" : "DOWN");
// Serial.print(" next: "); Serial.print(currentFront.next == nullptr);
// Serial.print(" prevRise: "); Serial.print(prevRise);
// Serial.print(" SUB: "); Serial.println(currentFront.time - prevRise);
#endif
isRecive = true;
}
}
@ -328,6 +334,7 @@ void IR_Decoder::writeToBuffer(bool bit) {
////////////////////// Проверка наличия битов синхранизации //////////////////////
if (isWrongPack = (err_syncBit >= syncBits)) {
start_RX();
firstUnHandledFront = firstUnHandledFront->next;
#ifdef IRDEBUG_INFO
Serial.print("****************");
#endif

View File

@ -218,18 +218,9 @@ private:
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 uint32_t time = 0; // Время
volatile bool dir = false; // Направление (true = ↑; false = ↓)
volatile FrontStorage* next = nullptr; // Указатель на следующий связанный фронт/спад, или nullptr если конец
};
volatile FrontStorage* lastFront = nullptr; // Указатель последнего фронта/спада
volatile FrontStorage* firstUnHandledFront = nullptr; // Указатель первого необработанного фронта/спада