From cafd53a0e569ebb2ae143a4ea4ae4cbba6d203ff Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 22 Dec 2024 23:12:45 +0100 Subject: [PATCH] clear progTrackSyncMain (join flag) when prog track is removed 2nd fix --- DCCACK.cpp | 8 ++++++-- TrackManager.cpp | 23 +++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/DCCACK.cpp b/DCCACK.cpp index d6b070e..7a43cd1 100644 --- a/DCCACK.cpp +++ b/DCCACK.cpp @@ -72,15 +72,19 @@ void DCCACK::Setup(int cv, byte byteValueOrBitnum, ackOp const program[], ACK_C // then unjoin if necessary. This requires that the joined flag is // cleared when the prog track is removed. ackManagerRejoin=TrackManager::isJoined(); + //DIAG(F("Joined is %d"), ackManagerRejoin); if (ackManagerRejoin) { // Change from JOIN must zero resets packet. TrackManager::setJoin(false); DCCWaveform::progTrack.clearResets(); } - progDriver=TrackManager::getProgDriver(); + //DIAG(F("Progdriver is %d"), progDriver); if (progDriver==NULL) { - TrackManager::setJoin(ackManagerRejoin); + if (ackManagerRejoin) { + DIAG(F("Joined but no Prog track")); + TrackManager::setJoin(false); + } callback(-3); // we dont have a prog track! return; } diff --git a/TrackManager.cpp b/TrackManager.cpp index 5ada17b..74e67de 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -356,17 +356,24 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr applyDCSpeed(trackToSet); } - // Turn off power if we changed the mode of this track - if (mode != oldmode) - track[trackToSet]->setPower(POWERMODE::OFF); - streamTrackState(NULL,trackToSet); - +#ifdef ARDUINO_ARCH_ESP32 #ifndef DISABLE_PROG - // If no prog track exists, the join flag should not say that - // the prog track is joined either, so clear flag here - if (getProgDriver() == NULL) progTrackSyncMain=false; + if (tempProgTrack == trackToSet && oldmode & TRACK_MODE_MAIN && !(mode & TRACK_MODE_PROG)) { + // If we just take away the prog track, the join should not + // be active either. So do in effect an unjoin + DIAG(F("Unsync")); + tempProgTrack = MAX_TRACKS+1; + progTrackSyncMain=false; + if (joinRelay!=UNUSED_PIN) digitalWrite(joinRelay,LOW); + } #endif +#endif + // Turn off power if we changed the mode of this track + if (mode != oldmode) { + track[trackToSet]->setPower(POWERMODE::OFF); + } + streamTrackState(NULL,trackToSet); //DIAG(F("TrackMode=%d"),mode); return true; }