-
Notifications
You must be signed in to change notification settings - Fork 4
/
RocSUBS.h
1455 lines (1274 loc) · 62.9 KB
/
RocSUBS.h
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
#ifndef RocSubs_h
#define RocSubs_h
uint8_t LastSetSpeed;
uint32_t PingSendTime;
boolean PingReflected;
uint8_t Value_for_PortD[PortsRange]; //ignore [0] so we can have 1...8
uint8_t Pi02_Port_Settings_D[PortsRange];
uint8_t DelaySetting_for_PortD[PortsRange];
uint8_t Configuration_for_PortD[PortsRange];
uint8_t EventMarker_for_PortD[PortsRange];
uint8_t ID_High_for_PortD[PortsRange]; //idh
uint8_t ID_Low_for_PortD[PortsRange]; //idl
uint8_t XRef_Port[PortsRange];
uint8_t Pi03_Setting_offposH[PortsRange];
uint8_t Pi03_Setting_offposL[PortsRange];
uint8_t Pi03_Setting_onposH[PortsRange];
uint8_t Pi03_Setting_onposL[PortsRange];
uint8_t Pi03_Setting_offsteps[PortsRange];
uint8_t Pi03_Setting_onsteps[PortsRange];
uint8_t Pi03_Setting_options[PortsRange];
uint32_t Pi03_Setting_LastUpdated[PortsRange];
int LastSFX1,LastSFX0;
extern void SERVOS(void);
extern uint16_t servoLR(int state, int port);
extern int FlashHL(int state, int port);
extern bool IsServo(uint8_t i);
extern bool IsPWM(uint8_t i);
extern bool IsInput(uint8_t i);
extern bool IsSetElsewhere(uint8_t i);
extern bool PortInvert(uint8_t i);
extern int DCC_Speed_Demand;
extern int Last_DCC_Speed_Demand;
uint8_t DIRF = 0 ;
extern IPAddress mosquitto;
extern bool OLED1Present,OLED2Present,OLED3Present,OLED4Present,OLED5Present,OLED6Present;
extern uint8_t OLED_Settings[7];
#define Recipient_Addr 1 //use with SetWordIn_msg_loc_value(sendMessage,Recipient_Addr,data , or get sender or recipient addr
#define Sender_Addr 3 //use with SetWordIn_msg_loc_value(sendMessage,Sender_Addr,data
#define Code_Request 0<<5
#define Code_Event 1<<5
#define Code_Reply 2<<5
uint8_t ROC_netid;
uint16_t ROC_recipient;
uint16_t ROC_sender;
uint8_t ROC_group;
uint8_t ROC_code;
uint8_t ROC_len;
uint8_t ROC_Data[260];
uint8_t ROC_OUT_DATA[200];
uint16_t RocNodeID;
//uint16_t EEPROMRocNodeID;
extern uint32_t StartedAt;
#include "EEPROMDEFAULTS.h"
extern void SetDefaultSVs(); // moved to EEPROMDEFAULTS.h
extern void OLEDsettingView(int OLed_x);
//extern uint8_t Interpolate(uint8_t Input,uint8_t lowerout,uint8_t upperout,uint8_t lowerin,uint8_t upperin);
uint8_t Interpolate(uint8_t Input,uint8_t lowerout,uint8_t upperout,uint8_t lowerin,uint8_t upperin){
int Result; // use int for internal function maths preciscion
uint8_t ResultInt;
if ((upperin-lowerin)!=0)// Check for div zero issue? //input is in range for interplolate checks are done after this
{
Result=1024*(upperout-lowerout);
Result=(Input-lowerin)*Result;
Result=Result/(upperin-lowerin);
Result=Result/1024;
Result=Result+lowerout;
}else
{
// no actual range, so just use average of output demands
Result=(lowerout+upperout)/2;
}
if (Input<=lowerin){Result=lowerout;}
if (Input>=upperin){Result=upperout;}
if (Result>=255){
Result=255;
}
ResultInt=abs(Result);
#ifdef _PWM_DEBUG
// DebugSprintfMsgSend( sprintf ( DebugMsg, "Interpolate<%d> INRange(<%d>to<%d>) OUTrange(<%d> to<%d>) Result<%d>",Input,lowerin,upperin,lowerout,upperout,Result));
#endif
return ResultInt;
}
uint8_t UseSpeedTablesorCV6(uint8_t Input){
int MidThrottle,MaxThrottle,ReverseTrim,ForwardTrim;
uint16_t Result;
uint8_t Bin;
uint8_t NumBins;
NumBins=28;
bool Dir,LightsOn,DirInvert,UseSpeedTable;
Dir=bitRead(DIRF,5);
LightsOn= bitRead(DIRF,4);
DirInvert=bitRead(CV[29],0);
UseSpeedTable=bitRead(CV[29],4);
ReverseTrim=CV[95];
ForwardTrim=CV[66];
if (CV[95]>=127){ReverseTrim=128-CV[95];}// can use negatives
if (CV[66]>=127){ForwardTrim=128-CV[66];}// use msb ss -ve sign
if (CV[95]<=1){ReverseTrim=0;}
if (CV[65]<=1){ForwardTrim=0;}
// assume simple CV5/6 table to start
MaxThrottle=128;// assume 0- 128 range
MidThrottle=MaxThrottle/2;
if (!UseSpeedTable){
if (Input>=MidThrottle){Result= Interpolate(Input,CV[6],CV[5],MidThrottle,MaxThrottle);}
else{Result=Interpolate(Input,0,CV[6],0,MidThrottle);}
}else
{ // speed table from cvs
Bin=abs(NumBins*Input/MaxThrottle);
Result=Interpolate(Input,CV[Bin+67],CV[Bin+68],(Bin*MaxThrottle/NumBins),((Bin+1)*MaxThrottle/NumBins));
}
if (DirInvert^Dir) {Result = Result + ForwardTrim; } //trims..
else {Result = Result + ReverseTrim; }
if (abs(Input)>=1){
Result=Result+CV[2];} //add Vstart
if (Result>=255) {Result=255;}
#ifdef _SpeedTableDEBUG
if(UseSpeedTable){
DebugSprintfMsgSend( sprintf ( DebugMsg, "Speed Table Input<%d> Result<%d>",Input,Result));}
else{ DebugSprintfMsgSend( sprintf ( DebugMsg, "CV5 CV6 Interpolated Input<%d> Result<%d>",Input,Result));}
#endif
return Result;
}
uint16_t Get_ROCNodeID(){
return RocNodeID;
}
String NICKname(){
return Nickname;
}
void PrintEEPROMSettings(){
//code to serial print eprom settings in a form that can be used in SetDefaultSVs()
Serial.println(F(" ------Current EEPROM Settings----------"));
for (int i = 1; i <= NumberOfPorts; i++) {
Serial.print(" Value_for_PortD[");
Serial.print(i);
Serial.print("]=");
Serial.print(Value_for_PortD[i]);Serial.println(";");
Serial.print(" Pi02_Port_Settings_D[");
Serial.print(i);
Serial.print("]=");
Serial.print(Pi02_Port_Settings_D[i]);Serial.println(";");
Serial.print(" DelaySetting_for_PortD[");
Serial.print(i);
Serial.print("]=");
Serial.print(DelaySetting_for_PortD[i]); Serial.println(";");
Serial.print(" Configuration_for_PortD[");
Serial.print(i);
Serial.print("]=");
Serial.print(Configuration_for_PortD[i]);Serial.println(";");
Serial.print(" EventMarker_for_PortD[");
Serial.print(i);
Serial.print("]=");
Serial.print(EventMarker_for_PortD[i]);Serial.println(";");
Serial.print(" ID_High_for_PortD[");
Serial.print(i);
Serial.print("]=");
Serial.print(ID_High_for_PortD[i]);Serial.println(";");
Serial.print(" ID_Low_for_PortD[");
Serial.print(i);
Serial.print("]=");
Serial.print(ID_Low_for_PortD[i]);Serial.println(";");
Serial.print(" XRef_Port[");
Serial.print(i);
Serial.print("]=");
Serial.print(XRef_Port[i]);Serial.println(";");
Serial.print(" Pi03_Setting_offposH[");
Serial.print(i);
Serial.print("]=");
Serial.print(Pi03_Setting_offposH[i]);Serial.println(";");
Serial.print(" Pi03_Setting_offposL[");
Serial.print(i);
Serial.print("]=");
Serial.print(Pi03_Setting_offposL[i]);Serial.println(";");
Serial.print(" Pi03_Setting_onposH[");
Serial.print(i);
Serial.print("]=");
Serial.print(Pi03_Setting_onposH[i]);Serial.println(";");
Serial.print(" Pi03_Setting_onposL[");
Serial.print(i);
Serial.print("]=");
Serial.print(Pi03_Setting_onposL[i]);Serial.println(";");
Serial.print(" Pi03_Setting_offsteps[");
Serial.print(i);
Serial.print("]=");
Serial.print(Pi03_Setting_offsteps[i]);Serial.println(";");
Serial.print(" Pi03_Setting_onsteps[");
Serial.print(i);
Serial.print("]=");
Serial.print(Pi03_Setting_onsteps[i]);Serial.println(";");
Serial.print(" Pi03_Setting_options[");
Serial.print(i);
Serial.print("]=");
Serial.print(Pi03_Setting_options[i]);Serial.println(";");
Serial.print(" Pi03_Setting_LastUpdated[");
Serial.print(i);
Serial.print("]=");
Serial.print(Pi03_Setting_LastUpdated[i]);Serial.println(";");
}
Serial.println(" //RN and CV settings");
for (int i = 0; i <= 35 ; i++) {
Serial.print(" RN[");
Serial.print(i);
Serial.print("]=");
Serial.print(RN[i]);Serial.println(";");
}
for (int i = 0; i <= 30 ; i++) {
Serial.print(" RNm[");
Serial.print(i);
Serial.print("]=");
Serial.print(RNm[i]);Serial.println(";");
}
for (int i = 0; i <= 160 ; i++) {
Serial.print(" CV[");
Serial.print(i);
Serial.print("]=");
Serial.print(CV[i]);Serial.println(";");
}
Serial.println("//end of defaults ");
}
void WriteEEPROM(void) {
#ifdef _DefaultPrintOut
PrintEEPROMSettings();
#endif
//write all variables across to RN[index's] for eeprom
for (int i = 0; i <= 160 ; i++) {
EEPROM.write(i, CV[i]);
}
// 0 to 160 are cvs
for (int i = 1; i <= 35 ; i++) {
EEPROM.write(i+180, RN[i]);
} // 180 to 215 are RN
for (int i = 1; i <= 30 ; i++) {
EEPROM.write(i+220, RNm[i]);
} // 220 to 225 are RNm
// space from 226 to 320 is free
for (int i = 0; i <= NumberOfPorts; i++) { //rn 314 for 8 ports 594 for 16
EEPROM.write(320 + (i*35), Value_for_PortD[i]);
EEPROM.write(320 + (i*35),Value_for_PortD[i]);
EEPROM.write(321 + (i*35),Pi02_Port_Settings_D[i]);
EEPROM.write(322 + (i*35),DelaySetting_for_PortD[i]);
EEPROM.write(323 + (i*35),Configuration_for_PortD[i]);
EEPROM.write(324 + (i*35),EventMarker_for_PortD[i]);
EEPROM.write(325 + (i*35),ID_High_for_PortD[i]); //idh
EEPROM.write(326 + (i*35),ID_Low_for_PortD[i]); //idl
EEPROM.write(327 + (i*35),XRef_Port[i]);
EEPROM.write(328 + (i*35),Pi03_Setting_offposH[i]);
EEPROM.write(329 + (i*35),Pi03_Setting_offposL[i]);
EEPROM.write(330 + (i*35),Pi03_Setting_onposH[i]);
EEPROM.write(331 + (i*35),Pi03_Setting_onposL[i]);
EEPROM.write(332 + (i*35),Pi03_Setting_offsteps[i]);
EEPROM.write(333 + (i*35),Pi03_Setting_onsteps[i]);
EEPROM.write(334 + (i*35),Pi03_Setting_options[i]);
}
Serial.println(" ");Serial.println(RocNodeID);
SetWordIn_msg_loc_value(RN, 1, RocNodeID);
Serial.println("--");//
Serial.println(F(" EEProm writing (but not committed yet--)"));//
// add the serial writes
writeString(ssidEEPROMLocation,wifiSSID);
writeString(passwordEEPROMLocation,wifiPassword);
Serial.print(" Broker Addr");Serial.println(BrokerAddr);
EEPROM.write(BrokerEEPROMLocation,BrokerAddr);
EEPROM.write(RocNodeIDLocation,RN[1]);
EEPROM.write(RocNodeIDLocation+1,RN[2]);
#ifdef _ROCDISP_EEPROM_DEBUG
Serial.println(" --------- Writing Oled Settings---------");
#endif
for (byte i = 0; i <=6; i++) {
EEPROM.write(DisplayClockBoolEEPROMLocation+(i),OLED_Settings[i]);
#ifdef _ROCDISP_EEPROM_DEBUG
OLEDsettingView(i);
#endif
}
#ifdef _ROCDISP_EEPROM_DEBUG
Serial.println();
#endif
// Serial.print(" RocNodeID:");Serial.println( (EEPROM.read(RocNodeIDLocation+1)*256)+ EEPROM.read(RocNodeIDLocation) );
delay(100); //
}
void ReadEEPROM() {
// eprom has: 0-256 ==cv's
for (int i = 0; i <= 160 ; i++) {
CV[i] = EEPROM.read(i);
}
// for (int i = 1; i <= (35 + (NumberOfPorts*35)) ; i++) {
// RN[i] = EEPROM.read(i+300); // so, =EEPROM.read(300+i)
// add the serial reads
// }
for (int i = 1; i <= 35 ; i++) { //preparatory to reducing size of RN array.
RN[i] = EEPROM.read(i+180); //
// add the serial reaads
}
for (int i = 1; i <= 30 ; i++) { //preparatory to reducing size of RN array.
RNm[i] = EEPROM.read(i+220); //
// add the serial reaads
}
//RN 1(lo),2hi is node address
RocNodeID= getTwoBytesFromMessageHL(RN, 1);
#ifdef _ForceRocnetNodeID_to_subIPL
RocNodeID=subIPL;
#endif
if (RocNodeID==0){RocNodeID=3;}
//Nickname, has length "RN[3]" followed by name ESP is default
//Nickname[32] so max should be RN3+32 =35
for (int i = 1 ; i <= 30; i++) { Nickname[i] =char(0);}
//Serial.println("--------------------------------------");
//Serial.print(" EEPROM Read: This Node Nickname is:");
for (int i = 1 ; i <= RN[3]; i++) {
Nickname[i-1] = char(RN[3 + i]);
}
Nickname[RN[3]]=char(0); //add null to end string nickname, length RN[3] ESP is default
//Serial.println("'");
//Serial.print("NICKNAME BUILT IS:");
//Serial.println(Nickname);
// eeprom 320 -> is saved port stuff
for (int i = 1; i <= NumberOfPorts; i++) {
Value_for_PortD[i] = EEPROM.read(320 + (i*35));
Pi02_Port_Settings_D[i] = EEPROM.read(321 + (i*35));
DelaySetting_for_PortD[i] = EEPROM.read(322 + (i*35));
Configuration_for_PortD[i] = EEPROM.read(323 + (i*35));
EventMarker_for_PortD[i] = EEPROM.read(324 + (i*35));
ID_High_for_PortD[i] = EEPROM.read(325 + (i*35)); //idh
ID_Low_for_PortD[i] = EEPROM.read(326 + (i*35)); //idl
XRef_Port[i] = EEPROM.read(327 + (i*35));
Pi03_Setting_offposH[i] = EEPROM.read(328 + (i*35));
Pi03_Setting_offposL[i] = EEPROM.read(329 + (i*35));
Pi03_Setting_onposH[i] = EEPROM.read(330 + (i*35));
Pi03_Setting_onposL[i] = EEPROM.read(331 + (i*35));
Pi03_Setting_offsteps[i] = EEPROM.read(332 + (i*35));
Pi03_Setting_onsteps[i] = EEPROM.read(333 + (i*35));
Pi03_Setting_options[i] = EEPROM.read(334 + (i*35));
}
wifiSSID=read_String(ssidEEPROMLocation);
//EEPROMRocNodeID=(EEPROM.read(RocNodeIDLocation+1)*256)+EEPROM.read(RocNodeIDLocation); // low then hi Plan to move to this from RocNodeID, or use it as backup
wifiPassword=read_String(passwordEEPROMLocation);
BrokerAddr=EEPROM.read(BrokerEEPROMLocation);
#ifdef _ROCDISP_EEPROM_DEBUG
Serial.println(" --------- Reading Oled Settings---------");
#endif
for (byte i = 0; i <=6; i++) {
OLED_Settings[i]=EEPROM.read(DisplayClockBoolEEPROMLocation+i);;
#ifdef _ROCDISP_EEPROM_DEBUG
OLEDsettingView(i);
#endif
}
#ifdef _ROCDISP_EEPROM_DEBUG
Serial.println();
#endif
Serial.print(" Broker Addr:");Serial.println(BrokerAddr);
Serial.print(" RocNodeID:");Serial.println(RocNodeID);
//Serial.print(" Copy of RocNodeID:");Serial.println(EEPROMRocNodeID);
}
uint8_t OLED_EEPROM_Setting(int OLed_x){
uint8_t Setting;
Setting=EEPROM.read(DisplayClockBoolEEPROMLocation+OLed_x);
return Setting;
}
void ROCSerialPrint(uint8_t *msg) {
Serial.print("NetId RidH RidL SidH SidL Grp Code Len");
for (byte i = 1; i <= (msg[7]); i++) {
Serial.print(" D");
Serial.print(i);
}
Serial.println();
for (byte i = 0; i <= (7 + msg[7]); i++) {
Serial.print(" ");
dump_byte(msg[i]);
}
Serial.println();
}
char* Show_ROC_MSG() {
if (Message_Length >= 1) {
DebugMessage[0] = 0;
strcat(DebugMessage, " NetID:");
snprintf(DebugMessage, sizeof(DebugMessage), "%s%d", DebugMessage, ROC_netid);
strcat(DebugMessage, " Rec:"); snprintf(DebugMessage, sizeof(DebugMessage), "%s%d", DebugMessage, ROC_recipient);
strcat(DebugMessage, " Sdr:"); snprintf(DebugMessage, sizeof(DebugMessage), "%s%d", DebugMessage, ROC_sender);
strcat(DebugMessage, " Grp:"); snprintf(DebugMessage, sizeof(DebugMessage), "%s%d", DebugMessage, ROC_group);
strcat(DebugMessage, " Code[");
if ((ROC_code & 0x60) == 0) {
strcat(DebugMessage, "Req]:");
}
if ((ROC_code & 0x60) == 0x20) {
strcat(DebugMessage, "Evt]:");
}
if ((ROC_code & 0x60) == 0x40) {
strcat(DebugMessage, "Rpy]:"); ////add request event reply then code.. (5 bits)
}
snprintf(DebugMessage, sizeof(DebugMessage), "%s%d", DebugMessage, (ROC_code & 0x1F));
for (byte i = 1; i <= ROC_len; i++) {
strcat(DebugMessage, " D"); snprintf(DebugMessage, sizeof(DebugMessage), "%s%d", DebugMessage, i);
strcat(DebugMessage, "="); snprintf(DebugMessage, sizeof(DebugMessage), "%s%d", DebugMessage, (ROC_Data[i]));
}
//Serial.print(DebugMessage);
} return DebugMessage;
}
void Show_ROC_MSGS(uint8_t *payload) {
ROC_netid = sendMessage[0];
ROC_recipient = IntFromPacket_at_Addr(sendMessage, Recipient_Addr);
ROC_sender = IntFromPacket_at_Addr(sendMessage, Sender_Addr);
ROC_group = sendMessage[5];
ROC_code = sendMessage[6];
ROC_len = sendMessage[7];
for (byte i = 1; i <= ROC_len; i++) {
ROC_Data[7 + i];
}
Message_Length = ROC_len + 7;
Serial.print(Show_ROC_MSG());
}
//extern void DoLocoMotor(void);
extern void SetMotorSpeed(int SpeedDemand_local,uint8_t dirf);
extern void ImmediateStop(void);
extern void WriteAnalogtoPort(uint8_t port,uint16_t demand);
void ROC_CS() { //group 1
uint16_t CVNum;
uint8_t OldValue;
switch (ROC_code) {
case 0: {} //NOP
break;
case 2: {
POWERON = ROC_Data[1];
Serial.println();
Serial.print(" Power set to:");
Serial.println(POWERON);
sendMessage[8] = 0x00; //clear before doing anything later .....
sendMessage[0] = ROC_netid;
SetWordIn_msg_loc_value(sendMessage, Recipient_Addr, 0x00 ); //response is to host, not cs
SetWordIn_msg_loc_value(sendMessage, Sender_Addr, RocNodeID ); //sent from the rocnode ?
sendMessage[5] = ROC_group;
sendMessage[6] = ROC_code | Code_Reply; //action group, response
sendMessage[7] = 1; //len of data coming next
bitWrite(sendMessage[8], 1, 1); //set 'Idle' ?
bitWrite(sendMessage[8], 0, POWERON); //set bit 0 in status to power state
//MQTTSend("rocnet/cs",sendMessage); //sends status response something wrong with this
//ROCSerialPrint(sendMessage);
Message_Decoded = true;
if (POWERON == false) {
DebugSprintfMsgSend(sprintf(DebugMsg," NODE OFF "));
ImmediateStop();
}
if (POWERON == true) {
DebugSprintfMsgSend(sprintf(DebugMsg," NODE ON "));
Last_DCC_Speed_Demand=0;
SetMotorSpeed(Last_DCC_Speed_Demand,DIRF);
//DoLocoMotor();
}
}
break;
case 8: {
//Serial.print(" debug**Group 1, Code 8: ROC_Data[1]<");Serial.print(ROC_Data[1]);Serial.print(" > ROC_Data[2]<");Serial.print(ROC_Data[2]); Serial.print("> equates to address of:"); Serial.println ((ROC_Data[2] + (ROC_Data[1] * 256)));
#ifdef _LOCO_SERVO_Driven_Port // do not do these cv response unless this is a LOCO
// Is this address to me?
if (((ROC_Data[2] + (ROC_Data[1] * 256)) == MyLocoAddr) || ((ROC_Data[2] == 0) && (ROC_Data[1] == 0))) {
#ifdef _SERIAL_DEBUG
if ((ROC_Data[2] + (ROC_Data[1] * 256)) == MyLocoAddr) { Serial.print(" Group 1, Code 8: CV change for this Loco ");
}else { Serial.print(" Group 1, Code 8: CV change for 0 0 ");}
#endif
CVNum = ((ROC_Data[3] * 256) + ROC_Data[4]);
OldValue = CV[CVNum]; //save the old value
Message_Decoded = true; //we understand these even if they are not for us
if (ROC_Data[6] == 1) { //this is a SET CV
if (CVNum == 8) { //set defaults or other things when CV8=13
if ((ROC_Data[5] == 8)) {// CV[8]==8 set to 8 - Triggers EEPROM defaults
Serial.print("Setting Defaults");
DebugSprintfMsgSend(sprintf(DebugMsg,"CV[8] set to 8 - Triggers EEPROM defaults"));
SetDefaultSVs();
if (millis()>=StartedAt+1000){// but do not do this if we have just started as it may have been just a re-send from mosquitto
Data_Updated = true;
WriteEEPROM();
EPROM_Write_Delay = millis() + 500; }
}
if ((ROC_Data[5] == 13)) { // // CV[8]==13 set to 13 triggers sending settings for recording or replacing those in the default code here
PrintEEPROMSettings();
}
if ((ROC_Data[5] == 100)) { // CV[8] set to 100 Spare trigger for tests
}
}
else { //Setting, but not special settings using CV[8] Y set if address is explicitly for me
if ((ROC_Data[2] + (ROC_Data[1] * 256)) == MyLocoAddr) { //only set if address is explicitly for me
//mqtt debug message
DebugSprintfMsgSend( sprintf ( DebugMsg, "Set CV[%d]=%d ",CVNum,ROC_Data[5]) );
CV[CVNum] = ROC_Data[5]; //set the new data
if (OldValue != ROC_Data[5]) {
if (millis()>=StartedAt+1000) { // Use started at Timer TO STOP EEPROM UPDATE if the mqtt sends a (repeated) valid set command on node connecting
Serial.println(" Setting new CV data");Data_Updated = true;
WriteEEPROM();
EPROM_Write_Delay = millis() + Ten_Sec; //update the time so you can press the same set and get another ten seconds delay
}
}
}
}
} //set end
if (ROC_Data[6] == 0) { //get
CVNum = ((ROC_Data[3] * 256) + ROC_Data[4]);
DebugSprintfMsgSend( sprintf ( DebugMsg, "Get CV[%d]=%d",CVNum,CV[CVNum]));
} //get end
//Serial.println(" Building and sending response"); Send response for both GET and SET...
//----------------SEND CV Starts-----------------
sendMessage[0] = ROC_netid;
SetWordIn_msg_loc_value(sendMessage, Recipient_Addr, 0x00 ); //response is to host, not cs
SetWordIn_msg_loc_value(sendMessage, Sender_Addr, MyLocoAddr ); //??sent from the loco not rocnodeid??
sendMessage[5] = ROC_group;
sendMessage[6] = ROC_code | Code_Reply; //action group, response
sendMessage[7] = 6; //len of data coming next
SetWordIn_msg_loc_value(sendMessage, 8, MyLocoAddr); //set 8 and 9 with which loco I am
SetWordIn_msg_loc_value(sendMessage, 10, CVNum); //set 10 and 11 with the CV number
sendMessage[12] = CV[CVNum]; //the CV value
sendMessage[13] = 1; //set
#ifdef _showRocMSGS
Serial.print("Response:");
Show_ROC_MSGS(sendMessage);
#endif
MQTTSend("rocnet/ht", sendMessage); //HOST not cs??
//------------SEND CV ENDS------------
//Serial.println("------------ double check message content------------");
//ROCSerialPrint(sendMessage);
//Serial.println("-------------- end double check --------------");
} //end of cv stuff for this loco or 00
#endif // _LOCO_SERVO_Driven_Port // add #ifdef to match #ifdef around "if" at the beginning else {}do not match!
Message_Decoded = true;
} //end of this case
break;
}
} // end of roc cs
extern bool ButtonState[PortsRange] ;
extern bool LastDebounceButtonState[PortsRange]; //Number of ports +2
extern bool LastSentButtonState[PortsRange]; //Number of ports +2
extern void SetSoundEffect(uint8_t Data1,uint8_t Data2,uint8_t Data3);
extern void BeginPlay(int Channel,const char *wavfilename, uint8_t Volume);
extern void SetMotorSpeed(int SpeedDemand,uint8_t dirf);
void ROC_MOBILE() { //group 2
uint8_t AdjustedSpeedDemand;
switch (ROC_code) {
case 0: {} //NOP
break;
case 1: {} //setup
break;
case 2: { //set Velocity, direction , lights
// Serial.print("Local:"); Serial.print(CV[1]); Serial.print(" MSG for:"); Serial.print(ROC_recipient);
//set Velocity, direction , lights
Message_Decoded = true; //we understand these even if they are not for us
#ifdef _LOCO_SERVO_Driven_Port
if (ROC_recipient == MyLocoAddr) { //data for me, do it!
//ROC_Data[1] is speed demand
bitWrite(DIRF, 5, ROC_Data[2]); // set direction and lights
bitWrite(DIRF, 4, ROC_Data[3]);
// Modifying output Moved all speed tables stuff to here
//DebugSprintfMsgSend( sprintf ( DebugMsg, " Set Speed<%d> Dir<%d> Lights<%d>",ROC_Data[1], bitRead(DIRF,5),bitRead(DIRF,4)));
AdjustedSpeedDemand=UseSpeedTablesorCV6(ROC_Data[1]);
#ifdef _SpeedTableDEBUG
DebugSprintfMsgSend( sprintf ( DebugMsg, "Roc Set Speed<%d> Adjusted_speed<%d> Config<%d>",ROC_Data[1],AdjustedSpeedDemand,bitRead(CV[29],4) ));
#endif
SetMotorSpeed(AdjustedSpeedDemand,DIRF);
//SetMotorSpeed(ROC_Data[1],DIRF);old
}
#endif
} //set Velocity, direction , lights
break;
case 3: {
Message_Decoded = true; //we understand these
if (ROC_recipient == MyLocoAddr) { //for me, do it!
//Serial.print(" Function change for :");
//Serial.print(ROC_recipient); Serial.print(" data :");
#ifdef _Audio
#ifdef _LOCO_SERVO_Driven_Port
if (millis()>=StartedAt+1000){ // but only after one second! (to miss out on any last will sound effect message from Mosquitto).
SetSoundEffect(ROC_Data[1],ROC_Data[2],ROC_Data[3]); //Move settings to SetSoundEffect
}else {Serial.println(" Ignoring sound effect request ");}
#endif
/* //ROC_Data[1]; //F1-F8
//ROC_Data[2]; //F9-F16
//ROC_Data[3]; //F17-F24
*/
#endif
}
} //end case 3 functions
break;
case 4: {} //query
break;
case 5: {} //fieldcmd
break;
}
}
void ROC_CLOCK() {
hrs = ROC_Data[5];
mins = ROC_Data[6];
secs = 1;
divider=ROC_Data[8];// Roc_data[7] is temperature
//PrintTime("Time synch \n");
//bad idea, to have lots of things transmitting immediately after synch..
//test with delay based on subIPL
delay(subIPL);
#ifndef _NoSend_Time_Synch_Debug // for a simpler display during debug
if (POWERON) {
DebugSprintfMsgSend( sprintf ( DebugMsg, " IPaddr.%d Time Synchronised. Power is ON",subIPL));
}
else{ DebugSprintfMsgSend( sprintf ( DebugMsg, " IPaddr.%d Time Synchronised. Power is OFF",subIPL));
}
#endif
Message_Decoded = true;
//set / synch clock
}
int NumberOfOLEDS;
void ROC_NODE() { //stationary decoders GROUP 3
uint8_t TEMP;
switch (ROC_code) {
uint8_t NodeClass;
case 8: { //Identify class manuID versionH versionL nr I/O subipH subipL //vendor @ class I/O revison
Message_Decoded = true; //we understand these even if they are not for us
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) { //Serial.println();
Serial.print("Responding to IDENTIFY. This node is:");
Serial.print(RocNodeID) ;
sendMessage[0] = ROC_netid;
SetWordIn_msg_loc_value(sendMessage, Recipient_Addr, ROC_sender);
SetWordIn_msg_loc_value(sendMessage, Sender_Addr, RocNodeID);
sendMessage[5] = 3;
sendMessage[6] = 8 + 32; //action group and code bit 6 (32) = set for REPLY
sendMessage[7] = 7 + RN[3]; //len of data coming next + nicname length
if (RN[3] >= 1) {
Serial.print(" Nickname :");
Serial.print(RN[3]);
Serial.print(" chars :'");
}
//Identify... data is: class manuID versionH versionL nr I/O subipH subipL
NodeClass= 0x01; //class? = io?"bit 0= accessory" bit 1= dcc Bit 3=RFID FF= Accessory DCC RFID
#ifdef _LOCO_SERVO_Driven_Port
NodeClass= 0x02;
#endif
sendMessage[8] = NodeClass;
sendMessage[9] = 13; //manuid
SetWordIn_msg_loc_value(sendMessage, 10, SW_REV);
sendMessage[12] = NumberOfPorts; //8 io seems fixed in rocrail?
sendMessage[13] = subIPH; //sub IPh
sendMessage[14] = subIPL; //sub IPl
for (int i = 1 ; i <= RN[3]; i++) {
Serial.print(char(RN[3 + i]));
sendMessage[14 + i] = RN[3 + i];
} //nickname, length RN[3] ESP is default
Serial.println("'");
//delay(subIPL*2); //prevent simultaneous responses to identify query
MQTTSend("rocnet/dc", sendMessage);
delay(100); //leave plenty of time before sending next mqtt
DebugSprintfMsgSend( sprintf ( DebugMsg, "Ver <%d> OLEDS<%d> Identifying ",SW_REV,NumberOfOLEDS));
//ROCSerialPrint(sendMessage);
}
Message_Decoded = true;
} //Identify... data is: class manuID versionH versionL nr I/O subipH subipL
break;
case 9: {
Message_Decoded = true; //we understand these even if they are not for us
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
if ( (ROC_recipient == RocNodeID) ){ //ONLY IF ITS actually me.. Do not switch off for generic 0 message.
DebugSprintfMsgSend( sprintf ( DebugMsg, "Grp 3 Code 9 Shutting Node <%d> power off ",ROC_recipient));
POWERON = false;}
else { DebugSprintfMsgSend( sprintf ( DebugMsg, "Grp 3 Code 9 for <%d> power off IGNORED. ",ROC_recipient)); }
}
Message_Decoded = true;
} //(Stationary ) Node Shutdown
break;
case 10: {
Message_Decoded = true; //we understand these even if they are not for us//Acknowledge
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
Serial.print("ACKnowledging action:"); //action and port in D1, D2
Serial.print(ROC_Data[1]);
if (ROC_len >= 2) {
Serial.print(" port:");
Serial.print(ROC_Data[2]);
}
Serial.println( );
//ROCSerialPrint(recMessage);
}
Message_Decoded = true;
}
break;
case 11: { //SHOW
Message_Decoded = true; //we understand these even if they are not for us
if (millis()>=StartedAt+1000){
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
FlashMessage("RocView Requests SHOW", 10, 500, 100); //hold the LED on for 800ms, off for 800ms, 6 times!
}}
Message_Decoded = true;
}
break;
}//end of case
}//end rocnode
extern int SDemand[PortsRange]; //Number of ports +2
void ROC_Programming() { //GROUP 5
bool Data_Changed;
switch (ROC_code) {
case 4: { //read port config
Message_Decoded = true; //we understand these even if they are not for us
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
int TEMP;
int port;
DebugSprintfMsgSend(sprintf(DebugMsg,"Multi byte READ Port# value, type delay :"));
//debugmsg
sendMessage[0] = ROC_netid;
SetWordIn_msg_loc_value(sendMessage, Recipient_Addr, ROC_sender );
SetWordIn_msg_loc_value(sendMessage, Sender_Addr, RocNodeID);
sendMessage[5] = ROC_group;
sendMessage[6] = ROC_code | Code_Reply; //action group and code bit 6 (64) = set for REPLY
sendMessage[7] = (((ROC_Data[2] - ROC_Data[1]) + 1) * 4); //len of data coming next from port to port..
TEMP = 0;
for (int i = 1 ; i <= ((((ROC_Data[2] - ROC_Data[1]) + 1)) * 4); i = i + 4) { //port# value type delay
port = ROC_Data[1] + TEMP;
sendMessage[7 + i] = port; //port# value type delay
sendMessage[7 + i + 1] = Value_for_PortD[port]; //value=
sendMessage[7 + i + 2] = Pi02_Port_Settings_D[port]; //type = switch
sendMessage[7 + i + 3] = DelaySetting_for_PortD[port]; //delay
//Serial.print(port);
TEMP = TEMP + 1;
}
MQTTSend("rocnet/ps", sendMessage);
//ROCSerialPrint(sendMessage);
} Message_Decoded = true;
}
break;
case 5: {
Message_Decoded = true; //we understand these even if they are not for us
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
DebugSprintfMsgSend(sprintf(DebugMsg,"Multi byte WRITE port, value type delay"));
//debugmsg
for (byte i = 1; i <= ROC_len; i = i + 4) {
//Serial.print(" Port:");
//Serial.print(ROC_Data[i]);
//Serial.print("] Value= :");
//Serial.print(ROC_Data[i+1]);
//Serial.print(" type= :");
//Serial.print(ROC_Data[i+2]);
//Serial.print(" Delay= :");
//Serial.print(ROC_Data[i+3]);
Value_for_PortD[ROC_Data[i]] = ROC_Data[i + 1]; //value=
Pi02_Port_Settings_D[ROC_Data[i]] = ROC_Data[i + 2]; //type =
DelaySetting_for_PortD[ROC_Data[i]] = ROC_Data[i + 3]; //delay
}
//Serial.println();
if (millis()>=StartedAt+1000){
Serial.println("Multi byte WRITE");
Data_Updated = true;
WriteEEPROM();
EPROM_Write_Delay = millis() + Ten_Sec;}
}
Message_Decoded = true;
}
break;
case 6: {
Message_Decoded = true; //we understand these even if they are not for us
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
if ((ROC_Data[3] == subIPH) && (ROC_Data[4] == subIPL)) {
Serial.print(F("Programming node: set RocNet ID from:"));
RocNodeID= getTwoBytesFromMessageHL(RN, 1);
// eepromrocnetnode set here soon!
Serial.print (RocNodeID);
Data_Changed=false;
RocNodeID= ((ROC_Data[1] << 8) + ROC_Data[2]);
#ifdef _ForceRocnetNodeID_to_subIPL
RocNodeID=subIPL;
Serial.print(" Node ID forced to subIPL");
#endif
if (getTwoBytesFromMessageHL(RN, 1)!= RocNodeID){
Serial.print(F("Programming node: set RocNet ID to:"));
SetWordIn_msg_loc_value(RN, 1, RocNodeID); //set RN 1 and 2
Serial.print (RocNodeID);
Data_Changed=true;}
else {Serial.print(F("Programming node: Unchanged ID"));}
//ROCSerialPrint(recMessage);
if ( ROC_len >= 6) { //set nickname
Serial.print(F("Programming nickname ?"));
Serial.print( ROC_len - 5); //new, uses rn3 as length
Serial.print(" bytes ");
if (RN[3]!= ROC_len - 5){Data_Changed=true;}
RN[3] = ROC_len - 5;
for (int p = 1; ((p <= RN[3]) && (p <= 25)); p++) { // p is max size of name
if (RN[3+p]!= ROC_Data[p + 5]){Data_Changed=true;}
Serial.write(ROC_Data[p + 5]);
RN[3 + p] = ROC_Data[p + 5];
}
}
Serial.println(" ");
if (Data_Changed){
if (millis()>=StartedAt+1000){
Data_Updated = true;
EPROM_Write_Delay = millis() + 100; // write quickly as we have changed something important!!
WriteEEPROM();}
}
else {Serial.print("Unchanged ID and nickname");}
}
}
Message_Decoded = true;
}
break;
case 7: { //report addr and status
Message_Decoded = true; //we understand these even if they are not for us
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
DebugSprintfMsgSend(sprintf(DebugMsg,"Reporting Node Addr and Status"));
//ROCSerialPrint(recMessage);
/*
NEW:
Reply Data 1 2 3 4 5 6 7 8 9 10 11 12 13
RN[15 16 17 18 19 20 21 22 23 24 15 26 27
iotype flags cstype csdevice I2C scan 0×20 H I2C scan 0×20 L I2C scan 0×30 H I2C scan 0×30 I2C scan 0×40 H I2C scan 0×40 L adc threshold I2C scan 0×50 H I2C scan 0×50 L
*/
RNm[15]=33; //iotype not a pi, i2c-0
RNm[16]=0; //0= no options set
RNm[17]=0; //cstype: 0=none, 1=dcc232, 2=sprog
RNm[18]=0; //csdevice: 0=/dev/ttyUSB0, 1=/dev/ttyUSB1 2= /dev/ttyACMO 3 gives error in radiobox in rocview..
RNm[19]=0; //020H //Pi o2's
RNm[20]=1; //020 l
RNm[21]=0; //030 H //Pi04's? / here is 0x38
RNm[22]=0;
if (OLED1Present){bitSet(RNm[22],0);} //030 L ??
if (OLED2Present){bitSet(RNm[22],1);}//not showing
RNm[23]=0; //040 H //Pi03's
RNm[24]=1; //040 l
RNm[25]=0; //adc thresh
RNm[26]=0; //050 H (ROC DISPLAYS)
RNm[27]=0; //050 L set 1 for "0x50" 3 for 0x50,0x51 etc..NOT USED BY my code as standard displays are 0x3C 0x3D
sendMessage[0] = ROC_netid;
SetWordIn_msg_loc_value(sendMessage, Recipient_Addr, ROC_sender);
SetWordIn_msg_loc_value(sendMessage, Sender_Addr, RocNodeID);
sendMessage[5] = ROC_group;
sendMessage[6] = ROC_code | Code_Reply; //action group and code bit 6 (64) = set for REPLY
sendMessage[7] = 13; //6 sends to RN20
for (int i = 1 ; i <= (sendMessage[7]); i = i + 1) {
sendMessage[7+i] = RNm[14+i];
}
MQTTSend("rocnet/ps", sendMessage);
//DebugSprintfMsgSend(sprintf(DebugMsg," sending (line 1159)"));
//ROCSerialPrint(sendMessage);
}
Message_Decoded = true;
}
break;
case 8: {//set rocnode options etc..
Message_Decoded = true; //we understand these even if they are not for us
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
//if ((recMessage[10]==subIPH) && (recMessage[11]==subIPL)){
DebugSprintfMsgSend(sprintf(DebugMsg,"Programming node options"));
RNm[15] = ROC_Data[1];
RNm[16] = ROC_Data[2];
RNm[17] = ROC_Data[3];
RNm[18] = ROC_Data[4];
RNm[19] = ROC_Data[5]; //ROC_Data[5] is minimal length of time a sensor will report occupied.
if (millis()>=StartedAt+1000){
EPROM_Write_Delay = millis() + 500; //not ten sec, as we have all the data now..
Data_Updated = true;
WriteEEPROM();}
}
Message_Decoded = true;
}
case 9: { //??Shutdown
Message_Decoded = true; //we understand these even if they are not for us
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
//this is used by the MACRO settings... I do not use this
DebugSprintfMsgSend(sprintf(DebugMsg,"Group5 Case 9 : Node on/off set to :"));
Serial.print("Data[1]:");
Serial.print(ROC_Data[1]);
Serial.print(" Data[2]:");
Serial.println(ROC_Data[2]);
//ROCSerialPrint(recMessage);
}
Message_Decoded = true;
}
break;
case 11: {
Message_Decoded = true; //we understand these even if they are not for us
if ( (ROC_recipient == RocNodeID) || ( ROC_recipient == 0)) {
DebugSprintfMsgSend (sprintf ( DebugMsg, "UPDATE FROM ROCRAIL Value <%d> <%d>",ROC_Data[1],ROC_Data[2]));
if ( (ROC_Data[1] == 0) && (ROC_Data[2] == 1)) {
DebugSprintfMsgSend(sprintf ( DebugMsg, "** RE-SETTING all to Defaults--"));
SetDefaultSVs();
CV[8] = 0x0D; //DIY MFR code
CV[7] = SW_REV; //ver
CV[1]=3; //set initial loco address as 3 (regardless of whatever the set defaults function says)
#ifdef _Force_Loco_Addr