From 40dfda47c7783566ec1f760d1b6687e324decfde Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 11 Jun 2022 19:57:45 +0200 Subject: [PATCH] make usePWM per track (trackPWM) 2nd half --- GITHUB_SHA.h | 2 +- MotorDriver.h | 6 ++++-- TrackManager.cpp | 22 +++++++++++++++++----- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index fc9fb44..8f67bc8 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "TM-PORTX-20220607" +#define GITHUB_SHA "TM-PORTX-20220607-1" diff --git a/MotorDriver.h b/MotorDriver.h index 7f0b169..93a337b 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -2,6 +2,7 @@ * © 2021 Mike S * © 2021 Fred Decker * © 2020 Chris Harlow + * © 2022 Harald Barth * All rights reserved. * * This file is part of Asbelos DCC API @@ -87,7 +88,7 @@ class MotorDriver { virtual void setPower( POWERMODE mode); virtual POWERMODE getPower() { return powerMode;} __attribute__((always_inline)) inline void setSignal( bool high) { - if (usePWM) { + if (trackPWM) { DCCTimer::setPWM(signalPin,high); } else { @@ -121,7 +122,8 @@ class MotorDriver { } bool isPWMCapable(); bool canMeasureCurrent(); - static bool usePWM; + bool trackPWM; + static bool usePWM; // TODO: Remove static bool commonFaultPin; // This is a stupid motor shield which has only a common fault pin for both outputs inline byte getFaultPin() { return faultPin; diff --git a/TrackManager.cpp b/TrackManager.cpp index 4727dc8..505a006 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -190,14 +190,26 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr // capable tracks is now DC or DCX. if (trackMode[t]==TRACK_MODE_DC || trackMode[t]==TRACK_MODE_DCX) { if (track[t]->isPWMCapable()) { - canDo=false; - break; + canDo=false; // this track is capable but can not run PWM + break; // in this mode, so abort and prevent globally below + } else { + track[t]->trackPWM=false; // this track sure can not run with PWM + //DIAG(F("Track %c trackPWM 0 (not capable)"), t+'A'); } - } else if (trackMode[t]==TRACK_MODE_MAIN || trackMode[t]==TRACK_MODE_PROG) - canDo &= track[t]->isPWMCapable(); + } else if (trackMode[t]==TRACK_MODE_MAIN || trackMode[t]==TRACK_MODE_PROG) { + track[t]->trackPWM = track[t]->isPWMCapable(); // trackPWM is still a guess here + //DIAG(F("Track %c trackPWM %d"), t+'A', track[t]->trackPWM); + canDo &= track[t]->trackPWM; + } } if (!canDo) { - DCCTimer::clearPWM(); + // if we discover that HA mode was globally impossible + // we must adjust the trackPWM capabilities + FOR_EACH_TRACK(t) { + track[t]->trackPWM=false; + //DIAG(F("Track %c trackPWM 0 (global override)"), t+'A'); + } + DCCTimer::clearPWM(); // has to be AFTER trackPWM changes because if trackPWM==true this is undone for that track } //if (MotorDriver::usePWM != canDo) // DIAG(F("HA mode changed from %d to %d"), MotorDriver::usePWM, canDo);