/
LocoNetThrottle.java
669 lines (609 loc) · 26 KB
/
LocoNetThrottle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package jmri.jmrix.loconet;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.annotation.CheckForNull;
import jmri.DccLocoAddress;
import jmri.DccThrottle;
import jmri.LocoAddress;
import jmri.Throttle;
import jmri.SpeedStepMode;
import jmri.jmrix.AbstractThrottle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jmri.ThrottleListener;
/**
* An implementation of DccThrottle via AbstractThrottle with code specific to a
* LocoNet connection.
* <p>
* Speed in the Throttle interfaces and AbstractThrottle is a float, but in
* LocoNet is an int with values from 0 to 127.
*
* @author Glen Oberhauser, Bob Jacobsen Copyright (C) 2003, 2004
* @author Stephen Williams Copyright (C) 2008
* @author B. Milhaupt, Copyright (C) 2018
*/
public class LocoNetThrottle extends AbstractThrottle implements SlotListener {
protected LocoNetSlot slot;
protected LocoNetInterface network;
protected LnThrottleManager throttleManager;
protected int address;
// members to record the last known spd/dirf/snd bytes AS READ FROM THE LAYOUT!!
protected int layout_spd;
protected int layout_dirf;
protected int layout_snd;
protected int layout_stat1 = 0;
// slot status to be warned if slot released or dispatched
protected int slotStatus;
protected boolean isDisposing = false;
// set isInitialized to false to enable setting the throttle ID.
protected boolean isInitialized = false;
/**
* Constructor
*
* @param memo connection details
* @param slot The LocoNetSlot this throttle will talk on.
*/
public LocoNetThrottle(LocoNetSystemConnectionMemo memo, LocoNetSlot slot) {
super(memo);
this.slot = slot;
network = memo.getLnTrafficController();
throttleManager = (LnThrottleManager)memo.getThrottleManager();
// save last known layout state for spd/dirf/snd so we can
// avoid race condition if another LocoNet process queries
// our slot while we are in the act of changing it.
layout_spd = slot.speed();
layout_dirf = slot.dirf();
layout_snd = slot.snd();
// cache settings
this.speedSetting = floatSpeed(slot.speed());
for (int i = 0; i < 29; i++) {
super.updateFunction(i,slot.isFunction(i));
}
// for LocoNet throttles, the default is f2 momentary (for the horn)
// all other functions are continuos (as set in AbstractThrottle).
this.f2Momentary = true;
this.address = slot.locoAddr();
this.isForward = slot.isForward();
this.slotStatus = slot.slotStatus();
switch (slot.decoderType()) {
case LnConstants.DEC_MODE_128:
case LnConstants.DEC_MODE_128A:
setSpeedStepMode(SpeedStepMode.NMRA_DCC_128);
break;
case LnConstants.DEC_MODE_28:
case LnConstants.DEC_MODE_28A:
case LnConstants.DEC_MODE_28TRI:
setSpeedStepMode(SpeedStepMode.NMRA_DCC_28);
break;
case LnConstants.DEC_MODE_14:
setSpeedStepMode(SpeedStepMode.NMRA_DCC_14);
break;
default:
log.warn("Unhandled decoder type: {}", slot.decoderType());
break;
}
// listen for changes
slot.addSlotListener(this);
// perform the null slot move
LocoNetMessage msg = new LocoNetMessage(4);
msg.setOpCode(LnConstants.OPC_MOVE_SLOTS);
msg.setElement(1, slot.getSlot());
msg.setElement(2, slot.getSlot());
network.sendLocoNetMessage(msg);
// start periodically sending the speed, to keep this
// attached
startRefresh();
log.debug("constructed a new throttle using slot {} for loco address {}", slot.getSlot(), slot.locoAddr());
}
/**
* Convert a LocoNet speed integer to a float speed value
*
* @param lSpeed LocoNet style speed value
* @return speed as float 0->1.0, or -1.0 to indicate E-Stop
*/
protected float floatSpeed(int lSpeed) {
log.debug("speed (int) is {}", lSpeed);
if (lSpeed == 0) {
return 0.f;
} else if (lSpeed == 1) {
return -1.f; // estop
}
if (getSpeedStepMode() == SpeedStepMode.NMRA_DCC_28) {
if (lSpeed <= 15) //Value less than 15 is in the stop/estop range bracket
{
return 0.f;
}
return (((lSpeed - 12) / 4f) / 28.f);
} else if (getSpeedStepMode() == SpeedStepMode.NMRA_DCC_14) {
if (lSpeed <= 15) //Value less than 15 is in the stop/estop range bracket
{
return 0.f;
}
return ((lSpeed - 8) / 8f) / 14.f;
} else {
return ((lSpeed - 1) / 126.f);
}
}
/**
* Computes the integer speed value from a float.
* <p>
* Values of less than 0 indicate Emergency Stop.
* <p>
* Value of 0.0 indicates stop.
* <p>
* Values between 0.0+ and 1.0 imply speed step values between 2 and the
* maximum value allowed for the loco's speed step mode.
*
* @param fSpeed is the floating-point speed value to be converted
* @return an integer which represents the speed step value
*/
@Override
protected int intSpeed(float fSpeed) {
log.debug("intSpeed speed is {}", fSpeed);
int speed = super.intSpeed(fSpeed);
if (speed <= 1) {
return speed; // return idle and emergency stop
}
switch (this.getSpeedStepMode()) {
case NMRA_DCC_28:
case MOTOROLA_28:
return (int) ((fSpeed * 28) * 4) + 12;
case NMRA_DCC_14:
return (int) ((fSpeed * 14) * 8) + 8;
case NMRA_DCC_128:
return speed;
default:
log.warn("Unhandled speed step: {}", this.getSpeedStepMode());
break;
}
return speed;
}
/**
* Send the LocoNet message to set the state of locomotive direction and
* functions F0, F1, F2, F3, F4
*/
@Override
protected void sendFunctionGroup1() {
int new_dirf = ((getIsForward() ? 0 : LnConstants.DIRF_DIR)
| (getF0() ? LnConstants.DIRF_F0 : 0)
| (getF1() ? LnConstants.DIRF_F1 : 0)
| (getF2() ? LnConstants.DIRF_F2 : 0)
| (getF3() ? LnConstants.DIRF_F3 : 0)
| (getF4() ? LnConstants.DIRF_F4 : 0));
log.debug("sendFunctionGroup1 sending {} to LocoNet slot {}", new_dirf, slot.getSlot());
LocoNetMessage msg = new LocoNetMessage(4);
msg.setOpCode(LnConstants.OPC_LOCO_DIRF);
msg.setElement(1, slot.getSlot());
msg.setElement(2, new_dirf);
network.sendLocoNetMessage(msg);
}
/**
* Send the LocoNet message to set the state of functions F5, F6, F7, F8
*/
@Override
protected void sendFunctionGroup2() {
int new_snd = ((getF8() ? LnConstants.SND_F8 : 0)
| (getF7() ? LnConstants.SND_F7 : 0)
| (getF6() ? LnConstants.SND_F6 : 0)
| (getF5() ? LnConstants.SND_F5 : 0));
log.debug("sendFunctionGroup2 sending {} to LocoNet slot {}", new_snd, slot.getSlot());
LocoNetMessage msg = new LocoNetMessage(4);
msg.setOpCode(LnConstants.OPC_LOCO_SND);
msg.setElement(1, slot.getSlot());
msg.setElement(2, new_snd);
network.sendLocoNetMessage(msg);
}
/**
* Sends Function Group 3 values - F9 thru F12, using an "OPC_IMM_PACKET" LocoNet
* Message.
*/
@Override
protected void sendFunctionGroup3() {
// LocoNet practice is to send F9-F12 as a DCC packet
byte[] result = jmri.NmraPacket.function9Through12Packet(address, (address >= 128),
getF9(), getF10(), getF11(), getF12());
log.debug("sendFunctionGroup3 sending {} to LocoNet slot {}", result, slot.getSlot());
((jmri.CommandStation) adapterMemo.get(jmri.CommandStation.class)).sendPacket(result, 4); // repeat = 4
}
/**
* Sends Function Group 4 values - F13 thru F20, using an "OPC_IMM_PACKET" LocoNet
* Message.
*/
@Override
protected void sendFunctionGroup4() {
// LocoNet practice is to send F13-F20 as a DCC packet
byte[] result = jmri.NmraPacket.function13Through20Packet(address, (address >= 128),
getF13(), getF14(), getF15(), getF16(),
getF17(), getF18(), getF19(), getF20());
log.debug("sendFunctionGroup4 sending {} to LocoNet slot {}", result, slot.getSlot());
((jmri.CommandStation) adapterMemo.get(jmri.CommandStation.class)).sendPacket(result, 4); // repeat = 4
}
/**
* Sends Function Group 5 values - F21 thru F28, using an "OPC_IMM_PACKET" LocoNet
* Message.
*/
@Override
protected void sendFunctionGroup5() {
// LocoNet practice is to send F21-F28 as a DCC packet
byte[] result = jmri.NmraPacket.function21Through28Packet(address, (address >= 128),
getF21(), getF22(), getF23(), getF24(),
getF25(), getF26(), getF27(), getF28());
log.debug("sendFunctionGroup5 sending {} to LocoNet slot {}", result, slot.getSlot());
((jmri.CommandStation) adapterMemo.get(jmri.CommandStation.class)).sendPacket(result, 4); // repeat = 4
}
/**
* Send a LocoNet message to set the loco speed speed.
*
* @param speed Number from 0 to 1; less than zero is "emergency stop"
*/
@Override
public void setSpeedSetting(float speed) {
setSpeedSetting(speed, false, false);
}
/**
* Set the Speed, ensuring that a LocoNet message is sent to update the slot
* even if the new speed is effectively the same as the current speed. Note: this
* can cause an increase in LocoNet traffic.
*
* @param speed Number from 0 to 1; less than zero is emergency stop
*/
@Override
public void setSpeedSettingAgain(float speed) {
setSpeedSetting(speed, true, true);
}
/**
* Set the speed. No LocoNet message is sent if the new speed would
* result in a 'duplicate' - ie. a speed setting no different to the one the slot
* currently has - unless the boolean paramters indicate it should be.
*
* @param speed Number from 0 to 1; less than zero is emergency stop
* @param allowDuplicates boolean - if true, send a LocoNet message no matter what
* @param allowDuplicatesOnStop boolean - if true, send a LocoNet message if the new speed is
* 'idle' or 'emergency stop', even if that matches the
* existing speed.
*
*/
@SuppressFBWarnings(value = "FE_FLOATING_POINT_EQUALITY") // OK to compare floating point, notify on any change
@Override
public void setSpeedSetting(float speed, boolean allowDuplicates, boolean allowDuplicatesOnStop) {
log.debug("setSpeedSetting: called with speed {} for LocoNet slot {}", speed, slot.getSlot());
if (LnConstants.CONSIST_MID == slot.consistStatus()
|| LnConstants.CONSIST_SUB == slot.consistStatus()) {
// Digitrax slots use the same memory location to store the
// speed AND the slot to which a locomotive is consisted.
// if the locomotive is either a CONSIST_MID or a CONSIST_SUB,
// we need to ignore the request to change the speed
log.debug("Attempt to change speed on locomotive {} which is a {}", getLocoAddress(), LnConstants.CONSIST_STAT(slot.consistStatus()));
return;
}
float oldSpeed = this.speedSetting;
this.speedSetting = speed;
if (speed < 0) {
this.speedSetting = -1.f;
}
int new_spd = intSpeed(speed);
// decide whether to send a new LocoNet message
boolean sendLoconetMessage = false;
if (new_spd != layout_spd) {
// the new speed is different - send a message
sendLoconetMessage = true;
} else if (allowDuplicates) {
// calling method wants a new mesage sent regardless
sendLoconetMessage = true;
} else if (allowDuplicatesOnStop && new_spd <= 1) {
// calling method wants a new message sent if the speed is idle or estop, which it is
sendLoconetMessage = true;
}
if (sendLoconetMessage) {
log.debug("setSpeedSetting: sending speed {} to LocoNet slot {}", speed, slot.getSlot());
LocoNetMessage msg = new LocoNetMessage(4);
msg.setOpCode(LnConstants.OPC_LOCO_SPD);
msg.setElement(1, slot.getSlot());
log.debug("setSpeedSetting: float speed: " + speed + " LocoNet speed: " + new_spd);
msg.setElement(2, new_spd);
network.sendLocoNetMessage(msg);
} else {
log.debug("setSpeedSetting: not sending LocoNet message to slot {}, new speed == old speed", slot.getSlot());
}
// reset timeout
if (mRefreshTimer != null) {
mRefreshTimer.stop();
mRefreshTimer.setRepeats(true); // refresh until stopped by dispose
mRefreshTimer.start();
log.debug("Initially starting refresh timer for slot {} address {}", slot.getSlot(), slot.locoAddr());
}
if (oldSpeed != this.speedSetting) {
notifyPropertyChangeListener(SPEEDSETTING, oldSpeed, this.speedSetting); // NOI18N
}
record(speed);
}
/**
* Sends a LocoNet message containing the specified direction of travel.
*
* LocoNet actually puts forward and backward in the same message as the
* first function group.
*
* @param forward is true for forward movement, else false
*/
@Override
public void setIsForward(boolean forward) {
boolean old = isForward;
isForward = forward;
log.debug("setIsForward to {}, old value {}", isForward, old);
sendFunctionGroup1();
if (old != this.isForward) {
notifyPropertyChangeListener(ISFORWARD, old, this.isForward); // NOI18N
}
}
/**
* Returns the LocoNetSlot which is used for controlling the loco assoicated
* with this throttle.
*
* @return the LocoNetSlot
*/
@CheckForNull
public LocoNetSlot getLocoNetSlot() {
if (slot == null) return slot;
log.debug("getLocoNetSlot is returning slot {}", slot.getSlot());
return slot;
}
@Override
public String toString() {
return getLocoAddress().toString();
}
/**
* Dispose the LocoNetThrottle when finished with this object.
*
* After this is executed, further use of this Throttle object will
* result in a JmriException.
*/
@Override
protected void throttleDispose() {
if (isDisposing) return;
log.debug("throttleDispose - disposing of throttle (and setting slot = null)");
isDisposing = true;
// Release throttle connections
if (slot != null) {
if (slot.slotStatus() != LnConstants.LOCO_COMMON) {
// Digitrax throttles do not set the slot speed to zero, so do
// not do so here.
// Make the slot common, after a little wait
log.debug("dispatchThrottle is dispatching slot {}", slot);
network.sendLocoNetMessage(slot.releaseSlot());
}
// Can remove the slot listener at any time; any further messages
// aren't needed.
slot.removeSlotListener(this);
// Stop the throttle speed refresh timer
if (mRefreshTimer != null) {
mRefreshTimer.stop();
log.debug("Stopped refresh timer for slot {} address {} as part of throttleDispose", slot.getSlot(), slot.locoAddr());
mRefreshTimer = null;
}
slot = null;
network = null;
finishRecord();
isDisposing = false;
}
}
javax.swing.Timer mRefreshTimer = null;
/**
* Starts the "refresh" timer. The "refresh" timer determines
* when to send a new LocoNet message to "refresh" the slot's speed
* setting, so that the slot does not get "purged".
*
*/
protected void startRefresh() {
mRefreshTimer = new javax.swing.Timer(50000, new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
timeout();
}
});
mRefreshTimer.setRepeats(true); // refresh until stopped by dispose
mRefreshTimer.start();
log.debug("Starting refresh timer for slot {} address {}", slot.getSlot(), slot.locoAddr());
}
/**
* Internal routine to resend the speed on a timeout
*/
synchronized protected void timeout() {
if (slot != null) {
log.debug("refresh timer timed-out on slot {}", slot.getSlot());
// clear the last known layout_spd so that we will actually send the
// message.
layout_spd = -1;
setSpeedSetting(speedSetting);
}
else {
log.debug("refresh timer time-out on a null slot");
}
}
/**
* Get notified when underlying slot acquisition process fails. Slot acquisition
* failure is handled by @link LnThrottleManager, so no code is required here.
*
* @param addr Locomotive address
* @param s reason the acquisition failed
*/
public void notifyRefused(int addr, String s) {
// don't do anything here; is handled by LnThrottleManager.
}
/**
* Get notified when underlying slot information changes
*
* @param pSlot the slot which was changed
*/
@SuppressFBWarnings(value = "FE_FLOATING_POINT_EQUALITY") // OK to compare floating point, notify on any change
@Override
public void notifyChangedSlot(LocoNetSlot pSlot) {
if (slot != pSlot) {
log.error("notified of change in different slot");
}
log.debug("notifyChangedSlot executing for slot {}, slotStatus {}", slot.getSlot(), Integer.toHexString(slot.slotStatus()));
if(!isInitialized && slot.slotStatus() == LnConstants.LOCO_IN_USE){
log.debug("Attempting to update slot with this JMRI instance's throttle id ({})", throttleManager.getThrottleID());
network.sendLocoNetMessage(slot.writeThrottleID(throttleManager.getThrottleID()));
isInitialized = true;
}
// Save current layout state of spd/dirf/snd so we won't run amok
// toggling values if another LocoNet entity accesses the slot while
// our most recent change request is still in-flight.
layout_spd = slot.speed();
layout_dirf = slot.dirf();
layout_snd = slot.snd();
// handle change in each state
if (this.speedSetting != floatSpeed(slot.speed())) {
Float newSpeed = Float.valueOf(floatSpeed(slot.speed()));
log.debug("notifyChangedSlot: old speed: " + this.speedSetting + " new Speed: " + newSpeed); // NOI18N
notifyPropertyChangeListener(SPEEDSETTING, Float.valueOf(this.speedSetting), newSpeed); // NOI18N
this.speedSetting = newSpeed.floatValue();
}
boolean temp;
if (this.isForward != slot.isForward()) {
temp = this.isForward;
this.isForward = slot.isForward();
notifyPropertyChangeListener(ISFORWARD, Boolean.valueOf(temp), Boolean.valueOf(slot.isForward())); // NOI18N
}
// Slot status
if (slotStatus != slot.slotStatus()) {
int newStat = slot.slotStatus();
if (log.isDebugEnabled()) {
log.debug("Slot status changed from " + LnConstants.LOCO_STAT(slotStatus) + " to " + LnConstants.LOCO_STAT(newStat)); // NOI18N
}
// PropertyChangeListeners notification: ThrottleConnected from True to False when disconnected
notifyPropertyChangeListener("ThrottleConnected", (slotStatus & LnConstants.LOCOSTAT_MASK) == LnConstants.LOCO_IN_USE, // NOI18N
!((slotStatus & LnConstants.LOCOSTAT_MASK) == LnConstants.LOCO_IN_USE));
slotStatus = newStat;
}
// It is possible that the slot status change we are being notified of
// is the slot being set to status COMMON. In which case the slot just
// got set to null. No point in continuing. In fact to do so causes a NPE.
if (slot == null) {
return;
}
switch (slot.decoderType()) {
case LnConstants.DEC_MODE_128:
case LnConstants.DEC_MODE_128A:
if(SpeedStepMode.NMRA_DCC_128 != getSpeedStepMode()) {
setSpeedStepMode(SpeedStepMode.NMRA_DCC_128);
}
break;
case LnConstants.DEC_MODE_28:
case LnConstants.DEC_MODE_28A:
case LnConstants.DEC_MODE_28TRI:
if(SpeedStepMode.NMRA_DCC_28 != getSpeedStepMode()) {
setSpeedStepMode(SpeedStepMode.NMRA_DCC_28);
}
break;
case LnConstants.DEC_MODE_14:
if(SpeedStepMode.NMRA_DCC_14 != getSpeedStepMode()) {
setSpeedStepMode(SpeedStepMode.NMRA_DCC_14);
}
break;
default:
log.warn("Unhandled decoder type: {}", slot.decoderType());
break;
}
// Functions
for (int i = 0; i < 29; i++) {
updateFunction(i,slot.isFunction(i));
}
}
/**
* Set the speed step value and the related speedIncrement value.
*
* @param Mode the current speed step mode - default should be 128
* speed step mode in most cases
*/
@Override
public void setSpeedStepMode(SpeedStepMode Mode) {
int status = slot.slotStatus();
if (log.isDebugEnabled()) {
log.debug("Speed Step Mode Change to Mode: " + Mode // NOI18N
+ " Current mode is: " + this.speedStepMode); // NOI18N
log.debug("Current Slot Mode: " + LnConstants.DEC_MODE(status)); // NOI18N
}
if (speedStepMode != Mode) {
notifyPropertyChangeListener(SPEEDSTEPS, this.speedStepMode, // NOI18N
this.speedStepMode = Mode);
}
if (Mode == SpeedStepMode.NMRA_DCC_14) {
log.debug("14 speed step change"); // NOI18N
status = status & ((~LnConstants.DEC_MODE_MASK)
| LnConstants.STAT1_SL_SPDEX)
| LnConstants.DEC_MODE_14;
} else if (Mode == SpeedStepMode.MOTOROLA_28) {
log.debug("28-Tristate speed step change");
status = status & ((~LnConstants.DEC_MODE_MASK)
| LnConstants.STAT1_SL_SPDEX)
| LnConstants.DEC_MODE_28TRI;
} else if (Mode == SpeedStepMode.NMRA_DCC_28) {
log.debug("28 speed step change");
status = status & ((~LnConstants.DEC_MODE_MASK)
| LnConstants.STAT1_SL_SPDEX);
// | LnConstants.DEC_MODE_28; // DEC_MODE_28 has a zero value, here for documentation
// it unfortunately shows a INT_VACUOUS_BIT_OPERATION in SpotBugs
// and I don't want to annote that around this entire long method
} else { // default to 128 speed step mode
log.debug("128 speed step change");
status = status & ((~LnConstants.DEC_MODE_MASK)
| LnConstants.STAT1_SL_SPDEX)
| LnConstants.DEC_MODE_128;
}
if (log.isDebugEnabled()) {
log.debug("New Slot Mode: " + LnConstants.DEC_MODE(status));
}
if (mRefreshTimer != null) // the refresh timer isn't created until
// after initilization. We only want to
// modify the slot after the initilization
// is complete.
{
network.sendLocoNetMessage(slot.writeMode(status));
}
}
/**
* Get the address controlled by this throttle. If the throttle is controlling.
*
* @return a LocoAddress for the address controlled by this throttle
*/
@Override
public LocoAddress getLocoAddress() {
if (slot != null) {
if ((slot.slotStatus() == LnConstants.LOCO_IN_USE) ||
(slot.slotStatus() == LnConstants.LOCO_COMMON)) {
log.debug("getLocoAddress replying address {} for slot {}", address, slot.getSlot());
return new DccLocoAddress(address, LnThrottleManager.isLongAddress(address));
}
}
log.debug("getLocoAddress replying address {} for slot not in-use or for sub-consisted slot or for null slot", address);
return new DccLocoAddress(address, LnThrottleManager.isLongAddress(address));
}
/**
* "Dispatch" a LocoNet throttle by setting the slot as "common" then performing
* a slot move to slot 0.
* <p>
* The throttle being dispatched no longer has control of the loco, but other
* throttles may continue to control the loco.
*
* @param t throttle being dispatched
* @param l throttle listener to remove
*/
public void dispatchThrottle(DccThrottle t, ThrottleListener l) {
log.debug("dispatchThrottle - throttle {}", t.getLocoAddress());
// set status to common & dispatch slot
// needs to be done one after another with no delay.
if (t instanceof LocoNetThrottle){
LocoNetThrottle lnt = (LocoNetThrottle) t;
LocoNetSlot tSlot = lnt.getLocoNetSlot();
if (tSlot != null) {
if (tSlot.slotStatus() != LnConstants.LOCO_COMMON) {
network.sendLocoNetMessage(tSlot.writeStatus(LnConstants.LOCO_COMMON));
log.debug("dispatchThrottle is dispatching slot {}", tSlot);
network.sendLocoNetMessage(tSlot.dispatchSlot());
}
}
}
}
// initialize logging
private final static Logger log = LoggerFactory.getLogger(LocoNetThrottle.class);
}