Skip to content
Permalink
Browse files

use a linked list as a queue to hold the speeds that have been sent.

  • Loading branch information...
pabender committed Nov 3, 2018
1 parent 0531830 commit becc445fa2ac8e53255d767b915b7fec38ff4243
@@ -87,17 +87,19 @@ public void propertyChange(PropertyChangeEvent event) {
}
if (eventName.matches("SpeedSetting")) {
float currentSpeed = ((Float) event.getNewValue()).floatValue();
if(internalAdjust && Math.abs(lastSentSpeed-currentSpeed)>0.0005 ) {
return;
}
StringBuilder message = new StringBuilder(buildPacketWithChar('A'));
message.append("V");
message.append(Math.round(currentSpeed / speedMultiplier));
for (ControllerInterface listener : controllerListeners) {
listener.sendPacketToDevice(message.toString());
log.debug("Speed Setting: {} head of queue {}",currentSpeed, lastSentSpeed.peek());
if(lastSentSpeed.isEmpty()) {
StringBuilder message = new StringBuilder(buildPacketWithChar('A'));
message.append("V");
message.append(Math.round(currentSpeed / speedMultiplier));
for (ControllerInterface listener : controllerListeners) {
listener.sendPacketToDevice(message.toString());
}
} else {
if( Math.abs(lastSentSpeed.peek().floatValue()-currentSpeed)<0.0005 ) {
lastSentSpeed.poll(); // remove the value from the list.
}
}
lastSentSpeed=currentSpeed;
internalAdjust = false;
}
}

@@ -165,8 +167,6 @@ synchronized protected void sendCurrentSpeed(DccThrottle t) {
for (ControllerInterface listener : controllerListeners) {
listener.sendPacketToDevice(message.toString());
}
lastSentSpeed=currentSpeed;
internalAdjust = false;
}

/**
@@ -35,6 +35,8 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import java.util.Queue;
import jmri.DccLocoAddress;
import jmri.DccThrottle;
import jmri.InstanceManager;
@@ -53,19 +55,20 @@
DccLocoAddress leadAddress;
char whichThrottle;
float speedMultiplier;
protected float lastSentSpeed;
protected Queue<Float> lastSentSpeed;
protected float newSpeed;
boolean isAddressSet;
protected ArrayList<ThrottleControllerListener> listeners;
protected ArrayList<ControllerInterface> controllerListeners;
boolean useLeadLocoF;
ConsistFunctionController leadLocoF = null;
String locoKey = "";
protected boolean internalAdjust = false;

final boolean isMomF2 = InstanceManager.getDefault(WiThrottlePreferences.class).isUseMomF2();

public ThrottleController() {
speedMultiplier = 1.0f / 126.0f;
lastSentSpeed = new LinkedList<Float>();
}

public ThrottleController(char whichThrottleChar, ThrottleControllerListener tcl, ControllerInterface cl) {
@@ -569,11 +572,12 @@ public void shutdownThrottle() {
*/
synchronized protected void setSpeed(int rawSpeed) {

internalAdjust = true;
float newSpeed = (rawSpeed * speedMultiplier);

log.debug("raw: {}, NewSpd: {}", rawSpeed, newSpeed);
lastSentSpeed = newSpeed;
while(lastSentSpeed.offer(new Float(newSpeed))==false){
log.debug("failed attempting to add speed to queue");
}
throttle.setSpeedSetting(newSpeed);
}

@@ -752,7 +756,12 @@ protected void handleMomentary(String inPackage) {
protected void handleRequest(String inPackage) {
switch (inPackage.charAt(0)) {
case 'V': {
sendCurrentSpeed(throttle);
if(lastSentSpeed.isEmpty()){
// send the current speed only
// if we aren't waiting for the back end
// to update the speed.
sendCurrentSpeed(throttle);
}
break;
}
case 'R': {
@@ -225,6 +225,8 @@ public void setSpeedSetting(float s){
controller.propertyChange(new PropertyChangeEvent(this,"SpeedSetting",32.0f/126.0f,45.0f/126.0f));
Assert.assertNull("outgoing message after property change", cis.getLastPacket() );
controller.propertyChange(new PropertyChangeEvent(this,"SpeedSetting",0.0f,63.0f/126.0f));
Assert.assertNull("outgoing message after property change", cis.getLastPacket() );
controller.propertyChange(new PropertyChangeEvent(this,"SpeedSetting",0.0f,63.0f/126.0f));
Assert.assertEquals("outgoing message after property change", "MAAtest<;>V63",cis.getLastPacket() );
}

0 comments on commit becc445

Please sign in to comment.
You can’t perform that action at this time.