diff --git a/DCC.cpp b/DCC.cpp index 4d5e567..6d47188 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -95,15 +95,6 @@ bool DCC::getThrottleDirection(int cab) { return (speedTable[reg].speedCode & 0x80) !=0; } -bool DCC::isThrottleInUse(int locoId) { - // return true if this loco address is already in table, false otherwise - int reg; - for (reg = 0; reg < MAX_LOCOS; reg++) { - if (speedTable[reg].loco == locoId) return true; - } - return false; -} - // Set function to value on or off void DCC::setFn( int cab, byte functionNumber, bool on) { if (cab<=0 || functionNumber>28) return; diff --git a/DCC.h b/DCC.h index 74ac951..d549490 100644 --- a/DCC.h +++ b/DCC.h @@ -56,7 +56,6 @@ class DCC { static void setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection); static uint8_t getThrottleSpeed(int cab); static bool getThrottleDirection(int cab); - static bool isThrottleInUse(int cab); 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); diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 9c317b5..20c5205 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -183,8 +183,7 @@ void DCCWaveform::setSignal(bool high) { if (progTrackSyncMain) { if (!isMainTrack) return; // ignore PROG track waveform while in sync // set both tracks to same signal - Hardware::setSignal(true, high); - Hardware::setSignal(false, high); + Hardware::setSyncSignal(high); return; } Hardware::setSignal(isMainTrack,high); diff --git a/Hardware.cpp b/Hardware.cpp index df40a06..51a1e68 100644 --- a/Hardware.cpp +++ b/Hardware.cpp @@ -64,6 +64,22 @@ void Hardware::setSignal(bool isMainTrack, bool high) { if (pin2 != UNUSED_PIN) WritePin(pin2, high ? LOW : HIGH); } +void Hardware::setSyncSignal(bool high) { + // This sets the same signal down both tracks at the same time. + // Speed notes.... + // Objective is to get the two track signals to change as close as possible + // the high ? HIGH:LOW will only be evaluated once + // The UNUSED_PIN check will be done at compile time. + // If even more speed is required, its possible (not SAMD) to pre-prepare the + // DIO pinnumber->pincode translation so the WritePin (digitalWrite2) does not + // have to calculate the register and bit numbers every time. + + WritePin(MAIN_SIGNAL_PIN, high ? HIGH : LOW); + WritePin(PROG_SIGNAL_PIN, high ? HIGH : LOW); + if (MAIN_SIGNAL_PIN_ALT != UNUSED_PIN) WritePin(MAIN_SIGNAL_PIN_ALT, high ? LOW : HIGH); + if (PROG_SIGNAL_PIN_ALT != UNUSED_PIN) WritePin(PROG_SIGNAL_PIN_ALT, high ? LOW : HIGH); +} + int Hardware::getCurrentRaw(bool isMainTrack) { // tooo much crap for a interrupt routine. Will see how that goes. byte faultpin = isMainTrack ? MAIN_FAULT_PIN : PROG_FAULT_PIN; diff --git a/Hardware.h b/Hardware.h index 4c86a70..2362741 100644 --- a/Hardware.h +++ b/Hardware.h @@ -24,6 +24,7 @@ class Hardware { static void init(); static void setPower(bool isMainTrack, bool on); static void setSignal(bool isMainTrack, bool high); + static void setSyncSignal( bool high); static unsigned int getCurrentMilliamps(bool isMainTrack, int rawValue); static int getCurrentRaw(bool isMainTrack); static void setBrake(bool isMainTrack, bool on); diff --git a/WiThrottle.cpp b/WiThrottle.cpp index 644b4a1..0323d09 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -55,10 +55,24 @@ bool WiThrottle::annotateLeftRight=false; WiThrottle* WiThrottle::getThrottle( int wifiClient) { for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle) - if (wt->clientid==wifiClient) return wt; + if (wt->clientid==wifiClient) return wt; return new WiThrottle( wifiClient); } +bool WiThrottle::isThrottleInUse(int cab) { + for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle) + if (wt->areYouUsingThrottle(cab)) return true; + return false; +} + +bool WiThrottle::areYouUsingThrottle(int cab) { + LOOPLOCOS('*', cab) { // see if I have this cab in use + return true; + } + return false; +} + + // One instance of WiThrottle per connected client, so we know what the locos are WiThrottle::WiThrottle( int wificlientid) { @@ -166,8 +180,7 @@ void WiThrottle::parse(Print & stream, byte * cmdx) { case 'Q': // LOOPLOCOS('*', -1) { //stop and drop all locos still assigned to this WiThrottle if (myLocos[loco].throttle!='\0') { - DCC::setThrottle(myLocos[loco].cab,0,1); - DCC::forgetLoco(myLocos[loco].cab); //unregister this loco address + DCC::setThrottle(myLocos[loco].cab,1,1); StringFormatter::send(stream, F("M%c-%c%d<;>\n"), myLocos[loco].throttle, LorS(myLocos[loco].cab), myLocos[loco].cab); myLocos[loco].throttle='\0'; } @@ -216,7 +229,7 @@ void WiThrottle::multithrottle(Print & stream, byte * cmd){ return; } //return error if address is already in use - if (DCC::isThrottleInUse(locoid)) { + if (isThrottleInUse(locoid)) { StringFormatter::send(stream, F("HMAddress '%d' in use!\n"), locoid); return; } @@ -239,8 +252,7 @@ void WiThrottle::multithrottle(Print & stream, byte * cmd){ case '-': // stop and remove loco(s) LOOPLOCOS(throttleChar, locoid) { myLocos[loco].throttle='\0'; - DCC::setThrottle(myLocos[loco].cab,0, DCC::getThrottleDirection(myLocos[loco].cab)); - DCC::forgetLoco(myLocos[loco].cab); //unregister this loco address + DCC::setThrottle(myLocos[loco].cab,1, DCC::getThrottleDirection(myLocos[loco].cab)); StringFormatter::send(stream, F("M%c-%c%d<;>\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab); } @@ -306,7 +318,7 @@ void WiThrottle::locoAction(Print & stream, byte* aval, char throttleChar, int c case 'I': // Idle, set speed to 0 case 'Q': // Quit, set speed to 0 LOOPLOCOS(throttleChar, cab) { - DCC::setThrottle(myLocos[loco].cab,0, DCC::getThrottleDirection(myLocos[loco].cab)); + DCC::setThrottle(myLocos[loco].cab,1, DCC::getThrottleDirection(myLocos[loco].cab)); } break; } @@ -326,7 +338,6 @@ void WiThrottle::checkHeartbeat() { if (myLocos[loco].throttle!='\0') { DIAG(F(" dropping cab %c"),clientid, myLocos[loco].cab); DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab)); //eStop - DCC::forgetLoco(myLocos[loco].cab); //unregister this loco address } } delete this; diff --git a/WiThrottle.h b/WiThrottle.h index d8fcc53..cbd7175 100644 --- a/WiThrottle.h +++ b/WiThrottle.h @@ -40,7 +40,9 @@ class WiThrottle { static WiThrottle* firstThrottle; static int getInt(byte * cmd); static int getLocoId(byte * cmd); - static char LorS(int cab); + static char LorS(int cab); + static bool isThrottleInUse(int cab); + bool areYouUsingThrottle(int cab); WiThrottle* nextThrottle; int clientid;