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/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;