diff --git a/Turnouts.cpp b/Turnouts.cpp index 96e0a1d..f30abdd 100644 --- a/Turnouts.cpp +++ b/Turnouts.cpp @@ -31,6 +31,12 @@ return true; } + bool Turnout::isActive(int n){ + Turnout * tt=get(n); + if (tt==NULL) return false; + return tt->data.tStatus & STATUS_ACTIVE; +} + // activate is virtual here so that it can be overridden by a non-DCC turnout mechanism void Turnout::activate(bool state) { //DIAG(F("\nTurnout::activate\n")); diff --git a/Turnouts.h b/Turnouts.h index 7a2edf0..e7ae0e1 100644 --- a/Turnouts.h +++ b/Turnouts.h @@ -41,6 +41,7 @@ class Turnout { static bool activate(int n, bool state); static Turnout* get(int); static bool remove(int); + static bool isActive(int); static void load(); static void store(); static Turnout *create(int id , int address , int subAddress); diff --git a/WiThrottle.cpp b/WiThrottle.cpp index df252f9..b65d890 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -65,7 +65,7 @@ WiThrottle::WiThrottle( int wificlientid) { firstThrottle= this; clientid=wificlientid; heartBeatEnable=false; // until client turns it on - firstCall=true; + callState=0; for (int loco=0;loconextTurnout){ - StringFormatter::send(stream,F("]\\[LT%d}|{%d}|{%d"), tt->data.id, tt->data.id, (bool)(tt->data.tStatus & STATUS_ACTIVE)); - } - StringFormatter::send(stream,F("\n*10\n")); - } + switch (callState) { + case 0: // first call in + callState++; + StringFormatter::send(stream,F("VN2.0\nHTDCC++EX\nRL0\nPPA%x\nPTT]\\[Turnouts}|{Turnout]\\[Closed}|{2]\\[Thrown}|{4\n*10\n"), + DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON); + break; + case 1: // second call... send the turnout table if we have one + callState++; + if (Turnout::firstTurnout) { + StringFormatter::send(stream,F("PTL")); + for(Turnout *tt=Turnout::firstTurnout;tt!=NULL;tt=tt->nextTurnout){ + StringFormatter::send(stream,F("]\\[DT%d}|{T%d}|{%d"), tt->data.id, tt->data.id, (bool)(tt->data.tStatus & STATUS_ACTIVE)); + } + StringFormatter::send(stream,F("\n")); + } + break; + default: // no more special headers required + break; + } while (cmd[0]) { switch (cmd[0]) { @@ -121,8 +129,16 @@ void WiThrottle::parse(Print & stream, byte * cmdx) { } else if (cmd[1]=='T' && cmd[2]=='A') { // PTA accessory toggle // TODO... if we are given an address that is not a known Turnout... - // should we create one or just send the DCC message. - Turnout::activate(getInt(cmd+4),cmd[3]=='T'); + // should we create one or just send the DCC message. + int id=getInt(cmd+6); + bool newstate=false; + switch (cmd[3]) { + case 'T': newstate=true; break; + case 'C': newstate=false; break; + case '2': newstate=!Turnout::isActive(id); + } + Turnout::activate(id,newstate); + StringFormatter::send(stream, F("PTA%cDT%d\n"),newstate?'4':'2',id ); } break; case 'N': // Heartbeat (2) diff --git a/WiThrottle.h b/WiThrottle.h index c04ba60..d039c8d 100644 --- a/WiThrottle.h +++ b/WiThrottle.h @@ -46,7 +46,7 @@ class WiThrottle { MYLOCO myLocos[MAX_MY_LOCO]; bool heartBeatEnable; - bool firstCall; + byte callState; // 0=first, 1=second, 3=third, 4=fourth call... >4=rest. unsigned long heartBeat; void multithrottle(Print & stream, byte * cmd);