forked from PE5PVB/TEF6686_ESP32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTEF6686_ESP32.ino
5496 lines (5106 loc) · 184 KB
/
TEF6686_ESP32.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
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include <WiFiClient.h>
#include <HTTPClient.h>
#include <EEPROM.h>
#include <Wire.h>
#include <math.h>
#include <TimeLib.h>
#include <TFT_eSPI.h> // https://github.com/ohmytime/TFT_eSPI_DynamicSpeed/archive/refs/heads/master.zip (please then edit the User_Setup.h as described in the Wiki)
#include <Hash.h> // https://github.com/bbx10/Hash_tng/archive/refs/heads/master.zip
#include <WebServer.h>
#include <SPIFFS.h>
#include "src/NTPupdate.h"
#include "src/WiFiConnect.h"
#include "src/WiFiConnectParam.h"
#include "src/FONT16.h"
#include "src/FONT16_CHS.h"
#include "src/FONT28.h"
#include "src/FONT28_CHS.h"
#include "src/FONT48DEC.h"
#include "src/FREQFONT.h"
#include "src/TEF6686.h"
#include "src/constants.h"
#include "src/config.h"
#include "src/language.h"
#include "src/gui.h"
#include "src/comms.h"
#include "src/rds.h"
#include "src/touch.h"
#include "src/logbook.h"
#define ROTARY_PIN_A 34
#define ROTARY_PIN_B 36
#define ROTARY_BUTTON 39
#define PIN_POT 35
#define BATTERY_PIN 13
#define BANDBUTTON 4
#define BWBUTTON 25
#define MODEBUTTON 26
#define CONTRASTPIN 2
#define STANDBYLED 19
#define SMETERPIN 27
#define TOUCHIRQ 33
#define EXT_IRQ 14
#define DYNAMIC_SPI_SPEED // uncomment to enable dynamic SPI Speed https://github.com/ohmytime/TFT_eSPI_DynamicSpeed
#ifdef ARS
TFT_eSPI tft = TFT_eSPI(320, 240);
#else
TFT_eSPI tft = TFT_eSPI(240, 320);
#endif
#ifdef DYNAMIC_SPI_SPEED
bool dynamicspi = true;
#else
bool dynamicspi = false;
#endif
bool advancedRDS;
bool afmethodBold;
bool afpage;
bool afscreen;
bool aftest;
bool artheadold;
bool autoDST;
bool autolog;
bool autologged;
bool autosquelch = true;
bool batterydetect = true;
bool beepresetstart;
bool beepresetstop;
bool BWreset;
bool bwtouchtune;
bool BWtune;
bool change;
bool clockampm;
bool compressedold;
bool direction;
bool dropout;
bool dynamicPTYold;
bool edgebeep;
bool externaltune;
bool findMemoryAF;
bool firstTouchHandled = false;
bool flashing;
bool fmsi;
bool fullsearchrds;
bool hasafold;
bool hasCTold;
bool haseonold;
bool hasrtplusold;
bool hastmcold;
bool initdxscan;
bool invertdisplay;
bool leave;
bool LowLevelInit;
bool memorystore;
bool memreset;
bool memtune;
bool menu;
bool menuopen;
bool mwstepsize;
#ifdef HAS_AIR_BAND
bool airstepsize;
#endif
bool nobattery;
bool NTPupdated;
bool optenc;
bool rdsflagreset;
bool rdsreset;
bool RDSSPYTCP;
bool RDSSPYUSB;
bool RDSstatus;
bool RDSstatusold;
bool rdsstereoold;
bool rotaryaccelerate = true;
bool rtcset;
bool scandxmode;
bool scanholdflag;
bool scanholdonsignal;
bool scanmem;
bool scanmute;
bool screenmute;
bool screensavertriggered = false;
bool seek;
bool seekinit;
bool setupmode;
bool showclock;
bool showlongps;
bool usesquelch;
bool softmuteam;
bool softmutefm;
bool SQ;
bool Stereostatusold;
bool StereoToggle;
bool store;
bool TAold;
bool TPold;
bool touchrepeat = false;
bool touch_detect;
bool tuned;
bool USBmode;
bool XDRGTKdata;
bool XDRGTKMuteScreen;
bool XDRGTKTCP;
bool XDRGTKUSB;
bool XDRMute;
bool XDRScan;
bool wifi;
bool wificonnected;
byte af_counterold;
byte aid_counterold;
byte af;
byte afpagenr;
byte amagc;
byte amnb;
byte amscansens;
byte audiomode;
byte band;
byte bandAM;
byte bandFM;
byte bandforbidden;
byte battery;
byte batteryold;
byte batteryoptions;
byte BWset;
byte BWsettemp;
byte BWsetAM;
byte BWsetFM;
byte BWsetRecall;
byte BWtemp;
byte charwidth = 8;
byte hardwaremodel;
byte ContrastSet;
byte CurrentSkin;
byte CurrentTheme;
byte displayflip;
byte ECCold;
byte eonptyold[20];
byte EQset;
byte fmagc;
byte fmscansens;
byte fmdefaultstepsize;
byte fmnb;
byte fmdeemphasis;
byte freqfont;
byte amcodect;
byte amcodectcount;
byte amgain;
byte freqoldcount;
byte HighCutLevel;
byte HighCutOffset;
byte items[10] = {10, static_cast<byte>(dynamicspi ? 10 : 9), 7, 10, 10, 10, 9, 10, 10, 9};
byte iMSEQ;
byte iMSset;
byte language;
byte licold;
byte longbandpress;
byte memdoublepi;
byte memorypos;
byte memoryposold;
byte memoryposstatus;
byte mempionly;
byte memstartpos;
byte memstoppos;
byte menuitem;
byte menupage;
byte MSold;
byte poweroptions;
byte rdsblockold;
byte rdsqualityold;
byte region;
byte rotarymode;
byte touchrotating;
byte scancancel;
byte scanstart;
byte scanstop;
byte scanhold;
byte scanmodeold;
byte screensaverOptions[5] = {0, 3, 10, 30, 60};
byte screensaverset;
byte showmodulation;
byte showrdserrors;
byte showSWMIBand;
byte submenu;
byte stationlistid;
byte nowToggleSWMIBand = 1;
byte stepsize;
byte StereoLevel;
byte subnetclient;
byte TEF;
byte tot;
byte tunemode;
byte unit;
byte spispeed;
char buff[16];
char eonpicodeold[20][6];
char programTypePrevious[18];
char rabbitearstime[100][21];
const uint8_t* currentFont = nullptr;
float vPerold;
int ActiveColor;
int ActiveColorSmooth;
int AGC;
int AMLevelOffset;
int BackgroundColor;
int BackgroundColor1;
int BackgroundColor2;
int BackgroundColor3;
int BackgroundColor4;
int BackgroundColor5;
int BarSignificantColor;
int BarInsignificantColor;
int BatteryValueColor;
int BatteryValueColorSmooth;
int batupdatetimer;
int BWAutoColor;
int BWAutoColorSmooth;
int BWOld;
int bwupdatetimer;
int DeEmphasis;
int DisplayedSegments;
int ForceMono;
int FrameColor;
int FreqColor;
int FreqColorSmooth;
int freq_in = 0;
int freqold;
int GreyoutColor;
int InsignificantColor;
int InsignificantColorSmooth;
int menuoption = ITEM1;
int ModBarInsignificantColor;
int ModBarSignificantColor;
int MStatusold;
int offsetupdatetimer;
int OStatusold;
int peakholdold;
int peakholdtimer;
int PrimaryColor;
int PrimaryColorSmooth;
int RDSColor;
int RDSColorSmooth;
int RDSDropoutColor;
int RDSDropoutColorSmooth;
int SignificantColor;
int SignificantColorSmooth;
int StereoColor;
int StereoColorSmooth;
int SquelchShow;
int rotary;
int rotarycounter;
int rotarycounteraccelerator;
int rssi;
int rssiold = 200;
int scanner_filter;
int SecondaryColor;
int SecondaryColorSmooth;
int SNRupdatetimer;
int Sqstatusold;
int Squelch;
int Squelchold;
int SStatusold;
int Stereostatus;
int volume;
int XDRBWset;
int XDRBWsetold;
int xPos;
int xPos2;
int xPos3;
int xPos4;
int xPos5;
int16_t OStatus;
int16_t SAvg;
int16_t SAvg2;
int16_t SAvg3;
int16_t SStatus;
int8_t LevelOffset;
int8_t LowLevelSet;
int8_t NTPoffset;
int8_t CN;
int8_t CNold;
int8_t VolSet;
float batteryVold;
IPAddress remoteip;
String AIDString;
String cryptedpassword;
String ECColdString;
String ECCString;
String eonpsold[20];
String LIColdString;
String LICString;
String pinstringold;
String PIold;
String PSold;
String ptynold = " ";
String PTYold;
String RabbitearsPassword;
String RabbitearsUser;
String rds_clock;
String rds_clockold;
String RDSSPYRDS;
String RDSSPYRDSold;
String RTold;
String salt;
String saltkey = " ";
String stationIDold;
String stationStateold;
String StereoStatusCommand;
String StereoStatusCommandold;
String SWMIBandstring = String();
String SWMIBandstringold = String();
String XDRGTK_key;
String XDRGTKRDS;
String XDRGTKRDSold;
uint16_t BW;
uint16_t MStatus;
uint16_t rabbitearspi[100]; // first is for 88.1, 2nd 88.3, etc. to 107.9 MHz
uint16_t SWMIBandPos;
uint16_t SWMIBandPosold;
uint16_t TouchCalData[5];
uint16_t USN;
uint16_t WAM;
uint8_t buff_pos;
unsigned int ConverterSet;
unsigned int freq_scan;
unsigned int frequency;
unsigned int frequency_OIRT;
unsigned int frequency_AM;
unsigned int frequency_LW;
unsigned int frequency_MIBand_11M;
unsigned int frequency_MIBand_120M;
unsigned int frequency_MIBand_13M;
unsigned int frequency_MIBand_15M;
unsigned int frequency_MIBand_160M;
unsigned int frequency_MIBand_16M;
unsigned int frequency_MIBand_19M;
unsigned int frequency_MIBand_22M;
unsigned int frequency_MIBand_25M;
unsigned int frequency_MIBand_31M;
unsigned int frequency_MIBand_41M;
unsigned int frequency_MIBand_49M;
unsigned int frequency_MIBand_60M;
unsigned int frequency_MIBand_75M;
unsigned int frequency_MIBand_90M;
unsigned int frequency_MW;
unsigned int frequency_SW;
#ifdef HAS_AIR_BAND
unsigned int frequency_AIR;
unsigned int AIRHighEdgeSet;
unsigned int AIRLowEdgeSet;
#endif
unsigned int frequencyold;
unsigned int HighEdgeOIRTSet;
unsigned int HighEdgeSet;
unsigned int LowEdgeOIRTSet;
unsigned int logcounter;
unsigned int LowEdgeSet;
unsigned int LWHighEdgeSet;
unsigned int LWLowEdgeSet;
unsigned int mappedfreqold[20];
unsigned int mappedfreqold2[20];
unsigned int mappedfreqold3[20];
unsigned int memstartfreq;
unsigned int memstopfreq;
unsigned int MWHighEdgeSet;
unsigned int MWLowEdgeSet;
unsigned int scanner_end;
unsigned int scanner_start;
unsigned int scanner_step;
unsigned int SWHighEdgeSet;
unsigned int SWLowEdgeSet;
unsigned long afticker;
unsigned long aftickerhold;
unsigned long aftimer;
unsigned long autosquelchtimer;
unsigned long eonticker;
unsigned long eontickerhold;
unsigned long flashingtimer;
unsigned long keypadtimer;
unsigned long lastTouchTime = 0;
unsigned long lowsignaltimer;
unsigned long ModulationpreviousMillis;
unsigned long ModulationpeakPreviousMillis;
unsigned long NTPtimer;
unsigned long peakholdmillis;
unsigned long pslongticker;
unsigned long pslongtickerhold;
unsigned long rtplusticker;
unsigned long rtplustickerhold;
unsigned long rtticker;
unsigned long rttickerhold;
unsigned long rotarytimer;
unsigned long scantimer;
unsigned long signalstatustimer;
unsigned long tottimer;
unsigned long tuningtimer;
unsigned long udptimer;
mem presets[EE_PRESETS_CNT];
TEF6686 radio;
ESP32Time rtc(0);
TFT_eSprite FrequencySprite = TFT_eSprite(&tft);
TFT_eSprite RDSSprite = TFT_eSprite(&tft);
TFT_eSprite SquelchSprite = TFT_eSprite(&tft);
TFT_eSprite FullLineSprite = TFT_eSprite(&tft);
TFT_eSprite OneBigLineSprite = TFT_eSprite(&tft);
TFT_eSprite SignalSprite = TFT_eSprite(&tft);
TFT_eSprite PSSprite = TFT_eSprite(&tft);
WiFiConnect wc;
WiFiServer Server(7373);
WiFiClient RemoteClient;
WiFiUDP Udp;
WebServer webserver(80);
hw_timer_t *timScreensaver = NULL;
byte screensaver_IRQ = OFF;
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
gpio_set_drive_capability((gpio_num_t) 5, GPIO_DRIVE_CAP_0);
gpio_set_drive_capability((gpio_num_t) 16, GPIO_DRIVE_CAP_0);
gpio_set_drive_capability((gpio_num_t) 17, GPIO_DRIVE_CAP_0);
gpio_set_drive_capability((gpio_num_t) 18, GPIO_DRIVE_CAP_0);
gpio_set_drive_capability((gpio_num_t) 19, GPIO_DRIVE_CAP_0);
gpio_set_drive_capability((gpio_num_t) 21, GPIO_DRIVE_CAP_0);
gpio_set_drive_capability((gpio_num_t) 22, GPIO_DRIVE_CAP_0);
gpio_set_drive_capability((gpio_num_t) 23, GPIO_DRIVE_CAP_0);
setupmode = true;
EEPROM.begin(EE_TOTAL_CNT);
if (EEPROM.readByte(EE_BYTE_CHECKBYTE) != EE_CHECKBYTE_VALUE) DefaultSettings();
frequency = EEPROM.readUInt(EE_UINT16_FREQUENCY_FM);
frequency_OIRT = EEPROM.readUInt(EE_UINT16_FREQUENCY_OIRT);
VolSet = EEPROM.readByte(EE_BYTE_VOLSET);
ConverterSet = EEPROM.readUInt(EE_UINT16_CONVERTERSET);
LowEdgeSet = EEPROM.readUInt(EE_UINT16_FMLOWEDGESET);
HighEdgeSet = EEPROM.readUInt(EE_UINT16_FMHIGHEDGESET);
ContrastSet = EEPROM.readByte(EE_BYTE_CONTRASTSET);
StereoLevel = EEPROM.readByte(EE_BYTE_STEREOLEVEL);
bandFM = EEPROM.readByte(EE_BYTE_BANDFM);
bandAM = EEPROM.readByte(EE_BYTE_BANDAM);
HighCutLevel = EEPROM.readByte(EE_BYTE_HIGHCUTLEVEL);
HighCutOffset = EEPROM.readByte(EE_BYTE_HIGHCUTOFFSET);
LevelOffset = EEPROM.readByte(EE_BYTE_LEVELOFFSET);
radio.rds.rtbuffer = EEPROM.readByte(EE_BYTE_RTBUFFER);
edgebeep = EEPROM.readByte(EE_BYTE_EDGEBEEP);
softmuteam = EEPROM.readByte(EE_BYTE_SOFTMUTEAM);
softmutefm = EEPROM.readByte(EE_BYTE_SOFTMUTEFM);
frequency_AM = EEPROM.readUInt(EE_UINT16_FREQUENCY_AM);
language = EEPROM.readByte(EE_BYTE_LANGUAGE);
showrdserrors = EEPROM.readByte(EE_BYTE_SHOWRDSERRORS);
TEF = EEPROM.readByte(EE_BYTE_TEF);
displayflip = EEPROM.readByte(EE_BYTE_DISPLAYFLIP);
rotarymode = EEPROM.readByte(EE_BYTE_ROTARYMODE);
tunemode = EEPROM.readByte(EE_BYTE_TUNEMODE);
if (tunemode == TUNE_MAN) stepsize = EEPROM.readByte(EE_BYTE_STEPSIZE); else stepsize = 0;
optenc = EEPROM.readByte(EE_BYTE_OPTENC);
iMSset = EEPROM.readByte(EE_BYTE_IMSSET);
EQset = EEPROM.readByte(EE_BYTE_EQSET);
band = EEPROM.readByte(EE_BYTE_BAND);
LowLevelSet = EEPROM.readByte(EE_BYTE_LOWLEVELSET);
memorypos = EEPROM.readByte(EE_BYTE_MEMORYPOS);
region = EEPROM.readByte(EE_BYTE_REGION);
radio.underscore = EEPROM.readByte(EE_BYTE_RDS_UNDERSCORE);
USBmode = EEPROM.readByte(EE_BYTE_USBMODE);
wifi = EEPROM.readByte(EE_BYTE_WIFI);
subnetclient = EEPROM.readByte(EE_BYTE_SUBNETCLIENT);
showSWMIBand = EEPROM.readByte(EE_BYTE_SHOWSWMIBAND);
radio.rds.filter = EEPROM.readByte(EE_BYTE_RDS_FILTER);
radio.rds.pierrors = EEPROM.readByte(EE_BYTE_RDS_PIERRORS);
frequency_LW = EEPROM.readUInt(EE_UINT16_FREQUENCY_LW);
frequency_MW = EEPROM.readUInt(EE_UINT16_FREQUENCY_MW);
frequency_SW = EEPROM.readUInt(EE_UINT16_FREQUENCY_SW);
#ifdef HAS_AIR_BAND
frequency_AIR = EEPROM.readUInt(EE_UINT16_FREQUENCY_AIR);
#endif
XDRGTK_key = EEPROM.readString(EE_STRING_XDRGTK_KEY);
RabbitearsUser = EEPROM.readString(EE_STRING_RABBITEARSUSER);
RabbitearsPassword = EEPROM.readString(EE_STRING_RABBITEARSPASSWORD);
usesquelch = EEPROM.readByte(EE_BYTE_USESQUELCH);
showmodulation = EEPROM.readByte(EE_BYTE_SHOWMODULATION);
amnb = EEPROM.readByte(EE_BYTE_AM_NB);
fmnb = EEPROM.readByte(EE_BYTE_FM_NB);
audiomode = EEPROM.readByte(EE_BYTE_AUDIOMODE);
touchrotating = EEPROM.readByte(EE_BYTE_TOUCH_ROTATING);
hardwaremodel = EEPROM.readByte(EE_BYTE_HARDWARE_MODEL);
poweroptions = EEPROM.readByte(EE_BYTE_POWEROPTIONS);
CurrentTheme = EEPROM.readByte(EE_BYTE_CURRENTTHEME);
fmdefaultstepsize = EEPROM.readByte(EE_BYTE_FMDEFAULTSTEPSIZE);
screensaverset = EEPROM.readByte(EE_BYTE_SCREENSAVERSET);
AMLevelOffset = EEPROM.readInt(EE_INT16_AMLEVELOFFSET);
unit = EEPROM.readByte(EE_BYTE_UNIT);
af = EEPROM.readByte(EE_BYTE_AF);
if (af == 2) radio.rds.afreg = true; else radio.rds.afreg = false;
StereoToggle = EEPROM.readByte(EE_BYTE_STEREO);
batteryoptions = EEPROM.readByte(EE_BYTE_BATTERY_OPTIONS);
amcodect = EEPROM.readByte(EE_BYTE_AM_CO_DECT);
amcodectcount = EEPROM.readByte(EE_BYTE_AM_CO_DECT_COUNT);
amgain = EEPROM.readByte(EE_BYTE_AM_RF_GAIN);
radio.rds.sortaf = EEPROM.readByte(EE_BYTE_SORTAF);
stationlistid = EEPROM.readByte(EE_BYTE_STATIONLISTID);
fmdeemphasis = EEPROM.readByte(EE_BYTE_FM_DEEMPHASIS);
BWsetFM = EEPROM.readByte(EE_BYTE_BWSET_FM);
BWsetAM = EEPROM.readByte(EE_BYTE_BWSET_AM);
nowToggleSWMIBand = EEPROM.readByte(EE_BYTE_BANDAUTOSW);
radio.rds.fastps = EEPROM.readByte(EE_BYTE_FASTPS);
tot = EEPROM.readByte(EE_BYTE_TOT);
mwstepsize = EEPROM.readByte(EE_BYTE_MWREGION);
#ifdef HAS_AIR_BAND
airstepsize = EEPROM.readByte(EE_BYTE_AIRSTEPSIZE);
#endif
spispeed = EEPROM.readByte(EE_BYTE_SPISPEED);
amscansens = EEPROM.readByte(EE_BYTE_AMSCANSENS);
fmscansens = EEPROM.readByte(EE_BYTE_FMSCANSENS);
freqfont = EEPROM.readByte(EE_BYTE_FREQFONT);
CurrentSkin = EEPROM.readByte(EE_BYTE_SKIN);
XDRGTKMuteScreen = EEPROM.readByte(EE_BYTE_XDRGTKMUTE);
fmagc = EEPROM.readByte(EE_BYTE_FMAGC);
amagc = EEPROM.readByte(EE_BYTE_AMAGC);
fmsi = EEPROM.readByte(EE_BYTE_FMSI);
scanstart = EEPROM.readByte(EE_BYTE_SCANSTART);
scanstop = EEPROM.readByte(EE_BYTE_SCANSTOP);
scanhold = EEPROM.readByte(EE_BYTE_SCANHOLD);
scanmem = EEPROM.readByte(EE_BYTE_SCANMEM);
scancancel = EEPROM.readByte(EE_BYTE_SCANCANCEL);
scanmute = EEPROM.readByte(EE_BYTE_SCANMUTE);
autosquelch = EEPROM.readByte(EE_BYTE_AUTOSQUELCH);
longbandpress = EEPROM.readByte(EE_BYTE_LONGBANDPRESS);
showclock = EEPROM.readByte(EE_BYTE_SHOWCLOCK);
showlongps = EEPROM.readByte(EE_BYTE_SHOWLONGPS);
memstartfreq = EEPROM.readUInt(EE_UINT16_MEMSTARTFREQ);
memstopfreq = EEPROM.readUInt(EE_UINT16_MEMSTOPFREQ);
memstartpos = EEPROM.readByte(EE_BYTE_MEMSTARTPOS);
memstoppos = EEPROM.readByte(EE_BYTE_MEMSTOPPOS);
mempionly = EEPROM.readByte(EE_BYTE_MEMPIONLY);
memdoublepi = EEPROM.readByte(EE_BYTE_MEMDOUBLEPI);
scanholdonsignal = EEPROM.readByte(EE_BYTE_WAITONLYONSIGNAL);
TouchCalData[0] = EEPROM.readUInt(EE_UINT16_CALTOUCH1);
TouchCalData[1] = EEPROM.readUInt(EE_UINT16_CALTOUCH2);
TouchCalData[2] = EEPROM.readUInt(EE_UINT16_CALTOUCH3);
TouchCalData[3] = EEPROM.readUInt(EE_UINT16_CALTOUCH4);
TouchCalData[4] = EEPROM.readUInt(EE_UINT16_CALTOUCH5);
invertdisplay = EEPROM.readByte(EE_BYTE_INVERTDISPLAY);
NTPoffset = EEPROM.readByte(EE_BYTE_NTPOFFSET);
autolog = EEPROM.readByte(EE_BYTE_AUTOLOG);
autoDST = EEPROM.readByte(EE_BYTE_AUTODST);
clockampm = EEPROM.readByte(EE_BYTE_CLOCKAMPM);
logcounter = EEPROM.readUInt(EE_UINT16_LOGCOUNTER);
radio.rds.PICTlock = EEPROM.readUInt(EE_UINT16_PICTLOCK);
#ifdef DYNAMIC_SPI_SPEED
if (spispeed == SPI_SPEED_DEFAULT) {
tft.setSPISpeed(SPI_FREQUENCY / 1000000);
} else if (spispeed == 7) {
setAutoSpeedSPI();
} else {
tft.setSPISpeed(spispeed * 10);
}
#endif
LWLowEdgeSet = FREQ_LW_LOW_EDGE_MIN;
LWHighEdgeSet = FREQ_LW_HIGH_EDGE_MAX;
MWLowEdgeSet = mwstepsize == false ? FREQ_MW_LOW_EDGE_MIN_9K : FREQ_MW_LOW_EDGE_MIN_10K;
MWHighEdgeSet = mwstepsize == false ? FREQ_MW_HIGH_EDGE_MAX_9K : FREQ_MW_HIGH_EDGE_MAX_10K;
SWLowEdgeSet = FREQ_SW_LOW_EDGE_MIN;
SWHighEdgeSet = FREQ_SW_HIGH_EDGE_MAX;
LowEdgeOIRTSet = FREQ_FM_OIRT_START;
HighEdgeOIRTSet = FREQ_FM_OIRT_END;
for (int i = 0; i < EE_PRESETS_CNT; i++) presets[i].band = EEPROM.readByte(i + EE_PRESETS_BAND_START);
for (int i = 0; i < EE_PRESETS_CNT; i++) presets[i].frequency = EEPROM.readUInt((i * 4) + EE_PRESETS_FREQUENCY_START);
for (int i = 0; i < EE_PRESETS_CNT; i++) presets[i].bw = EEPROM.readByte(i + EE_PRESET_BW_START);
for (int i = 0; i < EE_PRESETS_CNT; i++) presets[i].ms = EEPROM.readByte(i + EE_PRESET_MS_START);
for (int i = 0; i < EE_PRESETS_CNT; i++) {
for (int y = 0; y < 9; y++) {
presets[i].RDSPS[y] = EEPROM.readByte((i * 9) + y + EE_PRESETS_RDSPS_START);
}
for (int y = 0; y < 5; y++) {
presets[i].RDSPI[y] = EEPROM.readByte((i * 5) + y + EE_PRESETS_RDSPI_START);
}
}
if (USBmode) Serial.begin(19200); else Serial.begin(115200);
if (iMSset && EQset) iMSEQ = 2;
if (!iMSset && EQset) iMSEQ = 3;
if (iMSset && !EQset) iMSEQ = 4;
if (!iMSset && !EQset) iMSEQ = 1;
switch (band) {
case BAND_LW:
frequency_LW = frequency_AM;
if (stepsize > 3) stepsize = 3;
break;
case BAND_MW:
frequency_MW = frequency_AM;
if (stepsize > 3) stepsize = 3;
break;
case BAND_SW: frequency_SW = frequency_AM; break;
case BAND_FM:
if (frequency % 10 != 0) {
if (fmdefaultstepsize == 1) Round100K(frequency); else Round50K(frequency);
}
break;
case BAND_OIRT:
if (frequency % FREQ_OIRT_STEP_30K != 2) {
Round30K(frequency_OIRT);
}
break;
}
tft.init();
tft.initDMA();
doTheme();
if (displayflip == 0) {
#ifdef ARS
tft.setRotation(0);
#else
tft.setRotation(3);
#endif
} else {
#ifdef ARS
tft.setRotation(2);
#else
tft.setRotation(1);
#endif
}
tft.invertDisplay(!invertdisplay);
pinMode(BANDBUTTON, INPUT);
pinMode(MODEBUTTON, INPUT);
pinMode(BWBUTTON, INPUT);
pinMode(ROTARY_BUTTON, INPUT);
pinMode(ROTARY_PIN_A, INPUT);
pinMode(ROTARY_PIN_B, INPUT);
pinMode (STANDBYLED, OUTPUT);
pinMode(TOUCHIRQ, INPUT);
pinMode(EXT_IRQ, INPUT_PULLUP);
digitalWrite(STANDBYLED, HIGH);
attachInterrupt(digitalPinToInterrupt(TOUCHIRQ), Touch_IRQ_Handler, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_PIN_A), read_encoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_PIN_B), read_encoder, CHANGE);
tft.setSwapBytes(true);
tft.fillScreen(BackgroundColor);
SPIFFS.begin();
if (!SPIFFS.exists("/logbook.csv")) handleCreateNewLogbook();
FrequencySprite.createSprite(200, 50);
FrequencySprite.setTextDatum(TR_DATUM);
FrequencySprite.setSwapBytes(true);
RDSSprite.createSprite(165, 19);
RDSSprite.setTextDatum(TL_DATUM);
PSSprite.createSprite(150, 32);
PSSprite.setTextDatum(TL_DATUM);
PSSprite.setSwapBytes(true);
SquelchSprite.createSprite(47, 19);
SquelchSprite.setTextDatum(TL_DATUM);
SquelchSprite.setSwapBytes(true);
FullLineSprite.createSprite(308, 19);
FullLineSprite.setSwapBytes(true);
OneBigLineSprite.createSprite(270, 30);
OneBigLineSprite.setSwapBytes(true);
SignalSprite.createSprite(80, 48);
SignalSprite.setTextColor(PrimaryColor, PrimaryColorSmooth, false);
SignalSprite.setTextDatum(TR_DATUM);
SignalSprite.setSwapBytes(true);
UpdateFonts(0);
if (digitalRead(BWBUTTON) == LOW && digitalRead(ROTARY_BUTTON) == HIGH && digitalRead(MODEBUTTON) == HIGH && digitalRead(BANDBUTTON) == HIGH) {
if (rotarymode == 0) rotarymode = 1; else rotarymode = 0;
EEPROM.writeByte(EE_BYTE_ROTARYMODE, rotarymode);
EEPROM.commit();
analogWrite(CONTRASTPIN, map(ContrastSet, 0, 100, 15, 255));
Infoboxprint(myLanguage[language][1]);
tftPrint(0, myLanguage[language][2], 155, 130, ActiveColor, ActiveColorSmooth, 28);
while (digitalRead(BWBUTTON) == LOW) delay(50);
}
if (digitalRead(BWBUTTON) == HIGH && digitalRead(ROTARY_BUTTON) == HIGH && digitalRead(MODEBUTTON) == LOW && digitalRead(BANDBUTTON) == HIGH) {
if (displayflip == 0) {
displayflip = 1;
#ifdef ARS
tft.setRotation(2);
#else
tft.setRotation(1);
#endif
} else {
displayflip = 0;
#ifdef ARS
tft.setRotation(0);
#else
tft.setRotation(3);
#endif
}
EEPROM.writeByte(EE_BYTE_DISPLAYFLIP, displayflip);
EEPROM.commit();
analogWrite(CONTRASTPIN, map(ContrastSet, 0, 100, 15, 255));
Infoboxprint(myLanguage[language][3]);
tftPrint(0, myLanguage[language][2], 155, 130, ActiveColor, ActiveColorSmooth, 28);
while (digitalRead(MODEBUTTON) == LOW) delay(50);
}
if (digitalRead(BWBUTTON) == HIGH && digitalRead(ROTARY_BUTTON) == HIGH && digitalRead(MODEBUTTON) == HIGH && digitalRead(BANDBUTTON) == LOW) {
analogWrite(SMETERPIN, 511);
analogWrite(CONTRASTPIN, map(ContrastSet, 0, 100, 15, 255));
Infoboxprint(myLanguage[language][4]);
tftPrint(0, myLanguage[language][5], 155, 130, ActiveColor, ActiveColorSmooth, 28);
while (digitalRead(BANDBUTTON) == LOW) delay(50);
analogWrite(SMETERPIN, 0);
}
if (digitalRead(BWBUTTON) == HIGH && digitalRead(ROTARY_BUTTON) == LOW && digitalRead(MODEBUTTON) == HIGH && digitalRead(BANDBUTTON) == HIGH) {
analogWrite(CONTRASTPIN, map(ContrastSet, 0, 100, 15, 255));
if (optenc == 0) {
optenc = 1;
Infoboxprint(myLanguage[language][6]);
} else {
optenc = 0;
Infoboxprint(myLanguage[language][7]);
}
EEPROM.writeByte(EE_BYTE_OPTENC, optenc);
EEPROM.commit();
tftPrint(0, myLanguage[language][2], 155, 130, ActiveColor, ActiveColorSmooth, 28);
while (digitalRead(ROTARY_BUTTON) == LOW) delay(50);
}
if (digitalRead(BWBUTTON) == LOW && digitalRead(ROTARY_BUTTON) == LOW && digitalRead(MODEBUTTON) == HIGH && digitalRead(BANDBUTTON) == HIGH) {
analogWrite(CONTRASTPIN, map(ContrastSet, 0, 100, 15, 255));
DefaultSettings();
Infoboxprint(myLanguage[language][66]);
tftPrint(0, myLanguage[language][2], 155, 130, ActiveColor, ActiveColorSmooth, 28);
while (digitalRead(ROTARY_BUTTON) == LOW && digitalRead(BWBUTTON) == LOW) delay(50);
ESP.restart();
}
if (digitalRead(BWBUTTON) == LOW && digitalRead(ROTARY_BUTTON) == HIGH && digitalRead(MODEBUTTON) == LOW && digitalRead(BANDBUTTON) == HIGH) {
analogWrite(CONTRASTPIN, map(ContrastSet, 0, 100, 15, 255));
Infoboxprint(myLanguage[language][282]);
tftPrint(0, myLanguage[language][283], 155, 100, ActiveColor, ActiveColorSmooth, 28);
tft.calibrateTouch(TouchCalData, PrimaryColor, BackgroundColor, 30);
EEPROM.writeUInt(EE_UINT16_CALTOUCH1, TouchCalData[0]);
EEPROM.writeUInt(EE_UINT16_CALTOUCH2, TouchCalData[1]);
EEPROM.writeUInt(EE_UINT16_CALTOUCH3, TouchCalData[2]);
EEPROM.writeUInt(EE_UINT16_CALTOUCH4, TouchCalData[3]);
EEPROM.writeUInt(EE_UINT16_CALTOUCH5, TouchCalData[4]);
EEPROM.commit();
}
if (digitalRead(BWBUTTON) == LOW && digitalRead(ROTARY_BUTTON) == HIGH && digitalRead(MODEBUTTON) == HIGH && digitalRead(BANDBUTTON) == LOW) {
analogWrite(CONTRASTPIN, map(ContrastSet, 0, 100, 15, 255));
Infoboxprint(myLanguage[language][284]);
tftPrint(0, myLanguage[language][2], 155, 130, ActiveColor, ActiveColorSmooth, 28);
invertdisplay = !invertdisplay;
tft.invertDisplay(!invertdisplay);
while (digitalRead(BWBUTTON) == LOW && digitalRead(BANDBUTTON) == LOW) delay(50);
EEPROM.writeByte(EE_BYTE_INVERTDISPLAY, invertdisplay);
EEPROM.commit();
}
tft.setTouch(TouchCalData);
tft.fillScreen(BackgroundColor);
tftPrint(0, myLanguage[language][8], 160, 3, PrimaryColor, PrimaryColorSmooth, 28);
tftPrint(0, "Software " + String(VERSION), 160, 152, PrimaryColor, PrimaryColorSmooth, 16);
tft.fillRect(120, 230, 16, 6, GreyoutColor);
tft.fillRect(152, 230, 16, 6, GreyoutColor);
tft.fillRect(184, 230, 16, 6, GreyoutColor);
tft.pushImage (78, 34, 163, 84, openradiologo);
tft.drawBitmap(130, 124, TEFLogo, 59, 23, ActiveColor);
for (int x = 0; x <= ContrastSet; x++) {
analogWrite(CONTRASTPIN, map(x, 0, 100, 15, 255));
delay(30);
}
tft.fillRect(120, 230, 16, 6, PrimaryColor);
TEF = EEPROM.readByte(EE_BYTE_TEF);
if (TEF != 102 && TEF != 205) SetTunerPatch();
radio.init(TEF);
uint16_t device;
uint16_t hw;
uint16_t sw;
radio.getIdentification(device, hw, sw);
if (TEF != (highByte(hw) * 100 + highByte(sw))) SetTunerPatch();
if (lowByte(device) == 14) {
fullsearchrds = false;
fmsi = false;
tft.fillRect(152, 230, 16, 6, PrimaryColor);
tftPrint(0, "TEF6686 Lithio", 160, 172, ActiveColor, ActiveColorSmooth, 28);
} else if (lowByte(device) == 1) {
fullsearchrds = true;
tft.fillRect(152, 230, 16, 6, PrimaryColor);
tftPrint(0, "TEF6687 Lithio FMSI", 160, 172, ActiveColor, ActiveColorSmooth, 28);
} else if (lowByte(device) == 9) {
fullsearchrds = false;
fmsi = false;
tft.fillRect(152, 230, 16, 6, PrimaryColor);
tftPrint(0, "TEF6688 Lithio DR", 160, 172, ActiveColor, ActiveColorSmooth, 28);
} else if (lowByte(device) == 3) {
fullsearchrds = true;
tft.fillRect(152, 230, 16, 6, PrimaryColor);
tftPrint(0, "TEF6689 Lithio FMSI DR", 160, 172, ActiveColor, ActiveColorSmooth, 28);
} else {
tftPrint(0, myLanguage[language][9], 160, 172, SignificantColor, SignificantColorSmooth, 28);
tft.fillRect(152, 230, 16, 6, SignificantColor);
while (true);
for (;;);
}
tftPrint(0, "Patch: v" + String(TEF), 160, 202, ActiveColor, ActiveColorSmooth, 28);
Wire.beginTransmission(0x20);
Wire.write(0x06);
Wire.write(0xFF);
Wire.write(0xFF);
Wire.endTransmission();
if (analogRead(BATTERY_PIN) < 200) batterydetect = false;
if (wifi) {
tryWiFi();
tft.fillRect(184, 230, 16, 6, PrimaryColor);
delay(2000);
} else {
Server.end();
Udp.stop();
tft.fillRect(184, 230, 16, 6, SignificantColor);
}
delay(1500);
radio.setVolume(VolSet);
radio.setOffset(LevelOffset);
radio.setAMOffset(AMLevelOffset);
if (band > BAND_GAP && band != BAND_AIR) {
radio.setAMCoChannel(amcodect, amcodectcount);
radio.setAMAttenuation(amgain);
}
radio.setStereoLevel(StereoLevel);
radio.setHighCutLevel(HighCutLevel);
radio.setHighCutOffset(HighCutOffset);
radio.clearRDS(fullsearchrds);
radio.setMute();
if (!StereoToggle) radio.setMono(true);
radio.setSoftmuteFM(softmutefm);
radio.setSoftmuteAM(softmuteam);
radio.setAMNoiseBlanker(amnb);
radio.setFMNoiseBlanker(fmnb);
radio.setAudio(audiomode);
radio.setDeemphasis(fmdeemphasis);
radio.rds.region = region;
radio.setAGC(fmagc);
radio.setAMAGC(amagc);
if (fmsi) radio.setFMSI(2); else radio.setFMSI(1);
LowLevelInit = true;
if (ConverterSet >= 200) {
Wire.beginTransmission(0x12);
Wire.write(ConverterSet >> 8);
Wire.write(ConverterSet & (0xFF));
Wire.endTransmission();
}
BuildDisplay();
SelectBand();
if (tunemode == TUNE_MEM) DoMemoryPosTune();
setupmode = false;
if (edgebeep) radio.tone(50, -5, 2000);
if (!usesquelch) radio.setUnMute();
if (screensaverset) {
ScreensaverTimerInit();
ScreensaverTimerSet(screensaverOptions[screensaverset]);
}
tottimer = millis();
}
void loop() {
if (wifi && !menu) {
webserver.handleClient();
sendUDPlog();
if (millis() >= NTPtimer + 1800000) {
NTPupdate();
NTPtimer = millis();
}
}
if (hardwaremodel == PORTABLE_TOUCH_ILI9341 && touch_detect) {
if (tft.getTouchRawZ() > 100) { // Check if the touch is active
uint16_t x, y;
tft.getTouch(&x, &y);
if (x > 0 || y > 0) {
if (!firstTouchHandled) {
// Handle the initial touch event immediately
doTouchEvent(x, y);
firstTouchHandled = true; // Mark the first touch as handled
lastTouchTime = millis(); // Start tracking the time for the delay
} else if (touchrepeat) {
// Check if the initial 0.5-second delay has passed
if (millis() - lastTouchTime >= 500) {
// Repeat the touch action continuously without delay
doTouchEvent(x, y);
}
}
}
} else {
// Touch has been released
firstTouchHandled = false; // Reset the first touch flag
touch_detect = false; // Reset the touch detection flag
}
}
Communication();
if (tot != 0) {
unsigned long totprobe = tot * 60000;
if (millis() >= tottimer + totprobe) deepSleep();
}
if (freq_in != 0 && millis() >= keypadtimer + 2000) {
freq_in = 0;
ShowFreq(0);
}
if (scandxmode) {
unsigned long waitTime = (scanhold == 0) ? 500 : (scanhold * 1000);
if (!scanholdflag) scanholdflag = (USN < fmscansens * 30) && (WAM < 230) && (OStatus < 80) && (OStatus > -80);
bool bypassMillisCheck = scanholdonsignal && !scanholdflag;
bool shouldScan = bypassMillisCheck || (!bypassMillisCheck && (millis() >= scantimer + waitTime));
if (shouldScan) {
if (scanmute && scanholdonsignal) {