fix forever loop ?

This commit is contained in:
DashyFox 2024-02-26 15:42:27 +03:00
parent 1dc702f05d
commit 1f62eb8c0a
2 changed files with 22 additions and 16 deletions

View File

@ -46,7 +46,7 @@ void IR_DecoderRaw::isr() {
void IR_DecoderRaw::firstRX() { void IR_DecoderRaw::firstRX() {
#ifdef IRDEBUG_INFO #ifdef IRDEBUG_INFO
Serial.print("\n>"); Serial.print("\nRX>");
#endif #endif
errors.reset(); errors.reset();
@ -67,8 +67,9 @@ void IR_DecoderRaw::firstRX() {
memset(dataBuffer, 0x00, dataByteSizeMax); memset(dataBuffer, 0x00, dataByteSizeMax);
} }
void IR_DecoderRaw::listenEnd() { void IR_DecoderRaw::listenStart() {
if (isRecive && ((micros() - prevRise) > IR_timeout * 2)) { if (isRecive && ((micros() - prevRise) > IR_timeout * 2)) {
Serial.print("\nlis>");
isRecive = false; isRecive = false;
firstRX(); firstRX();
} }
@ -77,10 +78,11 @@ void IR_DecoderRaw::listenEnd() {
void IR_DecoderRaw::tick() { void IR_DecoderRaw::tick() {
FrontStorage currentFront; FrontStorage currentFront;
noInterrupts(); noInterrupts();
listenEnd(); listenStart();
if (firstUnHandledFront == nullptr) { interrupts(); return; } //Если данных нет - ничего не делаем if (firstUnHandledFront == nullptr) { interrupts(); return; } //Если данных нет - ничего не делаем
currentFront = *((FrontStorage*)firstUnHandledFront); //найти следующий необработанный фронт/спад currentFront = *((FrontStorage*)firstUnHandledFront); //найти следующий необработанный фронт/спад
interrupts(); interrupts();
if (currentFront.next == nullptr) { isRecive = false; return; }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (currentFront.time > prevRise && currentFront.time - prevRise > IR_timeout * 2 && !isRecive) { // первый if (currentFront.time > prevRise && currentFront.time - prevRise > IR_timeout * 2 && !isRecive) { // первый
@ -101,7 +103,7 @@ void IR_DecoderRaw::tick() {
} }
if (preambFrontCounter > 0) { // в преамбуле if (preambFrontCounter > 0) { // в преамбуле
uint32_t risePeriod = currentFront.time - prevRise; risePeriod = currentFront.time - prevRise;
if (currentFront.dir && risePeriod < IR_timeout) { // __/``` ↑ и мы в внутри пакета if (currentFront.dir && risePeriod < IR_timeout) { // __/``` ↑ и мы в внутри пакета
if (risePeriod < riseTimeMin << 1) { // fix рваной единицы if (risePeriod < riseTimeMin << 1) { // fix рваной единицы
@ -276,7 +278,10 @@ void IR_DecoderRaw::writeToBuffer(bool bit) {
#endif #endif
isBufferOverflow = true; isBufferOverflow = true;
} }
if (isBufferOverflow || isPreamb || isWrongPack) return; if (isBufferOverflow || isPreamb || isWrongPack) {
isRecive = false;
return;
}
// Переключение флага, data или syncBit // Переключение флага, data или syncBit
if (bufBitPos == nextControlBit) { if (bufBitPos == nextControlBit) {
@ -327,7 +332,7 @@ void IR_DecoderRaw::writeToBuffer(bool bit) {
#ifdef IRDEBUG_INFO #ifdef IRDEBUG_INFO
Serial.print("****************"); Serial.print("****************");
#endif #endif
isRecive = false;
}; };
}//**************************************************************************************************// }//**************************************************************************************************//
@ -349,7 +354,7 @@ void IR_DecoderRaw::writeToBuffer(bool bit) {
} }
#endif #endif
if (!isAvailable && isData) { if (!isAvailable && isData && !isWrongPack) {
if (i_dataBuffer == 8 * msgBytes) {// Ппервый байт if (i_dataBuffer == 8 * msgBytes) {// Ппервый байт
packSize = dataBuffer[0] & IR_MASK_MSG_INFO; packSize = dataBuffer[0] & IR_MASK_MSG_INFO;
Serial.print(" ["); Serial.print(packSize); Serial.print("] "); Serial.print(" ["); Serial.print(packSize); Serial.print("] ");

View File

@ -23,7 +23,7 @@
class IR_Encoder; class IR_Encoder;
class IR_DecoderRaw : public IR_FOX { class IR_DecoderRaw : virtual public IR_FOX {
friend IR_Encoder; friend IR_Encoder;
public: public:
const uint8_t isrPin; // Пин прерывания const uint8_t isrPin; // Пин прерывания
@ -44,9 +44,9 @@ public:
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
protected: protected:
PackInfo packInfo; PackInfo packInfo;
uint16_t id;
private: private:
ErrorsStruct errors; ErrorsStruct errors;
uint16_t id;
bool isAvailable = false; bool isAvailable = false;
uint16_t packSize; uint16_t packSize;
uint16_t crcValue; uint16_t crcValue;
@ -73,12 +73,13 @@ private:
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
uint8_t dataBuffer[dataByteSizeMax] { 0 }; // Буффер данных uint8_t dataBuffer[dataByteSizeMax] { 0 }; // Буффер данных
uint32_t prevRise, prevPrevRise, prevFall, prevPrevFall; // Время предыдущих фронтов/спадов uint32_t prevRise, prevPrevRise, prevFall, prevPrevFall; // Время предыдущих фронтов/спадов
uint32_t risePeriod;
uint16_t errorCounter = 0; // Счётчик ошибок uint16_t errorCounter = 0; // Счётчик ошибок
int8_t preambFrontCounter = 0; // Счётчик __/``` ↑ преамбулы int8_t preambFrontCounter = 0; // Счётчик __/``` ↑ преамбулы
int16_t bufBitPos = 0; // Позиция для записи бита в буффер int16_t bufBitPos = 0; // Позиция для записи бита в буффер
private: private:
void listenEnd(); // @brief Слушатель для работы isReciving() void listenStart(); // @brief Слушатель для работы isReciving()
/// @brief Проверка CRC. Проверяет len байт со значением crc, пришедшим в пакете /// @brief Проверка CRC. Проверяет len байт со значением crc, пришедшим в пакете
/// @param len Длина в байтах проверяемых данных /// @param len Длина в байтах проверяемых данных