From 9b75026eefb51d3f047b4bd77eb25fc67cad0e84 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 17 Jul 2023 02:26:29 +0200 Subject: [PATCH] change from trackMode[t] to track[t]->{get,set}Mode --- MotorDriver.h | 11 +++++++++++ TrackManager.cpp | 41 ++++++++++++++++++++--------------------- TrackManager.h | 5 ----- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/MotorDriver.h b/MotorDriver.h index 454015d..8458de1 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -27,6 +27,10 @@ #include "IODevice.h" #include "DCCTimer.h" +// use powers of two so we can do logical and/or on the track modes in if clauses. +enum TRACK_MODE : byte {TRACK_MODE_OFF = 1, TRACK_MODE_MAIN = 2, TRACK_MODE_PROG = 4, + TRACK_MODE_DC = 8, TRACK_MODE_DCX = 16, TRACK_MODE_EXT = 32}; + #define setHIGH(fastpin) *fastpin.inout |= fastpin.maskHIGH #define setLOW(fastpin) *fastpin.inout &= fastpin.maskLOW #define isHIGH(fastpin) (*fastpin.inout & fastpin.maskHIGH) @@ -203,6 +207,12 @@ class MotorDriver { bool sampleCurrentFromHW(); void startCurrentFromHW(); #endif + inline void setMode(TRACK_MODE m) { + trackMode = m; + }; + inline TRACK_MODE getMode() { + return trackMode; + }; private: char trackLetter = '?'; bool isProgTrack = false; // tells us if this is a prog track @@ -279,6 +289,7 @@ class MotorDriver { static const int TRIP_CURRENT_PROG=250; unsigned long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT; unsigned int power_good_counter = 0; + TRACK_MODE trackMode = TRACK_MODE_OFF; // we assume off at startup }; #endif diff --git a/TrackManager.cpp b/TrackManager.cpp index 8f66677..a23140c 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -31,7 +31,7 @@ #define APPLY_BY_MODE(findmode,function) \ FOR_EACH_TRACK(t) \ - if (trackMode[t]==findmode) \ + if (track[t]->getMode()==findmode) \ track[t]->function; #ifndef DISABLE_PROG const int16_t HASH_KEYWORD_PROG = -29718; @@ -44,7 +44,6 @@ const int16_t HASH_KEYWORD_EXT = 8201; // External DCC signal const int16_t HASH_KEYWORD_A = 65; // parser makes single chars the ascii. MotorDriver * TrackManager::track[MAX_TRACKS]; -TRACK_MODE TrackManager::trackMode[MAX_TRACKS]; int16_t TrackManager::trackDCAddr[MAX_TRACKS]; POWERMODE TrackManager::mainPowerGuess=POWERMODE::OFF; @@ -74,7 +73,7 @@ void TrackManager::sampleCurrent() { waiting = false; tr++; if (tr > lastTrack) tr = 0; - if (lastTrack < 2 || trackMode[tr] & TRACK_MODE_PROG) { + if (lastTrack < 2 || track[tr]->getMode() & TRACK_MODE_PROG) { return; // We could continue but for prog track we // rather do it in next interrupt beacuse // that gives us well defined sampling point. @@ -85,7 +84,7 @@ void TrackManager::sampleCurrent() { if (!waiting) { // look for a valid track to sample or until we are around while (true) { - if (trackMode[tr] & ( TRACK_MODE_MAIN|TRACK_MODE_PROG|TRACK_MODE_DC|TRACK_MODE_DCX|TRACK_MODE_EXT )) { + if (track[tr]->getMode() & ( TRACK_MODE_MAIN|TRACK_MODE_PROG|TRACK_MODE_DC|TRACK_MODE_DCX|TRACK_MODE_EXT )) { track[tr]->startCurrentFromHW(); // for scope debug track[1]->setBrake(1); waiting = true; @@ -138,10 +137,10 @@ void TrackManager::Setup(const FSH * shieldname, } void TrackManager::addTrack(byte t, MotorDriver* driver) { - trackMode[t]=TRACK_MODE_OFF; track[t]=driver; if (driver) { track[t]->setPower(POWERMODE::OFF); + track[t]->setMode(TRACK_MODE_OFF); track[t]->setTrackLetter('A'+t); lastTrack=t; } @@ -183,8 +182,8 @@ void TrackManager::setPROGSignal( bool on) { void TrackManager::setDCSignal(int16_t cab, byte speedbyte) { FOR_EACH_TRACK(t) { if (trackDCAddr[t]!=cab) continue; - if (trackMode[t]==TRACK_MODE_DC) track[t]->setDCSignal(speedbyte); - else if (trackMode[t]==TRACK_MODE_DCX) track[t]->setDCSignal(speedbyte ^ 128); + if (track[t]->getMode()==TRACK_MODE_DC) track[t]->setDCSignal(speedbyte); + else if (track[t]->getMode()==TRACK_MODE_DCX) track[t]->setDCSignal(speedbyte ^ 128); } } @@ -218,9 +217,9 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr #endif // only allow 1 track to be prog FOR_EACH_TRACK(t) - if (trackMode[t]==TRACK_MODE_PROG && t != trackToSet) { + if (track[t]->getMode()==TRACK_MODE_PROG && t != trackToSet) { track[t]->setPower(POWERMODE::OFF); - trackMode[t]=TRACK_MODE_OFF; + track[t]->setMode(TRACK_MODE_OFF); track[t]->makeProgTrack(false); // revoke prog track special handling streamTrackState(NULL,t); } @@ -228,7 +227,7 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr } else { track[trackToSet]->makeProgTrack(false); // only the prog track knows it's type } - trackMode[trackToSet]=mode; + track[trackToSet]->setMode(mode); trackDCAddr[trackToSet]=dcAddr; streamTrackState(NULL,trackToSet); @@ -255,7 +254,7 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr // DC tracks must not have the DCC PWM switched on // so we globally turn it off if one of the PWM // capable tracks is now DC or DCX. - if (trackMode[t]==TRACK_MODE_DC || trackMode[t]==TRACK_MODE_DCX) { + if (track[t]->getMode()==TRACK_MODE_DC || track[t]->getMode()==TRACK_MODE_DCX) { if (track[t]->isPWMCapable()) { canDo=false; // this track is capable but can not run PWM break; // in this mode, so abort and prevent globally below @@ -263,7 +262,7 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr 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) { + } else if (track[t]->getMode()==TRACK_MODE_MAIN || track[t]->getMode()==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; @@ -301,7 +300,7 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr void TrackManager::applyDCSpeed(byte t) { uint8_t speedByte=DCC::getThrottleSpeedByte(trackDCAddr[t]); - if (trackMode[t]==TRACK_MODE_DCX) + if (track[t]->getMode()==TRACK_MODE_DCX) speedByte = speedByte ^ 128; // reverse direction bit track[t]->setDCSignal(speedByte); } @@ -347,7 +346,7 @@ void TrackManager::streamTrackState(Print* stream, byte t) { // null stream means send to commandDistributor for broadcast if (track[t]==NULL) return; auto format=F(""); - switch(trackMode[t]) { + switch(track[t]->getMode()) { case TRACK_MODE_MAIN: format=F("<= %c MAIN>\n"); break; @@ -387,13 +386,13 @@ void TrackManager::loop() { if (nextCycleTrack>lastTrack) nextCycleTrack=0; if (track[nextCycleTrack]==NULL) return; MotorDriver * motorDriver=track[nextCycleTrack]; - bool useProgLimit=dontLimitProg? false: trackMode[nextCycleTrack]==TRACK_MODE_PROG; + bool useProgLimit=dontLimitProg? false: track[nextCycleTrack]->getMode()==TRACK_MODE_PROG; motorDriver->checkPowerOverload(useProgLimit, nextCycleTrack); } MotorDriver * TrackManager::getProgDriver() { FOR_EACH_TRACK(t) - if (trackMode[t]==TRACK_MODE_PROG) return track[t]; + if (track[t]->getMode()==TRACK_MODE_PROG) return track[t]; return NULL; } @@ -401,7 +400,7 @@ MotorDriver * TrackManager::getProgDriver() { std::vectorTrackManager::getMainDrivers() { std::vector v; FOR_EACH_TRACK(t) - if (trackMode[t]==TRACK_MODE_MAIN) v.push_back(track[t]); + if (track[t]->getMode()==TRACK_MODE_MAIN) v.push_back(track[t]); return v; } #endif @@ -411,7 +410,7 @@ void TrackManager::setPower2(bool setProg,POWERMODE mode) { FOR_EACH_TRACK(t) { MotorDriver * driver=track[t]; if (!driver) continue; - switch (trackMode[t]) { + switch (track[t]->getMode()) { case TRACK_MODE_MAIN: if (setProg) break; // toggle brake before turning power on - resets overcurrent error @@ -447,8 +446,8 @@ void TrackManager::setPower2(bool setProg,POWERMODE mode) { POWERMODE TrackManager::getProgPower() { FOR_EACH_TRACK(t) - if (trackMode[t]==TRACK_MODE_PROG) - return track[t]->getPower(); + if (track[t]->getMode()==TRACK_MODE_PROG) + return track[t]->getPower(); return POWERMODE::OFF; } @@ -492,7 +491,7 @@ void TrackManager::setJoin(bool joined) { #ifdef ARDUINO_ARCH_ESP32 if (joined) { FOR_EACH_TRACK(t) { - if (trackMode[t]==TRACK_MODE_PROG) { + if (track[t]->getMode()==TRACK_MODE_PROG) { tempProgTrack = t; setTrackMode(t, TRACK_MODE_MAIN); break; diff --git a/TrackManager.h b/TrackManager.h index 19e756d..965cfa3 100644 --- a/TrackManager.h +++ b/TrackManager.h @@ -27,10 +27,6 @@ #include "MotorDriver.h" // Virtualised Motor shield multi-track hardware Interface -// use powers of two so we can do logical and/or on the track modes in if clauses. -enum TRACK_MODE : byte {TRACK_MODE_OFF = 1, TRACK_MODE_MAIN = 2, TRACK_MODE_PROG = 4, - TRACK_MODE_DC = 8, TRACK_MODE_DCX = 16, TRACK_MODE_EXT = 32}; - // These constants help EXRAIL macros say SET_TRACK(2,mode) OR SET_TRACK(C,mode) etc. const byte TRACK_NUMBER_0=0, TRACK_NUMBER_A=0; const byte TRACK_NUMBER_1=1, TRACK_NUMBER_B=1; @@ -100,7 +96,6 @@ class TrackManager { static POWERMODE mainPowerGuess; static void applyDCSpeed(byte t); - static TRACK_MODE trackMode[MAX_TRACKS]; static int16_t trackDCAddr[MAX_TRACKS]; // dc address if TRACK_MODE_DC or TRACK_MODE_DCX #ifdef ARDUINO_ARCH_ESP32 static byte tempProgTrack; // holds the prog track number during join