Skip to content

Commit

Permalink
Add support for setting the Speed Step mode 28/128/14 etc. setSpeedS…
Browse files Browse the repository at this point in the history
…teps(), getSpeedSteps()
  • Loading branch information
flash62au committed Aug 4, 2024
1 parent 675c708 commit 1733bcc
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 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.20
version=1.1.21
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
51 changes: 48 additions & 3 deletions src/WiThrottleProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void WiThrottleProtocol::init() {
for (int multiThrottleIndex=0; multiThrottleIndex<6; multiThrottleIndex++) {
locomotiveSelected[multiThrottleIndex] = false;
currentSpeed[multiThrottleIndex] = 0;
speedSteps[multiThrottleIndex] = 0;
speedSteps[multiThrottleIndex] = 1; //1=128 steps
currentDirection[multiThrottleIndex] = Forward;
locomotives[multiThrottleIndex].resize(0);
locomotivesFacing[multiThrottleIndex].resize(0);
Expand Down Expand Up @@ -893,20 +893,24 @@ void WiThrottleProtocol::processSpeed(char multiThrottle, const String& speedDat


void WiThrottleProtocol::processSpeedSteps(char multiThrottle, const String& speedStepData) {
if (logLevel>0) { console->print("WiT:: processSpeedSteps(): "); console->println(multiThrottle); }
if (logLevel>0) { console->print("WiT:: processSpeedSteps(): "); console->print(multiThrottle); console->print(" : "); console->println(speedStepData); }
int multiThrottleIndex = getMultiThrottleIndex(multiThrottle);

if (delegate && speedStepData.length() >= 2) {
String speedStepStr = speedStepData.substring(1);
int steps = speedStepStr.toInt();

if (steps != 1 && steps != 2 && steps != 4 && steps != 8 && steps !=16) {
// 1 = 128step, 2 = 28step, 4 = 27step or 8 = 14step
if (steps != 1 && steps != 2 && steps != 4 && steps != 8) {
// error, not one of the known values
}
else {
if (multiThrottle == DEFAULT_MULTITHROTTLE) {
delegate->receivedSpeedSteps(steps);
speedSteps[0] = steps;
} else {
delegate->receivedSpeedStepsMultiThrottle(multiThrottle, steps);
speedSteps[multiThrottleIndex] = steps;
}
}
}
Expand Down Expand Up @@ -1264,6 +1268,47 @@ int WiThrottleProtocol::getNumberOfLocomotives(char multiThrottle) {
return size;
}

// ******************************************************************************************************

int WiThrottleProtocol::getSpeedSteps() {
return setSpeedSteps(DEFAULT_MULTITHROTTLE);
}

int WiThrottleProtocol::getSpeedSteps(char multiThrottle) {
if (logLevel>0) { console->print("WiT:: getSpeedSteps(): "); console->println(multiThrottle); }

int multiThrottleIndex = getMultiThrottleIndex(multiThrottle);
return speedSteps[multiThrottleIndex];
}

// ******************************************************************************************************

bool WiThrottleProtocol::setSpeedSteps(int steps) {
return setSpeedSteps(DEFAULT_MULTITHROTTLE, steps);
}

bool WiThrottleProtocol::setSpeedSteps(char multiThrottle, int steps) {
if (logLevel>0) { console->print("WiT:: setSpeedSteps(): "); console->print(multiThrottle); console->print(" : "); console->println(steps); }

int multiThrottleIndex = getMultiThrottleIndex(multiThrottle);

// 1 = 128step, 2 = 28step, 4 = 27step or 8 = 14step
if (steps != 1 && steps != 2 && steps != 4 && steps != 8) {
console->print("WiT:: setSpeedSteps(): Error, not one of the known values");
return false;
}

String cmd = "M" + String(multiThrottle) + "A*"
+ PROPERTY_SEPARATOR
+ "s"
+ String(steps);
sendDelayedCommand(cmd);
speedSteps[multiThrottleIndex] = steps;

return true;
}


// ******************************************************************************************************

bool WiThrottleProtocol::setSpeed(int speed) {
Expand Down
33 changes: 26 additions & 7 deletions src/WiThrottleProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/*
Version information:
1.1.21 - Add support for setting the Speed Step mode 28/128/14 etc. setSpeedSteps(), getSpeedSteps()
1.1.20 - Corrected the EStop for 'all' throttles
1.1.18/19- Added support for logLevel. Assigned levels to every log message
- changed all the log messages so that it is clearer which came from this library
Expand Down Expand Up @@ -242,7 +243,7 @@ class WiThrottleProtocolDelegate
virtual void receivedDirection(Direction dir) { } // R{0,1}

/// @brief Delegate method to receive the number of speed steps for the default (first) throttle from the Withrottle Server
/// @param steps 28 or 128
/// @param steps 1=128step, 2=28step, 4=27step or 8=14step
virtual void receivedSpeedSteps(int steps) { } // snn

/// @brief Delegate method to receive the speed for a specific throttle from the Withrottle Server
Expand All @@ -257,7 +258,7 @@ class WiThrottleProtocolDelegate

/// @brief Delegate method to receive the speed steps for a specific throttle from the Withrottle Server
/// @param multiThrottle Which Throttle. Supported multiThrottle codes are 'T' '0' '1' '2' '3' '4' '5' only.
/// @param steps 28 or 128
/// @param steps 1=128step, 2=28step, 4=27step or 8=14step
virtual void receivedSpeedStepsMultiThrottle(char multiThrottle, int steps) { } // snn

/// @brief Delegate method to receive from the Withrottle Server
Expand Down Expand Up @@ -486,28 +487,46 @@ class WiThrottleProtocol
/// @brief Get the direction of the default (first) Throttle
Direction getDirection();

/// @brief Get the speed step of the default (first) Throttle
int getSpeedSteps();

// multiThrottle support
/// @brief Get the speed step of a specified Throttle
/// @param multiThrottle Which Throttle. Supported multiThrottle codes are 'T' '0' '1' '2' '3' '4' '5' only.
int getSpeedSteps(char multiThrottle);

/// @brief Set the speed step of the default (first) Throttle
/// @param steps 1=128step, 2=28step, 4=27step or 8=14step
bool setSpeedSteps(int steps);

// multiThrottle support
/// @brief Set the speed step of a specified Throttle
/// @param multiThrottle Which Throttle. Supported multiThrottle codes are 'T' '0' '1' '2' '3' '4' '5' only.
/// @param steps 1=128step, 2=28step, 4=27step or 8=14step
bool setSpeedSteps(char multiThrottle, int steps);

// multiThrottle support
/// @brief Set the speed of the a specified Throttle
/// @brief Set the speed of a specified Throttle
/// @param multiThrottle Which Throttle. Supported multiThrottle codes are 'T' '0' '1' '2' '3' '4' '5' only.
/// @param speed Speed 0-126
bool setSpeed(char multiThrottle, int speed);

/// @brief Set the speed of the a specified Throttle
/// @brief Set the speed of a specified Throttle
/// @param multiThrottle Which Throttle. Supported multiThrottle codes are 'T' '0' '1' '2' '3' '4' '5' only.
/// @param speed Speed 0-126
/// @param forceSend Option to force the command to be sent, even if the protocol thinks it is at that speed
bool setSpeed(char multiThrottle, int speed, bool forceSend);

/// @brief Get the speed of the a specified Throttle
/// @brief Get the speed of a specified Throttle
/// @param multiThrottle Which Throttle. Supported multiThrottle codes are 'T' '0' '1' '2' '3' '4' '5' only.
int getSpeed(char multiThrottle);

/// @brief Set the direction of the a specified Throttle
/// @brief Set the direction of a specified Throttle
/// @param multiThrottle Which Throttle. Supported multiThrottle codes are 'T' '0' '1' '2' '3' '4' '5' only.
/// @param direction Direction. One of - Reverse = 0, Forward = 1
bool setDirection(char multiThrottle, Direction direction);

/// @brief Set the direction of the a specified Throttle, with the option to force the send
/// @brief Set the direction of a specified Throttle, with the option to force the send
/// @param multiThrottle Which Throttle. Supported multiThrottle codes are 'T' '0' '1' '2' '3' '4' '5' only.
/// @param direction Direction. One of - Reverse = 0, Forward = 1
/// @param forceSend Option to force the command to be sent, even if the protocol thinks it is in that Direction
Expand Down

0 comments on commit 1733bcc

Please sign in to comment.