Skip to content

Commit

Permalink
Simplify the heartbeat send, as this was creating problems for JMRI
Browse files Browse the repository at this point in the history
also, truly disable the heartbeat if directed
  • Loading branch information
flash62au committed Oct 16, 2024
1 parent 002e01f commit ec77825
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
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.24
version=1.1.25
author=Peter Akers <akersp62@gmail.com>, David Zuhn <zoo@statebeltrailway.org>, Luca Dentella <luca@dentella.it>
maintainer=Peter Akers <akersp62@gmail.com>
sentence=JMRI WiThrottle Protocol implementation for ESP32
Expand Down
45 changes: 27 additions & 18 deletions src/WiThrottleProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ bool WiThrottleProtocol::check() {

while(stream->available()) {
char b = stream->read();
if (logLevel>3) { console->print("WiT:: check() : "); console->println(b); }
if (b == NEWLINE || b==CR) {
// server sends TWO newlines after each command, we trigger on the
// first, and this skips the second one
Expand Down Expand Up @@ -1145,27 +1146,33 @@ bool WiThrottleProtocol::checkHeartbeat() {
if ((heartbeatPeriod > 0) && ((millis() - heartbeatTimer) > 0.5 * heartbeatPeriod * 1000)) {
if (logLevel>0) console->println("WiT:: checkHeartbeat(): ");

if (!heartbeatEnabled) {
if (logLevel>1) console->println("WiT:: checkHeartbeat(): heartbeat not enabled");
heartbeatTimer = millis();
return true;
}

sendDelayedCommand("*");
setDeviceName(currentDeviceName); // resent the device name instead of the heartbeat. this forces the wit server to respond

// if there are any locos under control, resend all their speeds
if ( (timeLastLocoAcquired!=0) && ((millis() - timeLastLocoAcquired) > 5000) ) { // wait at least 5 seconds from the last time that a loco was aqcuired, to give the server time to send any existing speeds
for (int i=0; i<MAX_WIT_THROTTLES; i++) {
char multiThrottleChar = '0' + i;
if (getNumberOfLocomotives(multiThrottleChar)>0) {
setSpeed(multiThrottleChar, getSpeed(multiThrottleChar), true);
int multiThrottleIndex = getMultiThrottleIndex(multiThrottleChar);
if (locomotives[multiThrottleIndex].size()==1) {
setDirection(multiThrottleChar, getDirection(multiThrottleChar), true);
} else {
for(int i=0;i<locomotives[multiThrottleIndex].size();i++) {
String loco = getLocomotiveAtPosition(multiThrottleChar,i);
setDirection(multiThrottleChar, loco, getDirection(multiThrottleChar, loco), true);
}
}
}
}
}
// // if there are any locos under control, resend all their speeds
// if ( (timeLastLocoAcquired!=0) && ((millis() - timeLastLocoAcquired) > 5000) ) { // wait at least 5 seconds from the last time that a loco was aqcuired, to give the server time to send any existing speeds
// for (int i=0; i<MAX_WIT_THROTTLES; i++) {
// char multiThrottleChar = '0' + i;
// if (getNumberOfLocomotives(multiThrottleChar)>0) {
// setSpeed(multiThrottleChar, getSpeed(multiThrottleChar), true);
// int multiThrottleIndex = getMultiThrottleIndex(multiThrottleChar);
// if (locomotives[multiThrottleIndex].size()==1) {
// setDirection(multiThrottleChar, getDirection(multiThrottleChar), true);
// } else {
// for(int i=0;i<locomotives[multiThrottleIndex].size();i++) {
// String loco = getLocomotiveAtPosition(multiThrottleChar,i);
// setDirection(multiThrottleChar, loco, getDirection(multiThrottleChar, loco), true);
// }
// }
// }
// }
// }

heartbeatTimer = millis();

Expand All @@ -1179,9 +1186,11 @@ bool WiThrottleProtocol::checkHeartbeat() {

void WiThrottleProtocol::requireHeartbeat(bool needed) {
if (needed) {
heartbeatEnabled = true;
sendDelayedCommand("*+");
}
else {
heartbeatEnabled = false;
sendDelayedCommand("*-");
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/WiThrottleProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/*
Version information:
1.1.25 - Simplify the heartbeat send, as this was creating problems for JMRI
1.1.24 - Add support for forcing a function
1.1.23 - Fix for individual loco direction (facing) changes in a consist
1.1.22 - Fix for the original 'steal' code
Expand Down Expand Up @@ -798,6 +799,7 @@ class WiThrottleProtocol
//Chrono heartbeatTimer;
unsigned long heartbeatTimer;
int heartbeatPeriod;
bool heartbeatEnabled = false;
unsigned long timeLastLocoAcquired;

//Chrono fastTimeTimer;
Expand Down

0 comments on commit ec77825

Please sign in to comment.