From b4a3b503bcc3f6c5b6dd4ac3eeec5e8e65feb616 Mon Sep 17 00:00:00 2001 From: Neil McKechnie Date: Fri, 20 Aug 2021 00:07:50 +0100 Subject: [PATCH] Turnout notification handling enhanced. Ensure that the message is sent on the serial USB (to JMRI) whenever the turnout is closed or thrown, even if the request didn't originate on the serial USB. --- DCCEXParser.cpp | 10 +++++++--- Turnouts.cpp | 7 ++++++- Turnouts.h | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 388cf60..6779178 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -679,25 +679,29 @@ bool DCCEXParser::parseT(Print *stream, int16_t params, int16_t p[]) // turnout 1 or T=THROW, 0 or C=CLOSE case 1: case 0x54: // 1 or T if (!Turnout::setClosed(p[0], false)) return false; + p[1] = 1; break; case 0: case 0x43: // 0 or C if (!Turnout::setClosed(p[0], true)) return false; + p[1] = 0; break; #else // turnout 0 or T=THROW,1 or C=CLOSE case 0: case 0x54: // 0 or T if (!Turnout::setClosed(p[0], false)) return false; + p[1] = 0; break; case 1: case 0x43: // 1 or C if (!Turnout::setClosed(p[0], true)) return false; + p[1] = 1; break; #endif default: return false; } - // Send acknowledgement to caller, and to Serial. - StringFormatter::send(stream, F("\n"), p[0], p[1]); - if (stream != &Serial) StringFormatter::send(Serial, F("\n"), p[0], p[1]); + // Send acknowledgement to caller if the command was not received over Serial (acknowledgement + // messages on Serial are sent by the Turnout class). + if (stream != &Serial) StringFormatter::send(stream, F("\n"), p[0], p[1]); return true; default: // Anything else is some kind of create function. diff --git a/Turnouts.cpp b/Turnouts.cpp index 0869a58..8a77f57 100644 --- a/Turnouts.cpp +++ b/Turnouts.cpp @@ -125,7 +125,12 @@ const int16_t HASH_KEYWORD_VPIN=-415; #if defined(RMFT_ACTIVE) RMFT2::turnoutEvent(id, closeFlag); - #endif + #endif + + // Send message to JMRI etc. over Serial USB. This is done here + // to ensure that the message is sent when the turnout operation + // is not initiated by a Serial command. + StringFormatter::send(Serial, F("\n"), id, closeFlag); return ok; } diff --git a/Turnouts.h b/Turnouts.h index 6f6c408..6659c24 100644 --- a/Turnouts.h +++ b/Turnouts.h @@ -466,7 +466,8 @@ public: } bool activate(bool close) override { - LCN::send('T', _turnoutData.id, close); + // Assume that the LCN command still uses 1 for throw and 0 for close... + LCN::send('T', _turnoutData.id, !close); // The _turnoutData.closed flag should be updated by a message from the LCN master, later. return true; }