From 26dffa3be318d5ba01c424067cc2c2e4d7d97c60 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Wed, 3 Jun 2020 09:42:17 +0100 Subject: [PATCH] Added DCC api readCVBit and getLocoId Sanity check needed --- DCC.cpp | 27 +++++++++++++++++++++++++++ DCC.h | 2 ++ 2 files changed, 29 insertions(+) diff --git a/DCC.cpp b/DCC.cpp index 79e42a3..722a14f 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -130,6 +130,13 @@ bool DCC::verifyCVBit(int cv, byte bNum, bool bValue) { return DCCWaveform::progTrack.schedulePacketWithAck(message, sizeof(message), 5); // NMRA recommends 6 write or reset packets for decoder recovery time } +int DCC::readCVBit(int cv, byte bNum) { + if (bNum>=8) return -1; + if (verifyCVBit(cv, bNum,true)) return 1; + // failed verify might be a zero, or an error so must check again + if (verifyCVBit(cv, bNum,false)) return 0; + return -1; +} int DCC::readCV(int cv) { @@ -164,6 +171,26 @@ void DCC::loop() { } } +int DCC::getLocoId() { + switch (readCVBit(29,5)) { + case 1: + // long address : get CV#17 and CV#18 + { + int cv17=readCV(17); + + if (cv17<0) break; + int cv18=readCV(18); + if (cv18<0) break; + return cv18 + ((cv17 - 192) <<8); + } + case 0: // short address in CV1 + return readCV(1); + default: // No response or loco + break; + } + return -1; +} + ///// Private helper functions below here ///////////////////// byte DCC::cv1(byte opcode, int cv) { diff --git a/DCC.h b/DCC.h index 041e576..7f9e98a 100644 --- a/DCC.h +++ b/DCC.h @@ -13,6 +13,7 @@ class DCC { // Public DCC API functions static void setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection); static int readCV(int cv); + static int readCVBit(int cv, byte bNum); // -1 for error static bool writeCVByte(int cv, byte bValue) ; static bool verifyCVByte(int cv,byte bValue); static bool writeCVBit(int cv, byte bNum, bool bValue); @@ -23,6 +24,7 @@ class DCC { static void setFunction( int cab, byte fByte); static void setAccessory(int aAdd, byte aNum, bool activate) ; static bool writeTextPacket( byte *b, int nBytes); + static int getLocoId(); private: struct LOCO {