diff --git a/DCC.cpp b/DCC.cpp index f3b895c..eb5dd2b 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -299,6 +299,7 @@ int DCC::ackManagerCv; byte DCC::ackManagerBitNum; bool DCC::ackReceived; int DCC::ackTriggerMilliamps; +long DCC::ackPulseStart; ACK_CALLBACK DCC::ackManagerCallback; @@ -333,11 +334,12 @@ void DCC::ackManagerLoop() { case W0: // write 0 bit case W1: // write 1 bit { - if (resetsackTriggerMilliamps) { + if (ackPulseStart==0)ackPulseStart=micros(); // leading edge of pulse detected + return; + } - if (current > ackTriggerMilliamps) { //ACK detected - // DIAG(F("\nACK %dmA, after %d resets\n"), current,resets); - ackReceived = true; - DCCWaveform::progTrack.killRemainingRepeats(); - break; // move on tho next step + // not in pulse + if (ackPulseStart==0) return; // keep waiting for leading edge + { + long pulseDuration=micros()-ackPulseStart; + // TODO handle timer wrapover + if (pulseDuration>4500 && pulseDuration<8500) { + ackReceived=true; + DCCWaveform::progTrack.killRemainingRepeats(); // probabaly no need after 8.5ms!! + break; // we have a genuine ACK result } - - return; // check again on next loop cycle. - + } + ackPulseStart=0; // We have detected a too-short or too-long pulse so ignore and wait for next leading edge + return; // keep waiting + case ITC0: case ITC1: // If True Callback(0 or 1) (if prevous WACK got an ACK) if (ackReceived) { diff --git a/DCC.h b/DCC.h index 4889f7a..e51fc49 100644 --- a/DCC.h +++ b/DCC.h @@ -72,6 +72,7 @@ private: static byte ackManagerStash; static bool ackReceived; static int ackTriggerMilliamps; + static long ackPulseStart; static ACK_CALLBACK ackManagerCallback; static void ackManagerSetup(int cv, byte bitNumOrbyteValue, ackOp const program[], ACK_CALLBACK callback); static void ackManagerLoop();