1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-30 03:26:13 +01:00

tidy and shorten loops

This commit is contained in:
Asbelos 2022-02-28 09:32:26 +00:00
parent b29b8c999e
commit 4f781074eb
2 changed files with 65 additions and 55 deletions

View File

@ -23,9 +23,10 @@
#include "MotorDriver.h" #include "MotorDriver.h"
#include "DIAG.h" #include "DIAG.h"
// Virtualised Motor shield multi-track hardware Interface // Virtualised Motor shield multi-track hardware Interface
#define FOR_EACH_TRACK(t) for (byte t=0;t<=lastTrack;t++)
#define LOOPMODE(findmode,function) \ #define APPLY_BY_MODE(findmode,function) \
for (byte t=0;t<8;t++) \ FOR_EACH_TRACK(t) \
if (trackMode[t]==findmode) \ if (trackMode[t]==findmode) \
track[t]->function; track[t]->function;
@ -37,30 +38,28 @@ const int16_t HASH_KEYWORD_DC = 2183;
MotorDriver * TrackManager::track[MAX_TRACKS]; MotorDriver * TrackManager::track[MAX_TRACKS];
int16_t TrackManager::trackMode[MAX_TRACKS]; int16_t TrackManager::trackMode[MAX_TRACKS];
POWERMODE TrackManager::mainPowerGuess=POWERMODE::OFF; POWERMODE TrackManager::mainPowerGuess=POWERMODE::OFF;
byte TrackManager::lastTrack=0;
// The setup call is done this way so that the tracks can be in a list
// from the config... the tracks default to NULL in the declaration
void TrackManager::Setup(const FSH * shieldname, void TrackManager::Setup(const FSH * shieldname,
MotorDriver * track0, MotorDriver * track1, MotorDriver * track2, MotorDriver * track0, MotorDriver * track1, MotorDriver * track2,
MotorDriver * track3, MotorDriver * track4, MotorDriver * track5, MotorDriver * track3, MotorDriver * track4, MotorDriver * track5,
MotorDriver * track6, MotorDriver * track7 ) { MotorDriver * track6, MotorDriver * track7 ) {
(void) shieldname; // TODO (void) shieldname; // TODO
track[0]=track0; addTrack(0,track0);
track[1]=track1; addTrack(1,track1);
track[2]=track2; addTrack(2,track2);
track[3]=track3; addTrack(3,track3);
track[4]=track4; addTrack(4,track4);
track[5]=track5; addTrack(5,track5);
track[6]=track6; addTrack(6,track6);
track[7]=track7; addTrack(7,track7);
// Default the first 2 tracks (which mat be null) and perform HA waveform check.
setTrackMode(0,TRACK_MODE_MAIN); setTrackMode(0,TRACK_MODE_MAIN);
setTrackMode(1,TRACK_MODE_PROG); setTrackMode(1,TRACK_MODE_PROG);
setTrackMode(2,TRACK_MODE_OFF);
setTrackMode(3,TRACK_MODE_OFF);
setTrackMode(4,TRACK_MODE_OFF);
setTrackMode(5,TRACK_MODE_OFF);
setTrackMode(6,TRACK_MODE_OFF);
setTrackMode(7,TRACK_MODE_OFF);
// TODO Fault pin config for odd motor boards (example pololu) // TODO Fault pin config for odd motor boards (example pololu)
// MotorDriver::commonFaultPin = ((mainDriver->getFaultPin() == progDriver->getFaultPin()) // MotorDriver::commonFaultPin = ((mainDriver->getFaultPin() == progDriver->getFaultPin())
// && (mainDriver->getFaultPin() != UNUSED_PIN)); // && (mainDriver->getFaultPin() != UNUSED_PIN));
@ -68,34 +67,42 @@ void TrackManager::Setup(const FSH * shieldname,
MotorDriver::usePWM ? F("high") : F("normal") ); MotorDriver::usePWM ? F("high") : F("normal") );
} }
void TrackManager::addTrack(byte t, MotorDriver* driver) {
track[t]=driver;
trackMode[t]=TRACK_MODE_OFF;
if (driver) lastTrack=t;
}
void TrackManager::setDCCSignal( bool on) { void TrackManager::setDCCSignal( bool on) {
LOOPMODE(TRACK_MODE_MAIN,setSignal(on)); APPLY_BY_MODE(TRACK_MODE_MAIN,setSignal(on));
} }
void TrackManager::setCutout( bool on) { void TrackManager::setCutout( bool on) {
(void) on; (void) on;
// TODO LOOPMODE(TRACK_MODE_MAIN,setCutout(on)); // TODO APPLY_BY_MODE(TRACK_MODE_MAIN,setCutout(on));
} }
void TrackManager::setPROGSignal( bool on) { void TrackManager::setPROGSignal( bool on) {
LOOPMODE(TRACK_MODE_PROG,setSignal(on)); APPLY_BY_MODE(TRACK_MODE_PROG,setSignal(on));
} }
void TrackManager::setDCSignal(int16_t cab, byte speedbyte) { void TrackManager::setDCSignal(int16_t cab, byte speedbyte) {
LOOPMODE(cab,setDCSignal(speedbyte)); APPLY_BY_MODE(cab,setDCSignal(speedbyte));
} }
bool TrackManager::setTrackMode(byte trackToSet, int16_t modeOrAddr) { bool TrackManager::setTrackMode(byte trackToSet, int16_t modeOrAddr) {
if (trackToSet>=8 || track[trackToSet]==NULL) return false; if (trackToSet>lastTrack || track[trackToSet]==NULL) return false;
if (modeOrAddr==TRACK_MODE_PROG) { if (modeOrAddr==TRACK_MODE_PROG) {
// only allow 1 track to be prog // only allow 1 track to be prog
for (byte t=0;t<8;t++) FOR_EACH_TRACK(t)
if (trackMode[t]==TRACK_MODE_PROG) trackMode[t]=TRACK_MODE_OFF; if (trackMode[t]==TRACK_MODE_PROG) trackMode[t]=TRACK_MODE_OFF;
} }
trackMode[trackToSet]=modeOrAddr; trackMode[trackToSet]=modeOrAddr;
// re-evaluate HighAccuracy mode // re-evaluate HighAccuracy mode
// We can only do this is all main and prog tracks agree
bool canDo=true; bool canDo=true;
for (byte t=0;t<8;t++) FOR_EACH_TRACK(t)
if (trackMode[t]==TRACK_MODE_MAIN ||trackMode[t]==TRACK_MODE_PROG) if (trackMode[t]==TRACK_MODE_MAIN ||trackMode[t]==TRACK_MODE_PROG)
canDo &= track[t]->isPWMCapable(); canDo &= track[t]->isPWMCapable();
MotorDriver::usePWM=canDo; MotorDriver::usePWM=canDo;
@ -106,8 +113,8 @@ bool TrackManager::parseJ(Print *stream, int16_t params, int16_t p[])
{ {
if (params==0) { // <J> List track assignments if (params==0) { // <J> List track assignments
for (byte t=0;t<8;t++) { FOR_EACH_TRACK(t)
if (track[t]==NULL) break; if (track[t]!=NULL) {
StringFormatter::send(stream,F("<j %d "),t); StringFormatter::send(stream,F("<j %d "),t);
switch(trackMode[t]) { switch(trackMode[t]) {
case TRACK_MODE_MAIN: case TRACK_MODE_MAIN:
@ -149,7 +156,7 @@ byte TrackManager::nextCycleTrack=MAX_TRACKS;
void TrackManager::loop(bool dontLimitProg) { void TrackManager::loop(bool dontLimitProg) {
nextCycleTrack++; nextCycleTrack++;
if (nextCycleTrack>=MAX_TRACKS) nextCycleTrack=0; if (nextCycleTrack>lastTrack) nextCycleTrack=0;
if (track[nextCycleTrack]==NULL) return; if (track[nextCycleTrack]==NULL) return;
MotorDriver * motorDriver=track[nextCycleTrack]; MotorDriver * motorDriver=track[nextCycleTrack];
bool useProgLimit=dontLimitProg? false: trackMode[nextCycleTrack]==TRACK_MODE_PROG; bool useProgLimit=dontLimitProg? false: trackMode[nextCycleTrack]==TRACK_MODE_PROG;
@ -157,25 +164,26 @@ void TrackManager::loop(bool dontLimitProg) {
} }
MotorDriver * TrackManager::getProgDriver() { MotorDriver * TrackManager::getProgDriver() {
for (byte t=0;t<8;t++) FOR_EACH_TRACK(t)
if (trackMode[t]==TRACK_MODE_PROG) return track[t]; if (trackMode[t]==TRACK_MODE_PROG) return track[t];
return NULL; return NULL;
} }
void TrackManager::setPower2(bool setProg,POWERMODE mode) { void TrackManager::setPower2(bool setProg,POWERMODE mode) {
if (setProg) { if (setProg) {
LOOPMODE(TRACK_MODE_PROG,setPower(mode)) APPLY_BY_MODE(TRACK_MODE_PROG,setPower(mode))
} }
else { else {
mainPowerGuess=mode; mainPowerGuess=mode;
for (byte t=0;t<8;t++) FOR_EACH_TRACK(t)
if (track[t] if (track[t]
&& trackMode[t]!=TRACK_MODE_OFF && trackMode[t]!=TRACK_MODE_OFF
&& trackMode[t]!=TRACK_MODE_PROG && trackMode[t]!=TRACK_MODE_PROG
) track[t]->setPower(mode); ) track[t]->setPower(mode);
} }
} }
POWERMODE TrackManager::getProgPower() { POWERMODE TrackManager::getProgPower() {
for (byte t=0;t<8;t++) FOR_EACH_TRACK(t)
if (trackMode[t]==TRACK_MODE_PROG) if (trackMode[t]==TRACK_MODE_PROG)
return track[t]->getPower(); return track[t]->getPower();
return POWERMODE::OFF; return POWERMODE::OFF;

View File

@ -59,6 +59,8 @@ class TrackManager {
private: private:
static void addTrack(byte t, MotorDriver* driver);
static byte lastTrack;
static byte nextCycleTrack; static byte nextCycleTrack;
static POWERMODE mainPowerGuess; static POWERMODE mainPowerGuess;