From bec8aea5a548fdd6a3c80be5335bc39067fa3a6d Mon Sep 17 00:00:00 2001 From: Asbelos Date: Mon, 6 Mar 2023 11:57:14 +0000 Subject: [PATCH] TM Broadcasts TM changes will trigger TM state broadcasts --- .gitignore | 1 + CommandDistributor.cpp | 4 +++ CommandDistributor.h | 1 + TrackManager.cpp | 66 ++++++++++++++++++++++-------------------- TrackManager.h | 2 ++ version.h | 3 +- 6 files changed, 45 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index c8e40c2..8f01818 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ my*.h !my*.example.h .vscode/extensions.json .vscode/extensions.json +compile_commands.json diff --git a/CommandDistributor.cpp b/CommandDistributor.cpp index 0c9185d..ad11b22 100644 --- a/CommandDistributor.cpp +++ b/CommandDistributor.cpp @@ -233,3 +233,7 @@ void CommandDistributor::broadcastText(const FSH * msg) { broadcastReply(WITHROTTLE_TYPE, F("Hm%S\n"), msg); #endif } + +void CommandDistributor::broadcastTrackState(FSH* format,byte trackLetter,int16_t dcAddr) { + broadcastReply(COMMAND_TYPE, format,trackLetter,dcAddr); +} diff --git a/CommandDistributor.h b/CommandDistributor.h index bbbc44c..a5751e9 100644 --- a/CommandDistributor.h +++ b/CommandDistributor.h @@ -51,6 +51,7 @@ public : static int16_t retClockTime(); static void broadcastPower(); static void broadcastText(const FSH * msg); + static void broadcastTrackState(FSH* format,byte trackLetter,int16_t dcAddr); template static void broadcastReply(clientType type, Targs... msg); static void forget(byte clientId); diff --git a/TrackManager.cpp b/TrackManager.cpp index 33d1d8a..633ed06 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -25,6 +25,7 @@ #include "MotorDriver.h" #include "DCCTimer.h" #include "DIAG.h" +#include"CommandDistributor.h" // Virtualised Motor shield multi-track hardware Interface #define FOR_EACH_TRACK(t) for (byte t=0;t<=lastTrack;t++) @@ -203,6 +204,7 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr track[t]->setPower(POWERMODE::OFF); trackMode[t]=TRACK_MODE_OFF; track[t]->makeProgTrack(false); // revoke prog track special handling + streamTrackState(NULL,t); } track[trackToSet]->makeProgTrack(true); // set for prog track special handling } else { @@ -210,7 +212,8 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr } trackMode[trackToSet]=mode; trackDCAddr[trackToSet]=dcAddr; - + streamTrackState(NULL,trackToSet); + // When a track is switched, we must clear any side effects of its previous // state, otherwise trains run away or just dont move. @@ -290,36 +293,7 @@ bool TrackManager::parseJ(Print *stream, int16_t params, int16_t p[]) if (params==0) { // <=> List track assignments FOR_EACH_TRACK(t) - if (track[t]!=NULL) { - StringFormatter::send(stream,F("<= %c "),'A'+t); - switch(trackMode[t]) { - case TRACK_MODE_MAIN: - StringFormatter::send(stream,F("MAIN")); - if (track[t]->trackPWM) - StringFormatter::send(stream,F("+")); - break; - case TRACK_MODE_PROG: - StringFormatter::send(stream,F("PROG")); - if (track[t]->trackPWM) - StringFormatter::send(stream,F("+")); - break; - case TRACK_MODE_OFF: - StringFormatter::send(stream,F("OFF")); - break; - case TRACK_MODE_EXT: - StringFormatter::send(stream,F("EXT")); - break; - case TRACK_MODE_DC: - StringFormatter::send(stream,F("DC %d"),trackDCAddr[t]); - break; - case TRACK_MODE_DCX: - StringFormatter::send(stream,F("DCX %d"),trackDCAddr[t]); - break; - default: - break; // unknown, dont care - } - StringFormatter::send(stream,F(">\n")); - } + streamTrackState(stream,t); return true; } @@ -349,6 +323,36 @@ bool TrackManager::parseJ(Print *stream, int16_t params, int16_t p[]) return false; } +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]) { + case TRACK_MODE_MAIN: + format=F("<= %c MAIN>\n"); + break; + case TRACK_MODE_PROG: + format=F("<= %c PROG>\n"); + break; + case TRACK_MODE_OFF: + format=F("<= %c OFF>\n"); + break; + case TRACK_MODE_EXT: + format=F("<= %c EXT>\n"); + break; + case TRACK_MODE_DC: + format=F("<= %c DC %d>\n"); + break; + case TRACK_MODE_DCX: + format=F("<= %c DCX %d>\n"); + break; + default: + break; // unknown, dont care + } + if (stream) StringFormatter::send(stream,format,'A'+t,trackDCAddr[t]); + else CommandDistributor::broadcastTrackState(format,'A'+t,trackDCAddr[t]); +} + byte TrackManager::nextCycleTrack=MAX_TRACKS; void TrackManager::loop() { diff --git a/TrackManager.h b/TrackManager.h index 4a548cc..ef4a47c 100644 --- a/TrackManager.h +++ b/TrackManager.h @@ -80,6 +80,8 @@ class TrackManager { static void reportGauges(Print* stream); static void reportCurrent(Print* stream); static void reportObsoleteCurrent(Print* stream); + static void streamTrackState(Print* stream, byte t); + static int16_t joinRelay; static bool progTrackSyncMain; // true when prog track is a siding switched to main static bool progTrackBoosted; // true when prog track is not current limited diff --git a/version.h b/version.h index eebf8ed..789ef2c 100644 --- a/version.h +++ b/version.h @@ -4,7 +4,8 @@ #include "StringFormatter.h" -#define VERSION "4.2.21" +#define VERSION "4.2.22" +// 4.2.22 - Implement broadcast of Track Manager changes // 4.2.21 - Implement non-blocking I2C for EX-IOExpander device driver // 4.2.20 - & commands for multi-track gauges // - Reinstate but remember its a bit useless when TM involved.