diff --git a/DCCRMT.h b/DCCRMT.h index 33257a0..3a20027 100644 --- a/DCCRMT.h +++ b/DCCRMT.h @@ -44,6 +44,12 @@ class RMTChannel { return true; return dataReady; }; + inline void waitForDataCopy() { + while(1) { // do nothing and wait for interrupt clearing dataReady to happen + if (dataReady == false) + break; + } + }; inline uint32_t packetCount() { return packetCounter; }; private: diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 3d77e9e..eb25144 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -278,7 +278,11 @@ void DCCWaveform::begin() { void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repeats) { if (byteCount > MAX_PACKET_SIZE) return; // allow for chksum - + RMTChannel *rmtchannel = (isMainTrack ? rmtMainChannel : rmtProgChannel); + if (rmtchannel == NULL) + return; // no idea to prepare packet if we can not send it anyway + + rmtchannel->waitForDataCopy(); // blocking wait so we can write into buffer byte checksum = 0; for (byte b = 0; b < byteCount; b++) { checksum ^= buffer[b]; @@ -296,13 +300,7 @@ void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repea { int ret = 0; do { - if(isMainTrack) { - if (rmtMainChannel != NULL) - ret = rmtMainChannel->RMTfillData(pendingPacket, pendingLength, pendingRepeats); - } else { - if (rmtProgChannel != NULL) - ret = rmtProgChannel->RMTfillData(pendingPacket, pendingLength, pendingRepeats); - } + ret = rmtchannel->RMTfillData(pendingPacket, pendingLength, pendingRepeats); } while(ret > 0); } }