From a9ce9101e68b99be6913d23a15ad5994c435de0e Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 20 Aug 2022 18:15:18 +0200 Subject: [PATCH] return success/fail from and command handling (setFn, parsef) --- DCC.cpp | 10 +++++--- DCC.h | 2 +- DCCEXParser.cpp | 68 +++++++++++++++++++++++-------------------------- DCCEXParser.h | 2 +- 4 files changed, 40 insertions(+), 42 deletions(-) diff --git a/DCC.cpp b/DCC.cpp index 8524308..7759516 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -163,8 +163,9 @@ bool DCC::getThrottleDirection(int cab) { } // Set function to value on or off -void DCC::setFn( int cab, int16_t functionNumber, bool on) { - if (cab<=0 ) return; +bool DCC::setFn( int cab, int16_t functionNumber, bool on) { + if (cab<=0 ) return false; + if (functionNumber < 0) return false; if (functionNumber>28) { //non reminding advanced binary bit set @@ -183,11 +184,11 @@ void DCC::setFn( int cab, int16_t functionNumber, bool on) { b[nB++] = functionNumber >>7 ; // high order bits } DCCWaveform::mainTrack.schedulePacket(b, nB, 4); - return; + return true; } int reg = lookupSpeedTable(cab); - if (reg<0) return; + if (reg<0) return false; // Take care of functions: // Set state of function @@ -202,6 +203,7 @@ void DCC::setFn( int cab, int16_t functionNumber, bool on) { updateGroupflags(speedTable[reg].groupFlags, functionNumber); CommandDistributor::broadcastLoco(reg); } + return true; } // Flip function state diff --git a/DCC.h b/DCC.h index b45c45e..46b3b83 100644 --- a/DCC.h +++ b/DCC.h @@ -62,7 +62,7 @@ public: static void writeCVByteMain(int cab, int cv, byte bValue); static void writeCVBitMain(int cab, int cv, byte bNum, bool bValue); static void setFunction(int cab, byte fByte, byte eByte); - static void setFn(int cab, int16_t functionNumber, bool on); + static bool setFn(int cab, int16_t functionNumber, bool on); static void changeFn(int cab, int16_t functionNumber); static int getFn(int cab, int16_t functionNumber); static uint32_t getFunctionMap(int cab); diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index e986406..21a9d3d 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -546,8 +546,8 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) if(params!=3) break; if (Diag::CMD) DIAG(F("Setting loco %d F%d %S"), p[0], p[1], p[2] ? F("ON") : F("OFF")); - DCC::setFn(p[0], p[1], p[2] == 1); - return; + if (DCC::setFn(p[0], p[1], p[2] == 1)) return; + break; #if WIFI_ON case '+': // Complex Wifi interface command (not usual parse) @@ -689,43 +689,39 @@ bool DCCEXParser::parseZ(Print *stream, int16_t params, int16_t p[]) //=================================== bool DCCEXParser::parsef(Print *stream, int16_t params, int16_t p[]) { - // JMRI sends this info in DCC message format but it's not exactly - // convenient for other processing - if (params == 2) - { - byte instructionField = p[1] & 0xE0; // 1110 0000 - if (instructionField == 0x80) // 1000 0000 Function group 1 - { - // Shuffle bits from order F0 F4 F3 F2 F1 to F4 F3 F2 F1 F0 - byte normalized = (p[1] << 1 & 0x1e) | (p[1] >> 4 & 0x01); - funcmap(p[0], normalized, 0, 4); - } - else if (instructionField == 0xA0) // 1010 0000 Function group 2 - { - if (p[1] & 0x10) // 0001 0000 Bit selects F5toF8 / F9toF12 - funcmap(p[0], p[1], 5, 8); - else - funcmap(p[0], p[1], 9, 12); - } - } - if (params == 3) - { - if (p[1] == 222) - funcmap(p[0], p[2], 13, 20); - else if (p[1] == 223) - funcmap(p[0], p[2], 21, 28); - } - (void)stream; // NO RESPONSE - return true; + // JMRI sends this info in DCC message format but it's not exactly + // convenient for other processing + if (params == 2) { + byte instructionField = p[1] & 0xE0; // 1110 0000 + if (instructionField == 0x80) { // 1000 0000 Function group 1 + // Shuffle bits from order F0 F4 F3 F2 F1 to F4 F3 F2 F1 F0 + byte normalized = (p[1] << 1 & 0x1e) | (p[1] >> 4 & 0x01); + return (funcmap(p[0], normalized, 0, 4)); + } else if (instructionField == 0xA0) { // 1010 0000 Function group 2 + if (p[1] & 0x10) // 0001 0000 Bit selects F5toF8 / F9toF12 + return (funcmap(p[0], p[1], 5, 8)); + else + return (funcmap(p[0], p[1], 9, 12)); + } + } + if (params == 3) { + if (p[1] == 222) { + return (funcmap(p[0], p[2], 13, 20)); + } else if (p[1] == 223) { + return (funcmap(p[0], p[2], 21, 28)); + } + } + (void)stream; // NO RESPONSE + return false; } -void DCCEXParser::funcmap(int16_t cab, byte value, byte fstart, byte fstop) +bool DCCEXParser::funcmap(int16_t cab, byte value, byte fstart, byte fstop) { - for (int16_t i = fstart; i <= fstop; i++) - { - DCC::setFn(cab, i, value & 1); - value >>= 1; - } + for (int16_t i = fstart; i <= fstop; i++) { + if (! DCC::setFn(cab, i, value & 1)) return false; + value >>= 1; + } + return true; } //=================================== diff --git a/DCCEXParser.h b/DCCEXParser.h index 797eab0..bb05ebf 100644 --- a/DCCEXParser.h +++ b/DCCEXParser.h @@ -71,7 +71,7 @@ struct DCCEXParser static FILTER_CALLBACK filterCallback; static FILTER_CALLBACK filterRMFTCallback; static AT_COMMAND_CALLBACK atCommandCallback; - static void funcmap(int16_t cab, byte value, byte fstart, byte fstop); + static bool funcmap(int16_t cab, byte value, byte fstart, byte fstop); static void sendFlashList(Print * stream,const int16_t flashList[]); };