From cd46d3c9e07a4641843a154046cc7482504932fe Mon Sep 17 00:00:00 2001 From: Colin Murdoch Date: Sat, 21 Jan 2023 10:18:54 +0000 Subject: [PATCH] Remove #ifdef and merge calcs Remove #idfef statements and merge duplicate routines into CommandDistributor --- CommandDistributor.cpp | 40 +++++++++++++++++++++++++++++++-- CommandDistributor.h | 6 +++-- DCCEXParser.cpp | 29 +++++------------------- EXRAIL2.cpp | 9 +------- EXRAIL2.h | 6 ----- EXRAIL2MacroReset.h | 4 ---- EXRAILMacros.h | 4 ---- IO_EXFastclock.h | 50 ++++++++++++++++++------------------------ 8 files changed, 69 insertions(+), 79 deletions(-) diff --git a/CommandDistributor.cpp b/CommandDistributor.cpp index 3fe1e96..652d852 100644 --- a/CommandDistributor.cpp +++ b/CommandDistributor.cpp @@ -29,6 +29,11 @@ #include "DCCWaveform.h" #include "DCC.h" #include "TrackManager.h" +#include "StringFormatter.h" + +// variables to hold clock time +int16_t lastclocktime; +int8_t lastclockrate; #if WIFI_ON || ETHERNET_ON || defined(SERIAL1_COMMANDS) || defined(SERIAL2_COMMANDS) || defined(SERIAL3_COMMANDS) @@ -155,7 +160,6 @@ void CommandDistributor::broadcastTurnout(int16_t id, bool isClosed ) { #endif } -#ifdef USEFASTCLOCK void CommandDistributor::broadcastClockTime(int16_t time, int8_t rate) { // The JMRI clock command is of the form : PFT65871<;>4 // The CS broadcast is of the form "%d\n"), time*60, rate); #endif } -#endif + +void CommandDistributor::setClockTime(int16_t clocktime, int8_t clockrate, byte opt) { + // opt - case 1 save the latest time if changed + // case 2 broadcast the time when requested + // case 3 display latest time + switch (opt) + { + case 1: + if (clocktime != lastclocktime){ + if (Diag::CMD) { + DIAG(F("Clock Command Received")); + DIAG(F("Received Clock Time is: %d at rate: %d"), clocktime, clockrate); + } + LCD(6,F("Clk Time:%d Sp %d"), clocktime, clockrate); + // look for an event for this time + RMFT2::clockEvent(clocktime,1); + // Now tell everyone else what the time is. + CommandDistributor::broadcastClockTime(clocktime, clockrate); + lastclocktime = clocktime; + lastclockrate = clockrate; + } + return; + + case 2: + CommandDistributor::broadcastClockTime(lastclocktime, lastclockrate); + return; + } + +} + +int16_t CommandDistributor::retClockTime() { + return lastclocktime; +} void CommandDistributor::broadcastLoco(byte slot) { DCC::LOCO * sp=&DCC::speedTable[slot]; diff --git a/CommandDistributor.h b/CommandDistributor.h index 3df477c..bbbc44c 100644 --- a/CommandDistributor.h +++ b/CommandDistributor.h @@ -25,6 +25,7 @@ #include "RingStream.h" #include "StringBuffer.h" #include "defines.h" +#include "EXRAIL2.h" #if WIFI_ON | ETHERNET_ON // Command Distributor must handle a RingStream of clients @@ -45,13 +46,14 @@ public : static void broadcastLoco(byte slot); static void broadcastSensor(int16_t id, bool value); static void broadcastTurnout(int16_t id, bool isClosed); -#ifdef USEFASTCLOCK static void broadcastClockTime(int16_t time, int8_t rate); -#endif + static void setClockTime(int16_t time, int8_t rate, byte opt); + static int16_t retClockTime(); static void broadcastPower(); static void broadcastText(const FSH * msg); template static void broadcastReply(clientType type, Targs... msg); static void forget(byte clientId); + }; #endif diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 3898f95..5c0cb84 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -97,10 +97,6 @@ Print *DCCEXParser::stashStream = NULL; RingStream *DCCEXParser::stashRingStream = NULL; byte DCCEXParser::stashTarget=0; -#ifdef USEFASTCLOCK -int16_t lastclocktime = 0; -#endif - // This is a JMRI command parser. // It doesnt know how the string got here, nor how it gets back. // It knows nothing about hardware or tracks... it just parses strings and @@ -574,34 +570,19 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) case 'J' : // throttle info access { -#ifdef USEFASTCLOCK if ((params<1) | (params>3)) break; // -#endif -#ifndef USEFASTCLOCK - if ((params<1) | (params>2)) break; // -#endif + //if ((params<1) | (params>2)) break; // int16_t id=(params==2)?p[1]:0; switch(p[0]) { -#ifdef USEFASTCLOCK case HASH_KEYWORD_C: // sets time and speed if (params==1) { // returns latest time - StringFormatter::send(stream, F("\n"), lastclocktime); + int16_t x = CommandDistributor::retClockTime(); + StringFormatter::send(stream, F("\n"), x); return; } - if (p[1] != lastclocktime){ - if (Diag::CMD) { - DIAG(F("Clock Command Received")); - DIAG(F("Received Clock Time is: %d at rate: %d"), p[1], p[2]); - } - LCD(6,F("Clk Time:%d Sp %d"), p[1], p[2]); - //LCD(7,F("Clock Speed: %d"), p[2]); - RMFT2::clockEvent(p[1],1); - // Now tell everyone else what the time is. - CommandDistributor::broadcastClockTime(p[1], p[2]); - lastclocktime = p[1]; - } + CommandDistributor::setClockTime(p[1], p[2], 1); return; -#endif + case HASH_KEYWORD_A: // returns automations/routes StringFormatter::send(stream, F(" diff --git a/EXRAIL2.cpp b/EXRAIL2.cpp index 1b5b51e..75ec63b 100644 --- a/EXRAIL2.cpp +++ b/EXRAIL2.cpp @@ -92,9 +92,7 @@ LookList * RMFT2::onRedLookup=NULL; LookList * RMFT2::onAmberLookup=NULL; LookList * RMFT2::onGreenLookup=NULL; LookList * RMFT2::onChangeLookup=NULL; -#ifdef USEFASTCLOCK LookList * RMFT2::onClockLookup=NULL; -#endif #define GET_OPCODE GETHIGHFLASH(RMFT2::RouteCode,progCounter) #define SKIPOP progCounter+=3 @@ -178,9 +176,8 @@ LookList* RMFT2::LookListLoader(OPCODE op1, OPCODE op2, OPCODE op3) { onAmberLookup=LookListLoader(OPCODE_ONAMBER); onGreenLookup=LookListLoader(OPCODE_ONGREEN); onChangeLookup=LookListLoader(OPCODE_ONCHANGE); - #ifdef USEFASTCLOCK onClockLookup=LookListLoader(OPCODE_ONTIME); - #endif + // Second pass startup, define any turnouts or servos, set signals red // add sequences onRoutines to the lookups @@ -981,9 +978,7 @@ void RMFT2::loop2() { case OPCODE_ONAMBER: case OPCODE_ONGREEN: case OPCODE_ONCHANGE: - #ifdef USEFASTCLOCK case OPCODE_ONTIME: - #endif break; @@ -1116,14 +1111,12 @@ void RMFT2::changeEvent(int16_t vpin, bool change) { if (change) handleEvent(F("CHANGE"),onChangeLookup,vpin); } -#ifdef USEFASTCLOCK void RMFT2::clockEvent(int16_t clocktime, bool change) { // Hunt for an ONTIME for this time if (Diag::CMD) DIAG(F("Looking for clock event at : %d"), clocktime); if (change) handleEvent(F("CLOCK"),onClockLookup,clocktime); } -#endif void RMFT2::handleEvent(const FSH* reason,LookList* handlers, int16_t id) { int pc= handlers->find(id); diff --git a/EXRAIL2.h b/EXRAIL2.h index 7f04458..69fd382 100644 --- a/EXRAIL2.h +++ b/EXRAIL2.h @@ -55,10 +55,8 @@ enum OPCODE : byte {OPCODE_THROW,OPCODE_CLOSE, OPCODE_SET_TRACK, OPCODE_ONRED,OPCODE_ONAMBER,OPCODE_ONGREEN, OPCODE_ONCHANGE, -#ifdef USEFASTCLOCK OPCODE_ONCLOCKTIME, OPCODE_ONTIME, -#endif // OPcodes below this point are skip-nesting IF operations // placed here so that they may be skipped as a group @@ -120,9 +118,7 @@ class LookList { static void turnoutEvent(int16_t id, bool closed); static void activateEvent(int16_t addr, bool active); static void changeEvent(int16_t id, bool change); - #ifdef USEFASTCLOCK static void clockEvent(int16_t clocktime, bool change); - #endif static const int16_t SERVO_SIGNAL_FLAG=0x4000; static const int16_t ACTIVE_HIGH_SIGNAL_FLAG=0x2000; static const int16_t DCC_SIGNAL_FLAG=0x1000; @@ -180,9 +176,7 @@ private: static LookList * onAmberLookup; static LookList * onGreenLookup; static LookList * onChangeLookup; -#ifdef USEFASTCLOCK static LookList * onClockLookup; -#endif // Local variables - exist for each instance/task RMFT2 *next; // loop chain diff --git a/EXRAIL2MacroReset.h b/EXRAIL2MacroReset.h index 409cdcd..a5e6d90 100644 --- a/EXRAIL2MacroReset.h +++ b/EXRAIL2MacroReset.h @@ -86,10 +86,8 @@ #undef ONDEACTIVATE #undef ONDEACTIVATEL #undef ONCLOSE -#ifdef USEFASTCLOCK #undef ONTIME #undef ONCLOCKTIME -#endif #undef ONGREEN #undef ONRED #undef ONTHROW @@ -202,10 +200,8 @@ #define ONACTIVATE(addr,subaddr) #define ONACTIVATEL(linear) #define ONAMBER(signal_id) -#ifdef USEFASTCLOCK #define ONTIME(value) #define ONCLOCKTIME(hours,mins) -#endif #define ONDEACTIVATE(addr,subaddr) #define ONDEACTIVATEL(linear) #define ONCLOSE(turnout_id) diff --git a/EXRAILMacros.h b/EXRAILMacros.h index 0d807d1..2ffbc75 100644 --- a/EXRAILMacros.h +++ b/EXRAILMacros.h @@ -55,11 +55,9 @@ // helper macro for turnout description as HIDDEN #define HIDDEN "\x01" -#ifdef USEFASTCLOCK // helper macro to strip leading zeros off time inputs // (10#mins)%100) #define STRIP_ZERO(value) 10##value%100 -#endif // Pass 1 Implements aliases #include "EXRAIL2MacroReset.h" @@ -303,10 +301,8 @@ const HIGHFLASH int16_t RMFT2::SignalDefinitions[] = { #define ONACTIVATEL(linear) OPCODE_ONACTIVATE,V(linear+3), #define ONAMBER(signal_id) OPCODE_ONAMBER,V(signal_id), #define ONCLOSE(turnout_id) OPCODE_ONCLOSE,V(turnout_id), -#ifdef USEFASTCLOCK #define ONTIME(value) OPCODE_ONTIME,V(value), #define ONCLOCKTIME(hours,mins) OPCODE_ONTIME,V((STRIP_ZERO(hours)*60)+STRIP_ZERO(mins)), -#endif #define ONDEACTIVATE(addr,subaddr) OPCODE_ONDEACTIVATE,V(addr<<2|subaddr), #define ONDEACTIVATEL(linear) OPCODE_ONDEACTIVATE,V(linear+3), #define ONGREEN(signal_id) OPCODE_ONGREEN,V(signal_id), diff --git a/IO_EXFastclock.h b/IO_EXFastclock.h index 22bb94e..0e1bf76 100644 --- a/IO_EXFastclock.h +++ b/IO_EXFastclock.h @@ -45,7 +45,7 @@ public: addDevice(this); } -static void EXFastClock::create(uint8_t _I2CAddress) { +static void create(uint8_t _I2CAddress) { DIAG(F("Checking for Clock")); // Start by assuming we will find the clock @@ -53,6 +53,7 @@ static void EXFastClock::create(uint8_t _I2CAddress) { // Returns I2C_STATUS_OK (0) if OK, or error code. uint8_t _checkforclock = I2CManager.checkAddress(_I2CAddress); DIAG(F("Clock check result - %d"), _checkforclock); + // XXXX change thistosave2 bytes if (_checkforclock == 0) { FAST_CLOCK_EXISTS = true; //DIAG(F("I2C Fast Clock found at x%x"), _I2CAddress); @@ -67,11 +68,8 @@ static void EXFastClock::create(uint8_t _I2CAddress) { } private: - //uint8_t _I2CAddress; - uint16_t _clocktime; - uint8_t _clockrate; - uint16_t _previousclocktime; - unsigned long _lastchecktime; +uint8_t _I2CAddress; + // Initialisation of Fastclock void _begin() override { @@ -98,39 +96,33 @@ void _loop(unsigned long currentMicros) override{ if (FAST_CLOCK_EXISTS==true) { uint8_t readBuffer[3]; byte a,b; - #if defined(EXRAIL_ACTIVE) + #ifdef EXRAIL_ACTIVE I2CManager.read(_I2CAddress, readBuffer, 3); + // XXXX change this to save a few bytes a = readBuffer[0]; b = readBuffer[1]; - _clocktime = (a << 8) + b; - _clockrate = readBuffer[2]; + //_clocktime = (a << 8) + b; + //_clockrate = readBuffer[2]; - if (_clocktime != _previousclocktime) { - _previousclocktime = _clocktime; - //if (Diag::CMD) - // DIAG(F("Received Clock Time is: %d at rate: %d"), _clocktime, _clockrate); - LCD(6,F(("Clk Time:%d Sp %d")), _clocktime, _clockrate); - RMFT2::clockEvent(_clocktime,1); - // Now tell everyone else what the time is. - CommandDistributor::broadcastClockTime(_clocktime, _clockrate); - - // As the maximum clock increment is 2 seconds delay a bit - say 1 sec. - delayUntil(currentMicros + 1000000); // Wait 1000ms before checking again, + CommandDistributor::setClockTime(((a << 8) + b), readBuffer[2], 1); + //setClockTime(int16_t clocktime, int8_t clockrate, byte opt); + + // As the minimum clock increment is 2 seconds delay a bit - say 1 sec. + // Clock interval is 60/ clockspeed i.e 60/b seconds + delayUntil(currentMicros + ((60/b) * 1000000)); } - _lastchecktime = currentMicros; - + #endif - } - -} -// Display EX-FastClock device driver info. -void _display() { - DIAG(F("FastCLock on I2C:x%x - %S"), _I2CAddress, (_deviceState==DEVSTATE_FAILED) ? F("OFFLINE") : F("")); -} + + // Display EX-FastClock device driver info. + void _display() { + DIAG(F("FastCLock on I2C:x%x - %S"), _I2CAddress, (_deviceState==DEVSTATE_FAILED) ? F("OFFLINE") : F("")); + } + }; #endif