-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPRSTRX.ino
2622 lines (2348 loc) · 88.1 KB
/
APRSTRX.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
/////////////////////////////////////////////////////////////////////////////////////////
// V2.9 Bug with beacons on main frequency
// V2.8 OTA
// V2.6 Screen rotation and calibration added
// V2.5 Battery level
// V2.4 Beeper
// V2.3 Removed delay from loop
// V2.2 Added a switch to enable/disable serial debugging
// V2.1 RS232 Read tasks replaced to second task (on other core)
// V2.0 GPS and DRA separated
// GPS is connected to pin 39 (only RX)
// DRA is connected to 16 (RX) and 17 (TX)
// Implemented S meter
// V1.4 Rename HandleButton->HandleFunction, change SaveConfig and replace APRS setter
// V1.3 Code review
// V1.2 Better highlighted button
// V1.1 Gradient buttons
// V1.0
// V0.9AC
//
// DRA818 Info :http://www.dorji.com/docs/data/DRA818V.pdf
// LibAPRS from :https://codeload.github.com/tomasbrincil/LibAPRS-esp32/zip/refs/heads/master
//
// *********************************
// ** Display connections **
// *********************************
// |------------|------------------|
// |Display 2.8 | ESP32 |
// | ILI9341 | |
// |------------|------------------|
// | Vcc | 3V3 |
// | GND | GND |
// | CS | 15 |
// | Reset | 4 |
// | D/C | 2 |
// | SDI | 23 |
// | SCK | 18 |
// | LED Coll.| 14 2K |
// | SDO | |
// | T_CLK | 18 |
// | T_CS | 5 |
// | T_DIN | 23 |
// | T_DO | 19 |
// | T_IRQ | 34 |
// |------------|------------------|
/////////////////////////////////////////////////////////////////////////////////////////
#include <Arduino.h>
#include <SPI.h>
#include <TFT_eSPI.h> // https://github.com/Bodmer/TFT_eSPI
#include <WiFi.h>
#include <WifiMulti.h>
#include <EEPROM.h>
#include "NTP_Time.h"
#include <TinyGPS++.h>
#include <LibAPRS.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <esp_task_wdt.h>
#include <HardwareSerial.h>
#include "esp_adc_cal.h"
#include <RDKOTA.h>
#define offsetEEPROM 32
#define EEPROM_SIZE 2048
#define AA_FONT_SMALL "fonts/NotoSansBold15" // 15 point sans serif bold
#define AA_FONT_LARGE "fonts/NotoSansBold36" // 36 point sans serif bold
#define VERSION "PA2RDK_IGATE_TCP"
#define INFO "Arduino PA2RDK IGATE"
#define WDT_TIMEOUT 10
#define TFT_GREY 0x5AEB
#define TFT_LIGTHYELLOW 0xFF10
#define TFT_DARKBLUE 0x016F
#define TFT_SHADOW 0xE71C
#define TFT_BUTTONTOPCOLOR 0xB5FE
#define BTN_NAV 32768
#define BTN_NEXT 16384
#define BTN_PREV 8192
#define BTN_CLOSE 4096
#define BTN_ARROW 2048
#define BTN_NUMERIC 1024
#define RXD1 39
#define TXD1 -1
#define RXD2 16
#define TXD2 17
#define PTTIN 27
#define PTTOUT 33
#define BTNSMIKEPIN 35
#define SQUELSHPIN 21
#define TRXONPIN 12
#define DISPLAYLEDPIN 14
#define HIPOWERPIN 13
#define MUTEPIN 22
#define BEEPPIN 32
#define ADC_REFERENCE REF_3V3
#define OPEN_SQUELCH false
#define SCAN_STOPPED 0
#define SCAN_INPROCES 1
#define SCAN_PAUSED 2
#define SCAN_TYPE_STOP 0
#define SCAN_TYPE_RESUME 1
#define SHIFT_POS 1
#define SHIFT_NONE 0
#define SHIFT_NEG -1
#define TONETYPENONE 0
#define TONETYPERX 1
#define TONETYPETX 2
#define TONETYPERXTX 3
#define LipoVoltpin 36
#define LipoMeasureTime 10 // Lipo check every 10 seconds
#define OTAHOST "https://www.rjdekok.nl/Updates/APRSTRX"
#define OTAVERSION "v2.9"
//#define DebugEnabled
#ifdef DebugEnabled
#define DebugPrint(x) Serial.print(x)
#define DebugPrintln(x) Serial.println(x)
#define DebugPrintf(x, ...) Serial.printf(x, __VA_ARGS__)
#else
#define DebugPrint(x)
#define DebugPrintln(x)
#define DebugPrintf(x, ...)
#endif
typedef struct { // WiFi Access
const char *SSID;
const char *PASSWORD;
} wlanSSID;
typedef struct { // Frequency parts
int fMHz;
int fKHz;
} SFreq;
typedef struct { // Buttons
const char *name; // Buttonname
const char *caption; // Buttoncaption
char waarde[12]; // Buttontext
uint16_t pageNo;
uint16_t xPos;
uint16_t yPos;
uint16_t width;
uint16_t height;
uint16_t bottomColor;
uint16_t topColor;
} Button;
const Button buttons[] = {
{ "ToLeft", "<<", "", BTN_ARROW, 2, 208, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "ToRight", ">>", "", BTN_ARROW, 242, 208, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Vol", "Vol", "", 1, 2, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "SQL", "SQL", "", 1, 82, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Scan", "Scan", "", 1, 162, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Off", "Off", "", 1, 242, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Freq", "Freq", "", 1, 2, 172, 74, 30, TFT_BLACK, TFT_WHITE },
{ "RPT", "RPT", "", 1, 82, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "MEM", "MEM", "", 1, 162, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Shift", "Shift", "", 2, 2, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Mute", "Mute", "", 2, 82, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Tone", "Tone", "", 2, 162, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Reverse", "Reverse", "", 2, 242, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "MOX", "MOX", "", 2, 82, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "APRS", "APRS", "", 2, 162, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Beacon", "Beacon", "", 2, 82, 208, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "TXBeacon", "TX Beacon", "", 2, 162, 208, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Power", "Power", "", 4, 2, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "SetBand", "Band", "", 4, 82, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Light", "Light", "", 4, 162, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Calibrate", "Calibrate", "", 4, 242, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Save", "Save", "", 4, 82, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Print", "Print", "", 4, 162, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A001", "1", "", BTN_NUMERIC, 42, 100, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A002", "2", "", BTN_NUMERIC, 122, 100, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A003", "3", "", BTN_NUMERIC, 202, 100, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A004", "4", "", BTN_NUMERIC, 42, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A005", "5", "", BTN_NUMERIC, 122, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A006", "6", "", BTN_NUMERIC, 202, 136, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A007", "7", "", BTN_NUMERIC, 42, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A008", "8", "", BTN_NUMERIC, 124, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A009", "9", "", BTN_NUMERIC, 202, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Clear", "Clear", "", BTN_NUMERIC, 42, 208, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "A000", "0", "", BTN_NUMERIC, 122, 208, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Enter", "Enter", "", BTN_NUMERIC, 202, 208, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Prev", "Prev", "", BTN_PREV, 2, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Next", "Next", "", BTN_NEXT, 242, 172, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "ToLeft", "<<", "", BTN_NAV, 2, 208, 154, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "ToRight", ">>", "", BTN_NAV, 162, 208, 154, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Navigate", "Freq", "", BTN_NAV, 2, 208, 314, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
{ "Close", "Close", "", BTN_CLOSE, 122, 208, 74, 30, TFT_BLACK, TFT_BUTTONTOPCOLOR },
};
typedef struct {
uint16_t rxChannel; // RX Freq channel (12.5KHz steps, 144.000MHz = 0, channel 128 is 145.600MHz)
uint16_t txChannel; // TX Freq channel (12.5KHz steps, 144.000MHz = 0, channel 128 is 145.600MHz)
uint8_t repeater; // Repeater number from the list of repeaters, overwrites RX and TX channel
int8_t txShift; // Shift + or -
byte hasTone; // 0=disabled, 1=only RX, 2=only TX, 3=RX and TX
byte ctcssTone; // See list of CTCSS codes in config.h
} Memory;
typedef struct {
byte chkDigit;
bool isUHF;
uint16_t aprsChannel;
uint16_t rxChannel;
uint16_t txChannel;
uint8_t repeater;
uint8_t memoryChannel;
uint8_t freqType;
int8_t txShift;
bool autoShift;
bool draPower;
byte hasTone;
bool disableRXTone;
byte ctcssTone;
byte volume;
byte squelsh;
byte scanType;
char wifiSSID[25];
char wifiPass[25];
char aprsIP[25];
uint16_t aprsPort;
char aprsPassword[6];
char dest[8];
byte destSsid;
char call[8];
byte ssid;
byte serverSsid;
uint16_t aprsGatewayRefreshTime;
char comment[16];
char symbool; // = auto.
char path1[8];
byte path1Ssid;
char path2[8];
byte path2Ssid;
byte interval;
byte multiplier;
byte power;
byte height;
byte gain;
byte directivity;
uint16_t preAmble;
uint16_t tail;
byte doTX;
byte bcnAfterTX;
byte txTimeOut;
uint16_t maxChannel;
uint16_t minUHFChannel;
uint16_t maxUHFChannel;
uint16_t currentBrightness;
float lat;
float lon;
bool useAPRS;
byte mikeLevel;
byte repeatSetDRA;
bool preEmphase;
bool highPass;
bool lowPass;
bool isDebug;
uint16_t calData0;
uint16_t calData1;
uint16_t calData2;
uint16_t calData3;
uint16_t calData4;
bool doRotate;
} Settings;
typedef struct {
char code[5];
char tone[8];
} CTCSSCode;
typedef struct { // Repeaterlist
char *name; // Repeatername
char *city; // Repeatercity
int8_t shift; // Shift + or -
uint16_t channel; // RX Freq channel (12.5KHz steps, 144.000MHz = 0, channel 128 is 145.600MHz)
uint16_t ctcssTone; // See list of CTCSS codes in config.h
uint16_t hasTone; // 0=disabled, 1=only RX, 2=only TX, 3=RX and TX
} Repeater;
// Settings & Variables
bool isPTT = false;
bool lastPTT = false;
bool isMOX = false;
bool isReverse = false;
uint8_t activeBtn = -1;
String commandButton = "";
long activeBtnStart = millis();
long lastBeacon = millis();
long loopDelay = millis();
long lastGetDraRSSI = millis();
int actualPage = 1;
int lastPage = 8;
int beforeDebugPage = 0;
int debugPage = 16;
int debugLine = 0;
long startTime = millis();
long gpsTime = millis();
long saveTime = millis();
long scanCheck = millis();
long lastPressed = millis();
long startedDebugScreen = millis();
long aprsGatewayRefreshed = millis();
long webRefresh = millis();
long waitForResume = 0;
long LipoMeasure = millis();
bool wifiAvailable = false;
bool wifiAPMode = false;
bool isOn = true;
bool gotPacket = false;
bool squelshClosed = true;
bool isMuted = false;
bool aprsGatewayConnected = false;
int scanMode = SCAN_STOPPED;
uint16_t lastCourse = 0;
char httpBuf[120] = "\0";
char buf[300] = "\0";
char draBuffer[300] = "\0";
int draBufferLength = 0;
int oldSwr, swr = 0;
int32_t keyboardNumber = 0;
bool dirtyScreen = false;
bool doTouch = false;
AX25Msg incomingPacket;
uint8_t *packetData;
const int ledFreq = 5000;
const int ledResol = 8;
const int ledChannelforTFT = 0;
#include "config.h"; // Change to config.h
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
WiFiMulti wifiMulti;
WiFiClient httpNet;
TinyGPSPlus gps;
RDKOTA rdkOTA(OTAHOST);
AsyncWebServer server(80);
AsyncEventSource events("/events");
Memory memories[10] = {};
int memTeller = 0;
HardwareSerial GPSSerial(1);
HardwareSerial DRASerial(2);
#include "webpages.h";
/***************************************************************************************
** Setup
***************************************************************************************/
void setup() {
pinMode(PTTOUT, OUTPUT);
digitalWrite(PTTOUT, 0); // low no PTT
pinMode(HIPOWERPIN, OUTPUT);
digitalWrite(HIPOWERPIN, 0); // low power from DRA
pinMode(TRXONPIN, OUTPUT);
digitalWrite(TRXONPIN, 0); // low TRX On
pinMode(DISPLAYLEDPIN, OUTPUT);
digitalWrite(DISPLAYLEDPIN, 0);
pinMode(MUTEPIN, OUTPUT);
digitalWrite(MUTEPIN, 0);
if (BEEPPIN > -1) {
pinMode(BEEPPIN, OUTPUT);
digitalWrite(BEEPPIN, 0);
}
pinMode(PTTIN, INPUT_PULLUP);
pinMode(BTNSMIKEPIN, INPUT);
pinMode(SQUELSHPIN, INPUT);
ledcSetup(ledChannelforTFT, ledFreq, ledResol);
ledcAttachPin(DISPLAYLEDPIN, ledChannelforTFT);
Serial.begin(115200);
GPSSerial.begin(9600, SERIAL_8N1, RXD1, TXD1);
DRASerial.begin(9600, SERIAL_8N1, RXD2, TXD2);
if (!EEPROM.begin(EEPROM_SIZE)) {
DrawButton(80, 120, 160, 30, "EEPROM Failed", "", TFT_BLACK, TFT_WHITE, "");
DebugPrintln("failed to initialise EEPROM");
while (1)
;
}
if (!LoadConfig()) {
DebugPrintln(F("Writing defaults"));
SaveConfig();
Memory myMemory = { 0, 0, 0, 0, 0, 0 };
for (int x = 0; x < 10; x++) {
memories[x] = myMemory;
}
SaveMemories();
}
LoadConfig();
LoadMemories();
tft.init();
tft.setRotation(settings.doRotate?3:1);
uint16_t calData[5] = {settings.calData0, settings.calData1, settings.calData2, settings.calData3, settings.calData4};
tft.setTouch(calData);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
// add Wi-Fi networks from All_Settings.h
for (int i = 0; i < sizeof(wifiNetworks) / sizeof(wifiNetworks[0]); i++) {
wifiMulti.addAP(wifiNetworks[i].SSID, wifiNetworks[i].PASSWORD);
DebugPrintf("Wifi:%s, Pass:%s.\r\n", wifiNetworks[i].SSID, wifiNetworks[i].PASSWORD);
}
DrawButton(80, 80, 160, 30, "Connecting to WiFi", "", TFT_BLACK, TFT_WHITE, "");
// show connected SSID
wifiMulti.addAP(settings.wifiSSID, settings.wifiPass);
DebugPrintf("Wifi:%s, Pass:%s.\r\n", settings.wifiSSID, settings.wifiPass);
if (Connect2WiFi()) {
wifiAvailable = true;
DrawButton(80, 80, 160, 30, "Connected to WiFi", WiFi.SSID(), TFT_BLACK, TFT_WHITE, "");
delay(1000);
if (rdkOTA.checkForUpdate(OTAVERSION)){
if (questionBox("Install update", TFT_WHITE, TFT_NAVY, 80, 80, 160, 40)){
DrawButton(80, 80, 160, 40, "Installing update", "", TFT_BLACK, TFT_RED, "");
rdkOTA.installUpdate();
}
}
udp.begin(localPort);
syncTime();
} else {
wifiAPMode = true;
WiFi.mode(WIFI_AP);
WiFi.softAP("APRSTRX", NULL);
}
DebugPrintf("Main loop running in Core:%d.\r\n", xPortGetCoreID());
ledcWrite(ledChannelforTFT, 256 - (settings.currentBrightness * 2.56));
digitalWrite(HIPOWERPIN, !settings.draPower); // low power from DRA
delay(25);
SetFreq(0, 0, 0, false);
delay(100);
SetFreq(0, 0, 0, false);
delay(100);
SetDraVolume(settings.volume);
delay(100);
SetDraSettings();
delay(100);
APRS_init(ADC_REFERENCE, OPEN_SQUELCH);
SetAPRSParameters();
xTaskCreatePinnedToCore(
ReadTask, "ReadTask" // A name just for humans
,
10000 // This stack size can be checked & adjusted by reading the Stack Highwater
,
NULL, 0 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
,
NULL, 0); // Core 0
esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
esp_task_wdt_add(NULL); //add current thread to WDT watch
DrawScreen();
}
void ReadTask(void *pvParameters) { // This is a task.
if (wifiAvailable || wifiAPMode) {
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", index_html, Processor);
});
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/css", css_html);
});
server.on("/command", HTTP_GET, [](AsyncWebServerRequest *request) {
if (request->hasParam("button")) commandButton = request->getParam("button")->value();
if (request->hasParam("setfreq")) {
SetFreq(request->getParam("setfreq")->value().toFloat());
dirtyScreen = true;
}
if (request->hasParam("setrepeater")) {
DebugPrintln(request->getParam("setrepeater")->value());
uint8_t i = request->getParam("setrepeater")->value().toInt();
if (i > -1) {
settings.repeater = i;
SetRepeater(i);
dirtyScreen = true;
}
}
delay(100);
request->send_P(200, "text/html", index_html, Processor);
});
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", settings_html, Processor);
});
server.on("/nummers", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", nummers_html, Processor);
});
server.on("/repeaters", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", repeaters_html, Processor);
});
server.on("/reboot", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Rebooting");
ESP.restart();
});
server.on("/calibrate", HTTP_GET, [] (AsyncWebServerRequest *request) {
if (request->client()->remoteIP()[0] == 192 || request->client()->remoteIP()[0] == 10 || request->client()->remoteIP()[0] == 172){
request->send_P(200, "text/html", index_html, Processor);
doTouch = true;
}
else
request->send_P(200, "text/html", warning_html, Processor);
});
server.on("/store", HTTP_GET, [](AsyncWebServerRequest *request) {
SaveSettings(request);
SaveConfig();
SetAPRSParameters();
request->send_P(200, "text/html", settings_html, Processor);
});
events.onConnect([](AsyncEventSourceClient *client) {
DebugPrintln("Connect web");
if (client->lastId()) {
DebugPrintf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
}
client->send("hello!", NULL, millis(), 10000);
});
server.addHandler(&events);
server.begin();
DebugPrintln("HTTP server started");
DebugPrintf("HTTP server running in Core:%d.\r\n", xPortGetCoreID());
}
for (;;) // A Task shall never return or exit.
{
ReadDRAPort();
readGPSData();
vTaskDelay(50 / portTICK_PERIOD_MS); // wait for 50 miliSec
}
}
void SetAPRSParameters() {
APRS_setCallsign(settings.call, settings.ssid);
APRS_setDestination(settings.dest, settings.destSsid);
APRS_setSymbol(settings.symbool);
APRS_setPath1(settings.path1, settings.path1Ssid);
APRS_setPath2(settings.path2, settings.path2Ssid);
APRS_setPower(settings.power);
APRS_setHeight(settings.height);
APRS_setGain(settings.gain);
APRS_setDirectivity(settings.directivity);
APRS_setPreamble(settings.preAmble);
APRS_setTail(settings.tail);
APRS_setLat(Deg2Nmea(settings.lat, true));
APRS_setLon(Deg2Nmea(settings.lon, false));
APRS_printSettings();
APRSGatewayUpdate();
aprsGatewayRefreshed = millis();
}
bool Connect2WiFi() {
startTime = millis();
DebugPrint("Connect to Multi WiFi");
while (wifiMulti.run() != WL_CONNECTED && millis() - startTime < 30000) {
esp_task_wdt_reset();
delay(1000);
DebugPrint(".");
}
DebugPrintln();
return (WiFi.status() == WL_CONNECTED);
}
void doBeep(int timeLen){
if (BEEPPIN > -1) {
digitalWrite(BEEPPIN, 1);
delay(timeLen);
digitalWrite(BEEPPIN, 0);
}
}
/***************************************************************************************
** Loop
***************************************************************************************/
void loop() {
esp_task_wdt_reset();
if (doTouch){
doTouch = false;
TouchCalibrate();
actualPage = 1;
DrawScreen();
}
if (millis() - loopDelay > 50) {
loopDelay = millis();
if (commandButton > "") {
if (commandButton == "ToLeft" || commandButton == "ToRight") {
HandleFunction(commandButton, -1, 0);
} else {
HandleFunction(commandButton, false);
}
commandButton = "";
ClearButtons();
}
if (scanMode == SCAN_INPROCES && millis() - scanCheck > 100) {
if (settings.freqType == FindButtonIDByName("Freq")) {
settings.rxChannel++;
if (!settings.isUHF && settings.rxChannel == settings.maxChannel) settings.rxChannel = 0;
if (settings.isUHF && settings.rxChannel == settings.maxUHFChannel) settings.rxChannel = settings.minUHFChannel;
SetFreq(0, settings.rxChannel, 0, false);
}
if (settings.freqType == FindButtonIDByName("RPT")) {
if (settings.repeater < (sizeof(repeaters) / sizeof(repeaters[0])) - 1) settings.repeater++;
else settings.repeater = 1;
SetRepeater(settings.repeater);
DrawButton("RPT");
}
if (settings.freqType == FindButtonIDByName("MEM")) {
if (settings.memoryChannel < 9) settings.memoryChannel++;
else settings.memoryChannel = 0;
SetMemory(settings.memoryChannel);
DrawButton("MEM");
}
DrawFrequency(false, false);
DrawButton("Shift");
DrawButton("Reverse");
DrawButton("Tone");
scanCheck = millis();
}
}
if (millis() - lastPressed > 200) {
uint16_t x = 0, y = 0;
bool pressed = tft.getTouch(&x, &y);
if (pressed) {
doBeep(25);
int showVal = ShowControls();
for (int i = 0; i < sizeof(buttons) / sizeof(buttons[0]); i++) {
if ((buttons[i].pageNo & showVal) > 0) {
if (x >= buttons[i].xPos && x <= buttons[i].xPos + buttons[i].width && y >= buttons[i].yPos && y <= buttons[i].yPos + buttons[i].height) {
DebugPrint(buttons[i].name);
DebugPrint(" pressed:");
HandleFunction(buttons[i], x, y);
}
}
}
}
lastPressed = millis();
}
WaitForWakeUp();
bool sql = digitalRead(SQUELSHPIN);
if (sql != squelshClosed) {
squelshClosed = sql;
DrawFrequency(false);
if (scanMode == SCAN_INPROCES && !squelshClosed) {
digitalWrite(MUTEPIN, isMuted);
scanMode = (settings.scanType == SCAN_TYPE_STOP) ? SCAN_STOPPED : SCAN_PAUSED;
DrawButton("Scan");
}
}
if (scanMode == SCAN_PAUSED && !squelshClosed) waitForResume = 0;
if (scanMode == SCAN_PAUSED && squelshClosed) {
if (waitForResume == 0) {
waitForResume = millis();
}
if (millis() - waitForResume > 3000) {
digitalWrite(MUTEPIN, 1);
scanMode = SCAN_INPROCES;
DrawButton("Scan");
waitForResume = 0;
}
}
int btnValue = analogRead(BTNSMIKEPIN);
if (btnValue < 2048) {
doBeep(25);
int firstBtnValue = btnValue;
long startPress = millis();
while (btnValue < 2048 && millis() - startPress < 2500) {
btnValue = analogRead(BTNSMIKEPIN);
}
if (millis() - startPress > 2000) {
Button button = FindButtonByName("Scan");
HandleFunction(button, -1, 0);
delay(200);
} else {
if (firstBtnValue < 2) {
Button button = FindButtonByName("ToRight");
HandleFunction(button, -1, 0);
delay(200);
}
if (firstBtnValue > 1 && firstBtnValue < 2048) {
Button button = FindButtonByName("ToLeft");
HandleFunction(button, -1, 0);
delay(200);
}
}
}
if (minute() != lastMinute) {
syncTime();
if (actualPage < lastPage && wifiAvailable) DrawTime();
lastMinute = minute();
}
//readGPSData();
if (millis() - gpsTime > 1000) {
gpsTime = millis();
PrintGPSInfo();
}
if (!isPTT) {
if (millis() - lastGetDraRSSI > 250) {
GetDraRSSI();
lastGetDraRSSI = millis();
}
if (oldSwr != swr) {
oldSwr = swr;
DrawMeter(2, 100, 314, 30, swr, isPTT, true);
}
}
bool isFromPTT = CheckAndSetPTT(false);
if (millis() - saveTime > 10000 && scanMode == SCAN_STOPPED) {
saveTime = millis();
SaveConfig();
}
if (millis() - activeBtnStart > 10000) {
if (activeBtn != FindButtonIDByName("Freq") && activeBtn != FindButtonIDByName("RPT") && activeBtn != FindButtonIDByName("MEM") && activeBtn != FindButtonIDByName("Save")) {
activeBtn = settings.freqType;
DrawButtons();
}
}
if (settings.useAPRS && (gps.location.isValid() || settings.isDebug)) {
bool doBeacon = false;
long beaconInterval = settings.interval * settings.multiplier * 1000;
int gpsSpeed = gps.speed.isValid() ? gps.speed.kmph() : 0;
if (gpsSpeed > 5) beaconInterval = settings.interval * 4 * 1000;
if (gpsSpeed > 25) beaconInterval = settings.interval * 2 * 1000;
if (gpsSpeed > 80) beaconInterval = settings.interval * 1000;
if (millis() - lastBeacon > beaconInterval) doBeacon = true;
if (millis() - lastBeacon > 20000 && settings.isDebug) doBeacon = true;
if (gps.course.isValid()) {
uint16_t sbCourse = (abs(gps.course.deg() - lastCourse));
if (sbCourse > 180) sbCourse = 360 - sbCourse;
if (sbCourse > 27) doBeacon = true;
}
if (isFromPTT && settings.bcnAfterTX) doBeacon = true;
if (millis() - lastBeacon < 5000) doBeacon = false;
if (doBeacon) {
lastCourse = gps.course.isValid() ? gps.course.deg() : -1;
SendBeacon(false, (isFromPTT && settings.bcnAfterTX));
lastBeacon = millis();
}
}
if (millis() - aprsGatewayRefreshed > (settings.aprsGatewayRefreshTime * 1000)) {
APRSGatewayUpdate();
aprsGatewayRefreshed = millis();
}
if ((millis() - webRefresh) > 1000) {
RefreshWebPage();
webRefresh = millis();
}
if (actualPage == debugPage && millis() - startedDebugScreen > 3000) {
actualPage = beforeDebugPage;
beforeDebugPage = 0;
DrawScreen();
}
// processPacket();
if (dirtyScreen) DrawScreen(true);
if (actualPage < lastPage && (millis() - LipoMeasure) > LipoMeasureTime * 1000 ) {
Lipovolt();
LipoMeasure = millis();
}
}
void processPacket() {
if (gotPacket) {
gotPacket = false;
DebugPrint(F("Received APRS packet. SRC: "));
DebugPrint(incomingPacket.src.call);
DebugPrint(F("-"));
DebugPrint(incomingPacket.src.ssid);
DebugPrint(F(". DST: "));
DebugPrint(incomingPacket.dst.call);
DebugPrint(F("-"));
DebugPrint(incomingPacket.dst.ssid);
DebugPrint(F(". Data: "));
for (int i = 0; i < incomingPacket.len; i++) {
Serial.write(incomingPacket.info[i]);
}
DebugPrintln("");
// Remeber to free memory for our buffer!
free(packetData);
// You can print out the amount of free
// RAM to check you don't have any memory
// leaks
// DebugPrint(F("Free RAM: ")); DebugPrintln(freeMemory());
}
}
void WaitForWakeUp() {
delay(10);
while (!isOn) {
esp_task_wdt_reset();
uint16_t x = 0, y = 0;
bool pressed = tft.getTouch(&x, &y);
if (pressed){
doBeep(25);
ESP.restart();
}
}
}
bool CheckAndSetPTT(bool isAPRS) {
bool retVal = false;
bool isPTTIN = !digitalRead(PTTIN);
if (isMOX || isPTTIN) isPTT = 1;
else isPTT = 0;
if (isPTT && scanMode != SCAN_STOPPED) {
digitalWrite(MUTEPIN, isMuted);
scanMode = SCAN_STOPPED;
DrawButton("Scan");
delay(1000);
} else {
if (isPTTIN && isMOX) isMOX = false;
if (lastPTT != isPTT && (!settings.isUHF || isAPRS)) {
digitalWrite(MUTEPIN, isPTT ? true : isMuted);
DebugPrintln("PTT=" + isPTT ? "True" : "False");
lastPTT = isPTT;
if (!isPTT && !isAPRS) retVal = true;
digitalWrite(PTTOUT, isPTT);
DrawButton("MOX");
DrawFrequency(isAPRS);
delay(10);
}
}
return retVal;
}
/***************************************************************************************
** APRS Call back - received package
***************************************************************************************/
void aprs_msg_callback(struct AX25Msg *msg) {
DebugPrintln("APRS packet received");
// If we already have a packet waiting to be
// processed, we must drop the new one.
if (!gotPacket) {
// Set flag to indicate we got a packet
gotPacket = true;
// The memory referenced as *msg is volatile
// and we need to copy all the data to a
// local variable for later processing.
memcpy(&incomingPacket, msg, sizeof(AX25Msg));
// We need to allocate a new buffer for the
// data payload of the packet. First we check
// if there is enough free RAM.
packetData = (uint8_t *)malloc(msg->len);
memcpy(packetData, msg->info, msg->len);
incomingPacket.info = packetData;
}
}
/***************************************************************************************
** APRS Send beacon
***************************************************************************************/
void SendBeacon(bool manual, bool afterTX) {
if (gps.location.age() < 5000 || manual || settings.isDebug) {
if (settings.isDebug) ShowDebugScreen("Send beacon");
if ((!wifiAvailable || !APRSGatewayConnect() || settings.isDebug) && (squelshClosed || afterTX)) SendBeaconViaRadio(); // || settings.isDebug
if (wifiAvailable && APRSGatewayConnect()) SendBeaconViaWiFi();
lastBeacon = millis();
}
}
void SendBeaconViaRadio() {
if (!isPTT) {
DebugPrintf("Mutestate is %s", digitalRead(MUTEPIN) ? "True" : "False");
buf[0] = '\0';
sprintf(buf, "Send APRS beacon via radio");
events.send(buf, "BEACONINFO", millis());
DrawDebugInfo("Send beacon via radio..");
int lastScanMode = scanMode;
scanMode = SCAN_STOPPED;
if (lastScanMode != SCAN_STOPPED) delay(500);
SetFreq(0, 0, 0, true);
isMOX = 1;
CheckAndSetPTT(true);
delay(500);
if (gps.location.age() > 5000) {
APRS_setLat(Deg2Nmea(settings.lat, true));
APRS_setLon(Deg2Nmea(settings.lon, false));
} else {
APRS_setLat(Deg2Nmea(gps.location.lat(), true));
APRS_setLon(Deg2Nmea(gps.location.lng(), false));
}
APRS_sendLoc(settings.comment, strlen(settings.comment));
isMOX = 0;
CheckAndSetPTT(false);
delay(500);
SetFreq(0, settings.rxChannel, settings.txShift + 10, false);
DrawFrequency(false);
delay(200);
scanMode = lastScanMode;
if (scanMode == SCAN_INPROCES) digitalWrite(MUTEPIN, true);
}
}
void SendBeaconViaWiFi() {
if (APRSGatewayConnect()) {
buf[0] = '\0';
sprintf(buf, "Send APRS beacon via WiFi");
events.send(buf, "BEACONINFO", millis());
if (!settings.isDebug) {
PrintTXTLine();
tft.fillCircle(50, 64, 24, TFT_RED);
tft.setTextDatum(MC_DATUM);
tft.setTextPadding(tft.textWidth("AP"));
tft.setTextColor(TFT_BLACK, TFT_RED);
tft.drawString("AP", 50, 66, 4);
}
String sLat;
String sLon;
if (gps.location.age() > 5000) {
sLat = Deg2Nmea(settings.lat, true);
sLon = Deg2Nmea(settings.lon, false);
} else {
sLat = Deg2Nmea(gps.location.lat(), true);
sLon = Deg2Nmea(gps.location.lng(), false);
}
sprintf(buf, "%s-%d>%s:=%s/%s%sPHG5000%s", settings.call, settings.ssid, settings.dest, sLat, sLon, String(settings.symbool), settings.comment);
DrawDebugInfo(buf);
httpNet.println(buf);
if (ReadHTTPNet()) DrawDebugInfo(buf);
if (!settings.isDebug) {
sprintf(buf, " ");
PrintTXTLine();
tft.fillCircle(50, 64, 24, TFT_BLACK);
}
}
}
char *Deg2Nmea(float fdeg, boolean is_lat) {
long deg = fdeg * 1000000;
bool is_negative = 0;
if (deg < 0) is_negative = 1;
// Use the absolute number for calculation and update the buffer at the end