diff --git a/CommandDistributor.cpp b/CommandDistributor.cpp index 9f2baa3..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,6 +160,50 @@ void CommandDistributor::broadcastTurnout(int16_t id, bool isClosed ) { #endif } +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 "\n"),time, rate); +#ifdef CD_HANDLE_RING + broadcastReply(WITHROTTLE_TYPE, F("PFT%d<;>%d\n"), time*60, rate); +#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]; broadcastReply(COMMAND_TYPE, F("\n"), sp->loco,slot,sp->speedCode,sp->functions); diff --git a/CommandDistributor.h b/CommandDistributor.h index 633ff77..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,10 +46,14 @@ public : static void broadcastLoco(byte slot); static void broadcastSensor(int16_t id, bool value); static void broadcastTurnout(int16_t id, bool isClosed); + static void broadcastClockTime(int16_t time, int8_t rate); + 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 cbb152e..5c0cb84 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -570,9 +570,19 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) case 'J' : // throttle info access { - if ((params<1) | (params>2)) break; // + if ((params<1) | (params>3)) break; // + //if ((params<1) | (params>2)) break; // int16_t id=(params==2)?p[1]:0; switch(p[0]) { + case HASH_KEYWORD_C: // sets time and speed + if (params==1) { // returns latest time + int16_t x = CommandDistributor::retClockTime(); + StringFormatter::send(stream, F("\n"), x); + return; + } + CommandDistributor::setClockTime(p[1], p[2], 1); + return; + case HASH_KEYWORD_A: // returns automations/routes StringFormatter::send(stream, F(" diff --git a/EXRAIL2.cpp b/EXRAIL2.cpp index 5f2bf8b..09566cb 100644 --- a/EXRAIL2.cpp +++ b/EXRAIL2.cpp @@ -92,6 +92,7 @@ LookList * RMFT2::onRedLookup=NULL; LookList * RMFT2::onAmberLookup=NULL; LookList * RMFT2::onGreenLookup=NULL; LookList * RMFT2::onChangeLookup=NULL; +LookList * RMFT2::onClockLookup=NULL; #define GET_OPCODE GETHIGHFLASH(RMFT2::RouteCode,progCounter) #define SKIPOP progCounter+=3 @@ -175,6 +176,8 @@ LookList* RMFT2::LookListLoader(OPCODE op1, OPCODE op2, OPCODE op3) { onAmberLookup=LookListLoader(OPCODE_ONAMBER); onGreenLookup=LookListLoader(OPCODE_ONGREEN); onChangeLookup=LookListLoader(OPCODE_ONCHANGE); + onClockLookup=LookListLoader(OPCODE_ONTIME); + // Second pass startup, define any turnouts or servos, set signals red // add sequences onRoutines to the lookups @@ -975,6 +978,7 @@ void RMFT2::loop2() { case OPCODE_ONAMBER: case OPCODE_ONGREEN: case OPCODE_ONCHANGE: + case OPCODE_ONTIME: break; @@ -1118,7 +1122,14 @@ void RMFT2::changeEvent(int16_t vpin, bool change) { // Hunt for an ONCHANGE for this sensor if (change) handleEvent(F("CHANGE"),onChangeLookup,vpin); } - + +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); +} + void RMFT2::handleEvent(const FSH* reason,LookList* handlers, int16_t id) { int pc= handlers->find(id); if (pc<0) return; diff --git a/EXRAIL2.h b/EXRAIL2.h index 2ea2ba1..69fd382 100644 --- a/EXRAIL2.h +++ b/EXRAIL2.h @@ -55,6 +55,8 @@ enum OPCODE : byte {OPCODE_THROW,OPCODE_CLOSE, OPCODE_SET_TRACK, OPCODE_ONRED,OPCODE_ONAMBER,OPCODE_ONGREEN, OPCODE_ONCHANGE, + OPCODE_ONCLOCKTIME, + OPCODE_ONTIME, // OPcodes below this point are skip-nesting IF operations // placed here so that they may be skipped as a group @@ -116,6 +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); + static void clockEvent(int16_t clocktime, bool change); 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; @@ -173,6 +176,7 @@ private: static LookList * onAmberLookup; static LookList * onGreenLookup; static LookList * onChangeLookup; + static LookList * onClockLookup; // Local variables - exist for each instance/task RMFT2 *next; // loop chain diff --git a/EXRAIL2MacroReset.h b/EXRAIL2MacroReset.h index 32e28a2..a5e6d90 100644 --- a/EXRAIL2MacroReset.h +++ b/EXRAIL2MacroReset.h @@ -86,6 +86,8 @@ #undef ONDEACTIVATE #undef ONDEACTIVATEL #undef ONCLOSE +#undef ONTIME +#undef ONCLOCKTIME #undef ONGREEN #undef ONRED #undef ONTHROW @@ -198,6 +200,8 @@ #define ONACTIVATE(addr,subaddr) #define ONACTIVATEL(linear) #define ONAMBER(signal_id) +#define ONTIME(value) +#define ONCLOCKTIME(hours,mins) #define ONDEACTIVATE(addr,subaddr) #define ONDEACTIVATEL(linear) #define ONCLOSE(turnout_id) diff --git a/EXRAILMacros.h b/EXRAILMacros.h index b5e78d9..2ffbc75 100644 --- a/EXRAILMacros.h +++ b/EXRAILMacros.h @@ -55,6 +55,10 @@ // helper macro for turnout description as HIDDEN #define HIDDEN "\x01" +// helper macro to strip leading zeros off time inputs +// (10#mins)%100) +#define STRIP_ZERO(value) 10##value%100 + // Pass 1 Implements aliases #include "EXRAIL2MacroReset.h" #undef ALIAS @@ -297,6 +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), +#define ONTIME(value) OPCODE_ONTIME,V(value), +#define ONCLOCKTIME(hours,mins) OPCODE_ONTIME,V((STRIP_ZERO(hours)*60)+STRIP_ZERO(mins)), #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 new file mode 100644 index 0000000..0e1bf76 --- /dev/null +++ b/IO_EXFastclock.h @@ -0,0 +1,128 @@ +/* + * © 2022, Colin Murdoch. All rights reserved. + * + * This file is part of CommandStation-EX + * + * This is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * It is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CommandStation. If not, see . +*/ + +/* +* The IO_EXFastclock device driver is used to interface the standalone fast clock and receive time data. +* +* The EX-fastClock code lives in a separate repo (https://github.com/DCC-EX/EX-Fastclock) and contains the clock logic. +* +* +*/ + +#ifndef IO_EXFastclock_h +#define IO_EXFastclock_h + + +#include "IODevice.h" +#include "I2CManager.h" +#include "DIAG.h" +#include "EXRAIL2.h" +#include "CommandDistributor.h" + +bool FAST_CLOCK_EXISTS = true; + +class EXFastClock : public IODevice { +public: + // Constructor + EXFastClock(uint8_t I2CAddress){ + _I2CAddress = I2CAddress; + addDevice(this); + } + +static void create(uint8_t _I2CAddress) { + + DIAG(F("Checking for Clock")); + // Start by assuming we will find the clock + // Check if specified I2C address is responding (blocking operation) + // 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); + new EXFastClock(_I2CAddress); + } + else { + FAST_CLOCK_EXISTS = false; + //DIAG(F("No Fast Clock found")); + LCD(6,F("CLOCK NOT FOUND")); + } + + } + +private: +uint8_t _I2CAddress; + + +// Initialisation of Fastclock +void _begin() override { + + if (FAST_CLOCK_EXISTS == true) { + I2CManager.begin(); + if (I2CManager.exists(_I2CAddress)) { + _deviceState = DEVSTATE_NORMAL; + #ifdef DIAG_IO + _display(); + #endif + } else { + _deviceState = DEVSTATE_FAILED; + //LCD(6,F("CLOCK NOT FOUND")); + DIAG(F("Fast Clock Not Found at address %d"), _I2CAddress); + } + } +} + +// Processing loop to obtain clock time + +void _loop(unsigned long currentMicros) override{ + + if (FAST_CLOCK_EXISTS==true) { + uint8_t readBuffer[3]; + byte a,b; + #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]; + + 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)); + + } + + #endif + + } + + + // Display EX-FastClock device driver info. + void _display() { + DIAG(F("FastCLock on I2C:x%x - %S"), _I2CAddress, (_deviceState==DEVSTATE_FAILED) ? F("OFFLINE") : F("")); + } + +}; + +#endif diff --git a/config.example.h b/config.example.h index aae18a9..a2d97c5 100644 --- a/config.example.h +++ b/config.example.h @@ -224,4 +224,8 @@ The configuration file for DCC-EX Command Station // //#define SERIAL_BT_COMMANDS + +// FastClock Enabler +// To build the FastClock code into the CS please uncomment the line below +//#define USEFASTCLOCK ///////////////////////////////////////////////////////////////////////////////////// diff --git a/myHal.cpp_example.txt b/myHal.cpp_example.txt index 5470f76..64adac1 100644 --- a/myHal.cpp_example.txt +++ b/myHal.cpp_example.txt @@ -21,7 +21,7 @@ #include "IO_VL53L0X.h" // Laser time-of-flight sensor #include "IO_DFPlayer.h" // MP3 sound player //#include "IO_EXTurntable.h" // Turntable-EX turntable controller - +//#include "IO_EXFastClock.h" // FastClock driver //========================================================================== // The function halSetup() is invoked from CS if it exists within the build. @@ -206,6 +206,19 @@ void halSetup() { //RotaryEncoder::create(700, 1, 0x70); //RotaryEncoder::create(701, 2, 0x71); + //======================================================================= + // The following directive defines an EX-FastClock instance. + //======================================================================= + // EXFastCLock::create(I2C Address) + // + // The parameters are: + // + // I2C address=0x55 (decimal 85) + // + // Note that the I2C address is defined in the EX-FastClock code, and 0x55 is the default. + + + // EXFastClock::create(0x55); }