From 5872659ff288f752fafe1640a819bf39ed2e27d5 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Thu, 18 Jun 2020 19:36:37 +0100 Subject: [PATCH] Command Filter and some extra APIs. --- CVReader.ino | 9 +++++++++ DCC.cpp | 8 ++++++++ DCC.h | 7 ++++++- DCCEXParser.cpp | 16 ++++++++++++---- DCCEXParser.h | 8 +++++++- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CVReader.ino b/CVReader.ino index bb676a1..1dda384 100644 --- a/CVReader.ino +++ b/CVReader.ino @@ -18,6 +18,14 @@ void myCallback(int result) { DIAG(F("\n getting Loco Id callback result=%d"),result); } + +void myFilter(Stream & stream, byte & opcode, byte & paramCount, int p[]) { + if (opcode=='T') { + DIAG(F("\nStop messing with Turnouts!")); + opcode=0; // tell parssr to ignore it + } +} + DCCEXParser serialParser; void setup() { @@ -28,6 +36,7 @@ void setup() { DCC::getLocoId(myCallback); // myCallback will be called with the result DIAG(F("\n===== DCC::getLocoId has returned, but wont be executed until we are in loop() ======\n")); DIAG(F("\nReady for JMRI commands\n")); + DCCEXParser::setFilter(myFilter); } void loop() { diff --git a/DCC.cpp b/DCC.cpp index 32c131f..67e1857 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -234,6 +234,14 @@ void DCC::getLocoId(ACK_CALLBACK callback) { ackManagerSetup(0,0, LOCO_ID_PROG, callback); } +void DCC::forgetLoco(int cab) { // removes any speed reminders for this loco + for (int i=0;i to send - switch(com[0]) { - + switch(opcode) { + case '\0': return; // filterCallback asked us to ignore case 't': // THROTTLE DCC::setThrottle(p[1],p[2],p[3]); StringFormatter::send(stream,F(""), p[0], p[2],p[3]); diff --git a/DCCEXParser.h b/DCCEXParser.h index 23c1b47..6246936 100644 --- a/DCCEXParser.h +++ b/DCCEXParser.h @@ -1,15 +1,20 @@ #ifndef DCCEXParser_h #define DCCEXParser_h #include + +typedef void (*FILTER_CALLBACK)(Print & stream, byte & opcode, byte & paramCount, int p[]); + struct DCCEXParser { DCCEXParser(); void loop(Stream & pstream); void parse(Print & stream, const char * command); void flush(); + static void setFilter(FILTER_CALLBACK filter); + static const int MAX_PARAMS=10; // Must not exceed this + private: - static const int MAX_PARAMS=10; // longest command sent in static const int MAX_BUFFER=50; // longest command sent in byte bufferLength=0; bool inCommandPayload=false; @@ -28,6 +33,7 @@ struct DCCEXParser static void callback_W(int result); static void callback_B(int result); static void callback_R(int result); + static FILTER_CALLBACK filterCallback; };