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"