forked from Digital-MoBa/XpressNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XpressNet.cpp
1385 lines (1284 loc) · 47.6 KB
/
XpressNet.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
/*
*****************************************************************************
* XpressNet.h - library for a client XpressNet protocoll
* Copyright (c) 2015-2022 Philipp Gahtow All right reserved.
*
** Free for Private usage only!
*****************************************************************************
* IMPORTANT:
*
* Please contact Lenz Inc. for more details.
*****************************************************************************
* see for changes in XpressNet.h!
*/
// include this library's description file
#include "XpressNet.h"
#if defined(__AVR__)
#include <avr/interrupt.h>
XpressNetClass *XpressNetClass::active_object = 0; //Static
#endif
#if defined(ESP8266) || defined(ESP32) //ESP8266 and ESP32 Support
#include <SoftwareSerial.h>
SoftwareSerial XNetSerial;
#endif
#define interval 10500 //interval for Status LED (milliseconds)
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
XpressNetClass::XpressNetClass()
{
// initialize this instance's variables
Railpower = 0xFF; //Ausgangs undef.
XNetclearSendBuf();
XNetRun = false; //XNet ist inactive;
xLokStsclear(); //löschen aktiver Loks in Slotserver
ReqLocoAdr = 0;
ReqLocoAgain = 0;
ReqFktAdr = 0;
SlotLast = 0;
ReadData = false; //keine Serial Daten Speichern
}
//******************************************Serial*******************************************
#if defined(ESP8266) || defined(ESP32)
void XpressNetClass::start(byte XAdr, uint8_t XNetSerial_Port, uint8_t XControl) //Initialisierung Serial
#else
void XpressNetClass::start(byte XAdr, uint8_t XControl) //Initialisierung Serial
#endif
{
ledState = LOW; // Status LED, used to set the LED
previousMillis = 0; //Reset Time Count
SlotTime = millis(); // will store last time LED was updated
if (notifyXNetStatus)
notifyXNetStatus (ledState);
MY_ADDRESS = XAdr;
MAX485_CONTROL = XControl;
// LISTEN_MODE
pinMode(MAX485_CONTROL, OUTPUT);
digitalWrite (MAX485_CONTROL, LOW);
myRequestAck = callByteParity (MY_ADDRESS | 0x00) | 0x100;
myCallByteInquiry = callByteParity (MY_ADDRESS | 0x40) | 0x100;
myDirectedOps = callByteParity (MY_ADDRESS | 0x60) | 0x100;
#if defined(ESP8266) || defined(ESP32)
XNetSerial.begin(62500, SWSERIAL_8S1, XNetSerial_Port, XNetSerial_Port, false, 95); //One Wire Half Duplex Serial, parity mode SPACE
if (!XNetSerial) { // If the object did not initialize, then its configuration is invalid
Serial.println("Invalid SoftwareSerial pin configuration, check config");
while (1) { // Don't continue with invalid configuration
delay (1000);
}
}
// high speed half duplex, turn off interrupts during tx
XNetSerial.enableIntTx(false);
#else
//Set up on 62500 Baud
cli(); //disable interrupts while initializing the USART
#if defined(SERIAL_PORT)
UBRRH = 0;
UBRRL = 0x0F;
UCSRA = 0;
UCSRB = (1<<RXEN) | (1<<TXEN) | (1<<RXCIE) | (1<<UCSZ2);
UCSRC = (1<<UCSZ1) | (1<<UCSZ0);
#elif defined(SERIAL_PORT_0)
UBRR0H = 0;
UBRR0L = 0x0F;
UCSR0A = 0;
UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (1<<UCSZ02);
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
#elif defined(SERIAL_PORT_1)
UBRR1H = 0;
UBRR1L = 0x0F;
UCSR1A = 0;
UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1) | (1<<UCSZ12);
UCSR1C = (1<<UCSZ11) | (1<<UCSZ10);
#endif
sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed
/*
* Enable reception (RXEN = 1).
* Enable transmission (TXEN0 = 1).
* Enable Receive Interrupt (RXCIE = 1).
* Set 8-bit character mode (UCSZ00, UCSZ01, and UCSZ02 together control this,
* But UCSZ00, UCSZ01 are in Register UCSR0C).
*/
active_object = this; //hold Object to call it back in ISR
#endif
}
// Public Methods //////////////////////////////////////////////////////////////
// Functions available in Wiring sketches, this library, and other libraries
//*******************************************************************************************
//Daten ermitteln und Auswerten
void XpressNetClass::receive(void)
{
#if defined(ESP8266) || defined(ESP32)
XNetget();
#endif
/*
XNetMsg[XNetlength] = 0x00;
XNetMsg[XNetmsg] = 0x00;
XNetMsg[XNetcommand] = 0x00; savedData[1]
XNetMsg[XNetdata1] = 0x00; savedData[2]
*/
unsigned long currentMillis = millis(); //aktuelle Zeit setzten
if (DataReady == true) { //Serial Daten dekodieren
DataReady = false;
// previousMillis = millis(); // will store last time LED was updated
//Daten, setzte LED = ON!
if (ledState == LOW) { //LED -> aktivieren!
ledState = HIGH;
if (notifyXNetStatus)
notifyXNetStatus (ledState);
}
if (XNetMsg[XNetmsg] == 0x60) { //GENERAL_BROADCAST
if (XNetMsg[XNetlength] == 4 && XNetMsg[XNetcom] == 0x61) {
if ((XNetMsg[XNetdata1] == 0x01) && (XNetMsg[XNetdata2] == 0x60)) {
// Normal Operation Resumed
Railpower = csNormal;
if (notifyXNetPower)
notifyXNetPower(Railpower);
}
else if ((XNetMsg[XNetdata1] == 0x00) && (XNetMsg[XNetdata2] == 0x61)) {
// Track power off
Railpower = csTrackVoltageOff;
if (notifyXNetPower)
notifyXNetPower(Railpower);
}
else if ((XNetMsg[XNetdata1] == 0x08)) {
// Track Short
Railpower = csShortCircuit;
if (notifyXNetPower) {
notifyXNetPower(csTrackVoltageOff);
notifyXNetPower(Railpower);
}
}
else if ((XNetMsg[XNetdata1] == 0x02) && (XNetMsg[XNetdata2] == 0x63)) {
// Service Mode Entry
Railpower = csServiceMode;
if (notifyXNetPower)
notifyXNetPower(Railpower);
}
}
else if (XNetMsg[XNetcom] == 0x81) {
if ((XNetMsg[XNetdata1] == 0x00) && (XNetMsg[XNetdata2] == 0x81)) {
//Emergency Stop
Railpower = csEmergencyStop;
if (notifyXNetPower)
notifyXNetPower(Railpower);
}
}
else if ((XNetMsg[XNetcom] & 0xF0) == 0x40) {
//Rückmeldung
byte len = (XNetMsg[XNetcom] & 0x0F) / 2; //each Adr and Data
for (byte i = 1; i <= len; i++) {
sendSchaltinfo(false, XNetMsg[XNetcom+(i*2)-1] /*Adr*/, XNetMsg[XNetcom+(i*2)]/*Data*/);
}
}
else if (XNetMsg[XNetlength] == 8 && XNetMsg[XNetcom] == 0x05 && XNetMsg[XNetdata1] == 0xF1) {
//DCC FAST CLOCK set request
/* 0x05 0xF1 TCODE1 TCODE2 TCODE3 TCODE4 [XOR]
00mmmmmm TCODE1, mmmmmm = denotes minutes, range 0...59.
100HHHHH TCODE2, HHHHH = denotes hours, range 0...23.
01000www TCODE3, www = denotes day of week, 0=monday, 1=tuesday, a.s.o.
110fffff TCODE4, fffff = denotes speed ratio, range 0..31. (0=stopped)
*/
}
}
//else if (XNetMsg[XNetmsg] == myDirectedOps && XNetMsg[XNetlength] >= 3) {
//change by André Schenk
else if (XNetMsg[XNetlength] >= 3) {
/*
Serial.print("RX: ");
Serial.print(XNetMsg[XNetcom], HEX);
Serial.print("-");
Serial.print(XNetMsg[XNetdata1], HEX);
Serial.print(" ");
Serial.print(XNetMsg[XNetdata2], HEX);
Serial.print(" ");
Serial.print(XNetMsg[XNetdata3], HEX);
Serial.print(" ");
Serial.print(XNetMsg[XNetdata4], HEX);
Serial.print("..");
*/
switch (XNetMsg[XNetcom]) {
//add by Norberto Redondo Melchor:
case 0x52: // Some other device asked for an accessory change
//if (XNetMsg[XNetlength] >= 3) {
//change by André Schenk
if (XNetMsg[XNetdata2] >= 0x80) {
// Pos = 0000A00P A = turnout output (active 0/inactive 1); P = Turn v1 or --0
byte A_bit = (XNetMsg[XNetdata2] >> 3) & B0001;
unsigned int Adr = (XNetMsg[XNetdata1] << 2) | ((XNetMsg[XNetdata2] & B0110) >> 1); // Dir afectada
byte Pos = (XNetMsg[XNetdata2] & B0001) + 1;
if (!A_bit) { // Accessory activation request
if (notifyTrnt)
notifyTrnt(highByte(Adr), lowByte(Adr), Pos);
}
else { // Accessory deactivation request
Pos = Pos | B1000;
if (notifyTrnt)
notifyTrnt(highByte(Adr), lowByte(Adr), Pos);
}
}
break;
case 0x62:
if (XNetMsg[XNetdata1] == 0x21 && XNetMsg[XNetlength] >= 4) { //Sw Version 2.3
// old version - version 1 and version 2 response.
}
else if (XNetMsg[XNetdata1] == 0x22 && XNetMsg[XNetlength] >= 5) {
if (XNetRun == false) { //Softwareversion anfragen
unsigned char commandVersionSequence[] = {0x21, 0x21, 0x00};
XNetSendadd (commandVersionSequence, 3);
XNetRun = true;
}
Railpower = csNormal;
if (XNetMsg[XNetdata2] != 0) {
// is track power turned off?
if ((XNetMsg[XNetdata2] & 0x01) == 0x01) { Railpower = csEmergencyStop; } //Bit 0: wenn 1, Anlage in Nothalt
// is it in emergency stop?
if ((XNetMsg[XNetdata2] & 0x02) == 0x02) { Railpower = csTrackVoltageOff; } //Bit 1: wenn 1, Anlage in Notaus
// in service mode?
if ((XNetMsg[XNetdata2] & 0x08) == 0x08) {Railpower = csServiceMode;} //Bit 3: wenn 1, dann Programmiermode aktiv
// in powerup mode - wait until complete
if ((XNetMsg[XNetdata2] & 0x40) == 0x40) {
// put us in a state where we do the status request again...
XNetRun = false;
}
}
if (notifyXNetPower)
notifyXNetPower(Railpower);
}
break;
case 0x61:
if (XNetMsg[XNetlength] >= 4) {
if (XNetMsg[XNetdata1] == 0x13) {
//Programmierinfo Daten nicht gefunden
if (notifyCVInfo)
notifyCVInfo(0x02);
}
if (XNetMsg[XNetdata1] == 0x1F) {
//Programmierinfo Zentrale Busy
if (notifyCVInfo)
notifyCVInfo(0x01);
}
if (XNetMsg[XNetdata1] == 0x11) {
//Programmierinfo Zentrale Bereit
if (notifyCVInfo)
notifyCVInfo(0x00);
}
if (XNetMsg[XNetdata1] == 0x12) {
//Programmierinfo short-circuit
if (notifyCVInfo)
notifyCVInfo(0x03);
}
if (XNetMsg[XNetdata1] == 0x80) {
//Transfer Error
if (notifyCVInfo)
notifyCVInfo(0xE1);
}
if (XNetMsg[XNetdata1] == 0x82) {
//Befehl nicht vorhanden Rückmeldung
}
}
break;
case 0x63:
//Softwareversion Zentrale
if ((XNetMsg[XNetdata1] == 0x21) && (XNetMsg[XNetlength] >= 5)) {
if (notifyXNetVer)
notifyXNetVer(XNetMsg[XNetdata2], XNetMsg[XNetdata3]);
}
//Programmierinfo Daten 3-Byte-Format & Daten 4-Byte-Format
if ((XNetMsg[XNetdata1] == 0x10 || XNetMsg[XNetdata1] == 0x14) && XNetMsg[XNetlength] >= 5) {
byte cvAdr = XNetMsg[XNetdata2];
byte cvData = XNetMsg[XNetdata3];
if (notifyCVResult)
notifyCVResult(cvAdr, cvData);
}
break;
case 0xE4: //Antwort der abgefragen Lok
if ((XNetMsg[XNetlength] >= 7) && (ReqLocoAdr != 0) && ((XNetMsg[XNetdata1] >> 4) != 0)) {
byte Adr_MSB = highByte(ReqLocoAdr);
byte Adr_LSB = lowByte(ReqLocoAdr);
ReqLocoAdr = 0;
uint8_t Steps = XNetMsg[XNetdata1]; //0000 BFFF - B=Busy; F=Fahrstufen
bitWrite(Steps, 3, 0); //Busy bit löschen
if (Steps == B100)
Steps = B11;
boolean Busy = false;
if (bitRead(XNetMsg[XNetdata1], 3) == 1)
Busy = true;
uint8_t Speed = XNetMsg[XNetdata2] & 0x7F; //RVVV VVVV - R=Richtung; V=Geschwindigkeit
uint8_t Direction = false;
if (bitRead(XNetMsg[XNetdata2], 7) == 1)
Direction = true;
uint8_t F0 = XNetMsg[XNetdata3]; //0 0 0 F0 F4 F3 F2 F1
uint8_t F1 = XNetMsg[XNetdata4]; //F12 F11 F10 F9 F8 F7 F6 F5
byte BSteps = Steps;
if (Busy)
bitWrite(BSteps, 3, 1);
byte funcsts = F0; //FktSts = Chg-F, X, Dir, F0, F4, F3, F2, F1
bitWrite(funcsts, 5, Direction); //Direction hinzufügen
bool chg = xLokStsadd (Adr_MSB, Adr_LSB, BSteps, Speed, funcsts); //Eintrag in SlotServer
chg = chg | xLokStsFunc1 (Adr_MSB, Adr_LSB, F1);
if (chg == true) //Änderungen am Zustand?
getLocoStateFull(Adr_MSB, Adr_LSB, true);
if (Speed == 0) { //Lok auf Besetzt schalten
setLocoHalt (Adr_MSB, Adr_LSB);//Sende Lok HALT um Busy zu erzeugen!
}
}
else {
byte Adr_MSB = XNetMsg[XNetdata2];
byte Adr_LSB = XNetMsg[XNetdata3];
byte Slot = xLokStsgetSlot(Adr_MSB, Adr_LSB);
switch (XNetMsg[XNetdata1]) {
case 0x10: xLokSts[Slot].speed = XNetMsg[XNetdata4] & 0x7F;//14 Speed steps
bitWrite(xLokSts[Slot].f0, 5, bitRead(XNetMsg[XNetdata4], 7));
getLocoStateFull(Adr_MSB, Adr_LSB, true);
break;
case 0x11: xLokSts[Slot].speed = XNetMsg[XNetdata4] & 0x7F;//27 Speed steps
bitWrite(xLokSts[Slot].f0, 5, bitRead(XNetMsg[XNetdata4], 7));
getLocoStateFull(Adr_MSB, Adr_LSB, true);
break;
case 0x12: xLokSts[Slot].speed = XNetMsg[XNetdata4] & 0x7F;//28 Speed steps
bitWrite(xLokSts[Slot].f0, 5, bitRead(XNetMsg[XNetdata4], 7));
getLocoStateFull(Adr_MSB, Adr_LSB, true);
break;
case 0x13: xLokSts[Slot].speed = XNetMsg[XNetdata4] & 0x7F;//128 Speed steps
bitWrite(xLokSts[Slot].f0, 5, bitRead(XNetMsg[XNetdata4], 7));
getLocoStateFull(Adr_MSB, Adr_LSB, true);
break;
case 0x20: xLokSts[Slot].f0 = (xLokSts[Slot].f0 & 0x20 ) | (XNetMsg[XNetdata4] & 0x1F);//Fkt Group1 - Attention keep Direction Bit!
getLocoStateFull(Adr_MSB, Adr_LSB, true);
break;
case 0x21: xLokSts[Slot].f1 = (XNetMsg[XNetdata4] & 0x0F) | (xLokSts[Slot].f1 & 0xF0);//Fkt Group2
getLocoStateFull(Adr_MSB, Adr_LSB, true);
break;
case 0x22: xLokSts[Slot].f1 = (XNetMsg[XNetdata4] << 4) | (xLokSts[Slot].f1 & 0x0F);//Fkt Group3
getLocoStateFull(Adr_MSB, Adr_LSB, true);
break;
case 0x23: xLokSts[Slot].f2 = XNetMsg[XNetdata4];//Fkt Group4 f13..f20
getLocoStateFull(Adr_MSB, Adr_LSB, true);
break;
case 0x28: xLokSts[Slot].f3 = XNetMsg[XNetdata4];//Fkt Group5 f21..f28
getLocoStateFull(Adr_MSB, Adr_LSB, true);
break;
case 0x24: //Fkt Status
break;
}
}
break;
case 0xE3: //Antwort abgefrage Funktionen F13-F28
if (XNetMsg[XNetdata1] == 0x52 && XNetMsg[XNetlength] >= 6 && ReqFktAdr != 0) { //Funktionszustadn F13 bis F28
byte Adr_MSB = highByte(ReqFktAdr);
byte Adr_LSB = lowByte(ReqFktAdr);
ReqFktAdr = 0;
byte F2 = XNetMsg[XNetdata2]; //F2 = F20 F19 F18 F17 F16 F15 F14 F13
byte F3 = XNetMsg[XNetdata3]; //F3 = F28 F27 F26 F25 F24 F23 F22 F21
if (xLokStsFunc23 (Adr_MSB, Adr_LSB, F2, F3) == true) { //Änderungen am Zustand?
if (notifyLokFunc)
notifyLokFunc(Adr_MSB, Adr_LSB, F2, F3 );
getLocoStateFull(Adr_MSB, Adr_LSB, true);
}
}
if (XNetMsg[XNetdata1] == 0x40 && XNetMsg[XNetlength] >= 6) { // Locomotive is being operated by another device
XLokStsSetBusy (XNetMsg[XNetdata2], XNetMsg[XNetdata3]);
}
break;
case 0xE1:
if (XNetMsg[XNetlength] >= 3) {
//Fehlermeldung Lok control
if (notifyCVInfo)
notifyCVInfo(0xE1);
}
break;
case 0x42: //Antwort Schaltinformation
if (XNetMsg[XNetlength] >= 4) {
sendSchaltinfo(true, XNetMsg[XNetdata1], XNetMsg[XNetdata2]);
}
break;
case 0xA3: // Locomotive is being operated by another device
if (XNetMsg[XNetlength] >= 4) {
if (notifyXNetPower)
notifyXNetPower(XNetMsg[XNetdata1]);
}
break;
} //switch myDirectedOps ENDE
}
// if (ReadData == false) //Nachricht komplett empfangen, dann hier löschen!
XNetclear(); //alte verarbeitete Nachricht löschen
} //Daten vorhanden ENDE
else { //keine Daten empfangen, setzte LED = Blink
previousMillis++;
if (previousMillis > interval) { //WARTEN
XNetRun = false; //Keine Zentrale vorhanden
// save the last time you blinked the LED
previousMillis = 0;
// if the LED is off turn it on and off (Blink):
ledState = !ledState;
if (notifyXNetStatus)
notifyXNetStatus (ledState);
}
}
//Slot Server aktualisieren
if (currentMillis - SlotTime > SlotInterval) {
SlotTime = currentMillis;
UpdateBusySlot(); //Server Update - Anfrage nach Statusänderungen
}
}
//--------------------------------------------------------------------------------------------
//Aufbereiten der Schaltinformation
void XpressNetClass::sendSchaltinfo(bool schaltinfo, byte data1, byte data2)
{
int Adr = data1 * 4;
byte nibble = bitRead(data2, 4);
byte Pos1 = data2 & B11;
byte Pos2 = (data2 >> 2) & B11;
if (nibble == 1)
Adr = Adr + 2;
if (schaltinfo) {
if (notifyTrnt)
notifyTrnt(highByte(Adr), lowByte(Adr), Pos1);
if (notifyTrnt)
notifyTrnt(highByte(Adr+1), lowByte(Adr+1), Pos2);
}
else {
if (notifyFeedback)
notifyFeedback(highByte(Adr), lowByte(Adr), Pos1);
if (notifyFeedback)
notifyFeedback(highByte(Adr+1), lowByte(Adr+1), Pos2);
}
}
//--------------------------------------------------------------------------------------------
//Zustand der Gleisversorgung setzten
bool XpressNetClass::setPower(byte Power)
{
switch (Power) {
case csNormal: {
unsigned char PowerAn[] = { 0x21, 0x81, 0xA0 };
return XNetSendadd(PowerAn, 3);
}
case csEmergencyStop: {
unsigned char EmStop[] = { 0x80, 0x80 };
return XNetSendadd(EmStop, 2);
}
case csTrackVoltageOff: {
unsigned char PowerAus[] = { 0x21, 0x80, 0xA1 };
return XNetSendadd(PowerAus, 3);
}
/* case csShortCircuit:
return false;
case csServiceMode:
return false; */
}
return false;
}
//--------------------------------------------------------------------------------------------
//Abfrage letzte Meldung über Gleispannungszustand
byte XpressNetClass::getPower()
{
return Railpower;
}
//--------------------------------------------------------------------------------------------
//Halt Befehl weiterleiten
void XpressNetClass::setHalt()
{
setPower(csEmergencyStop);
}
//--------------------------------------------------------------------------------------------
//Abfragen der Lokdaten (mit F0 bis F12)
bool XpressNetClass::getLocoInfo (byte Adr_High, byte Adr_Low)
{
bool ok = false;
getLocoStateFull(Adr_High, Adr_Low, false);
byte Slot = xLokStsgetSlot(Adr_High, Adr_Low);
if (xLokSts[Slot].state < 0xFF)
xLokSts[Slot].state++; //aktivität
if (xLokStsBusy(Slot) == true && ReqLocoAdr == 0) { //Besetzt durch anderen XPressNet Handregler
ReqLocoAdr = word(Adr_High, Adr_Low); //Speichern der gefragen Lok Adresse
unsigned char getLoco[] = {0xE3, 0x00, Adr_High, Adr_Low, 0x00};
if (ReqLocoAdr > 99)
getLoco[2] = Adr_High | 0xC0;
getXOR(getLoco, 5);
ok = XNetSendadd (getLoco, 5);
}
return ok;
}
//--------------------------------------------------------------------------------------------
//Abfragen der Lok Funktionszustände F13 bis F28
bool XpressNetClass::getLocoFunc (byte Adr_High, byte Adr_Low)
{
if (ReqFktAdr == 0) {
ReqFktAdr = word(Adr_High, Adr_Low); //Speichern der gefragen Lok Adresse
unsigned char getLoco[] = {0xE3, 0x09, Adr_High, Adr_Low, 0x00};
if (word(Adr_High,Adr_Low) > 99)
getLoco[2] = Adr_High | 0xC0;
getXOR(getLoco, 5);
return XNetSendadd (getLoco, 5);
}
unsigned char getLoco[] = {0xE3, 0x09, highByte(ReqFktAdr), lowByte(ReqFktAdr), 0x00};
if (ReqFktAdr > 99)
getLoco[2] = highByte(ReqFktAdr) | 0xC0;
getXOR(getLoco, 5);
return XNetSendadd (getLoco, 5);
}
//--------------------------------------------------------------------------------------------
//Lok Stoppen
bool XpressNetClass::setLocoHalt (byte Adr_High, byte Adr_Low)
{
bool ok = false;
unsigned char setLocoStop[] = {0x92, Adr_High, Adr_Low, 0x00};
if (word(Adr_High,Adr_Low) > 99)
setLocoStop[2] = Adr_High | 0xC0;
getXOR(setLocoStop, 4);
ok = XNetSendadd (setLocoStop, 4);
byte Slot = xLokStsgetSlot(Adr_High, Adr_Low);
xLokSts[Slot].speed = 0; //STOP
getLocoStateFull(Adr_High, Adr_Low, true);
return ok;
}
//--------------------------------------------------------------------------------------------
//Lokdaten setzten
bool XpressNetClass::setLocoDrive (byte Adr_High, byte Adr_Low, uint8_t Steps, uint8_t Speed)
{
bool ok = false;
unsigned char setLoco[] = {0xE4, 0x10, Adr_High, Adr_Low, Speed, 0x00};
if (word(Adr_High,Adr_Low) > 99)
setLoco[2] = Adr_High | 0xC0;
setLoco[1] |= Steps;
getXOR(setLoco, 6);
ok = XNetSendadd (setLoco, 6);
byte Slot = xLokStsgetSlot(Adr_High, Adr_Low);
xLokSts[Slot].mode = (xLokSts[Slot].mode & B11111100) | Steps; //Fahrstufen
xLokSts[Slot].speed = Speed & B01111111;
bitWrite(xLokSts[Slot].f0, 5, bitRead(Speed, 7)); //Dir
// getLocoStateFull(Adr_High, Adr_Low, true);
//Nutzung protokollieren:
if (xLokSts[Slot].state < 0xFF)
xLokSts[Slot].state++; //aktivität
return ok;
}
//--------------------------------------------------------------------------------------------
//Lokfunktion setzten
bool XpressNetClass::setLocoFunc (byte Adr_High, byte Adr_Low, uint8_t type, uint8_t fkt)
{
bool ok = false; //Funktion wurde nicht gesetzt!
bool fktbit = 0; //neue zu ändernde fkt bit
if (type == 1) //ein
fktbit = 1;
byte Slot = xLokStsgetSlot(Adr_High, Adr_Low);
//zu änderndes bit bestimmen und neu setzten:
if (fkt <= 4) {
byte func = xLokSts[Slot].f0 & B00011111; //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 über XNet senden:
unsigned char setLocoFunc[] = {0xE4, 0x20, Adr_High, Adr_Low, func, 0x00}; //Gruppe1 = 0 0 0 F0 F4 F3 F2 F1
if (word(Adr_High,Adr_Low) > 99)
setLocoFunc[2] = Adr_High | 0xC0;
getXOR(setLocoFunc, 6);
ok = XNetSendadd (setLocoFunc, 6);
//Slot anpassen:
if (fkt == 0)
bitWrite(xLokSts[Slot].f0, 4, fktbit);
else bitWrite(xLokSts[Slot].f0, fkt-1, fktbit);
}
else if ((fkt >= 5) && (fkt <= 8)) {
byte funcG2 = xLokSts[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 über XNet senden:
unsigned char setLocoFunc[] = {0xE4, 0x21, Adr_High, Adr_Low, funcG2, 0x00}; //Gruppe2 = 0 0 0 0 F8 F7 F6 F5
if (word(Adr_High,Adr_Low) > 99)
setLocoFunc[2] = Adr_High | 0xC0;
getXOR(setLocoFunc, 6);
ok = XNetSendadd (setLocoFunc, 6);
//Slot anpassen:
bitWrite(xLokSts[Slot].f1, fkt-5, fktbit);
}
else if ((fkt >= 9) && (fkt <= 12)) {
byte funcG3 = xLokSts[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 über XNet senden:
unsigned char setLocoFunc[] = {0xE4, 0x22, Adr_High, Adr_Low, funcG3, 0x00}; //Gruppe3 = 0 0 0 0 F12 F11 F10 F9
if (word(Adr_High,Adr_Low) > 99)
setLocoFunc[2] = Adr_High | 0xC0;
getXOR(setLocoFunc, 6);
ok = XNetSendadd (setLocoFunc, 6);
//Slot anpassen:
bitWrite(xLokSts[Slot].f1, fkt-9+4, fktbit);
}
else if ((fkt >= 13) && (fkt <= 20)) {
byte funcG4 = xLokSts[Slot].f2;
if (type == 2) //um
fktbit = !(bitRead(funcG4, fkt-13));
bitWrite(funcG4, fkt-13, fktbit);
//Daten über XNet senden:
//unsigned char setLocoFunc[] = {0xE4, 0x23, Adr_High, Adr_Low, funcG4, 0x00}; //Gruppe4 = F20 F19 F18 F17 F16 F15 F14 F13
unsigned char setLocoFunc[] = {0xE4, 0xF3, Adr_High, Adr_Low, funcG4, 0x00}; //Gruppe4 = F20 F19 F18 F17 F16 F15 F14 F13
//0xF3 = undocumented command is used when a mulitMAUS is controlling functions f20..f13.
if (word(Adr_High,Adr_Low) > 99)
setLocoFunc[2] = Adr_High | 0xC0;
getXOR(setLocoFunc, 6);
ok = XNetSendadd (setLocoFunc, 6);
//Slot anpassen:
bitWrite(xLokSts[Slot].f2, (fkt-13), fktbit);
}
else if ((fkt >= 21) && (fkt <= 28)) {
byte funcG5 = xLokSts[Slot].f3;
if (type == 2) //um
fktbit = !(bitRead(funcG5, fkt-21));
bitWrite(funcG5, fkt-21, fktbit);
//Daten über XNet senden:
unsigned char setLocoFunc[] = {0xE4, 0x28, Adr_High, Adr_Low, funcG5, 0x00}; //Gruppe5 = F28 F27 F26 F25 F24 F23 F22 F21
if (word(Adr_High,Adr_Low) > 99)
setLocoFunc[2] = Adr_High | 0xC0;
getXOR(setLocoFunc, 6);
ok = XNetSendadd (setLocoFunc, 6);
//Slot anpassen:
bitWrite(xLokSts[Slot].f3, (fkt-21), fktbit);
}
getLocoStateFull(Adr_High, Adr_Low, true); //Alle aktiven Geräte Senden!
return ok;
}
//--------------------------------------------------------------------------------------------
//Gruppe 1: 0 0 0 F0 F4 F3 F2 F1
bool XpressNetClass::setFunc0to4(uint16_t Adr, uint8_t G1) {
unsigned char setLocoFunc[] = {0xE4, 0x20, 0x00, 0x00, G1, 0x00 };
if (Adr > 99) //Xpressnet long addresses (100 to 9999: AH/AL = 0xC064 to 0xE707)
setLocoFunc[2] = (Adr >> 8) | 0xC0;
else setLocoFunc[2] = Adr >> 8; //short addresses (0 to 99: AH = 0x0000 and AL = 0x0000 to 0x0063)
setLocoFunc[3] = Adr & 0xFF;
//Slot anpassen:
xLokSts[xLokStsgetSlot(setLocoFunc[2], setLocoFunc[3])].f0 = G1 & 0x1F;
getXOR(setLocoFunc, 6);
return XNetSendadd (setLocoFunc, 6);
}
//--------------------------------------------------------------------------------------------
//Gruppe 2: 0 0 0 0 F8 F7 F6 F5
bool XpressNetClass::setFunc5to8(uint16_t Adr, uint8_t G2) {
unsigned char setLocoFunc[] = {0xE4, 0x21, 0x00, 0x00, G2, 0x00 };
if (Adr > 99) //Xpressnet long addresses (100 to 9999: AH/AL = 0xC064 to 0xE707)
setLocoFunc[2] = (Adr >> 8) | 0xC0;
else setLocoFunc[2] = Adr >> 8; //short addresses (0 to 99: AH = 0x0000 and AL = 0x0000 to 0x0063)
setLocoFunc[3] = Adr & 0xFF;
//Slot anpassen:
xLokSts[xLokStsgetSlot(setLocoFunc[2], setLocoFunc[3])].f1 = (G2 & 0x0F) | (xLokSts[xLokStsgetSlot(setLocoFunc[2], setLocoFunc[3])].f1 & 0xF0); //F12..F9-F8..F5
getXOR(setLocoFunc, 6);
return XNetSendadd (setLocoFunc, 6);
}
//--------------------------------------------------------------------------------------------
//Gruppe 3: 0 0 0 0 F12 F11 F10 F9
bool XpressNetClass::setFunc9to12(uint16_t Adr, uint8_t G3) {
unsigned char setLocoFunc[] = {0xE4, 0x22, 0x00, 0x00, G3, 0x00 };
if (Adr > 99) //Xpressnet long addresses (100 to 9999: AH/AL = 0xC064 to 0xE707)
setLocoFunc[2] = (Adr >> 8) | 0xC0;
else setLocoFunc[2] = Adr >> 8; //short addresses (0 to 99: AH = 0x0000 and AL = 0x0000 to 0x0063)
setLocoFunc[3] = Adr & 0xFF;
//Slot anpassen:
xLokSts[xLokStsgetSlot(setLocoFunc[2], setLocoFunc[3])].f1 = (G3 << 4) | (xLokSts[xLokStsgetSlot(setLocoFunc[2], setLocoFunc[3])].f1 & 0x0F); //F12..F9-F8..F5
getXOR(setLocoFunc, 6);
return XNetSendadd (setLocoFunc, 6);
}
//--------------------------------------------------------------------------------------------
//Gruppe 4: F20 F19 F18 F17 F16 F15 F14 F13
bool XpressNetClass::setFunc13to20(uint16_t Adr, uint8_t G4) {
unsigned char setLocoFunc[] = {0xE4, 0xF3, 0x00, 0x00, G4, 0x00 };
if (Adr > 99) //Xpressnet long addresses (100 to 9999: AH/AL = 0xC064 to 0xE707)
setLocoFunc[2] = (Adr >> 8) | 0xC0;
else setLocoFunc[2] = Adr >> 8; //short addresses (0 to 99: AH = 0x0000 and AL = 0x0000 to 0x0063)
setLocoFunc[3] = Adr & 0xFF;
//Slot anpassen:
xLokSts[xLokStsgetSlot(setLocoFunc[2], setLocoFunc[3])].f2 = G4;
getXOR(setLocoFunc, 6);
XNetSendadd (setLocoFunc, 6);
setLocoFunc[0] = 0xE4;
setLocoFunc[1] = 0x23; //MultiMaus only
setLocoFunc[2] = Adr >> 8;
setLocoFunc[3] = Adr & 0xFF;
setLocoFunc[4] = G4;
getXOR(setLocoFunc, 6);
return XNetSendadd (setLocoFunc, 6);
}
//--------------------------------------------------------------------------------------------
//Gruppe 5: F28 F27 F26 F25 F24 F23 F22 F21
bool XpressNetClass::setFunc21to28(uint16_t Adr, uint8_t G5) {
unsigned char setLocoFunc[] = {0xE4, 0x28, 0x00, 0x00, G5, 0x00 };
if (Adr > 99) //Xpressnet long addresses (100 to 9999: AH/AL = 0xC064 to 0xE707)
setLocoFunc[2] = (Adr >> 8) | 0xC0;
else setLocoFunc[2] = Adr >> 8; //short addresses (0 to 99: AH = 0x0000 and AL = 0x0000 to 0x0063)
setLocoFunc[3] = Adr & 0xFF;
//Slot anpassen:
xLokSts[xLokStsgetSlot(setLocoFunc[2], setLocoFunc[3])].f3 = G5;
getXOR(setLocoFunc, 6);
return XNetSendadd (setLocoFunc, 6);
}
//--------------------------------------------------------------------------------------------
//Gibt aktuellen Lokstatus an Anfragenden Zurück
void XpressNetClass::getLocoStateFull (byte Adr_High, byte Adr_Low, bool bc)
{
byte Slot = xLokStsgetSlot(Adr_High, Adr_Low);
byte Busy = bitRead(xLokSts[Slot].mode, 3);
byte Dir = bitRead(xLokSts[Slot].f0, 5);
byte F0 = xLokSts[Slot].f0 & B00011111;
byte F1 = xLokSts[Slot].f1;
byte F2 = xLokSts[Slot].f2;
byte F3 = xLokSts[Slot].f3;
if (notifyLokAll)
notifyLokAll(Adr_High, Adr_Low, Busy, xLokSts[Slot].mode & B11, xLokSts[Slot].speed & 0x7F, Dir, F0, F1, F2, F3, bc);
//Nutzung protokollieren:
if (xLokSts[Slot].state < 0xFF)
xLokSts[Slot].state++; //aktivität
}
//--------------------------------------------------------------------------------------------
//Ermitteln der Schaltstellung einer Weiche
bool XpressNetClass::getTrntInfo (byte FAdr_High, byte FAdr_Low)
{
int Adr = word(FAdr_High, FAdr_Low);
byte nibble = 0; // 0 = Weiche 0 und 1; 1 = Weiche 2 und 3
if ((Adr & 0x03) >= 2)
nibble = 1;
unsigned char getTrntPos[] = {0x42, 0x00, 0x80, 0x00};
getTrntPos[1] = Adr >> 2;
getTrntPos[2] += nibble;
getXOR(getTrntPos, 4);
return XNetSendadd (getTrntPos, 4);
}
//--------------------------------------------------------------------------------------------
//Schalten einer Weiche
bool XpressNetClass::setTrntPos (byte FAdr_High, byte FAdr_Low, byte Pos)
//Pos = 0000A00P A=Weichenausgang (Aktive/Inaktive); P=Weiche nach links oder nach rechts
{
int Adr = word(FAdr_High, FAdr_Low);
byte AdrL = ((Pos & 0x0F) | B110) & (((Adr & 0x03) << 1) | B1001); //1000ABBP -> A00P = Pos | BB = Adr & 0x03 (LSB Weichenadr.)
Adr = Adr >> 2;
bitWrite(AdrL, 7, 1);
unsigned char setTrnt[] = {0x52, 0x00, AdrL, 0x00}; //old: 0x52, Adr, AdrL, 0x00
//setTrnt[1] = (Adr >> 2) & 0xFF;
//change by André Schenk:
setTrnt[1] = Adr;
getXOR(setTrnt, 4);
//getTrntInfo(FAdr_High, FAdr_Low); //Schaltstellung abfragen
if (notifyTrnt)
notifyTrnt(FAdr_High, FAdr_Low, (Pos & B1) + 1);
return XNetSendadd (setTrnt, 4);
}
//--------------------------------------------------------------------------------------------
//CV-Mode CV Lesen
void XpressNetClass::readCVMode (byte CV)
{
unsigned char cvRead[] = {0x22, 0x15, CV, 0x00};
getXOR(cvRead, 4);
XNetSendadd (cvRead, 4);
getresultCV(); //Programmierergebnis anfordern
}
//--------------------------------------------------------------------------------------------
//Schreiben einer CV im CV-Mode
void XpressNetClass::writeCVMode (byte CV, byte Data)
{
unsigned char cvWrite[] = {0x23, 0x16, CV, Data, 0x00};
getXOR(cvWrite, 5);
XNetSendadd (cvWrite, 5);
//getresultCV(); //Programmierergebnis anfordern
if (notifyCVResult)
notifyCVResult(CV, Data);
}
//--------------------------------------------------------------------------------------------
//Programmierergebnis anfordern
void XpressNetClass::getresultCV ()
{
unsigned char getresult[] = {0x21, 0x10, 0x31};
XNetSendadd (getresult, 3);
}
// Private Methods ///////////////////////////////////////////////////////////////////////////////////////////////////
// Functions only available to other functions in this library *******************************************************
//--------------------------------------------------------------------------------------------
// calculate the XOR
void XpressNetClass::getXOR (unsigned char *data, byte length) {
byte XOR = 0x00;
for (int i = 0; i < (length-1); i++) {
XOR = XOR ^ *data;
data++;
}
*data = XOR;
}
//--------------------------------------------------------------------------------------------
// calculate the parity bit in the call byte for this guy
byte XpressNetClass::callByteParity (byte me) {
byte parity = (1==0);
byte vv;
me &= 0x7f;
vv = me;
while (vv) {
parity = !parity;
vv &= (vv-1);
}
if (parity) me |= 0x80;
return me;
}
//--------------------------------------------------------------------------------------------
int XpressNetClass::USART_Receive(void)
{
#if defined(ESP8266) || defined(ESP32)
if (XNetSerial.available()) { // If anything comes in Serial
uint16_t data = XNetSerial.read();
if (XNetSerial.readParity()) { //detect parity bit set on the last message (MARK parity)
data = data | 0x100;
}
return data;
}
else return -1;
#else
unsigned char status, resh, resl;
// Wait for data to be received
#if defined(SERIAL_PORT)
status = UCSRA;
while (!(status & (1 << RXC))) { return -1; }//status = UCSRA;}
// Get status and 9th bit, then data
resh = UCSRB;
resl = UDR;
// If error, return -1
if (status & ((1 << FE) | (1 << DOR) | (1 << PE))) { return -1; }
#elif defined(SERIAL_PORT_0)
status = UCSR0A;
while (!(status & (1 << RXC0))) { return -1; }//status = UCSR0A;}
// Get status and 9th bit, then data
resh = UCSR0B;
resl = UDR0;
//If error, return -1
if (status & ((1 << FE0) | (1 << DOR0) | (1 << UPE0))) { return -1; }
#elif defined(SERIAL_PORT_1)
status = UCSR1A;
while (!(status & (1 << RXC1))) { return -1; }//status = UCSR1A;}
// Get status and 9th bit, then data
resh = UCSR1B;
resl = UDR1;
// If error, return -1
if (status & ((1 << FE1) | (1 << DOR1) | (1 << UPE1))) { return -1; }
#endif
// Filter the 9th bit, then return
resh = (resh >> 1) & 0x01;
return ((resh << 8) | resl);
#endif
}
//--------------------------------------------------------------------------------------------
void XpressNetClass::USART_Transmit(unsigned char data8) {
#if defined(ESP8266) || defined(ESP32)
XNetSerial.write(data8); //we didn't want to MARK = SWSERIAL_PARITY_MARK
#else
// wait for empty transmit buffer
#if defined(SERIAL_PORT)
while (!(UCSRA & (1<<UDRE))) {}
// put the data into buffer, and send
UDR = data8;
#elif defined(SERIAL_PORT_0)
while (!(UCSR0A & (1<<UDRE0))) {}
// put the data into buffer, and send
UDR0 = data8;
#elif defined(SERIAL_PORT_1)
while (!(UCSR1A & (1<<UDRE1))) {}
// put the data into buffer, and send
UDR1 = data8;
#endif
#endif
}
//--------------------------------------------------------------------------------------------
//Löschen des letzten gesendeten Befehls
void XpressNetClass::XNetclear()
{
XNetMsg[XNetlength] = 0x00;
XNetMsg[XNetmsg] = 0x00;
XNetMsg[XNetcom] = 0x00;
XNetMsg[XNetdata1] = 0x00;
XNetMsg[XNetdata2] = 0x00;
XNetMsg[XNetdata3] = 0x00;
XNetMsg[XNetdata4] = 0x00;
XNetMsg[XNetdata5] = 0x00;
}
#if defined(__AVR__)
//--------------------------------------------------------------------------------------------
//Interrupt routine for reading via Serial
#if defined(SERIAL_PORT)
ISR(USART_RX_vect) {
XpressNetClass::handle_interrupt(); //weiterreichen an die Funktion
}
#elif defined(SERIAL_PORT_0)
ISR(USART_RX_vect) {
XpressNetClass::handle_interrupt(); //weiterreichen an die Funktion
}
#elif defined(SERIAL_PORT_1)
ISR(USART1_RX_vect) {
XpressNetClass::handle_interrupt(); //weiterreichen an die Funktion
}
#endif
// Interrupt handling
/* static */
inline void XpressNetClass::handle_interrupt()
{
if (active_object)
{
active_object->XNetget(); //Daten Einlesen und Speichern
}
}