diff --git a/DCC.cpp b/DCC.cpp index 92aa4d7..81f2d43 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -643,6 +643,21 @@ void DCC::ackManagerLoop(bool blocking) { opcode=pgm_read_byte_near(ackManagerProg); } break; + case POWERON: + if (DCCWaveform::progTrack.getPowerMode() == POWERMODE::OFF) { + DCCWaveform::progTrack.setPowerMode(POWERMODE::ON); + DCCWaveform::progTrack.sentResetsSincePacket = 0; + DCCWaveform::progTrack.autoPowerOff=true; + return; + } + if (checkResets(blocking, 20)) return; + break; + case POWEROFF: + if (DCCWaveform::progTrack.autoPowerOff) { + DCCWaveform::progTrack.setPowerMode(POWERMODE::OFF); + DCCWaveform::progTrack.autoPowerOff=false; + } + break; case SKIPTARGET: break; default: diff --git a/DCC.h b/DCC.h index 195a918..6b3e499 100644 --- a/DCC.h +++ b/DCC.h @@ -44,6 +44,8 @@ SETCV, // sets cv number to next prog byte STASHLOCOID, // keeps current byte value for later COMBINELOCOID, // combines current value with stashed value and returns it ITSKIP, // skip to SKIPTARGET if ack true +POWERON, // check if power on prog track is on and remember state +POWEROFF, // turn power off again if it was off before POWERON SKIPTARGET=0xFF // jump to target }; @@ -120,7 +122,7 @@ private: static ACK_CALLBACK ackManagerCallback; static void ackManagerSetup(int cv, byte bitNumOrbyteValue, ackOp const program[], ACK_CALLBACK callback, bool blocking); static void ackManagerLoop(bool blocking); - static bool checkResets(bool blocking); + static bool checkResets(bool blocking, uint8_t numResets); static const int PROG_REPEATS=8; // repeats of programming commands (some decoders need at least 8 to be reliable) diff --git a/DCCWaveform.h b/DCCWaveform.h index 6537611..ebb543e 100644 --- a/DCCWaveform.h +++ b/DCCWaveform.h @@ -60,6 +60,7 @@ class DCCWaveform { void schedulePacket(const byte buffer[], byte byteCount, byte repeats); volatile bool packetPending; volatile byte sentResetsSincePacket; + volatile bool autoPowerOff=false; void setAckBaseline(bool debug); //prog track only void setAckPending(bool debug); //prog track only byte getAck(bool debug); //prog track only 0=NACK, 1=ACK 2=keep waiting