From 38a9585a412a9ae1b492a031b2d785e3eab4e66f Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 6 Apr 2024 19:46:23 +0200 Subject: [PATCH] ESP32 Trackmanager reset cab number to 0 when track is not DC --- TrackManager.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/TrackManager.cpp b/TrackManager.cpp index 06b6a18..d66b999 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -38,8 +38,8 @@ if (track[t]->getMode()==findmode) \ track[t]->function; -MotorDriver * TrackManager::track[MAX_TRACKS]; -int16_t TrackManager::trackDCAddr[MAX_TRACKS]; +MotorDriver * TrackManager::track[MAX_TRACKS] = { NULL }; +int16_t TrackManager::trackDCAddr[MAX_TRACKS] = { 0 }; int8_t TrackManager::lastTrack=-1; bool TrackManager::progTrackSyncMain=false; @@ -251,7 +251,6 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr } else { track[trackToSet]->makeProgTrack(false); // only the prog track knows it's type } - track[trackToSet]->setMode(mode); // When a track is switched, we must clear any side effects of its previous // state, otherwise trains run away or just dont move. @@ -259,8 +258,13 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr // This can be done BEFORE the PWM-Timer evaluation (methinks) if (mode & TRACK_MODE_DC) { if (trackDCAddr[trackToSet] != dcAddr) { - // if we change dcAddr, detach first old signal - track[trackToSet]->detachDCSignal(); + // new or changed DC Addr, run the new setup + if (trackDCAddr[trackToSet] != 0) { + // if we change dcAddr and not only + // change from another mode, + // first detach old DC signal + track[trackToSet]->detachDCSignal(); + } #ifdef ARDUINO_ARCH_ESP32 int trackfound = -1; FOR_EACH_TRACK(t) { @@ -285,7 +289,9 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr // DCC tracks need to have set the PWM to zero or they will not work. track[trackToSet]->detachDCSignal(); track[trackToSet]->setBrake(false); + trackDCAddr[trackToSet]=0; // clear that an addr is set for DC as this is not a DC track } + track[trackToSet]->setMode(mode); // BOOST: // Leave it as is