diff --git a/IR_DecoderRaw.cpp b/IR_DecoderRaw.cpp index ef74883..d748143 100644 --- a/IR_DecoderRaw.cpp +++ b/IR_DecoderRaw.cpp @@ -112,16 +112,36 @@ void IR_DecoderRaw::firstRX() void IR_DecoderRaw::listenStart() { - if (isRecive && ((micros() - prevRise) > IR_timeout * 2)) + if (isReciveRaw && ((micros() - prevRise) > IR_timeout * 2)) { // Serial.print("\nlis>"); - isRecive = false; + isReciveRaw = false; firstRX(); } } + +// ---- быстрая проверка конца пакета --------------------------------- +inline void IR_DecoderRaw::checkTimeout() +{ + if (!isRecive) return; // уже не принимаем – нечего проверять + + if (micros() - lastEdgeTime > IR_timeout * 2U) + { + isRecive = false; // приём завершён + // firstRX(); // подготовка к новому пакету + lastEdgeTime = micros(); // защита от повторного срабатывания + } +} +// ==================================================================== + void IR_DecoderRaw::tick() { + // FrontStorage *currentFrontPtr; + // noInterrupts(); + // currentFrontPtr = subBuffer.pop(); + // interrupts(); + FrontStorage currentFront; noInterrupts(); listenStart(); @@ -130,12 +150,24 @@ void IR_DecoderRaw::tick() if (currentFrontPtr == nullptr) { isSubBufferOverflow = false; + checkTimeout(); // <--- новое место проверки interrupts(); return; } // Если данных нет - ничего не делаем currentFront = *currentFrontPtr; interrupts(); + // ---------- буфер пуст: фронтов нет, проверяем тайм-аут ---------- + // if (currentFrontPtr == nullptr) + // { + // isSubBufferOverflow = false; + // return; + // } + + // // ---------- есть фронт: продолжаем обработку ---------- + // FrontStorage currentFront = *currentFrontPtr; + lastEdgeTime = currentFront.time; // запоминаем любой фронт + //////////////////////////////////////////////////////////////////////////////////////////////////////////// if (currentFront.dir) @@ -197,7 +229,7 @@ void IR_DecoderRaw::tick() digitalWrite(errOut, currentFront.dir); #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 errPulse(up, 50); @@ -209,6 +241,7 @@ void IR_DecoderRaw::tick() isPreamb = true; isRecive = true; + isReciveRaw = true; isWrongPack = false; } @@ -441,6 +474,7 @@ void IR_DecoderRaw::writeToBuffer(bool bit) if (isBufferOverflow || isPreamb || isWrongPack) { isRecive = false; + isReciveRaw = false; return; } @@ -564,6 +598,7 @@ void IR_DecoderRaw::writeToBuffer(bool bit) packInfo.rTime = riseSyncTime; isRecive = false; + isReciveRaw = false; isAvailable = crcCheck(packSize - crcBytes, crcValue); #ifdef BRUTEFORCE_CHECK diff --git a/IR_DecoderRaw.h b/IR_DecoderRaw.h index 8110251..5e9d2b0 100644 --- a/IR_DecoderRaw.h +++ b/IR_DecoderRaw.h @@ -65,6 +65,8 @@ private: uint16_t riseSyncTime = bitTime; // Подстраиваемое время бита в мкс + volatile uint32_t lastEdgeTime = 0; // время последнего фронта + //////////////////////////////////////////////////////////////////////// volatile uint32_t currentSubBufferIndex; // Счетчик текущей позиции во вспомогательном буфере фронтов/спадов @@ -100,7 +102,9 @@ private: int16_t bufBitPos = 0; // Позиция для записи бита в буффер private: - void listenStart(); // @brief Слушатель для работы isReciving() +bool isReciveRaw; + void listenStart(); + void checkTimeout(); // /// @brief Проверка CRC. Проверяет len байт со значением crc, пришедшим в пакете /// @param len Длина в байтах проверяемых данных