Skip to content

Commit

Permalink
Change to the fix for the wifiTrax WFD-30,
Browse files Browse the repository at this point in the history
Change to the fix for the_wifiTrax WFD-30, so that leading CR+LF is always sent
         - Removal of setSpeedCommandShouldBeSenttwice(bool twice)
  • Loading branch information
flash62au committed Feb 26, 2024
1 parent 4dd1ffa commit 69d7e48
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 41 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ WiThrottleProtocolDelegate *delegate
This variable holds a pointer to a class that subclasses the ```WiThrottleProtocolDelegate``` class. Once this is set, various methods will be called when protocol commands come in and have been processed.

```
setSpeedCommandShouldBeSentTwice(bool twice)
setCommandsNeedLeadingCrLf(bool needed);
```
This should be set to true for wifiTrax to get around a fault in their implementation of WiThrottle protocol.
For some reason WifiTrax WFD-30 system don't respond unless the commands are preceeded with CR+LF
These will be sent if the SSID name contains "wftrx_"

### Fast Time
```
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=WiThrottleProtocol
version=1.1.10
version=1.1.11
author=David Zuhn <zoo@statebeltrailway.org>, Luca Dentella <luca@dentella.it>, Peter Akers <akersp62@gmail.com>
maintainer=Peter Akers <akersp62@gmail.com>
sentence=JMRI WiThrottle Protocol implementation
Expand Down
42 changes: 9 additions & 33 deletions src/WiThrottleProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void WiThrottleProtocol::init() {

// output buffer
outboundBuffer = "";
outboundBufferCommandsNeedsToBeSentTwice = "";
outboundCmdsTimeLastSent = millis();

// init heartbeat
Expand Down Expand Up @@ -141,8 +140,8 @@ void WiThrottleProtocol::setDeviceID(String deviceId) {
sendDelayedCommand(command);
}

void WiThrottleProtocol::setSpeedCommandsNeedToBeSentTwice(bool twice) {
speedCommandsNeedToBeSentTwice = twice;
void WiThrottleProtocol::setCommandsNeedLeadingCrLf(bool needed) {
commandsNeedLeadingCrLf = needed;
}


Expand Down Expand Up @@ -190,17 +189,9 @@ bool WiThrottleProtocol::check() {
}

void WiThrottleProtocol::sendCommand(String cmd) {
sendCommand(cmd, 0);
}

void WiThrottleProtocol::sendCommand(String cmd, bool twice) {
if (stream) {
// TODO: what happens when the write fails?
stream->println(cmd);
if (twice) {
stream->println(cmd);
console->println("==> Sending Twice !!!!!!!!!!!");
}
if (server) {
stream->println("");
}
Expand All @@ -209,56 +200,41 @@ void WiThrottleProtocol::sendCommand(String cmd, bool twice) {
}

void WiThrottleProtocol::sendDelayedCommand(String cmd) {
sendDelayedCommand(cmd, 0);
}

void WiThrottleProtocol::sendDelayedCommand(String cmd, bool twice) {
if (stream) {
// TODO: what happens when the write fails?

if (cmd.length()>0) {
outboundBuffer = outboundBuffer + cmd + '\n';
outboundBufferCommandsNeedsToBeSentTwice = outboundBufferCommandsNeedsToBeSentTwice + ((twice) ? "Y" : "N") +"\n";
// console->print("sendDelayedCommand() : "); console->print(cmd); console->print(" - twice: "); console->println(((twice) ? "Y" : "N"));
// console->println(" XTwice: "); console->println(outboundBufferCommandsNeedsToBeSentTwice);
}

if ( (outboundBuffer.length()>0) &&((millis()-outboundCmdsTimeLastSent) > outboundCmdsMininumDelay) ) {
// console->print("sendDelayedCommand() : Flushing outbound buffer - delay: "); console->print(outboundCmdsMininumDelay); console->print(" Buffer: "); console->println(outboundBuffer);
int end = outboundBuffer.indexOf("\n");
int endTwice = outboundBufferCommandsNeedsToBeSentTwice.indexOf("\n");
String thisCmd = outboundBuffer; // default to sending the lot
bool thisTwice = false;
if (end>0) {
thisCmd = outboundBuffer.substring(0,end);
thisTwice = (outboundBufferCommandsNeedsToBeSentTwice.substring(0,endTwice)=="Y") ? true : false;
end++;
endTwice++;
outboundBuffer = outboundBuffer.substring(end);
outboundBufferCommandsNeedsToBeSentTwice = outboundBufferCommandsNeedsToBeSentTwice.substring(endTwice);
if (outboundBuffer.length()>0) {
console->print("sendDelayedCommand() : deferring cmds: "); console->println(outboundCmdsMininumDelay);
console->println(" Buffer: "); console->println(outboundBuffer);
console->println(" Twice: "); console->println(outboundBufferCommandsNeedsToBeSentTwice);
}
} else if (end==0) {
thisCmd = "";
outboundBuffer = outboundBuffer.substring(end+1);
} else {
thisCmd = outboundBuffer;
thisTwice = twice;
outboundBuffer = "";
}

if (thisCmd.length()>0) {
outboundCmdsTimeLastSent = millis();
stream->println(thisCmd);
if (thisTwice) {
stream->println(thisCmd);
// console->println("==> Sending Twice !!!!!!!!!!!");
console->print("==> "); console->print(thisCmd);
console->print(" ("); console->print(millis()); console->println(")");
if (commandsNeedLeadingCrLf) {
stream->write(0x0D);
stream->write(0x0A);
}
stream->println(thisCmd);

if (server) {
stream->println("");
}
Expand Down Expand Up @@ -1261,7 +1237,7 @@ bool WiThrottleProtocol::setSpeed(char multiThrottle, int speed, bool forceSend)
+ PROPERTY_SEPARATOR
+ "V"
+ String(speed);
sendDelayedCommand(cmd, speedCommandsNeedToBeSentTwice);
sendDelayedCommand(cmd);
currentSpeed[multiThrottleIndex] = speed;
}
return true;
Expand Down Expand Up @@ -1316,7 +1292,7 @@ bool WiThrottleProtocol::setDirection(char multiThrottle, String address, Direct
else {
cmd += "1";
}
sendDelayedCommand(cmd, speedCommandsNeedToBeSentTwice);
sendDelayedCommand(cmd);

if (address.equals(ALL_LOCOS_ON_THROTTLE)) {
currentDirection[multiThrottleIndex] = direction;
Expand Down
26 changes: 21 additions & 5 deletions src/WiThrottleProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@
*
*/

/*
Version information:
1.1.11 - Change to the fix for the _wifiTrax WFD-30, so that leading CR+LF is always sent
- Removal of setSpeedCommandShouldBeSenttwice(bool twice)
1.1.10 - discarded
1.1.9 - discarded
1.1.8 - Addition of setSpeedCommandShouldBeSenttwice(bool twice)
1.1.7 - Addition of minimum time separation/delay between commands sent
1.1.6 - Change to support 32 functions
1.1.5 - Adjust the aggressive heartbeat
1.1.4 - Use more aggressive heartbeat commands
1.1.3 - Add add ability to specify which loco in a consist is sent a function co…
1.1.2 - Bug fix
1.1.1 - Improve consist support
1.1.0 - Muiti-throttle support added
*/

#ifndef WITHROTTLE_H
#define WITHROTTLE_H

Expand Down Expand Up @@ -147,7 +166,7 @@ class WiThrottleProtocol
void setDelegate(WiThrottleProtocolDelegate *delegate, int delayBetweenCommandsSent);
void setLogStream(Stream *console);

void setSpeedCommandsNeedToBeSentTwice(bool twice);
void setCommandsNeedLeadingCrLf(bool needed);

void connect(Stream *stream);
void connect(Stream *stream, int delayBetweenCommandsSent);
Expand Down Expand Up @@ -229,10 +248,9 @@ class WiThrottleProtocol
Stream *console;
NullStream nullStream;
String outboundBuffer;
String outboundBufferCommandsNeedsToBeSentTwice;
double outboundCmdsTimeLastSent;
int outboundCmdsMininumDelay;
bool speedCommandsNeedToBeSentTwice = false;
bool commandsNeedLeadingCrLf = false;

WiThrottleProtocolDelegate *delegate = NULL;

Expand Down Expand Up @@ -264,9 +282,7 @@ class WiThrottleProtocol
bool checkHeartbeat();

void sendCommand(String cmd);
void sendCommand(String cmd, bool twice);
void sendDelayedCommand(String cmd);
void sendDelayedCommand(String cmd, bool twice);

void setCurrentFastTime(const String& s);

Expand Down

0 comments on commit 69d7e48

Please sign in to comment.