From b03776c6f801883707d08e7c08d3a14acfd29990 Mon Sep 17 00:00:00 2001 From: SteveT Date: Fri, 14 Aug 2020 21:10:02 -0400 Subject: [PATCH] sync power state and turnout list/states to all clients --- WiThrottle.cpp | 32 ++++++++++++++++++++------------ WiThrottle.h | 2 ++ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/WiThrottle.cpp b/WiThrottle.cpp index 5d948a2..a95d850 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -71,7 +71,14 @@ bool WiThrottle::areYouUsingThrottle(int cab) { } return false; } - +void WiThrottle::setSendPowerState() { + for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle) + wt->sendPowerState = true; +} +void WiThrottle::setSendTurnoutList() { + for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle) + wt->sendTurnoutList = true; +} // One instance of WiThrottle per connected client, so we know what the locos are @@ -140,6 +147,7 @@ void WiThrottle::parse(Print & stream, byte * cmdx) { if (cmd[1]=='P' && cmd[2]=='A' ) { //PPA power mode DCCWaveform::mainTrack.setPowerMode(cmd[3]=='1'?POWERMODE::ON:POWERMODE::OFF); StringFormatter::send(stream,F("PPA%x\n"),DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON); + setSendPowerState(); //tell all WiThrottle instances to send power state at next heartbeat } else if (cmd[1]=='T' && cmd[2]=='A') { // PTA accessory toggle int id=getInt(cmd+4); @@ -159,7 +167,7 @@ void WiThrottle::parse(Print & stream, byte * cmdx) { } Turnout::activate(id,newstate); StringFormatter::send(stream, F("PTA%c%d\n"),newstate?'4':'2',id ); - sendTurnoutList = true; + setSendTurnoutList(); //tell all WiThrottle instances to send turnout list at next heartbeat } break; case 'N': // Heartbeat (2) @@ -267,16 +275,16 @@ void WiThrottle::locoAction(Print & stream, byte* aval, char throttleChar, int c } break; case 'F': //F onOff function - { - bool funcstate; - bool pressed=aval[1]=='1'; - int fKey = getInt(aval+2); - LOOPLOCOS(throttleChar, cab) { - funcstate = DCC::changeFn(myLocos[loco].cab, fKey, pressed); - if(funcstate==0 || funcstate==1) - StringFormatter::send(stream,F("M%cA%c%d<;>F%d%d\n"), throttleChar, LorS(myLocos[loco].cab), - myLocos[loco].cab, funcstate, fKey); - } + { + bool funcstate; + bool pressed=aval[1]=='1'; + int fKey = getInt(aval+2); + LOOPLOCOS(throttleChar, cab) { + funcstate = DCC::changeFn(myLocos[loco].cab, fKey, pressed); + if(funcstate==0 || funcstate==1) + StringFormatter::send(stream,F("M%cA%c%d<;>F%d%d\n"), throttleChar, LorS(myLocos[loco].cab), + myLocos[loco].cab, funcstate, fKey); + } } break; case 'q': diff --git a/WiThrottle.h b/WiThrottle.h index dee5b4f..4c1dc63 100644 --- a/WiThrottle.h +++ b/WiThrottle.h @@ -42,6 +42,8 @@ class WiThrottle { static int getLocoId(byte * cmd); static char LorS(int cab); static bool isThrottleInUse(int cab); + static void setSendTurnoutList(); + static void setSendPowerState(); bool areYouUsingThrottle(int cab); WiThrottle* nextThrottle; int clientid;