From 67b14ec57d91c5df6cc9e337ac91326e90f0cccc Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Thu, 4 Aug 2022 09:50:20 +0200 Subject: [PATCH] take into account that the reset packets are sent first #repeat packets into the future --- DCCACK.cpp | 4 ---- DCCWaveform.cpp | 3 ++- DCCWaveform.h | 15 ++++++++++----- GITHUB_SHA.h | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/DCCACK.cpp b/DCCACK.cpp index 82deb7f..d159383 100644 --- a/DCCACK.cpp +++ b/DCCACK.cpp @@ -113,11 +113,7 @@ void DCCACK::Setup(int wordval, ackOp const program[], ACK_CALLBACK callback) { Setup(0, 0, program, callback); } -#ifdef ARDUINO_ARCH_ESP32 -const byte RESET_MIN=12; // Ugly fix until counting code is right -#else const byte RESET_MIN=8; // tuning of reset counter before sending message -#endif // checkRessets return true if the caller should yield back to loop and try later. bool DCCACK::checkResets(uint8_t numResets) { diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index ac7ecb1..7140e91 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -243,7 +243,8 @@ void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repea pendingPacket[byteCount] = checksum; pendingLength = byteCount + 1; pendingRepeats = repeats; - clearResets(); + // The resets will be zero not only now but as well repeats packets into the future + clearResets(repeats+1); { int ret; do { diff --git a/DCCWaveform.h b/DCCWaveform.h index 5ff9805..108a63d 100644 --- a/DCCWaveform.h +++ b/DCCWaveform.h @@ -57,15 +57,20 @@ class DCCWaveform { inline void clearResets() { sentResetsSincePacket=0; } inline byte getResets() { return sentResetsSincePacket; } #else - inline void clearResets() { resetPacketBase = isMainTrack ? - rmtMainChannel->packetCount() : rmtProgChannel->packetCount(); }; + // extrafudge is added when we know that the resets will first come extrafudge packets in the future + inline void clearResets(byte extrafudge=0) { + resetPacketBase = isMainTrack ? rmtMainChannel->packetCount() : rmtProgChannel->packetCount(); + resetPacketBase += extrafudge; + }; inline byte getResets() { uint32_t packetcount = isMainTrack ? rmtMainChannel->packetCount() : rmtProgChannel->packetCount(); - uint32_t count = packetcount - resetPacketBase; - if (count > 255) // cap to 255 + uint32_t count = packetcount - resetPacketBase; // Beware of unsigned interger arithmetic. + if (count > UINT32_MAX/2) // we are in the extrafudge area + return 0; + if (count > 255) // cap to 255 return 255; - return count; + return count; // all special cases handled above }; #endif void schedulePacket(const byte buffer[], byte byteCount, byte repeats); diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index 6080bfb..c8e93ff 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "PORTX-HAL-20220804" +#define GITHUB_SHA "PORTX-HAL-20220804-1"