-
Notifications
You must be signed in to change notification settings - Fork 1
/
DCCPacketScheduler.cpp
1550 lines (1367 loc) · 53.8 KB
/
DCCPacketScheduler.cpp
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
/*
* DCC Waveform Generator
*
* modified by Philipp Gahtow
* Copyright 2015-2022
* [email protected], http://pgahtow.de
*
*/
#include "DCCPacketScheduler.h"
#include "DCCHardware.h"
#include "DDCHardware_config.h"
#if defined(GLOBALRAILCOMREADER)
#include <avr/interrupt.h>
#endif
#if defined(__SAM3X8E__) //Arduino DUE
#include <DueFlashStorage.h>
DueFlashStorage DueFlash;
#define FSTORAGE DueFlash
#define FSTORAGEMODE write
#elif defined(ESP32) //ESP32 only!
#include "z21nvs.h"
z21nvsClass EEPROMDCC;
#define FSTORAGE EEPROMDCC
#define FSTORAGEMODE write
#else
// AVR based Boards follows
#include <EEPROM.h>
#define FSTORAGE EEPROM
#if defined(ESP8266)
#define FSTORAGEMODE write
#else
#define FSTORAGEMODE update
#endif
#endif
#if defined(ESP32)
extern portMUX_TYPE timerMux;
#endif
/// Request the next packet for the rails
extern volatile bool get_next_packet;
/// The Pin where the DCC Waveform comes out.
extern uint8_t DCCPin;
extern uint8_t DCCPin2;
extern bool RailCom;
extern uint8_t DCCS88Pin;
/// The currently queued packet to be put on the rails. Default is a reset packet.
extern uint8_t current_packet[6];
/// is in Service Mode:
extern volatile uint8_t current_packet_service;
extern volatile uint8_t ProgRepeat;
/// How many data uint8_ts in the queued packet?
extern volatile uint8_t current_packet_size;
/// current status of railcom
extern volatile uint8_t RailComActiv;
volatile uint16_t current_cv = 0; //cv that we are working on
volatile uint8_t current_cv_value = 0; //value that is read
volatile uint8_t current_cv_bit = 0xFF; //bit that will be read - 0xFF = ready, nothing to read!
uint8_t cv_read_count = 0; //count number of cv read
#define NON_PROG_OP 0x00
#define WAIT_FOR_ACK 0x01
#define ACK_DETECTED 0x02
#define ACK_READ_SUCCESS 0x10
#define ACK_READ_FAIL 0xFF
uint8_t current_ack_status = NON_PROG_OP;
//current_ack_read = false;
unsigned long ack_start_time = 0;
#if defined(ESP32)
extern hw_timer_t * timer;
#endif
#if defined(GLOBALRAILCOMREADER)
//DCCPacketScheduler *DCCPacketScheduler::active_object = 0; //Static handle object for interrupt
#endif
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
//---------------------------------------------------------------------------------
DCCPacketScheduler::DCCPacketScheduler(void) : /*default_speed_steps(128),*/ /*last_packet_address(255),*/ packet_counter(1)
{
e_stop_queue.setup(E_STOP_QUEUE_SIZE); //just to send once for set repeat circle
// high_priority_queue.setup(HIGH_PRIORITY_QUEUE_SIZE);
// low_priority_queue.setup(LOW_PRIORITY_QUEUE_SIZE);
repeat_queue.setup(REPEAT_QUEUE_SIZE); //all repeat packets are inserted here, repeat first with priority!
//NEW
periodic_refresh_queue.setup(PERIODIC_REFRESH_QUEUE_SIZE); //collect all packtes there are permanent to repeat
ops_programmming_queue.setup(PROG_QUEUE_SIZE); //for CV programming only
}
//---------------------------------------------------------------------------------
void DCCPacketScheduler::setup(uint8_t pin, uint8_t pin2, uint8_t steps, uint8_t format, uint8_t power) //for any post-constructor initialization
{
loadEEPROMconfig(); //load the configuration
DCCPin = pin; //set DCC Waveform pin
DCCPin2 = pin2; //set inverted DCC Waveform pin2
DCCS88Pin = 0xFF; //disable per default
setup_DCC_waveform_generator(); //Timer neu configurieren
#if defined(__SAM3X8E__)
TC_Start(DCC_ARM_TC_TIMER, DCC_ARM_TC_CHANNEL);
#elif defined(ESP8266) //ESP8266
timer1_enable(DCC_ESP_TIMER_DIV, DCC_ESP_TIMER_SET, DCC_ESP_TIMER_LOOP);
#endif
setpower(power, true); //DCC signal on (active) and inform other over the new power state!
slotFullNext = 0; //don't override, start with free slots
TrntFormat = format; //The way BasicAccessory Messages Addressing works (Intellbox/ROCO/etc)
DCCdefaultSteps = steps;
ProgState = ProgStart; //default Direct CV Zustand
//Following RP 9.2.4, begin by putting 20 reset packets and 10 idle packets on the rails.
//reset packet: address 0x00, data 0x00, XOR 0x00; S 9.2 line 75
opsDecoderReset(RSTsRepeat); //send first a Reset Packet
//idle packet: address 0xFF, data 0x00, XOR 0xFF; S 9.2 line 90
//Automatically send idle packet until first action!
#if defined(GLOBALRAILCOMREADER)
Serial3.begin(250000);
POMCVAdr = 0xFFFF;
#endif
}
//---------------------------------------------------------------------------------
//extra DCC signal for S88/LocoNet without Shutdown and Railcom
void DCCPacketScheduler::enable_additional_DCC_output(uint8_t pin)
{
DCCS88Pin = pin; //set PIN for S88/LocoNet true DCC output
setup_DCC_waveform_generator(); //Timer neu configurieren
}
//---------------------------------------------------------------------------------
void DCCPacketScheduler::disable_additional_DCC_output(void)
{
enable_additional_DCC_output(0xFF); //disable - no PIN set
}
//---------------------------------------------------------------------------------
void DCCPacketScheduler::loadEEPROMconfig(void)
{
if (FSTORAGE.read(EEPROMRailCom) > 1)
FSTORAGE.FSTORAGEMODE(EEPROMRailCom,0x01); //Default activ
if (FSTORAGE.read(EEPROMProgReadMode) > 3)
FSTORAGE.FSTORAGEMODE(EEPROMProgReadMode,0x03); //Default "Beides"
if ((FSTORAGE.read(EEPROMProgRepeat) > 64) | (FSTORAGE.read(EEPROMRSTcRepeat) > 64)) {
FSTORAGE.FSTORAGEMODE(EEPROMProgRepeat,OPS_MODE_PROGRAMMING_REPEAT); //range 7-64
FSTORAGE.FSTORAGEMODE(EEPROMRSTsRepeat,RESET_START_REPEAT); //range 25-255
FSTORAGE.FSTORAGEMODE(EEPROMRSTcRepeat,RESET_CONT_REPEAT); //range 6-64
}
#if defined(ESP8266) || defined(ESP32) //ESP8266 or ESP32
FSTORAGE.commit();
#endif
RailCom = FSTORAGE.read(EEPROMRailCom); //define if railcom cutout is active
ProgReadMode = FSTORAGE.read(EEPROMProgReadMode); //Auslese-Modus: 0=Nichts, 1=Bit, 2=Byte, 3=Beides
ProgRepeat = FSTORAGE.read(EEPROMProgRepeat); //Repaet for Packet Programming
RSTsRepeat = FSTORAGE.read(EEPROMRSTsRepeat); //Repaet for Reset start Packet
RSTcRepeat = FSTORAGE.read(EEPROMRSTcRepeat); //Repaet for Reset contingue Packet
}
//---------------------------------------------------------------------------------
//set the power for the dcc signal
void DCCPacketScheduler::setpower(uint8_t state, bool notify)
{
if (railpower != state) {
railpower = state; //save the state of the railpower
if (state == OFF || state == SHORT) {
DCC_stop_output_signal(); //RailPower-Signal generate OFF
}
else {
DCC_run_output_signal(); //generate RailPower-Signal
}
if (notifyRailpower && notify)
notifyRailpower(railpower);
}
}
//---------------------------------------------------------------------------------
//get the actual state of power
byte DCCPacketScheduler::getpower(void)
{
return railpower;
}
//---------------------------------------------------------------------------------
//to de-/activate RailCom cutout rail output
void DCCPacketScheduler::setrailcom(bool rc)
{
RailCom = rc;
}
//---------------------------------------------------------------------------------
//return the State of RailCom cutout output
bool DCCPacketScheduler::getrailcom(void)
{
return RailCom;
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setSpeed(uint16_t address, uint8_t speed)
{
//set Loco to speed with default settings!
switch(DCCdefaultSteps)
{
case 14:
return(setSpeed14(address, speed));
case 28:
return(setSpeed28(address, speed));
case 128:
return(setSpeed128(address, speed));
}
return false; //invalid number of steps specified.
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setSpeed14(uint16_t address, uint8_t speed)
{
if (address == 0) //check if Adr is ok?
return false;
byte slot = LokStsgetSlot(address);
LokDataUpdate[slot].speed = speed; //write Dir and Speed into register to SAVE
if ((LokDataUpdate[slot].adr >> 14) != DCC14) //0=>14steps, write speed steps into register
LokDataUpdate[slot].adr = (LokDataUpdate[slot].adr & 0x3FFF) | (DCC14 << 14);
uint8_t speed_data_uint8_ts[] = {0x40}; //speed indecator
/*
if (speed == 1) //estop!
//return eStop(address);//
speed_data_uint8_ts[0] |= 0x01; //estop
else if (speed == 0) //regular stop!
speed_data_uint8_ts[0] |= 0x00; //stop
else //movement
speed_data_uint8_ts[0] |= map(speed, 2, 127, 2, 15); //convert from [2-127] to [1-14]
speed_data_uint8_ts[0] |= (0x20 * bitRead(speed, 7)); //flip bit 3 to indicate direction;
*/
speed_data_uint8_ts[0] |= speed & 0x1F; //5 Bit Speed
speed_data_uint8_ts[0] |= (speed & 0x80) >> 2; //Dir
DCCPacket p(address);
p.addData(speed_data_uint8_ts,1);
p.setRepeat(SPEED_REPEAT);
p.setKind(speed_packet_kind);
//speed packets get refreshed indefinitely, and so the repeat doesn't need to be set.
//speed packets go to the high proirity queue
//return(high_priority_queue.insertPacket(&p));
if (railpower == ESTOP) //donot send to rails now!
return periodic_refresh_queue.insertPacket(&p);
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setSpeed28(uint16_t address, uint8_t speed)
{
if (address == 0) //check if Adr is ok?
return false;
byte slot = LokStsgetSlot(address);
LokDataUpdate[slot].speed = speed; // speed & B01111111 + Dir; //write into register to SAVE
if ((LokDataUpdate[slot].adr >> 14) != DCC28) //2=>28steps, write into register
LokDataUpdate[slot].adr = (LokDataUpdate[slot].adr & 0x3FFF) | (DCC28 << 14);
uint8_t speed_data_uint8_ts[] = {0x40}; //Speed indecator
/*
if(speed == 1) //estop!
//return eStop(address);//
speed_data_uint8_ts[0] |= 0x01; //estop
else if (speed == 0) //regular stop!
speed_data_uint8_ts[0] |= 0x00; //stop
else //movement
{
speed_data_uint8_ts[0] |= map(speed, 2, 127, 2, 0x1F); //convert from [2-127] to [2-31]
//most least significant bit has to be shufled around
speed_data_uint8_ts[0] = (speed_data_uint8_ts[0]&0xE0) | ((speed_data_uint8_ts[0]&0x1F) >> 1) | ((speed_data_uint8_ts[0]&0x01) << 4);
}
speed_data_uint8_ts[0] |= (0x20 * bitRead(speed, 7)); //flip bit 3 to indicate direction;
*/
speed_data_uint8_ts[0] |= speed & 0x1F; //5 Bit Speed
speed_data_uint8_ts[0] |= (speed & 0x80) >> 2; //Dir
DCCPacket p(address);
p.addData(speed_data_uint8_ts,1);
p.setRepeat(SPEED_REPEAT);
p.setKind(speed_packet_kind);
//speed packets get refreshed indefinitely, and so the repeat doesn't need to be set.
//speed packets go to the high proirity queue
//return(high_priority_queue.insertPacket(&p));
if (railpower == ESTOP) //donot send to rails now!
return periodic_refresh_queue.insertPacket(&p);
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setSpeed128(uint16_t address, uint8_t speed)
{
if (address == 0) {
// Serial.println("ERROR ADR0");
return false;
}
byte slot = LokStsgetSlot(address);
LokDataUpdate[slot].speed = speed; //write Speed and Dir into register to SAVE
if ((LokDataUpdate[slot].adr >> 14) != DCC128) //3=>128steps, write into register
LokDataUpdate[slot].adr = (LokDataUpdate[slot].adr & 0x3FFF) | (DCC128 << 14);
uint8_t speed_data_uint8_ts[] = { 0x3F, 0x00 };
// if (speed == 1) //estop!
// return eStop(address);//speed_data_uint8_ts[1] |= 0x01; //estop
//else
speed_data_uint8_ts[1] = speed; //no conversion necessary.
//why do we get things like this?
// 03 3F 16 15 3F (speed packet addressed to loco 03)
// 03 3F 11 82 AF (speed packet addressed to loco 03, speed hex 0x11);
DCCPacket p(address);
p.addData(speed_data_uint8_ts, 2);
p.setRepeat(SPEED_REPEAT);
p.setKind(speed_packet_kind);
//speed packets get refreshed indefinitely, and so the repeat doesn't need to be set.
//speed packets go to the high proirity queue
//return(high_priority_queue.insertPacket(&p));
if (railpower == ESTOP) //donot send to rails now!
return periodic_refresh_queue.insertPacket(&p);
return repeat_queue.insertPacket(&p);
}
//--------------------------------------------------------------------------------------------
//Lokfunktion setzten
void DCCPacketScheduler::setLocoFunc(uint16_t address, uint8_t type, uint8_t fkt)
{ //type => 0 = AUS; 1 = EIN; 2 = UM; 3 = error
bool fktbit = 0; //neue zu ändernde fkt bit
if (type == 1) //ein
fktbit = 1;
byte Slot = LokStsgetSlot(address);
//zu änderndes bit bestimmen und neu setzten:
if (fkt <= 4) {
byte func = LokDataUpdate[Slot].f0 & 0x1F; //letztes Zustand der Funktionen 000 F0 F4..F1
if (type == 2) { //um
if (fkt == 0)
fktbit = !(bitRead(func, 4));
else fktbit = !(bitRead(func, fkt - 1));
}
if (fkt == 0)
bitWrite(func, 4, fktbit);
else bitWrite(func, fkt - 1, fktbit);
//Daten senden:
setFunctions0to4(address, func); //func = 0 0 0 F0 F4 F3 F2 F1
}
else if ((fkt >= 5) && (fkt <= 8)) {
byte funcG2 = LokDataUpdate[Slot].f1 & 0x0F; //letztes Zustand der Funktionen 0000 F8..F5
if (type == 2) //um
fktbit = !(bitRead(funcG2, fkt - 5));
bitWrite(funcG2, fkt - 5, fktbit);
//Daten senden:
setFunctions5to8(address, funcG2); //funcG2 = 0 0 0 0 F8 F7 F6 F5
}
else if ((fkt >= 9) && (fkt <= 12)) {
byte funcG3 = LokDataUpdate[Slot].f1 >> 4; //letztes Zustand der Funktionen 0000 F12..F9
if (type == 2) //um
fktbit = !(bitRead(funcG3, fkt - 9));
bitWrite(funcG3, fkt - 9, fktbit);
//Daten senden:
setFunctions9to12(address, funcG3); //funcG3 = 0 0 0 0 F12 F11 F10 F9
}
else if ((fkt >= 13) && (fkt <= 20)) {
byte funcG4 = LokDataUpdate[Slot].f2;
if (type == 2) //um
fktbit = !(bitRead(funcG4, fkt - 13));
bitWrite(funcG4, fkt - 13, fktbit);
//Daten senden:
setFunctions13to20(address, funcG4); //funcG4 = F20 F19 F18 F17 F16 F15 F14 F13
}
else if ((fkt >= 21) && (fkt <= 28)) {
byte funcG5 = LokDataUpdate[Slot].f3;
if (type == 2) //um
fktbit = !(bitRead(funcG5, fkt - 21));
bitWrite(funcG5, fkt - 21, fktbit);
//Daten senden:
setFunctions21to28(address, funcG5); //funcG5 = F28 F27 F26 F25 F24 F23 F22 F21
}
#if defined(EXTENDFUNCTION)
else if ((fkt >= 29) && (fkt <= 36)) {
byte func = LokDataUpdate[Slot].f0 >> 5;
if (type == 2) //um
fktbit = !(bitRead(func, fkt - 29));
bitWrite(func, fkt - 29, fktbit);
//Daten senden:
setFunctions29to36(address, func);
}
else if ((fkt >= 37) && (fkt <= 44)) {
byte func = 0; //LokDataUpdate[Slot].f5;
if (type == 2) //um
fktbit = !(bitRead(func, fkt - 37));
bitWrite(func, fkt - 37, fktbit);
//Daten senden:
setFunctions37to44(address, func);
}
else if ((fkt >= 45) && (fkt <= 52)) {
byte func = 0; //LokDataUpdate[Slot].f6;
if (type == 2) //um
fktbit = !(bitRead(func, fkt - 45));
bitWrite(func, fkt - 45, fktbit);
//Daten senden:
setFunctions45to52(address, func);
}
else if ((fkt >= 53) && (fkt <= 60)) {
byte func = 0; //LokDataUpdate[Slot].f7;
if (type == 2) //um
fktbit = !(bitRead(func, fkt - 53));
bitWrite(func, fkt - 53, fktbit);
//Daten senden:
setFunctions53to60(address, func);
}
else if ((fkt >= 61) && (fkt <= 68)) {
byte func = 0; //LokDataUpdate[Slot].f8;
if (type == 2) //um
fktbit = !(bitRead(func, fkt - 61));
bitWrite(func, fkt - 61, fktbit);
//Daten senden:
setFunctions61to68(address, func);
}
#endif
}
//--------------------------------------------------------------------------------------------
//Lokfunktion Binary State setzten
void DCCPacketScheduler::setLocoFuncBinary(uint16_t address, uint8_t low, uint8_t high) {
/* Preamble | 0AAA-AAAA | AAAA-AAAA | 110xxxxx | FLLL LLLL | HHHH HHHH | Err.Det.B
F Das oberste Bit F legt fest, ob der Binärzustand eingeschaltet oder ausgeschaltet ist.
LLLLLLL Die niederwertigen sieben (!) Bits der Binärzustandsadresse.
HHHHHHHH Die die höherwertigen acht Bits der Binärzustandsadresse.
DCC Binärzustandssteuerungsbefehle werden drei Mal am Gleis ausgegeben,
und danach gemäß RCN-212 nicht mehr regelmäßig wiederholt! */
if (high == 0) { //Binärzustandsadressen < 128 ==> kurze Form
if ((low & 0x7F) < 29) //nur Binärzustandsadressen von 29 bis 32767
return;
//Binärzustandssteuerungsbefehl kurze Form: 1101-1101 DLLL-LLLL
DCCPacket p(address);
uint8_t data[] = { B11011101, low};
p.addData(data, 2);
p.setKind(function_packet_b_kind);
p.setRepeat(FUNCTION_REPEAT);
/*
//save:
#if defined(EXTENDFUNCTION)
if (fkt < 69) {
uint8_t bitPos = (fkt - 29) % 8;
uint8_t num = (fkt - 29) / 8;
switch (num) {
case 0: bitWrite(f4, bitPos, fktbit); break; //F36 - F29
case 1: bitWrite(f5, bitPos, fktbit); break; //F44 - F37
case 2: bitWrite(f6, bitPos, fktbit); break; //F52 - F45
case 3: bitWrite(f7, bitPos, fktbit); break; //F60 - F53
case 4: bitWrite(f8, bitPos, fktbit); break; //F68 - F61
}
}
#endif
*/
e_stop_queue.insertPacket(&p);
}
else { //bis max 32767
//Binärzustandssteuerungsbefehl lange Form: 1100-0000 DLLL-LLLL HHHH-HHHH
DCCPacket p(address);
uint8_t data[] = { B11000000, low, high};
p.addData(data, 3);
p.setKind(function_packet_b_kind);
p.setRepeat(FUNCTION_REPEAT);
e_stop_queue.insertPacket(&p);
}
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions0to4(uint16_t address, uint8_t functions)
{
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { 0x80 };
//Obnoxiously, the headlights (F0, AKA FL) are not controlled
//by bit 0, but in DCC via bit 4. !
data[0] |= functions & 0x1F; //new - normal way of DCC! F0, F4, F3, F2, F1
p.addData(data,1);
p.setKind(function_packet_1_kind);
p.setRepeat(FUNCTION_REPEAT);
LokDataUpdate[LokStsgetSlot(address)].f0 = (functions & 0x1F) | (LokDataUpdate[LokStsgetSlot(address)].f0 & B11100000); //write into register to SAVE
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions5to8(uint16_t address, uint8_t functions)
{
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { 0xB0 };
data[0] |= functions & 0x0F;
p.addData(data,1);
p.setKind(function_packet_2_kind);
p.setRepeat(FUNCTION_REPEAT);
LokDataUpdate[LokStsgetSlot(address)].f1 = (LokDataUpdate[LokStsgetSlot(address)].f1 | 0x0F) & (functions | 0xF0); //write into register to SAVE
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions9to12(uint16_t address, uint8_t functions)
{
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { 0xA0 };
//least significant four functions (F5--F8)
data[0] |= functions & 0x0F;
p.addData(data,1);
p.setKind(function_packet_3_kind);
p.setRepeat(FUNCTION_REPEAT);
LokDataUpdate[LokStsgetSlot(address)].f1 = (LokDataUpdate[LokStsgetSlot(address)].f1 | 0xF0) & ((functions << 4) | 0x0F); //write into register to SAVE
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions13to20(uint16_t address, uint8_t functions) //F20 F19 F18 F17 F16 F15 F14 F13
{
//Funktionssteuerung F13-F20: 1101-1110 DDDD-DDDD
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { B11011110, functions }; //significant functions (F20--F13)
p.addData(data, 2);
p.setKind(function_packet_4_kind);
p.setRepeat(FUNCTION_REPEAT);
LokDataUpdate[LokStsgetSlot(address)].f2 = functions; //write into register to SAVE
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions21to28(uint16_t address, uint8_t functions) //F28 F27 F26 F25 F24 F23 F22 F21
{
//Funktionssteuerung F21-F28: 1101-1111 DDDD-DDDD
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { B11011111, functions};
p.addData(data, 2);
p.setKind(function_packet_5_kind);
p.setRepeat(FUNCTION_REPEAT);
LokDataUpdate[LokStsgetSlot(address)].f3 = functions; //write into register to SAVE
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions29to36(uint16_t address, uint8_t functions) //F29-F36
{
//Funktionssteuerung F29-F36: 1101-1000 DDDD-DDDD
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { B11011000, functions};
p.addData(data, 2);
p.setKind(function_packet_5_kind);
p.setRepeat(FUNCTION_REPEAT);
#if defined(EXTENDFUNCTION)
LokDataUpdate[LokStsgetSlot(address)].f0 = (functions << 5) | (LokDataUpdate[LokStsgetSlot(address)].f0 & 0x1F); //write into register to SAVE
#endif
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions37to44(uint16_t address, uint8_t functions) //F37-F44
{
//Funktionssteuerung F37-F44: 1101-1001 DDDD-DDDD
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { B11011001, functions};
p.addData(data, 2);
p.setKind(function_packet_5_kind);
p.setRepeat(FUNCTION_REPEAT);
#if defined(EXTENDFUNCTION)
//LokDataUpdate[LokStsgetSlot(address)].f5 = functions; //write into register to SAVE
#endif
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions45to52(uint16_t address, uint8_t functions) //F45-52
{
//Funktionssteuerung F45-F52: 1101-1010 DDDD-DDDD
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { B11011010, functions};
p.addData(data, 2);
p.setKind(function_packet_5_kind);
p.setRepeat(FUNCTION_REPEAT);
#if defined(EXTENDFUNCTION)
//LokDataUpdate[LokStsgetSlot(address)].f6 = functions; //write into register to SAVE
#endif
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions53to60(uint16_t address, uint8_t functions) //F53-60
{
//Funktionssteuerung F53-F60: 1101-1011 DDDD-DDDD
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { B11011011, functions};
p.addData(data, 2);
p.setKind(function_packet_5_kind);
p.setRepeat(FUNCTION_REPEAT);
#if defined(EXTENDFUNCTION)
//LokDataUpdate[LokStsgetSlot(address)].f7 = functions; //write into register to SAVE
#endif
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setFunctions61to68(uint16_t address, uint8_t functions) //F61-68
{
//Funktionssteuerung F61-F68: 1101-1100 DDDD-DDDD
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
uint8_t data[] = { B11011100, functions};
p.addData(data, 2);
p.setKind(function_packet_5_kind);
p.setRepeat(FUNCTION_REPEAT);
#if defined(EXTENDFUNCTION)
//LokDataUpdate[LokStsgetSlot(address)].f8 = functions; //write into register to SAVE
#endif
return repeat_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
byte DCCPacketScheduler::getFunktion0to4(uint16_t address) //gibt Funktionszustand - F0 F4 F3 F2 F1 zurück
{
return LokDataUpdate[LokStsgetSlot(address)].f0 & 0x1F;
}
//---------------------------------------------------------------------------------
byte DCCPacketScheduler::getFunktion5to8(uint16_t address) //gibt Funktionszustand - F8 F7 F6 F5 zurück
{
return LokDataUpdate[LokStsgetSlot(address)].f1 & 0x0F;
}
//---------------------------------------------------------------------------------
byte DCCPacketScheduler::getFunktion9to12(uint16_t address) //gibt Funktionszustand - F12 F11 F10 F9 zurück
{
return LokDataUpdate[LokStsgetSlot(address)].f1 >> 4;
}
//---------------------------------------------------------------------------------
byte DCCPacketScheduler::getFunktion13to20(uint16_t address) //gibt Funktionszustand F20 - F13 zurück
{
return LokDataUpdate[LokStsgetSlot(address)].f2;
}
//---------------------------------------------------------------------------------
byte DCCPacketScheduler::getFunktion21to28(uint16_t address) //gibt Funktionszustand F28 - F21 zurück
{
return LokDataUpdate[LokStsgetSlot(address)].f3;
}
//---------------------------------------------------------------------------------
byte DCCPacketScheduler::getFunktion29to31(uint16_t address) //gibt Funktionszustand F31 - F29 zurück
{
return LokDataUpdate[LokStsgetSlot(address)].f0 >> 5;
}
//---------------------------------------------------------------------------------
bool DCCPacketScheduler::setBasicAccessoryPos(uint16_t address, bool state)
{
return setBasicAccessoryPos(address, state, true); //Ausgang aktivieren
}
//---------------------------------------------------------------------------------
//send an accessory message
bool DCCPacketScheduler::setBasicAccessoryPos(uint16_t address, bool state, bool activ)
{
/*
Accessory decoder packet format:
================================
1111..11 0 1000-0001 0 1111-1011 0 EEEE-EEEE 1
Preamble | 10AA-AAAA | 1aaa-CDDX | Err.Det.B
aaaAAAAAA -> 111000001 -> Acc. decoder number 1
UINT16 FAdr = (FAdr_MSB << 8) + FAdr_LSB;
UINT16 Dcc_Addr = FAdr >> 2 //aaaAAAAAA
Beispiel:
FAdr=0 ergibt DCC-Addr=0 Port=0;
FAdr=3 ergibt DCC-Addr=0 Port=3;
FAdr=4 ergibt DCC-Addr=1 Port=0; usw
C on/off: 1 => on // Ausgang aktivieren oder deaktivieren
DD turnout: 01 => 2 // FAdr & 0x03 // Port
X str/div: 1 => set to diverging // Weiche nach links oder nach rechts
=> X=0 soll dabei Weiche auf Abzweig bzw. Signal auf Halt kennzeichnen.
=> COMMAND: SET TURNOUT NUMBER 2 DIVERGING
1111..11 0 1000-0001 0 1111-0011 0 EEEE-EEEE 1
=> COMMAND: SET TURNOUT NUMBER 6 DIVERGING
*/
if (address > 0x7FF) //check if Adr is ok, (max. 11-bit for Basic Adr)
return false;
DCCPacket p((address + TrntFormat) >> 2); //9-bit Address + Change Format Roco / Intellibox
uint8_t data[1];
data[0] = ((address + TrntFormat) & 0x03) << 1; //0000-CDDX -> set DD
if (state == true) //SET X Weiche nach links oder nach rechts
bitWrite(data[0], 0, 1); //set turn
if (activ == true ) //SET C Ausgang aktivieren oder deaktivieren
bitWrite(data[0], 3, 1); //set ON
p.addData(data, 1);
p.setKind(basic_accessory_packet_kind);
p.setRepeat(OTHER_REPEAT);
if (notifyTrnt)
notifyTrnt(address, state, activ);
bitWrite(BasicAccessory[address / 8], address % 8, state); //pro SLOT immer 8 Zustände speichern!
return e_stop_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
//return the state of an accessory
bool DCCPacketScheduler::getBasicAccessoryInfo(uint16_t address)
{
switch (TrntFormat) {
case IB: address = address + IB; break;
}
return bitRead(BasicAccessory[address / 8], address % 8); //Zustand aus Slot lesen
}
//---------------------------------------------------------------------------------
//send an extended accessory message
bool DCCPacketScheduler::setExtAccessoryPos(uint16_t address, uint8_t state)
{
/*
Extended Accessory decoder packet format:
================================
1111..11 0 1000-0001 0 0111-1011 0 xxxx-xxxx 0 EEEE-EEEE 1
Preamble | 10AA-AAAA | 0aaa-0AA1 | DDDD-DDDD | Err.Det.B
*/
if (address > 0x7FF) //check if Adr is ok, (max. 11-bit for Basic Adr)
return false;
DCCPacket p((address + TrntFormat) >> 2); //9-bit Address + Change Format Roco / Intellibox
uint8_t data[2];
data[0] = (((address + TrntFormat) & 0x03) << 1 | 0x01); //0000-0AA1
data[1] = state; //DDDD-DDDD
p.addData(data, 2);
p.setKind(extended_accessory_packet_kind);
p.setRepeat(OTHER_REPEAT);
if (notifyExtTrnt)
notifyExtTrnt(address, state);
return e_stop_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
//Special Function for programming, switch and estop:
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//write CV byte value
bool DCCPacketScheduler::opsProgDirectCV(uint16_t CV, uint8_t CV_data)
{
//check if CV# is between 0 - 1023
if (CV > 1023) {
if (notifyCVNack)
notifyCVNack(CV);
return false;
}
if (railpower != SERVICE) { //time to wait for the relais!
setpower(SERVICE, true);
}
ProgState = ProgStart;
ProgMode = ProgModeWriteByte;
current_cv = CV;
current_cv_value = CV_data;
return true;
}
//##################################################################################
//intern Function!
void DCCPacketScheduler::opsWriteCV(uint16_t CV, uint8_t CV_data)
{
//for CV#1 is the Adress 0
//Long-preamble 0 0111CCAA 0 AAAAAAAA 0 DDDDDDDD 0 EEEEEEEE 1
//CC=10 Bit Manipulation
//CC=01 Verify byte
//CC=11 Write byte <--
DCCPacket p(((CV >> 8) & B11) | B01111100);
uint8_t data[] = { 0x00 , 0x00};
data[0] = CV & 0xFF;
data[1] = CV_data;
p.addData(data, 2);
p.setKind(ops_mode_programming_kind); //always use short Adress Mode!
p.setRepeat(1); //auto repeat inside ISR! (ProgRepeat)
ops_programmming_queue.insertPacket(&p); //send on the rails
}
//---------------------------------------------------------------------------------
//verify CV value extern Function:
bool DCCPacketScheduler::opsVerifyDirectCV(uint16_t CV, uint8_t CV_data)
{
//check if CV# is between 0 - 1023
if (CV > 1023) {
if (notifyCVNack)
notifyCVNack(CV);
return false;
}
if (railpower != SERVICE) { //time to wait for the relais!
setpower(SERVICE, true);
}
ProgState = ProgStart;
ProgMode = ProgModeByteVerify;
current_cv = CV;
current_cv_value = CV_data;
return true;
}
//##################################################################################
//intern Function!
void DCCPacketScheduler::opsVerifyCV(uint16_t CV, uint8_t CV_data)
{
//for CV#1 is the Adress 0
//Long-preamble 0 0111CCAA 0 AAAAAAAA 0 DDDDDDDD 0 EEEEEEEE 1
//CC=10 Bit Manipulation
//CC=01 Verify byte <--
//CC=11 Write byte
DCCPacket p(((CV >> 8) & B11) | B01110100);
uint8_t data[2];
data[0] = CV & 0xFF;
data[1] = CV_data;
p.addData(data, 2);
p.setKind(ops_mode_programming_kind); //always use short Adress Mode!
p.setRepeat(1); //auto repeat inside ISR! (ProgRepeat)
ops_programmming_queue.insertPacket(&p); //send on the rails
}
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//read a CV in bit-Mode
bool DCCPacketScheduler::opsReadDirectCV(uint16_t CV)
{
//check if CV# is between 0 - 1023
if (CV > 1023) {
if (notifyCVNack)
notifyCVNack(CV);
return false;
}
if (railpower != SERVICE) { //time to wait for the relais!
setpower(SERVICE, true);
}
ProgState = ProgStart;
if (ProgReadMode == 2)
ProgMode = ProgModeByte;
else ProgMode = ProgModeBit;
current_cv = CV;
return true;
}
//##################################################################################
//intern Function!
void DCCPacketScheduler::opsReadCV(uint16_t CV, uint8_t bitToRead, bool bitState)
{
//for CV#1 is the Adress 0
//long-preamble 0 0111CCAA 0 AAAAAAAA 0 111KDBBB 0 EEEEEEEE 1
//CC=10 Bit Manipulation <--
//CC=01 Verify byte
//CC=11 Write byte
//BBB represents the bit position
//D contains the value of the bit to be verified or written
//K=1 signifies a "Write Bit" operation and K=0 signifies a "Bit Verify"
DCCPacket p(((CV >> 8) & B11) | B01111000);
uint8_t data[] = { 0x00 , 0x00};
data[0] = CV & 0xFF;
data[1] = B11100000 | (bitToRead & 0x07) | (bitState << 3); //verify Bit is "bitSet"? ("1" or "0")
p.addData(data, 2);
p.setKind(ops_mode_programming_kind); //always use short Adress Mode!
p.setRepeat(1); //auto repeat inside ISR! (ProgRepeat)
ops_programmming_queue.insertPacket(&p); //send on the rails
}
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//POM - write CV byte value
bool DCCPacketScheduler::opsProgramCV(uint16_t address, uint16_t CV, uint8_t CV_data)
{
//format of packet:
// {preamble} 0 [ AAAAAAAA ] 0 111011VV 0 VVVVVVVV 0 DDDDDDDD 0 EEEEEEEE 1 (write)
// {preamble} 0 [ AAAAAAAA ] 0 111001VV 0 VVVVVVVV 0 DDDDDDDD 0 EEEEEEEE 1 (verify/read)
// {preamble} 0 [ AAAAAAAA ] 0 111010VV 0 VVVVVVVV 0 DDDDDDDD 0 EEEEEEEE 1 (bit manipulation)
// only concerned with "write" form here!!!
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);
// split the CV address up among data uint8_ts 0 and 1
uint8_t data[] = { ((CV >> 8) & B11) | B11101100, CV & 0xFF, CV_data };
p.addData(data, 3);
p.setKind(pom_mode_programming_kind);
p.setRepeat(ProgRepeat);
//return low_priority_queue.insertPacket(&p); //Standard
return ops_programmming_queue.insertPacket(&p);
}
//---------------------------------------------------------------------------------
//POM - write CV in bit-Mode
bool DCCPacketScheduler::opsPOMwriteBit(uint16_t address, uint16_t CV, uint8_t Bit_data)
{
//format of packet:
// {preamble} 0 [ AAAAAAAA ] 0 111011VV 0 VVVVVVVV 0 DDDDDDDD 0 EEEEEEEE 1 (write)
// {preamble} 0 [ AAAAAAAA ] 0 111001VV 0 VVVVVVVV 0 DDDDDDDD 0 EEEEEEEE 1 (verify/read)
// {preamble} 0 [ AAAAAAAA ] 0 111010VV 0 VVVVVVVV 0 DDDDDDDD 0 EEEEEEEE 1 (bit manipulation)
// only concerned with "write" form here!!!
if (address == 0) //check if Adr is ok?
return false;
DCCPacket p(address);