From b886a09f82c21a346219eaaa52ade8afae6a9741 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 25 Jul 2020 18:50:23 +0200 Subject: [PATCH] Autocreate turnouts --- Turnouts.cpp | 1 + WiThrottle.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Turnouts.cpp b/Turnouts.cpp index f30abdd..5578905 100644 --- a/Turnouts.cpp +++ b/Turnouts.cpp @@ -21,6 +21,7 @@ #include "StringFormatter.h" #include "Hardware.h" #include "PWMServoDriver.h" +//#include "DIAG.h" // uncomment if you need DIAG below bool Turnout::activate(int n,bool state){ //DIAG(F("\nTurnout::activate(%d,%d)\n"),n,state); diff --git a/WiThrottle.cpp b/WiThrottle.cpp index 0310cb9..3a97e74 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -133,14 +133,19 @@ 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. - int id=getInt(cmd+6); + int id=getInt(cmd+4); 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); + if (Turnout::activate(id,newstate) == false) { + int addr = ((id - 1) / 4) + 1; + int subaddr = (id - 1) % 4; + Turnout::create(id,addr,subaddr); + Turnout::activate(id,newstate); + } StringFormatter::send(stream, F("PTA%cDT%d\n"),newstate?'4':'2',id ); } break;