forked from fredlcore/BSB-LAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSB_lan.ino
5910 lines (5562 loc) · 204 KB
/
BSB_lan.ino
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
char version[] = "0.41";
/*
*
* BSB Boiler-System-Bus LAN Interface
*
* ATTENION:
* There is no waranty that this system will not damage your heating system!
*
* Author: Gero Schumacher ([email protected]) (up to version 0.16)
* Frederik Holst ([email protected]) (from version 0.17 onwards)
* (based on the code and work from many other developers. Many thanks!)
*
* see README and HOWTO files for more information
*
* Version:
* 0.1 - 21.01.2015 - initial version
* 0.5 - 02.02.2015
* 0.6 - 02.02.2015
* 0.7 - 06.02.2015
* 0.8 - 05.03.2015
* 0.9 - 09.03.2015
* 0.10 - 15.03.2015
* 0.11 - 07.04.2015
* 0.12 - 09.04.2015
* 0.13 - 31.03.2016
* 0.14 - 04.04.2016
* 0.15 - 21.04.2016
* 0.15a - 25.07.2016
* 0.16 - 20.11.2016
* 0.17 - 20.12.2016
* 0.17a - 20.12.2016
* 0.18 - 22.12.2016
* 0.19 - 01.01.2017
* 0.20 - 27.01.2017
* 0.21 - 06.02.2017
* 0.22 - 07.02.2017
* 0.23 - 12.02.2017
* 0.24 - 14.02.2017
* 0.25 - 21.02.20172
* 0.26 - 27.02.2017
* 0.27 - 01.03.2017
* 0.28 - 05.03.2017
* 0.29 - 07.03.2017
* 0.30 - 22.03.2017
* 0.31 - 10.04.2017
* 0.32 - 18.04.2017
* 0.33 - 09.05.2017
* 0.34 - 29.05.2017
* 0.35 - 25.06.2017
* 0.36 - 23.08.2017
* 0.37 - 08.09.2017
* 0.38 - 22.11.2017
* 0.39 - 02.01.2018
* 0.40 - 21.01.2018
* 0.41 - 19.03.2018
*
* Changelog:
* version 0.41
* - Improved graph legend when plotting several parameters
* - Added JSON export; query with /JQ=a,b,c,d... or push queries to /JQ or push set commands to /JS
* - Logging of MAX! parameters now possible with logging parameter 20007
* - Added unit to log file as well as average output
* - Rewrote device matching in cmd_tbl to accomodate also device variant (Gerätevariante). Run /Q with activated "#definde DEBUG" to see if transition has worked for your device!
* - Bugfix ENUM memory adressing
* - Bugfix in reset function (/N)
* version 0.40
* - Implemented polling of MAX! heating thermostats, display with URL command /X.
* See BSB_lan_custom.h for an example to transmit average room temperature to heating system.
* - Added new category "22 - Energiezähler" - please note that all subsequent categories move one up!
* - New virtual parameter 1601 (manual TWW push)
* - Added Fujitsu Waterstage WSYP100DG6 device family (211)
* - Added CTC device family (103)
* - New definement "#define TRUSTED_IP2" to grant access to a second local IP address
* - Added optional definement "#define GatewayIP" in BSB_lan_config.h to enable setting router address different from x.x.x.1
* - Removed parameter 10109 because it is the same as 10000
* - Added function to check all known CommandIDs on your own heating system. Use /Q after enabling definement "#define DEBUG" in BSB_lan_config.h
* - Added parameter numbers to category menu
* - Updated analyze.sh
* - hopefully fixing the memory issue
* - Moved HTML strings to html_strings.h
* version 0.39
* - Implemntation of PPS-Bus protocol.
* See /K40 for the limited commands available for this bus.
* Use setBusType(2) to set to PPS upon boot or /P2 to switch temporarily.
* - Set GPIOs to input by using /Gxx,I
* - Definement "#define CUSTOM_COMMANDS" added.
* Use this in your configuration to include individual code from "BSB_lan_custom.h"
* (needs to be created by you!) which is executed at the end of each main loop.
* Variables "custom_timer" and "custom_timer_compare" have been added to execute
* code at arbitrary intervals.
* - Added LogoBloc Unit L-UB 25C device family (95)
* - several new parameters added
* - Bugfix for logging Brennerlaufzeit Stufe 2
* version 0.38
* - ATTENTION: New BSB_lan_config.h configurations! You need to adjust your configuration when upgrading to this version!
* Webserver port is now defined in #define Port xx
* IP address is now defined in #define IPAddr 88,88,88,88 form - note the commas instead of dots!
* Special log parameters 20002 to 20006 have changed, see BSB_lan_config.h for their new meaning
* - Added new virtual parameter 701 (Präsenztaste) which enters reduced temperature mode until next timed switch
* - Added Brötje BOB device family (138), including many new parameters!
* - Added Brötje SOB26 device family (28)
* - Added Elco Aquatop 8es device family (85)
* - Added Elco Thision 13 Plus device family (203)
* - Added Weishaupt WTU 25-G familiy (50)
* - Added output for absolute humidity (g/m3) for DHT22 sensors
* - New schematics for Arduino/Raspberry board layout
* - Included support for W5500 Ethernet2 shields. Activate definement ETHERNET_W5500 in BSB_lan_config.h
* - Including two-stage oil furnaces BC-counters and logging - please note that logging parameters have been adjusted, see BSB_lan_config.h for new values!
* - Added new options for commands /P and /S to allow specifying a different destination device during runtime
* - Added new configuration definement CUSTOM_COMMANDS which includes BSB_lan_custom.h at the end of each main loop. You may use custom_timer (set to current millis()) and custom_timer_compare to execute only every x milliseconds.
* - Bugfixing SD-card logging in monitor mode
* - Bugfix for setting hour:time parameters via webinterface
* version 0.37
* - LPB implementation! More than 450 parameters supported! Switch temporarily between LPB and BSB with the Px command (0=BSB, 1=LPB) or use the setBusType config option to set bus-type at boot-time. Parameter numbers are the same as for BSB.
* version 0.36
* - bugfix: brought back VT_BIT list of options which were erroneously deleted :(, fixed/freed several memory issues
* version 0.35
* - new category "Sitherm Pro"; caution: category numbers all move up by one, starting from category "Wärmepumpe" (from 20 to 21) onwards.
* - graph display of logging data now comes with crosshair and shows detailed values as tooltip
* - improved SD-card output by factor 3 (from 16 to 45 kbps), switching SD-card library from from SD.h to SdFat.h (https://github.com/greiman/SdFat) brings another 10% performance boost
* - adjusted paths and directory layout of SdFat to enable compiling from sketch directory.
* - new data type vt_sint for signed int data, currently only used in some Sitherm Pro parameters
* version 0.34
* - Log data can now be displayed as graph
* - Webinterface can now display and set vt_bit type parameters in human-readable form
* - added KonfigRGx descriptions; caution: various sources used, no guarantee that descriptions match your individual heating system!
* - vt_bit is generally read-only in the webinterface. To set, use URL command /S with decimal representation of value
* - fixed a bug with vt_seconds_short5, affecting parameters 9500 and 9540.
* - fixed bug regarding Fujitsu's device family (from 127 to 170)
* - moved libraries from folder libraries to src so they can be included without copying them to the Arduino libraries folder
* - modified DallasTemperature.h's include path for OneWire.h
* version 0.33
* - no more heating system definements anymore due to new autodetect function based on device family (parameter 6225), or set device_id variable to parameter value directly
* - two more security options: TRUSTED_IP to limit access to one IP address only, and HTTP authentication with username and password
* - Average values are saved on SD-card if present and LOGGER definement is activated
* - deactivate logging by setting /L0=0 - this way you can enable LOGGER definement without filling up SD card but still save average values
* - new error codes for THISION
* - added dump of data payload on website for commands of unknown type, greyed out unsopported parameters
* - enable logging of telegrams (log parameter 30000) also in monitor mode (bsb.cpp and bsb.h updated)
* - time from heating system is now retreived periodically from broadcast telegrams, further reducing bus activity
* - new data type vt_bit for parameters that set individual bits. Display as binary digits, setting still using decimal representation
* - new data type vt_temp_short5_us for unsigned one byte temperatures divided by 2 (so far only 887 Vorlaufsoll NormAussentemp)
* - new data type vt_percent5 for unsigned one byte temperatures divided by 2 (so far only 885 Pumpe-PWM Minimum)
* - new data type vt_seconds_word5 for two byte seconds divided by 2 (so far only 2232, 9500 and 9540)
* - new data type vt_seconds_short4 for (signed?) one byte seconds divided by 4 (so far only 2235)
* - new data type vt_seconds_short5 for (signed?) one byte seconds divided by 5 (so far only 9500, 9540)
* - new data type vt_speed2 for two byte rpm (so far only 7050)
* - cleaned up set() function from apparent duplicate cases
* - added cases for vt_temp_word, vt_seconds_word5, vt_temp_short, vt_temp_short5, vt_seconds_short4 to set() function
* version 0.32
* - lots of new parameters suppoerted
* - newly designed webinterface allows control over heating system without any additional software or cryptic URL commands. URL commands of course are still available, so no need to change anything when using FHEM etc.
* - German webinterface available with definement LANG_DE
* - new URL-command /LB=x to log only broadcast messages (x=1) or all bus messages (x=0)
* - new URL-command /X to reset the Arduino (need to enable RESET definement in BSB_lan_config.h)
* - new logging parameters 20002 and 20003 for hot water loading times and cycles
* - moved DS18B20 logging parameters from 20010-20019 to 20200-20299 and DHT22 logging parameters from 20020-20029 to 20100 to 20199
* - moved average logging parameter from 20002 to 20004
* - set numerous parameters to read-only because that's what they obviously are (K33-36)
* - various bugfixes
* version 0.31
* - increased dumping of logfile by factor 5 / as long as we still have memory left, you can increase logbuflen from 100 to 1000 to increase transfer speed from approx. 16 to 18 kB/s
* - adjusted burner activity monitoring based on broadcast messages for Brötje systems
* - removed definement PROGNR_5895 because so far, it has only disabled an ENUM definition.
* - removed definement PROGNR_6030 because double command ID could be resolved via BROETJE / non-BROETJE definements
* - renamed BROETJE_SOB to BROETJE in order to allow for fine-grained distinction between different BROETJE cases (e.g. 6800ff)
* This means you have to activate TWO definements when using a Brötje system now: The general BROETJE as well as BROETJE_SOB or BROETJE_BSW.
* Have a look at your serial log for parameters 6800 to see which command IDs fit your system and activate one of both accordingly.
* - changed 16-Bit addressing of flash memory to 32-Bit to address crashes due to ever growing PROGMEM tables - now we have lots of air to breathe again for new command IDs :)
* - removed trailing \0 string from several ENUMs that led to wrong ENUM listings. Please keep in mind not to end ENUMs with a trailing \0 !
* version 0.30
* - Time library by Paul Stoffregen (https://github.com/PaulStoffregen/Time) is now required and included in the library folder.
* - adds logging of raw telegram data to SD card with logging parameter 30000. Logging telegram data is affected by commands /V and /LU
* - adds command /LU=x to log only known (x=0) or unknown (x=1) command IDs when logging telegram data
* - removed define USE_BROADCAST, broadcast data is now always processed
* - new internal functions GetDateTime, TranslateAddr, TranslateType
* version 0.29
* - adds command /C to display current configuration
* - adds command /L to configure logging interval and parameters
* - adds option for command /A to set 24h average parameters during runtime
* - adds special parameter 20002 for logging /A command (24h averages, only makes sense for long logging intervals)
* - bugfixes for logging DS18B20 sensors
* version 0.28
* - adds special parameters 20000++ for SD card logging of /B, /T and /H commands (see BSB_lan_config.h for examples)
* - adds version info to BSB_LAN web interface
* version 0.27
* - adds date field to log file (requires exact time to be sent by heating system)
* - /D0 recreates datalog.txt file with table header
* - added "flags" field to command table structure. Currently, only FL_RONLY is supported to make a parameter read-only
* - added DEFAULT_FLAG in config. Defaults to NULL, i.e. all fields are read/writeable.
* Setting it to FL_RONLY makes all parameters read-only, e.g. for added level of security.
* Individual parameters can be set to NULL/FL_RONLY to make only these parameters writable/read-only.
* version 0.26
* - added functionality for logging on micro SD card, using the slot of the w5100 Ethernet shield
* - more parameters added (e.g. 8009)
* version 0.25
* - more FUJITSU parameters added
* version 0.24
* - updated README with added functions
* - added German translations of FAQ and README, courtesy of Ulf Dieckmann
* version 0.23
* - minor bugfix
* version 0.22
* - more FUJITSU parameters
* - (hopefully) correct implementation of VT_VOLTAGE readings
* - minor bugfixes
* version 0.21
* - added numerous parameters for Fujitsu Wärmepumpe, including new #define FUJITSU directive to activate these parameters due to different parameter numbers
* - minor bugfixes
* version 0.20
* - added more parameters for Feststoffkessel
* - minor bugfixes
* version 0.19
* - added humidity command "H", currently for DHT22 sensors
* - added 24h average command "A", define parameters in BSB_lan_config.h
* - removed trailing whitespace from menu strings
* - fixed command id 0x053D04A2 for THISION heaters
* - included Rob Tillaart's DHT library because there are various libraries implementing the protocol and this one is used in the code for its ability to address multiple sensors with one object.
* - removed /temp URL parameter as it is a duplicate of /T
* - included loop to display DHT22 sensors in IPWE
* - making compiling IPWE extensions optional (#define IPWE)
* version 0.18
* - split off configuration into bsb_lan_config.h
* - split off command definitions into bsb_lan_defs.h
* - changed GPIO return values from LOW/HIGH to 1/0
* - reactivated and updated IPWE (define parameters in config)
* - check for protected pins when accessing GPIO (define in config)
* - added schematics and PCB files to new subfolder "schematics"
* version 0.17a
* - minor errors corrected
* version 0.17
* - merged v0.16 with FHEM user miwi's changes
* version 0.16
* - removed IPWE and EthRly interface
* - added GPIO interface
* - merged parameters from J.Weber
* - resolved duplicate command IDs
* version 0.15a
* - collated the commands from a Python project and this project,
* merged the two versions, corrected obvious errors.
* Inserted hypothetical numerical values in ENUM definitions
* where Broetje manuals documented only the message texts.
* - added information from traces in a Broetje installation with
* an ISR-SSR controller and a WOB 25C oil furnace.
* version 0.15
* - added Solar and Pufferspeicher from Elco Logon B & Logon B MM
* version 0.14
* - minor bugfixes for Broetje SOB
* - extended broadcast handling (experimental)
* version 0.13
* - change resistor value in receiving path from 4k7 to 1k5
* - added values 0x0f and 0x10 to Enum8005
* - fixed strings for Zeitprogramme
* - added timeout for sending a message (1 second)
* - added option T for querying one wire temperature sensors in mixed querys
* - added special handling for Broetje SOB
* - simplified settings
* version 0.12
* - added ONEWIRE_SENSORS to ipwe
* - fixed parameter decoding for ELCO Thision heating system
* version 0.11
* - fixed parameter decoding for ELCO Thision heating system
* version 0.10
* - added more parameters for ELCO Thision heating system
* version 0.9
* - added more parameters for ELCO Thision heating system
* - printTelegramm returns value string for further processing
* version 0.8
* - added parameters for ELCO Thision heating system
* - added IPWE extension
* - minor bugfixes
* version 0.7
* - added bus monitor functionality
* version 0.6
* - renamed SoftwareSerial to BSBSoftwareSerial
* - changed folder structure to enable simple build with arduino sdk
* version 0.5
* - bugfixes
* - added documentation (README)
* - added passkey feature
* - added R feature (query reset value)
* - added E feature (list enum values)
* - added setter for almost all value types
* - fixed indentation
* - added V feature to set verbosity for serial output
* - set baudrate to 115200 for serial output
* - redirecting favicon request
* - added some images of the BSB adapter
*
*/
#include "src/BSB/BSBSoftwareSerial.h"
#include "src/BSB/bsb.h"
#include "BSB_lan_config.h"
#include "BSB_lan_defs.h"
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <Arduino.h>
#include <SPI.h>
#include <EEPROM.h>
#include <util/crc16.h>
#include "src/Time/TimeLib.h"
#include "html_strings.h"
#ifdef ETHERNET_W5500
#include "src/Ethernet2/src/Ethernet2.h"
#else
#include <Ethernet.h>
#endif
#ifdef TRUSTED_IP
#ifdef ETHERNET_W5500
#include "src/Ethernet2/src/utility/w5500.h"
#else
#include <utility/w5100.h>
#endif
#endif
IPAddress ip(IPAddr);
EthernetServer server(Port);
#ifdef GatewayIP
IPAddress gateway(GatewayIP);
#endif
uint8_t len_idx, pl_start;
uint8_t myAddr = bus.getBusAddr();
uint8_t destAddr = bus.getBusDest();
/* buffer to load PROGMEM values in RAM */
#define BUFLEN 100
char buffer[BUFLEN] = { 0 };
/* buffer to print output lines*/
#define OUTBUF_LEN 300
char outBuf[OUTBUF_LEN] = { 0 };
byte outBufLen=0;
char div_unit[10];
EthernetClient client;
#ifdef MAX_CUL
EthernetClient max_cul;
uint16_t max_cur_temp[20] = { 0 };
uint8_t max_dst_temp[20] = { 0 };
int8_t max_valve[20] = { -1 , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int32_t max_devices[20] = { 0 };
#endif
/*
int16_t json_parameters[20] = { -1 , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
double json_values[20] = { 0 };
uint8_t json_types[20] = { 0 };
*/
// char _ipstr[INET6_ADDRSTRLEN]; // addr in format xxx.yyy.zzz.aaa
char _ipstr[20]; // addr in format xxx.yyy.zzz.aaa
byte __remoteIP[4] = {0,0,0,0}; // IP address in bin format
#ifdef LOGGER
// #include <SD.h> // if you run into troubles with SdFat.h, just remove the following two lines and uncomment this line.
#include "src/SdFat/SdFat.h"
SdFat SD;
File Logfile;
#endif
#ifdef ONE_WIRE_BUS
#include "src/OneWire/OneWire.h"
#include "src/DallasTemperature/DallasTemperature.h"
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numSensors;
#endif
#ifdef DHT_BUS
#include "src/DHT/dht.h"
int DHT_Pins[] = {DHT_BUS};
dht DHT;
#endif
char date[20];
static unsigned long lastAvgTime = millis();
static unsigned long lastLogTime = millis();
static unsigned long custom_timer = millis();
unsigned long custom_timer_compare = 0;
int numAverages = sizeof(avg_parameters) / sizeof(int);
int anz_ex_gpio = sizeof(exclude_GPIO) / sizeof(byte);
int numLogValues = sizeof(log_parameters) / sizeof(int);
double *avgValues = new double[numAverages];
double *avgValues_Old = new double[numAverages];
double *avgValues_Current = new double[numAverages];
int avgCounter = 1;
uint_farptr_t enumstr_offset = 0;
uint8_t my_dev_fam = 0;
uint8_t my_dev_var = 0;
// variables for handling of broadcast messages
int brenner_stufe = 0;
unsigned long brenner_start = 0;
unsigned long brenner_start_2 = 0;
unsigned long brenner_duration= 0;
unsigned long brenner_duration_2= 0;
unsigned long brenner_count = 0;
unsigned long brenner_count_2 = 0;
unsigned long TWW_start = 0;
unsigned long TWW_duration= 0;
unsigned long TWW_count = 0;
// PPS-bus variables
uint8_t msg_cycle = 0;
double pps_values[PPS_ANZ] = { 0 };
/* ******************************************************************
* ************** Program code starts here **************
* *************************************************************** */
/** ****************************************************************
* Function: outBufclear()
* Does: Sets ouBufLen = 0 and puts the end-of-string character
* into the first buffer position.
* Pass parameters:
* none
* Parameters passed back:
* none
* Function value returned:
* none
* Global resources used:
* char outBuf[]
* *************************************************************** */
void outBufclear(void){
outBufLen=0;
outBuf[0]='\0';
}
/** *****************************************************************
* Function: dayofweek()
* Does: Accepts a date as day / month / year and calculates
* the day of the week for this day.
* 1=Monday, .. , 7=Sunday
* No sanity checks are performed on the pass parameters.
* Pass parameters:
* uint8 day 1 .. 31
* uint8 month 1 .. 12
* uint16 year
* Parameters passed back:
* none
* Function value returned:
* day-of-week [1-7]
* Global resources used:
* none
* *************************************************************** */
int dayofweek(uint8_t day, uint8_t month, uint16_t year)
{
/** Zeller's congruence for the Gregorian calendar. **/
/** With 1=Monday, ... 5=Saturday, 7=Sunday **/
if (month < 3) {
month += 12;
year--;
}
return (((13*month+3)/5 + day + year + year/4 - year/100 + year/400) % 7) + 1;
}
/** *****************************************************************
* Function: findLine()
* Does: Scans the command table struct for a matching line
* number (ProgNr) and returns the command code.
* Pass parameters:
* uint16 line the requested match (ProgNr)
* uint16 startidx starting line (ProgNr) for search .
* Works best if i>0 ;-)
* uint32 *cmd pointer to 32-bit command code variable
* Parameters passed back:
* uint32 *cmd 32-bit command code value filled in
* Function value returned:
* -1 command (ProgNr) not found
* i >= 0 success, usable to index the matching table row
* Global resources used:
* none
* *************************************************************** */
int findLine(uint16_t line
, uint16_t start_idx //
, uint32_t *cmd) // 32-bit command code
{
int found;
int i;
int save_i;
uint32_t c, save_c;
uint16_t l;
// search for the line in cmdtbl
i=start_idx;
found=0;
do{
c=pgm_read_dword_far(pgm_get_far_address(cmdtbl[0].cmd) + i * sizeof(cmdtbl[0]));
// c=pgm_read_dword(&cmdtbl[i].cmd); // command code
if(c==CMD_END) break;
l=pgm_read_word_far(pgm_get_far_address(cmdtbl[0].line) + i * sizeof(cmdtbl[0]));
// l=pgm_read_word(&cmdtbl[i].line); // ProgNr
if(l==line){
uint8_t dev_fam = pgm_read_dword_far(pgm_get_far_address(cmdtbl[0].dev_fam) + i * sizeof(cmdtbl[0]));
uint8_t dev_var = pgm_read_dword_far(pgm_get_far_address(cmdtbl[0].dev_var) + i * sizeof(cmdtbl[0]));
uint8_t dev_flags = pgm_read_dword_far(pgm_get_far_address(cmdtbl[0].flags) + i * sizeof(cmdtbl[0]));
if ((dev_fam == my_dev_fam || dev_fam == 255) && (dev_var == my_dev_var || dev_var == 255)) {
if (dev_fam == my_dev_fam && dev_var == my_dev_var) {
if ((dev_flags & FL_NO_CMD) == FL_NO_CMD) {
while (c==pgm_read_dword_far(pgm_get_far_address(cmdtbl[0].cmd) + i * sizeof(cmdtbl[0]))) {
i++;
}
found=0;
i--;
} else {
found=1;
break;
}
} else if ((!found && dev_fam!=my_dev_fam) || (dev_fam==my_dev_fam)) { // wider match has hit -> store in case of best match
if ((dev_flags & FL_NO_CMD) == FL_NO_CMD) {
while (c==pgm_read_dword_far(pgm_get_far_address(cmdtbl[0].cmd) + i * sizeof(cmdtbl[0]))) {
i++;
}
found=0;
i--;
} else {
found=1;
save_i=i;
save_c=c;
}
}
}
}
if(l>line){
if (found) {
i=save_i;
c=save_c;
}
break;
}
i++;
}while(1);
if(!found){
return -1;
}
if(cmd!=NULL) *cmd=c;
return i;
}
/** *****************************************************************
* Function: freeRam()
* Does: Returns the amount of available RAM
*
* *************************************************************** */
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
/** *****************************************************************
* Function: SerialPrintHex()
* Does: Sends the hex representation of one byte to the PC
* hardware serial interface. Adds a leading zero if
* it is a one-digit hex value.
* Pass parameters:
* byte the value to convert and send
* Parameters passed back:
* none
* Function value returned:
* none
* Global resources used:
* Serial instance
* *************************************************************** */
void SerialPrintHex(byte val) {
if (val < 16) Serial.print(F("0")); // add a leading zero to single-digit values
Serial.print(val, HEX);
}
/** *****************************************************************
* Function: SerialPrintHex32()
* Does: Sends the hex representation of a 32-bit value to the
* PC hardware serial interface. Pad with leading zeroes
* to get an eight-character hex representation.
* Pass parameters:
* uint32 val The value to convert and send
* Parameters passed back:
* none
* Function value returned:
* none
* Global resources used:
* Serial instance
* *************************************************************** */
void SerialPrintHex32(uint32_t val) {
if (val <= 0x0fffffff) Serial.print(F("0"));
if (val <= 0x00ffffff) Serial.print(F("0"));
if (val <= 0x000fffff) Serial.print(F("0"));
if (val <= 0x0000ffff) Serial.print(F("0"));
if (val <= 0x00000fff) Serial.print(F("0"));
if (val <= 0x000000ff) Serial.print(F("0"));
if (val <= 0x0000000f) Serial.print(F("0"));
Serial.print(val, HEX);
}
/** *****************************************************************
* Function: SerialPrintData()
* Does: Sends the payload of a telegram in hex to the PC
* hardware serial interface in hex.
* Pass parameters:
* byte *msg bus telegram with magic byte, header, payload and
* checksum
* Parameters passed back:
*
* Function value returned:
*
* Global resources used:
* Serial instance
* *************************************************************** */
void SerialPrintData(byte* msg){
// Calculate pure data length without housekeeping info
int data_len=0;
if (bus_type == 1) {
data_len=msg[len_idx]-14; // get packet length, then subtract
}
if (bus_type == 0) {
data_len=msg[len_idx]-11; // get packet length, then subtract
}
if (bus_type == 2) {
data_len=9;
}
// Start indexing where the payload begins
for(int i=0;i<data_len;i++){
SerialPrintHex(msg[pl_start+i]);
Serial.print(F(" "));
}
}
/** *****************************************************************
* Function: SerialPrintRAW()
* Does: Sends the telegram content in hex to the PC hardware
* serial interface, starting at position null. It stops
* when it has sent the requested number of message bytes.
* WARNING: this routine does not perform a sanity check
* of the length pass parameter.
* Pass parameters:
* byte *msg pointer to the telegram buffer
* byte len the requested number of bytes to send.
* Parameters passed back:
* none
* Function value returned:
* none
* Global resources used:
* Serial instance
* *************************************************************** */
void SerialPrintRAW(byte* msg, byte len){
for(int i=0;i<len;i++){
SerialPrintHex(msg[i]);
Serial.print(F(" "));
}
}
/** *****************************************************************
* Function: TranslateAddr()
* Does: Returns human-readable device names
* selected by a device address.
* If the device number is not in the list of known
* addresses, return the number itself.
*
* Pass parameters:
* byte addr the Int8 address value for which we seek the device
* name; values GT 127 are mapped to 0 <= addr <= 127
* Parameters passed back:
* none
* Function value returned:
* device string
* Global resources used:
* none
* *************************************************************** */
char *TranslateAddr(byte addr, char *device){
switch(addr&0x7F){
case ADDR_HEIZ: strncpy(device, "HEIZ", 4); break;
case ADDR_EM1: strncpy(device, "EM1", 4); break;
case ADDR_EM2: strncpy(device, "EM2", 4); break;
case ADDR_RGT1: strncpy(device, "RGT1", 4); break;
case ADDR_RGT2: strncpy(device, "RGT2", 4); break;
case ADDR_CNTR: strncpy(device, "CNTR", 4); break;
case ADDR_DISP: strncpy(device, "DISP", 4); break;
case ADDR_SRVC: strncpy(device, "SRVC", 4); break;
case ADDR_ALL: strncpy(device, "ALL ", 4); break;
default: sprintf(device, "%02X", addr); break;
}
device[4] = 0;
return device;
}
/** *****************************************************************
* Function: SerialPrintAddr()
* Does: Sends human-readable device names to the PC hardware
* serial interface, selected by a device address.
* If the device number is not in the list of known
* addresses, send the number itself.
*
* Pass parameters:
* byte addr the Int8 address value for which we seek the device
* name; values GT 127 are mapped to 0 <= addr <= 127
* Parameters passed back:
* none
* Function value returned:
* none
* Global resources used:
* Serial instance
* *************************************************************** */
void SerialPrintAddr(byte addr){
switch(addr&0x7F){
case ADDR_HEIZ: Serial.print(F("HEIZ")); break;
case ADDR_EM1: Serial.print(F("EM1")); break;
case ADDR_EM2: Serial.print(F("EM2")); break;
case ADDR_RGT1: Serial.print(F("RGT1")); break;
case ADDR_RGT2: Serial.print(F("RGT2")); break;
case ADDR_CNTR: Serial.print(F("CNTR")); break;
case ADDR_SRVC: Serial.print(F("SRVC")); break;
case ADDR_DISP: Serial.print(F("DISP")); break;
case ADDR_ALL: Serial.print(F("ALL ")); break;
default: SerialPrintHex(addr); break;
}
}
/** *****************************************************************
* Function: TranslateAddr()
* Does: Returns the message type in human-readable form.
* Pass parameters:
* byte type a number which indicates the type
* Parameters passed back:
* none
* Function value returned:
* message type
* Global resources used:
* none
* *************************************************************** */
char *TranslateType(byte type, char *mtype){
switch(type){
case TYPE_QINF: strncpy(mtype, "QINF", 4); break;
case TYPE_INF: strncpy(mtype, "INF", 4); break;
case TYPE_SET: strncpy(mtype, "SET", 4); break;
case TYPE_ACK: strncpy(mtype, "ACK", 4); break;
case TYPE_NACK: strncpy(mtype, "NACK", 4); break;
case TYPE_QUR: strncpy(mtype, "QUR", 4); break;
case TYPE_ANS: strncpy(mtype, "ANS", 4); break;
case TYPE_QRV: strncpy(mtype, "QRV", 4); break;
case TYPE_ARV: strncpy(mtype, "ARV", 4); break;
case TYPE_ERR: strncpy(mtype, "ERR", 4); break;
// If no match found: print the hex value
default: sprintf(mtype, "%02X", type); break;
} // endswitch
mtype[4] = 0;
return mtype;
}
/** *****************************************************************
* Function: SerialPrintType()
* Does: Prints the message type in human-readable form.
* Pass parameters:
* byte type a number which indicates the type
* Parameters passed back:
* none
* Function value returned:
* none
* Global resources used:
* Serial the hardware serial interface to a PC
* *************************************************************** */
void SerialPrintType(byte type){
switch(type){
case TYPE_QINF: Serial.print(F("QINF")); break;
case TYPE_INF: Serial.print(F("INF")); break;
case TYPE_SET: Serial.print(F("SET")); break;
case TYPE_ACK: Serial.print(F("ACK")); break;
case TYPE_NACK: Serial.print(F("NACK")); break;
case TYPE_QUR: Serial.print(F("QUR")); break;
case TYPE_ANS: Serial.print(F("ANS")); break;
case TYPE_QRV: Serial.print(F("QRV")); break;
case TYPE_ARV: Serial.print(F("ARV")); break;
case TYPE_ERR:
Serial.print(F("ERR"));
//outBufLen+=sprintf(outBuf+outBufLen,"ERR");
break;
// If no match found: print the hex value
default: SerialPrintHex(type); break;
} // endswitch
} // --- SerialPrintType() ---
/** *****************************************************************
* Function:
* Does:
* Pass parameters:
*
* Parameters passed back:
*
* Function value returned:
*
* Global resources used:
*
* *************************************************************** */
void printLineNumber(uint32_t val) {
char *p=outBuf+outBufLen;
outBufLen+=sprintf(outBuf+outBufLen,"%4ld",val);
Serial.print(p);
}
/** *****************************************************************
* Function:
* Does:
* Pass parameters:
*
* Parameters passed back:
*
* Function value returned:
*
* Global resources used:
*
* *************************************************************** */
void printBIT(byte *msg,byte data_len){
char *p=outBuf+outBufLen;
if(data_len == 2){
if(msg[pl_start]==0){
for (int i=7;i>=0;i--) {
outBufLen+=sprintf(outBuf+outBufLen,"%d",msg[pl_start+1] >> i & 1);
}
} else {
outBufLen+=sprintf(outBuf+outBufLen,"---");
}
Serial.print(p);
}else{
Serial.print(F("BYTE len error len!=2: "));
SerialPrintData(msg);
outBufLen+=sprintf(outBuf+outBufLen,"decoding error");
}
}
/** *****************************************************************
* Function:
* Does:
* Pass parameters:
*
* Parameters passed back:
*
* Function value returned:
*
* Global resources used:
*
* *************************************************************** */
void printBYTE(byte *msg,byte data_len,const char *postfix){
char *p=outBuf+outBufLen;
if(data_len == 2){
if(msg[pl_start]==0){
outBufLen+=sprintf(outBuf+outBufLen,"%d",msg[pl_start+1]);
} else {
outBufLen+=sprintf(outBuf+outBufLen,"---");
}
if(postfix!=NULL){
outBufLen+=sprintf(outBuf+outBufLen," %s",postfix);
}
Serial.print(p);
}else{
Serial.print(F("BYTE len error len!=2: "));
SerialPrintData(msg);
outBufLen+=sprintf(outBuf+outBufLen,"decoding error");
}
}
/** *****************************************************************
* Function:
* Does:
* Pass parameters:
*
* Parameters passed back:
*
* Function value returned:
*
* Global resources used:
*
* *************************************************************** */
void printWORD(byte *msg,byte data_len, long multiplier, const char *postfix){
long lval;
char *p=outBuf+outBufLen;
if(data_len == 3){
if(msg[pl_start]==0){
lval=((long(msg[pl_start+1])<<8)+long(msg[pl_start+2])) * multiplier;
outBufLen+=sprintf(outBuf+outBufLen,"%ld",lval);
} else {
outBufLen+=sprintf(outBuf+outBufLen,"---");
}
if(postfix!=NULL){
outBufLen+=sprintf(outBuf+outBufLen," %s",postfix);
}
Serial.print(p);
}else{
Serial.print(F("WORD len error len!=3: "));
SerialPrintData(msg);
outBufLen+=sprintf(outBuf+outBufLen,"decoding error");
}
}
/** *****************************************************************
* Function:
* Does:
* Pass parameters:
*
* Parameters passed back:
*
* Function value returned:
*
* Global resources used:
*
* *************************************************************** */
void printSINT(byte *msg,byte data_len, long multiplier, const char *postfix){
int16_t lval;
char *p=outBuf+outBufLen;
if(data_len == 3){
if(msg[pl_start]==0){
lval=(((int16_t)(msg[pl_start+1])<<8) + (int16_t)(msg[pl_start+2])) * multiplier;
outBufLen+=sprintf(outBuf+outBufLen,"%d",lval);
} else {
outBufLen+=sprintf(outBuf+outBufLen,"---");
}
if(postfix!=NULL){
outBufLen+=sprintf(outBuf+outBufLen," %s",postfix);
}
Serial.print(p);
}else{
Serial.print(F("WORD len error len!=3: "));
SerialPrintData(msg);
outBufLen+=sprintf(outBuf+outBufLen,"decoding error");
}
}
/** *****************************************************************
* Function:
* Does:
* Pass parameters:
*
* Parameters passed back:
*
* Function value returned:
*
* Global resources used:
*
* *************************************************************** */
void printDWORD(byte *msg,byte data_len,long divider, const char *postfix){
long lval;
char *p=outBuf+outBufLen;
if(data_len == 5){
if(msg[pl_start]==0){
lval=((long(msg[pl_start+1])<<24)+(long(msg[pl_start+2])<<16)+(long(msg[pl_start+3])<<8)+long(msg[pl_start+4]))/divider;
outBufLen+=sprintf(outBuf+outBufLen,"%ld",lval);
} else {
outBufLen+=sprintf(outBuf+outBufLen,"---");
}
if(postfix!=NULL){
outBufLen+=sprintf(outBuf+outBufLen," %s",postfix);
}
Serial.print(p);
}else{
Serial.print(F("DWORD len error len!=5: "));
SerialPrintData(msg);
outBufLen+=sprintf(outBuf+outBufLen,"decoding error");
}
}
/** *****************************************************************
* Function:
* Does:
* Pass parameters:
*
* Parameters passed back:
*
* Function value returned:
*
* Global resources used:
*
* *************************************************************** */
void _printFIXPOINT(double dval, int precision){
int a,b,i;
if(dval<0){
outBufLen+=sprintf(outBuf+outBufLen,"-");
dval*=-1.0;
}
double rval=10.0;