Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/WiThrottle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ WiThrottle::sendCommand(String cmd)
if (server) {
stream->println("");
}
stream->flush();
console->print("==> "); console->println(cmd);
}
}
Expand Down Expand Up @@ -315,6 +316,10 @@ WiThrottle::processCommand(char *c, int len)
processTrackPower(c+3, len-3);
return true;
}
else if (len > 3 && c[0]=='P' && c[1]=='T' && c[2]=='A') {
processTurnoutState(c+3, len-3);
return true;
}
else if (len > 1 && c[0]=='*') {
return processHeartbeat(c+1, len-1);
}
Expand Down Expand Up @@ -358,6 +363,7 @@ WiThrottle::processCommand(char *c, int len)
console->printf("unknown command '%s'\n", c);
// all other commands are explicitly ignored
}
return changed;
}


Expand Down Expand Up @@ -529,6 +535,26 @@ WiThrottle::processFunctionState(const String& functionData)
}
}

void
WiThrottle::processTurnoutState(char *c, int len)
{
String remainder(c); // the leading "PTA" was not passed to this method

if (delegate) {
Turnout state = remainder[0] == '2' ? Closed : Thrown;
String idStr = remainder.substring(1);
uint8_t idNum = idStr.toInt();

if (idNum == 0 && idStr != "0") {
// error in parsing
}
else {
delegate->receivedTurnoutState(idNum, state);
}
}
}



void
WiThrottle::processSpeed(const String& speedData)
Expand Down Expand Up @@ -820,6 +846,30 @@ WiThrottle::emergencyStop()
sendCommand(cmd);
}

void
WiThrottle::setTurnout(int id, Turnout state) {
if (id < 1 || id > 99) {
return;
}

String cmd = "PTA";

if (state == Thrown) {
cmd += "T";
}
else
if (state == Closed) {
cmd += "C";
}
else {
cmd += "2";
}

cmd += id;

sendCommand(cmd);
}


void
WiThrottle::setFunction(int funcNum, bool pressed)
Expand Down
10 changes: 10 additions & 0 deletions src/WiThrottle.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
#define NEWLINE '\n'
#define CR '\r'

typedef enum Turnout {
Thrown = 0,
Closed = 1,
Toggle = 2,
} Turnout;

typedef enum Direction {
Reverse = 0,
Forward = 1
Expand Down Expand Up @@ -78,6 +84,7 @@ class WiThrottleDelegate
virtual void heartbeatConfig(int seconds) { }

virtual void receivedFunctionState(uint8_t func, bool state) { }
virtual void receivedTurnoutState(uint8_t id, Turnout state) { }

virtual void receivedSpeed(int speed) { } // Vnnn
virtual void receivedDirection(Direction dir) { } // R{0,1}
Expand Down Expand Up @@ -131,6 +138,8 @@ class WiThrottle

void setTrackPower(TrackPower state);

void setTurnout(int id, Turnout state);

void emergencyStop();


Expand All @@ -156,6 +165,7 @@ class WiThrottle
void processRosterList(char *c, int len);
void processTrackPower(char *c, int len);
void processFunctionState(const String& functionData);
void processTurnoutState(char *c, int len);
void processSpeedSteps(const String& speedStepData);
void processDirection(const String& speedStepData);
void processSpeed(const String& speedData);
Expand Down