/
NmraPacket.java
1589 lines (1406 loc) · 57 KB
/
NmraPacket.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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package jmri;
import javax.annotation.CheckForNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utilities for coding/decoding NMRA {@literal S&RP} DCC packets.
* <p>
* Packets are (now) represented by an array of bytes. Preamble/postamble not
* included. Note that this is a data representation, _not_ a representation of
* the waveform! But this is a class, which might eventually also form a
* representation object.
* <p>
* This is meant to be a general Java NMRA implementation, so does NOT use JMRI
* utilities. In particular, it returns null instead of throwing JmriException
* for invalid requests. Callers need to check upstream.
* <p>
* The function is provided by static member functions; objects of this class
* should not be created.
* <p>
* Note that these functions are structured by packet type, not by what want to
* do. E.g. there are functions to create specific packet formats instead of a
* general "loco speed packet" routine which figures out which type of packet to
* use. Those decisions are to be made somewhere else.
* <p>
* Range and value checking is intended to be aggressive; if we can check, we
* should. Problems are reported as warnings.
* <p>
* The basic function is to build a packet with proper addressing, etc:
* <ul>
* <li>oneBytePacket
* <li>twoBytePacket
* <li>threeBytePacket
* <li>fourBytePacket
* </ul>
* On top of those are built various special-purpose packet formats.
* <hr>
* This file is part of JMRI.
* <p>
* JMRI is free software; you can redistribute it and/or modify it under the
* terms of version 2 of the GNU General Public License as published by the Free
* Software Foundation. See the "COPYING" file for a copy of this license.
* <p>
* JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* @author Bob Jacobsen Copyright (C) 2001, 2003
*/
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
justification = "null returned is documented in each method to mean no valid result")
public class NmraPacket {
static final public int accIdLowLimit = 1;
static final public int accIdHighLimit = 2044;
static final public int accIdAltHighLimit = 2048;
/**
* Create a packet containing a decoder idle instruction.
*
* @return the packet as a byte array or null if the address is not valid
*/
@CheckForNull
public static byte[] idlePacket() {
byte[] retVal;
retVal = new byte[3];
retVal[0] = (byte) (0xFF); // address byte for decoder idle
retVal[1] = (byte) (0); // decoder idle instruction
retVal[2] = (byte) (0xFF); // checksum byte
return retVal;
}
/**
* Create a packet containing a one-byte instruction.
*
* @param address the address to send the instruction to
* @param longAddr true if address is long, false otherwise
* @param byte1 the byte to send as an instruction
* @return the packet as a byte array or null if the address is not valid
*/
@CheckForNull
public static byte[] oneBytePacket(int address, boolean longAddr, byte byte1) {
if (!addressCheck(address, longAddr)) {
return null; // failed!
}
// end sanity check, format output
byte[] retVal;
if (longAddr) {
// long address form
retVal = new byte[4];
retVal[0] = (byte) (192 + ((address / 256) & 0x3F));
retVal[1] = (byte) (address & 0xFF);
retVal[2] = byte1;
retVal[3] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2]);
} else {
// short address form
retVal = new byte[3];
retVal[0] = (byte) (address & 0xFF);
retVal[1] = byte1;
retVal[2] = (byte) (retVal[0] ^ retVal[1]);
}
return retVal;
}
/**
* Create a packet containing a two-byte instruction.
*
* @param address the address to send the instruction to
* @param longAddr true if address is long, false otherwise
* @param byte1 first byte in the instruction
* @param byte2 second byte in the instruction
* @return the packet as a byte array or null if the address is not valid
*/
@CheckForNull
public static byte[] twoBytePacket(int address, boolean longAddr, byte byte1, byte byte2) {
if (!addressCheck(address, longAddr)) {
return null; // failed!
}
// end sanity check, format output
byte[] retVal;
if (longAddr) {
// long address form
retVal = new byte[5];
retVal[0] = (byte) (192 + ((address / 256) & 0x3F));
retVal[1] = (byte) (address & 0xFF);
retVal[2] = byte1;
retVal[3] = byte2;
retVal[4] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3]);
} else {
// short address form
retVal = new byte[4];
retVal[0] = (byte) (address & 0xFF);
retVal[1] = byte1;
retVal[2] = byte2;
retVal[3] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2]);
}
return retVal;
}
/**
* Create a packet containing a three-byte instruction.
*
* @param address the address to send the instruction to
* @param longAddr true if address is long, false otherwise
* @param byte1 first byte in the instruction
* @param byte2 second byte in the instruction
* @param byte3 third byte in the instruction
* @return the packet as a byte array or null if the address is not valid
*/
@CheckForNull
public static byte[] threeBytePacket(int address, boolean longAddr, byte byte1, byte byte2, byte byte3) {
if (!addressCheck(address, longAddr)) {
return null; // failed!
}
// end sanity check, format output
byte[] retVal;
if (longAddr) {
// long address form
retVal = new byte[6];
retVal[0] = (byte) (192 + ((address / 256) & 0x3F));
retVal[1] = (byte) (address & 0xFF);
retVal[2] = byte1;
retVal[3] = byte2;
retVal[4] = byte3;
retVal[5] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3] ^ retVal[4]);
} else {
// short address form
retVal = new byte[5];
retVal[0] = (byte) (address & 0xFF);
retVal[1] = byte1;
retVal[2] = byte2;
retVal[3] = byte3;
retVal[4] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3]);
}
return retVal;
}
/**
* Create a packet containing a four-byte instruction.
*
* @param address the address to send the instruction to
* @param longAddr true if address is long, false otherwise
* @param byte1 first byte in the instruction
* @param byte2 second byte in the instruction
* @param byte3 third byte in the instruction
* @param byte4 forth byte in the instruction
* @return the packet as a byte array or null if the address is not valid
*/
@CheckForNull
public static byte[] fourBytePacket(int address, boolean longAddr, byte byte1, byte byte2, byte byte3, byte byte4) {
if (!addressCheck(address, longAddr)) {
return null; // failed!
}
// end sanity check, format output
byte[] retVal;
if (longAddr) {
// long address form
retVal = new byte[7];
retVal[0] = (byte) (192 + ((address / 256) & 0x3F));
retVal[1] = (byte) (address & 0xFF);
retVal[2] = byte1;
retVal[3] = byte2;
retVal[4] = byte3;
retVal[5] = byte4;
retVal[6] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3] ^ retVal[4] ^ retVal[5]);
} else {
// short address form
retVal = new byte[6];
retVal[0] = (byte) (address & 0xFF);
retVal[1] = byte1;
retVal[2] = byte2;
retVal[3] = byte3;
retVal[4] = byte4;
retVal[5] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3] ^ retVal[4]);
}
return retVal;
}
public static byte[] accDecoderPkt(int addr, int active, int outputChannel) {
// From the NMRA RP:
// 0 10AAAAAA 0 1AAACDDD 0 EEEEEEEE 1
// Accessory Digital Decoders can be designed to control momentary or
// constant-on devices, the duration of time each output is active being controlled
// by configuration variables CVs #515 through 518. Bit 3 of the second byte "C" is
// used to activate or deactivate the addressed device. (Note if the duration the
// device is intended to be on is less than or equal the set duration, no deactivation
// is necessary.) Since most devices are paired, the convention is that bit "0" of
// the second byte is used to distinguish between which of a pair of outputs the
// accessory decoder is activating or deactivating. Bits 1 and 2 of byte two is used
// to indicate which of 4 pairs of outputs the packet is controlling. The significant
// bits of the 9 bit address are bits 4-6 of the second data byte. By convention
// these three bits are in ones complement. The use of bit 7 of the second byte
// is reserved for future use.
// Note that A=1 is the first (lowest) valid address field, and the
// largest is 512! I don't know why this is, but it gets the
// right hardware addresses
if (addr < 1 || addr > 511) {
log.error("invalid address {}", addr);
//return null;
throw new IllegalArgumentException();
}
if (active < 0 || active > 1) {
log.error("invalid active (C) bit {}", addr);
return null;
}
if (outputChannel < 0 || outputChannel > 7) {
log.error("invalid output channel {}", addr);
return null;
}
int lowAddr = addr & 0x3F;
int highAddr = ((~addr) >> 6) & 0x07;
byte[] retVal = new byte[3];
retVal[0] = (byte) (0x80 | lowAddr);
retVal[1] = (byte) (0x80 | (highAddr << 4) | (active << 3) | outputChannel & 0x07);
retVal[2] = (byte) (retVal[0] ^ retVal[1]);
return retVal;
}
/**
* Provide a basic operations mode accessory CV programming packet.
* <br><br>
* From the NMRA Standard: Basic Accessory Decoder Packet address for
* operations mode programming
* <br><br>
* 10AAAAAA 0 1AAACDDD
* <br><br>
* Where DDD is used to indicate the output whose CVs are being modified and
* C=1.
* <br>
* If CDDD= 0000 then the CVs refer to the entire decoder.
* <br><br>
* The resulting packet would be
* <br><br>
* {preamble} 10AAAAAA 0 1AAACDDD 0 (1110CCVV 0 VVVVVVVV 0 DDDDDDDD) 0
* EEEEEEEE 1
*
* @param addr the decoder address
* @param active 1 or 0
* @param outputChannel the output on the accessory
* @param cvNum the CV
* @param data the data
* @return a packet
*/
public static byte[] accDecoderPktOpsMode(int addr, int active, int outputChannel, int cvNum, int data) {
if (addr < 1 || addr > 511) {
log.error("invalid address {}", addr);
throw new IllegalArgumentException();
}
if (active < 0 || active > 1) {
log.error("invalid active (C) bit {}", addr);
return null;
}
if (outputChannel < 0 || outputChannel > 7) {
log.error("invalid output channel {}", addr);
return null;
}
if (cvNum < 1 || cvNum > 1024) {
log.error("invalid CV number {}", cvNum);
return null;
}
if (data < 0 || data > 255) {
log.error("invalid data {}", data);
return null;
}
int lowAddr = addr & 0x3F;
int highAddr = ((~addr) >> 6) & 0x07;
// log.info("addr = {} active = {} outputChannel = {} cvNum = {} data = {}", addr, active, outputChannel, cvNum, data);
// log.info("hex lowAddr = {} highAddr = {}", String.format("%H", lowAddr), String.format("%H", highAddr));
// log.info("lowAddr = {} highAddr = {}", lowAddr, highAddr);
int lowCVnum = (cvNum - 1) & 0xFF;
int highCVnum = ((cvNum - 1) >> 8) & 0x03;
byte[] retVal = new byte[6];
retVal[0] = (byte) (0x80 | lowAddr);
retVal[1] = (byte) (0x80 | (highAddr << 4) | (active << 3) | outputChannel & 0x07);
retVal[2] = (byte) (0xEC | highCVnum);
retVal[3] = (byte) (lowCVnum);
retVal[4] = (byte) (0xFF & data);
retVal[5] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3] ^ retVal[4]);
return retVal;
}
/**
* Provide a legacy operations mode accessory CV programming packet via a
* simplified interface, given a decoder address.
* <br><br>
* From the NMRA Standard: The format for Accessory Decoder Configuration
* Variable Access Instructions is: {preamble} 0 10AAAAAA 0 0AAA11VV 0
* VVVVVVVV 0 DDDDDDDD 0 EEEEEEEE 1 Where: A = Decoder address bits V =
* Desired CV address - (CV 513 = 10 00000000) D = Data for CV
* <br><br>
* This is the old "legacy" format, newer decoders use the "Basic Accessory
* Decoder Packet"
*
* @param decAddr Address of decoder, in the range 1 to 511
* @param cvNum the CV
* @param data the data
* @return a packet
*/
public static byte[] accDecPktOpsModeLegacy(int decAddr, int cvNum, int data) {
if (decAddr < 1 || decAddr > 511) {
log.error("invalid address {}", decAddr);
return null;
}
if (cvNum < 1 || cvNum > 1024) {
log.error("invalid CV number {}", cvNum);
return null;
}
if (data < 0 || data > 255) {
log.error("invalid data {}", data);
return null;
}
int lowAddr = decAddr & 0x3F;
int highAddr = ((~decAddr) >> 6) & 0x07;
int lowCVnum = (cvNum - 1) & 0xFF;
int highCVnum = ((cvNum - 1) >> 8) & 0x03;
byte[] retVal = new byte[5];
retVal[0] = (byte) (0x80 | lowAddr);
retVal[1] = (byte) (0x0C | (highAddr << 4) | highCVnum);
retVal[2] = (byte) (lowCVnum);
retVal[3] = (byte) (0xFF & data);
retVal[4] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3]);
return retVal;
}
/**
* Create a signal accessory instruction packet.
* <p>
* From the RP: Extended Accessory Decoder Control Packet Format The
* Extended Accessory Decoder Control Packet is included for the purpose of
* transmitting aspect control to signal decoders or data bytes to more
* complex accessory decoders. Each signal head can display one aspect at a
* time.
* <p>
* {@code {preamble} 0 10AAAAAA 0 0AAA0AA1 0 000XXXXX 0 EEEEEEEE 1}
* <p>
* XXXXX is for a single head. A value of 00000 for XXXXX indicates the
* absolute stop aspect. All other aspects represented by the values for
* XXXXX are determined by the signaling system used and the prototype being
* modeled.
* <p>
* Despite this being an NMRA standard, or perhaps because of it, the
* addressing is not clear. The other form of packet generated by
* {@link #altAccSignalDecoderPkt(int, int)} seems to be the one thats
* generally supported by hardware.
*
* @param outputAddr Address of accessory output, starting with 1 and a
* maximum of 2044
* @param aspect Aspect Number starting with 0 and a maximum of 31
* @return the instruction packet
*/
public static byte[] accSignalDecoderPkt(int outputAddr, int aspect) {
if (outputAddr < accIdLowLimit || outputAddr > accIdHighLimit) {
log.error("invalid signal decoder address {}", outputAddr);
return null;
}
outputAddr -= 1; // Make the address 0 based
int lowAddr = (outputAddr & 0x03); // Output Pair Address
int boardAddr = (outputAddr >> 2) + 1; // Board Address
return accSignalDecoderPktCommon(lowAddr, boardAddr, aspect);
}
/**
* Provide an extended operations mode accessory CV programming packet via a
* simplified interface, given a signal address.
* <br><br>
* From the NMRA Standard: Extended Decoder Packet address for operations
* mode programming
* <br><br>
* 10AAAAAA 0 0AAA0AA1
* <br><br>
* <br>
* The resulting packet would be
* <br><br>
* {preamble} 10AAAAAA 0 0AAA0AA1 0 (1110CCVV 0 VVVVVVVV 0 DDDDDDDD) 0
* EEEEEEEE 1
*
* @param addr the signal address
* @param cvNum the CV
* @param data the data
* @return a packet
*/
public static byte[] accSignalDecoderPktOpsMode(int addr, int cvNum, int data) {
if (addr < 1 || addr > 2044) {
log.error("invalid address {}", addr);
throw new IllegalArgumentException();
}
if (cvNum < 1 || cvNum > 1024) {
log.error("invalid CV number {}", cvNum);
return null;
}
if (data < 0 || data > 255) {
log.error("invalid data {}", data);
return null;
}
int outputAddr = addr - 1; // Make the address 0 based
int lowAddr = (outputAddr & 0x03);
int boardAddr = (outputAddr >> 2) + 1; // Board Address
int midAddr = (boardAddr & 0x3F);
int highAddr = (~(boardAddr >> 6)) & 0x07;
int lowCVnum = (cvNum - 1) & 0xFF;
int highCVnum = ((cvNum - 1) >> 8) & 0x03;
byte[] retVal = new byte[6];
retVal[0] = (byte) (0x80 | midAddr);
retVal[1] = (byte) (0x01 | (highAddr << 4) | (lowAddr << 1));
retVal[2] = (byte) (0xEC | highCVnum);
retVal[3] = (byte) (lowCVnum);
retVal[4] = (byte) (0xFF & data);
retVal[5] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3] ^ retVal[4]);
return retVal;
}
/**
* Determine if a packet is an Extended Accessory Decoder Control Packet
* otherwise known as a Signal Decoder Packet.
* <p>
* This inverts the computation done by the
* {@link #accSignalDecoderPkt(int, int)} method.
*
* @param packet a DCC packet to inspect
* @return true if a Signal Decoder Packet; false otherwise
*/
public static boolean isAccSignalDecoderPkt(byte[] packet) {
if (packet == null || packet.length != 3 && packet.length != 4) {
return false; // allow ECC to be present or not
}
if ((packet[0] & 0xC0) != 0x80) {
return false;
}
if ((packet[1] & 0x01) != 0x01) {
return false;
}
if ((packet[2] & 0xE0) != 0x00) {
return false;
}
return true;
}
/**
* Determine if a packet is a Basic Accessory Decoder Packet address for
* operations mode programming.
* <p>
* This inverts the computation done by the
* {@link #accDecPktOpsMode(int, int, int)} method.
*
* @param packet the packet to test
* @return true if the packet is a basic accessory decoder packet address
*/
public static boolean isAccDecoderPktOpsMode(byte[] packet) {
if (packet.length != 5 && packet.length != 6) {
return false; // allow ECC to be present or not
}
if ((packet[0] & 0xC0) != 0x80) {
return false;
}
if (((packet[1] & 0x88) != 0x88) && ((packet[1] & 0x8F) != 0x80)) {
return false;
}
if ((packet[2] & 0xFC) != 0xEC) {
return false;
}
return true;
}
/**
* Determine if a packet is a Legacy Accessory Decoder Packet address for
* operations mode programming.
* <p>
* This inverts the computation done by the
* {@link #accDecoderPktOpsModeLegacy(int, int, int)} method.
*
* @param packet the packet to extract the address from
* @return the address
*/
public static boolean isAccDecoderPktOpsModeLegacy(byte[] packet) {
if (packet.length != 4 && packet.length != 5) {
return false; // allow ECC to be present or not
}
if ((packet[0] & 0xC0) != 0x80) {
return false;
}
if ((packet[1] & 0x8C) != 0x0C) {
return false;
}
return true;
}
/**
* Recover the decoder address from a Legacy Accessory Decoder Packet Ops
* Mode Packet.
*
* @param packet the packet to extract the address from
* @return the decoder address
*/
public static int getAccDecPktOpsModeLegacyAddress(byte[] packet) {
int midAddr = packet[0] & 0x3f;
int hiAddr = ((~packet[1]) & 0x70) >> 4;
return (hiAddr << 6 | midAddr);
}
/**
* Recover the equivalent accessory address from a Legacy Accessory Decoder
* Packet Ops Mode Packet.
*
* @param packet the packet to extract the address from
* @return the accessory address
*/
public static int getAccDecoderPktOpsModeLegacyAddress(byte[] packet) {
int midAddr = packet[0] & 0x3f;
int hiAddr = ((~packet[1]) & 0x70) >> 4;
int boardAddr = (hiAddr << 6 | midAddr) - 1;
return ((boardAddr << 2)) + 1;
}
/**
* Recover the accessory address from a Basic Accessory Decoder Packet Ops
* Mode Packet.
*
* @param packet the packet to extract the address from
* @return the accessory address
*/
public static int getAccDecoderPktOpsModeAddress(byte[] packet) {
int midAddr = packet[0] & 0x3f;
int lowAddr = (packet[1] & 0x06) >> 1;
int hiAddr = ((~packet[1]) & 0x70) >> 4;
int boardAddr = (hiAddr << 6 | midAddr) - 1;
return ((boardAddr << 2) | lowAddr) + 1;
}
/**
* Recover the equivalent decoder address from a Basic Accessory Decoder
* Packet Ops Mode Packet.
*
* @param packet the packet to extract the address from
* @return the decoder address
*/
public static int getAccDecPktOpsModeAddress(byte[] packet) {
int lowAddr = packet[0] & 0x3f;
int hiAddr = ((~packet[1]) & 0x70) >> 4;
return (hiAddr << 6 | lowAddr);
}
/**
* Recover the 1-based output address from an Extended Accessory Decoder
* Control Packet otherwise known as a Signal Decoder Packet.
*
* @param packet the packet to extract the address from
* @return the address
*/
public static int getAccSignalDecoderPktAddress(byte[] packet) {
int midAddr = packet[0] & 0x3f;
int lowAddr = (packet[1] & 0x0E) >> 1;
int hiAddr = ((~packet[1]) & 0x70) >> 4;
int boardAddr = (hiAddr << 6 | midAddr) - 1;
return ((boardAddr << 2) | lowAddr) + 1;
}
/**
* An alternative interpretation of RP-9.2.1 due to an omission in the
* address definition of extended accessory packets. Since there is no such
* description for the address bits of the Extended Accessory Decoder
* Control Packet, this interpretation assumes that the least significant
* bits of the extended packet type are still in bits 1 and 2 of byte two,
* see Basic Accessory Packet.
*
* @param outputAddr Address of accessory output, starting with 1 and a
* maximum of 2044
* @param aspect Aspect Number starting with 0 and a maximum of 31
* @return a packet
*/
public static byte[] altAccSignalDecoderPkt(int outputAddr, int aspect) {
if (outputAddr < 1 || outputAddr > 2048) {
log.error("invalid signal decoder address {}", outputAddr);
return null;
}
outputAddr -= 1; // Make the address 0 based
int lowAddr = (outputAddr & 0x03); // Output Pair Address
int boardAddr = (outputAddr >> 2); // Board Address
return accSignalDecoderPktCommon(lowAddr, boardAddr, aspect);
}
/**
* Provide an extended operations mode accessory CV programming packet via a
* simplified interface, given a signal address, using the alternative
* interpretation of S-9.2.1, due to an omission in the address definition
* of extended accessory packets.
*
* @param addr the signal address
* @param cvNum the CV
* @param data the data
* @return a packet
*/
public static byte[] altAccSignalDecoderPktOpsMode(int addr, int cvNum, int data) {
if (addr < 1 || addr > 2044) {
log.error("invalid address {}", addr);
throw new IllegalArgumentException();
}
if (cvNum < 1 || cvNum > 1024) {
log.error("invalid CV number {}", cvNum);
return null;
}
if (data < 0 || data > 255) {
log.error("invalid data {}", data);
return null;
}
int outputAddr = addr - 1; // Make the address 0 based
int lowAddr = (outputAddr & 0x03);
int boardAddr = (outputAddr >> 2); // Board Address
int midAddr = (boardAddr & 0x3F);
int highAddr = (~(boardAddr >> 6)) & 0x07;
int lowCVnum = (cvNum - 1) & 0xFF;
int highCVnum = ((cvNum - 1) >> 8) & 0x03;
byte[] retVal = new byte[6];
retVal[0] = (byte) (0x80 | midAddr);
retVal[1] = (byte) (0x01 | (highAddr << 4) | (lowAddr << 1));
retVal[2] = (byte) (0xEC | highCVnum);
retVal[3] = (byte) (lowCVnum);
retVal[4] = (byte) (0xFF & data);
retVal[5] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3] ^ retVal[4]);
return retVal;
}
protected static byte[] accSignalDecoderPktCommon(int lowAddr, int boardAddr, int aspect) {
if (aspect < 0 || aspect > 31) {
log.error("invalid signal decoder aspect {}", aspect);
return null;
}
int midAddr = boardAddr & 0x3F;
int highAddr = ((~boardAddr) >> 6) & 0x07;
byte[] retVal = new byte[4];
retVal[0] = (byte) (0x80 | midAddr);
retVal[1] = (byte) (0x01 | (highAddr << 4) | (lowAddr << 1));
retVal[2] = (byte) (0x1F & aspect);
retVal[3] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2]);
return retVal;
}
/**
* Recover the 1-based output address from an Accessory Decoder Control
* Packet, typically considered a turnout control packet
*
* @param packet the packet to get an address from
* @return the accessory decoder address
*/
public static int getAccDecoderPktAddress(byte[] packet) {
// case turnout accessory decoder
// from Alex Shepherd
int boardAddress = (((~packet[1]) & 0x70) << 2) | (packet[0] & 0x3F);
int outputAddress = packet[1] & 0x07;
int outputIndex = outputAddress >> 1;
return (((boardAddress - 1) << 2) | outputIndex) + 1;
}
/**
* Provide an accessory control packet via a simplified interface
*
* @param number Address of accessory output, starting with 1
* @param closed true if the output is to be configured to the "closed",
* a.k.a. the "normal" or "unset" position
* @return a packet
*/
public static byte[] accDecoderPkt(int number, boolean closed) {
// dBit is the "channel" info, least 7 bits, for the packet
// The lowest channel bit represents CLOSED (1) and THROWN (0)
int dBits = (((number - 1) & 0x03) << 1); // without the low CLOSED vs THROWN bit
dBits = closed ? (dBits | 1) : dBits;
// aBits is the "address" part of the nmra packet, which starts with 1
// 07/01/05 R.Scheffler - Removed the mask, this will allow any 'too high' numbers
// through to accDecoderPkt() above which will log the error if out of bounds. If we
// mask it here, then the number will 'wrap' without any indication that it did so.
int aBits = (number - 1) >> 2; // Divide by 4 to get the 'base'
aBits += 1; // Base is +1
// cBit is the control bit, we're always setting it active
int cBit = 1;
// get the packet
return NmraPacket.accDecoderPkt(aBits, cBit, dBits);
}
/**
* Provide a basic operations mode accessory CV programming packet via a
* simplified interface, given an accessory address.
* <br><br>
*
* @param accAddr Address of accessory, in the range 1 to 2044
* @param cvNum CV number to access
* @param data Data to be written
* @return a packet
*/
public static byte[] accDecoderPktOpsMode(int accAddr, int cvNum, int data) {
// dBit is the "channel" info, least 7 bits, for the packet
// The lowest channel bit represents CLOSED (1) and THROWN (0)
int dBits = (((accAddr - 1) & 0x03) << 1) | 1; // assume CLOSED
// aBits is the "address" part of the nmra packet, which starts with 1
int aBits = (accAddr - 1) >> 2; // Divide by 4 to get the 'base'
aBits += 1; // Base is +1
// cBit is the control bit, we're always setting it active
int cBit = 1;
// get the packet
return NmraPacket.accDecoderPktOpsMode(aBits, cBit, dBits, cvNum, data);
}
/**
* Provide a basic operations mode accessory CV programming packet via a
* simplified interface, given a decoder address.
* <br><br>
* From the NMRA Standard: Basic Accessory Decoder Packet address for
* operations mode programming
* <br><br>
* 10AAAAAA 0 1AAACDDD
* <br><br>
* Where DDD is used to indicate the output whose CVs are being modified and
* C=1.
* <br>
* If CDDD= 0000 then the CVs refer to the entire decoder.
* <br><br>
* Hence this method uses CDDD= 0000.
* <br><br>
* For programming individual outputs use
* {@link #accDecoderPktOpsMode(int accAddr, int cvNum, int data)}
* <br><br>
*
* @param decAddr Address of decoder, in the range 1 to 511
* @param cvNum CV number to access
* @param data Data to be written
* @return a packet
*/
public static byte[] accDecPktOpsMode(int decAddr, int cvNum, int data) {
// dBit is the "channel" info, least 7 bits, for the packet
// The lowest channel bit represents CLOSED (1) and THROWN (0)
int dBits = 0; // dBits is the "channel" info, CDDD= 0000 indicates the entire decoder
// aBits is the "address" part of the nmra packet, which starts with 1
int aBits = decAddr;
// cBit is the control bit, CDDD= 0000 indicates the entire decoder
int cBit = 0;
// get the packet
return NmraPacket.accDecoderPktOpsMode(aBits, cBit, dBits, cvNum, data);
}
/**
* Provide a legacy operations mode accessory CV programming packet via a
* simplified interface, given an accessory address.
* <br><br>
* From the NMRA Standard: The format for Accessory Decoder Configuration
* Variable Access Instructions is: {preamble} 0 10AAAAAA 0 0AAA11VV 0
* VVVVVVVV 0 DDDDDDDD 0 EEEEEEEE 1 Where: A = Decoder address bits V =
* Desired CV address - (CV 513 = 10 00000000) D = Data for CV
* <br><br>
* This is the old "legacy" format, newer decoders use the "Basic Accessory
* Decoder Packet"
*
* @param accAddr Address of accessory, in the range 1 to 2044
* @param cvNum CV number to access
* @param data Data to be written
* @return a packet
*/
public static byte[] accDecoderPktOpsModeLegacy(int accAddr, int cvNum, int data) {
// aBits is the "address" part of the nmra packet, which starts with 1
int aBits = (accAddr - 1) >> 2; // Divide by 4 to get the 'base'
aBits += 1; // Base is +1
// get the packet
return NmraPacket.accDecPktOpsModeLegacy(aBits, cvNum, data);
}
public static byte[] opsCvWriteByte(int address, boolean longAddr, int cvNum, int data) {
log.debug("opswrite {} {} {}", address, cvNum, data);
if (!addressCheck(address, longAddr)) {
return null; // failed!
}
if (data < 0 || data > 255) {
log.error("invalid data {}", data);
return null;
}
if (cvNum < 1 || cvNum > 1024) {
log.error("invalid CV number {}", cvNum);
return null;
}
// end sanity checks, format output
int arg1 = 0xEC + (((cvNum - 1) >> 8) & 0x03);
int arg2 = (cvNum - 1) & 0xFF;
int arg3 = data & 0xFF;
return NmraPacket.threeBytePacket(address, longAddr, (byte) arg1, (byte) arg2, (byte) arg3);
}
public static byte[] speedStep128Packet(int address, boolean longAddr, int speed, boolean fwd) {
log.debug("128 step packet {} {}", address, speed);
if (!addressCheck(address, longAddr)) {
return null; // failed!
}
if (speed < 0 || speed > 127) {
log.error("invalid speed {}", speed);
return null;
}
// end sanity checks, format output
byte[] retVal;
int arg1 = 0x3F;
int arg2 = (speed & 0x7F) | (fwd ? 0x80 : 0);
if (longAddr) {
// long address form
retVal = new byte[5];
retVal[0] = (byte) (192 + ((address / 256) & 0x3F));
retVal[1] = (byte) (address & 0xFF);
retVal[2] = (byte) arg1;
retVal[3] = (byte) arg2;
retVal[4] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2] ^ retVal[3]);
} else {
// short address form
retVal = new byte[4];
retVal[0] = (byte) (address & 0xFF);
retVal[1] = (byte) arg1;
retVal[2] = (byte) arg2;
retVal[3] = (byte) (retVal[0] ^ retVal[1] ^ retVal[2]);
}
return retVal;
}
/**
* From NMRA RP 9.2.1 [A Crosland 05/02/12] There is an issue with this
* method in that it cannot create a 28 step speed packet for maximum speed.
* Input speed value in the range 0 - 28 is converted to speed steps, 0,
* estop, 1, 2, ..., 27.
* <p>
* This method should probably be deprecated. It is used only by
* NceThrottle.java and EasyDccThrottle.java which themselves have issues in
* the way floating point speed values are converted to integer speed steps.
* <p>
* A speed and direction instruction is used send information to motors
* connected to Multi Function Digital Decoders. Instruction "010" indicates
* a Speed and Direction Instruction for reverse operation and instruction
* "011" indicates a Speed and Direction Instruction for forward operation.
* In these instructions the data is used to control speed with bits 0-3
* being defined exactly as in S-9.2 Section B. If Bit 1 of CV#29 has a
* value of one (1), then bit 4 is used as an intermediate speed step, as
* defined in S-9.2, Section B. If Bit 1 of CV#29 has a value of zero (0),
* then bit 4 shall 230 be used to control FL4. In this mode, Speed U0000 is
* stop, speed U0001 is emergency stop, speed U0010 is the first speed step
* and speed U1111 is full speed. This provides 14 discrete speed steps in
* each direction.
*
* @param address the DCC locomotive address
* @param longAddr true if the address is long; false if short
* @param speed the speed from 0-28
* @param fwd true for forward direction; false for reverse
* @return the instruction or null if address or speed is invalid
*/
public static byte[] speedStep28Packet(int address, boolean longAddr, int speed, boolean fwd) {
log.debug("28 step packet {} {}", address, speed);
if (!addressCheck(address, longAddr)) {
return null; // failed!
}
if (speed < 0 || speed > 28) {
log.error("invalid speed {}", speed);
return null;
}
int speedC = (speed & 0x1F) >> 1;
if (speed > 0) {
speedC = speedC + 1;
}
int c = (speed & 0x01) << 4; // intermediate speed step
speedC = speedC + c;
// end sanity checks, format output
int arg1 = (fwd ? 0x60 : 0x40) | speedC;
return NmraPacket.oneBytePacket(address, longAddr, (byte) arg1);
}
/**
* New version of speedStep28Packet to allow access to the whole range of 28
* step speed packets.
* <p>
* Simply constructs a packet using the 5 bit speed value. This is
* consistent with the 128 and 14 step methods which do no further
* processing of the speed value.
*
* @param full must be true
* @param address DCC address
* @param longAddr true if DCC address is long; false if short
* @param speed speed step value 0 - 31 for insertion into DC packet
* @param fwd true for forward direction; false for reverse
* @return the instruction or null if address or speed is invalid
*/
@CheckForNull
public static byte[] speedStep28Packet(boolean full, int address, boolean longAddr, int speed, boolean fwd) {