diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index a68b398..aee7e05 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -552,84 +552,88 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) case '1': // POWERON <1 [MAIN|PROG|JOIN]> { - bool main=false; - bool prog=false; - bool join=false; - bool singletrack=false; - byte t=0; - if (params > 1) break; - if (params==0) { // All - main=true; - prog=true; - } - if (params==1) { - if (p[0]==HASH_KEYWORD_MAIN) { // <1 MAIN> - main=true; - } + bool main=false; + bool prog=false; + bool join=false; + bool singletrack=false; + //byte t=0; + if (params > 1) break; + if (params==0) { // All + main=true; + prog=true; + } + if (params==1) { + if (p[0]==HASH_KEYWORD_MAIN) { // <1 MAIN> + main=true; + } #ifndef DISABLE_PROG - else if (p[0] == HASH_KEYWORD_JOIN) { // <1 JOIN> - main=true; - prog=true; - join=true; - } - else if (p[0]==HASH_KEYWORD_PROG) { // <1 PROG> - prog=true; - } + else if (p[0] == HASH_KEYWORD_JOIN) { // <1 JOIN> + main=true; + prog=true; + join=true; + } + else if (p[0]==HASH_KEYWORD_PROG) { // <1 PROG> + prog=true; + } #endif - else if (p[0] >= 'A' && p[0] <= 'H') { // <1 A-H> - t = (p[0] - 'A'); - //DIAG(F("Processing track - %d "), t); - if (TrackManager::isProg(t)) { - main = false; - prog = true; - } - else - { - main=true; - prog=false; - } - singletrack=true; + else if (p[0] >= 'A' && p[0] <= 'H') { // <1 A-H> + byte t = (p[0] - 'A'); + //DIAG(F("Processing track - %d "), t); + if (TrackManager::isProg(t)) { + main = false; + prog = true; + } + else + { + main=true; + prog=false; + } + singletrack=true; + if (main) TrackManager::setTrackPower(false, POWERMODE::ON, t); + if (prog) TrackManager::setTrackPower(true, POWERMODE::ON, t); + //CommandDistributor::broadcastPower(); + //TrackManager::streamTrackState(stream, t); + TrackManager::streamTrackState(NULL,t); + StringFormatter::send(stream, F("Track %d ON\n"), t); + return; + } + + else break; // will reply + } - } - else break; // will reply - } - TrackManager::setJoin(join); if (!singletrack) { + TrackManager::setJoin(join); if (main) TrackManager::setMainPower(POWERMODE::ON); if (prog) TrackManager::setProgPower(POWERMODE::ON); - } - else { - if (main) TrackManager::setTrackPower(false, POWERMODE::ON, t); - if (prog) { - TrackManager::setTrackPower(true, POWERMODE::ON, t); + CommandDistributor::broadcastPower(); + + return; } - } - CommandDistributor::broadcastPower(); - return; + } case '0': // POWEROFF <0 [MAIN | PROG] > { - bool main=false; - bool prog=false; - bool singletrack=false; - byte t=0; - if (params > 1) break; - if (params==0) { // All - main=true; - prog=true; - } - if (params==1) { - if (p[0]==HASH_KEYWORD_MAIN) { // <0 MAIN> - main=true; - } + bool main=false; + bool prog=false; + bool singletrack=false; + //byte t=0; + if (params > 1) break; + if (params==0) { // All + main=true; + prog=true; + } + if (params==1) { + if (p[0]==HASH_KEYWORD_MAIN) { // <0 MAIN> + main=true; + } #ifndef DISABLE_PROG - else if (p[0]==HASH_KEYWORD_PROG) { // <0 PROG> - prog=true; - } -#endif <= - else if (p[0] >= 'A' && p[0] <= 'H') { // <1 A-H> - t = (p[0] - 'A'); + else if (p[0]==HASH_KEYWORD_PROG) { // <0 PROG> + prog=true; + } +#endif + else if (p[0] >= 'A' && p[0] <= 'H') { // <1 A-H> + byte t = (p[0] - 'A'); //DIAG(F("Processing track - %d "), t); if (TrackManager::isProg(t)) { main = false; @@ -640,28 +644,33 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) main=true; prog=false; } - singletrack=true; - } - else break; // will reply - } - - TrackManager::setJoin(false); - if (!singletrack) { - if (main) TrackManager::setMainPower(POWERMODE::OFF); + singletrack=true; + TrackManager::setJoin(false); + if (main) TrackManager::setTrackPower(false, POWERMODE::OFF, t); if (prog) { + TrackManager::progTrackBoosted=false; // Prog track boost mode will not outlive prog track off + TrackManager::setTrackPower(true, POWERMODE::OFF, t); + CommandDistributor::broadcastPower(); + } + StringFormatter::send(stream, F("Track %d OFF\n"), t); + TrackManager::streamTrackState(NULL, t); + return; + } + + else break; // will reply + } + + if (!singletrack) { + TrackManager::setJoin(false); + + if (main) TrackManager::setMainPower(POWERMODE::OFF); + if (prog) { TrackManager::progTrackBoosted=false; // Prog track boost mode will not outlive prog track off TrackManager::setProgPower(POWERMODE::OFF); } - } - else { - if (main) TrackManager::setTrackPower(false, POWERMODE::OFF, t); - if (prog) { - TrackManager::progTrackBoosted=false; // Prog track boost mode will not outlive prog track off - TrackManager::setTrackPower(true, POWERMODE::OFF, t); - } + CommandDistributor::broadcastPower(); + return; } - CommandDistributor::broadcastPower(); - return; } case '!': // ESTOP ALL diff --git a/TrackManager.cpp b/TrackManager.cpp index d653645..f66fb4d 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -26,7 +26,8 @@ #include "MotorDriver.h" #include "DCCTimer.h" #include "DIAG.h" -#include"CommandDistributor.h" +#include "CommandDistributor.h" +#include "DCCEXParser.h" // Virtualised Motor shield multi-track hardware Interface #define FOR_EACH_TRACK(t) for (byte t=0;t<=lastTrack;t++) @@ -319,6 +320,7 @@ bool TrackManager::parseJ(Print *stream, int16_t params, int16_t p[]) FOR_EACH_TRACK(t) streamTrackState(stream,t); return true; + } p[0]-=HASH_KEYWORD_A; // convert A... to 0.... @@ -378,8 +380,10 @@ void TrackManager::streamTrackState(Print* stream, byte t) { default: break; // unknown, dont care } - if (stream) StringFormatter::send(stream,format,'A'+t, trackDCAddr[t]); + + if (stream) StringFormatter::send(stream,format,'A'+t, trackDCAddr[t]); else CommandDistributor::broadcastTrackState(format,'A'+t, trackDCAddr[t]); + } byte TrackManager::nextCycleTrack=MAX_TRACKS; @@ -416,47 +420,16 @@ std::vectorTrackManager::getMainDrivers() { void TrackManager::setPower2(bool setProg,POWERMODE mode) { if (!setProg) mainPowerGuess=mode; FOR_EACH_TRACK(t) { + TrackManager::setTrackPower(setProg, mode, t); - // MotorDriver * driver=track[t]; - // if (!driver) continue; - // switch (track[t]->getMode()) { - // case TRACK_MODE_MAIN: - // if (setProg) break; - // // toggle brake before turning power on - resets overcurrent error - // // on the Pololu board if brake is wired to ^D2. - // // XXX see if we can make this conditional - // driver->setBrake(true); - // driver->setBrake(false); // DCC runs with brake off - // driver->setPower(mode); - // break; - // case TRACK_MODE_DC: - // case TRACK_MODE_DCX: - // if (setProg) break; - // driver->setBrake(true); // DC starts with brake on - // applyDCSpeed(t); // speed match DCC throttles - // driver->setPower(mode); - // break; - // case TRACK_MODE_PROG: - // if (!setProg) break; - // driver->setBrake(true); - // driver->setBrake(false); - // driver->setPower(mode); - // break; - // case TRACK_MODE_EXT: - // driver->setBrake(true); - // driver->setBrake(false); - // driver->setPower(mode); - // break; - // case TRACK_MODE_NONE: - // break; - // } } + return; } void TrackManager::setTrackPower(bool setProg, POWERMODE mode, byte thistrack) { - DIAG(F("SetTrackPower Processing Track %d"), thistrack); + //DIAG(F("SetTrackPower Processing Track %d"), thistrack); MotorDriver * driver=track[thistrack]; if (!driver) return; @@ -491,16 +464,22 @@ void TrackManager::setTrackPower(bool setProg, POWERMODE mode, byte thistrack) { case TRACK_MODE_NONE: break; } - if (mode == POWERMODE::ON) {DIAG(F("Power Track %d ON"), thistrack);} - else {DIAG(F("Power Track %d OFF"), thistrack);} - //driver->setPower(mode); } + void TrackManager::reportPowerChange(Print* stream, byte thistrack) { + // This function is for backward JMRI compatibility only + // It reports the first track only, as main, regardless of track settings. + // + int maxCurrent=track[0]->raw2mA(track[0]->getRawCurrentTripValue()); + StringFormatter::send(stream, F("\n"), + track[0]->raw2mA(track[0]->getCurrentRaw(false)), maxCurrent, maxCurrent); +} + POWERMODE TrackManager::getProgPower() { FOR_EACH_TRACK(t) if (track[t]->getMode()==TRACK_MODE_PROG) - return track[t]->getPower(); + return track[t]->getPower(); return POWERMODE::OFF; } diff --git a/TrackManager.h b/TrackManager.h index 39f708a..cf2d54b 100644 --- a/TrackManager.h +++ b/TrackManager.h @@ -64,12 +64,12 @@ class TrackManager { #ifdef ARDUINO_ARCH_ESP32 static std::vectorgetMainDrivers(); #endif - static void setTrackPower(bool setProg, POWERMODE mode, byte thistrack); + static void setPower2(bool progTrack,POWERMODE mode); static void setPower(POWERMODE mode) {setMainPower(mode); setProgPower(mode);} static void setMainPower(POWERMODE mode) {setPower2(false,mode);} static void setProgPower(POWERMODE mode) {setPower2(true,mode);} - + static void setTrackPower(bool setProg, POWERMODE mode, byte thistrack); static const int16_t MAX_TRACKS=8; @@ -84,6 +84,7 @@ class TrackManager { static void sampleCurrent(); static void reportGauges(Print* stream); static void reportCurrent(Print* stream); + static void reportPowerChange(Print* stream, byte thistrack); static void reportObsoleteCurrent(Print* stream); static void streamTrackState(Print* stream, byte t); static bool isPowerOn(byte t);