This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
TEF6686_remastered.ino
4325 lines (4063 loc) · 127 KB
/
TEF6686_remastered.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
/*
********************************************************************************
Advanced Tuner software for NXP TEF668x ESP32
(c) 2023 Sjef Verhoeven pe5pvb@het-bar.net & Noobish noobish@noobish.eu
Based on Catena / NXP semiconductors API
********************************************************************************
Analog signalmeter:
to meter
|
R V
IO27 -------=====-----=====---|
1 k 5 k POT
********************************************************************************
Setup:
During boot:
Hold BW button to change rotary encoder direction
Hold Mode button the flip screen
Hold Standby button to calibrate analogue s-meter
Hold rotary button to switch between normal and optical rotary encoder
Manual:
STANDBY SHORT PRESS: Switch band
STANDBY LONG PRESS: Standby
ROTARY SHORT PRESS: Set stepsize or navigate
ROTARY LONG PRESS: Toggle iMS & EQ
MODE SHORT PRESS: Switch between auto/manual
or exit menu
MODE LONG PRESS: Open menu
BW SHORT PRESS: Switch bandwidth setting
BW LONG PRESS: Switch mono, or auto stereo
* ***********************************************
ATTENTION: If you want to compile this code, use Arduino Core for ESP32 v1.0.6 - newer versions won't work!
* ***********************************************
Use these settings in TFT_eSPI User_Setup.h
#define ILI9341_DRIVER
#define TFT_CS 5
#define TFT_DC 17
#define TFT_RST 16
#define LOAD_GLCD
#define LOAD_FONT2
#define LOAD_FONT4
#define LOAD_FONT6
#define LOAD_FONT7
#define LOAD_FONT8
#define LOAD_GFXFF
#define SMOOTH_FONT
#define SPI_FREQUENCY 10000000
ALL OTHER SETTINGS SHOULD STAY !!!!
*/
#include "src/TEF6686.h"
#include "src/constants.h"
#include <EEPROM.h>
#include <Wire.h>
#include "SPIFFS.h"
#include <analogWrite.h> // https://github.com/ERROPiX/ESP32_AnalogWrite
#include <TFT_eSPI.h> // https://github.com/Bodmer/TFT_eSPI
#include <RotaryEncoder.h> // https://github.com/enjoyneering/RotaryEncoder
#include <Hash.h> // https://github.com/bbx10/Hash_tng
#include <WiFiConnect.h> // https://registry.platformio.org/libraries/mrfaptastic/WiFiConnect%20Lite
#include <ESPAsyncWebServer.h> // https://github.com/me-no-dev/ESPAsyncWebServer
#include <AsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
/* Button -> PIN connections */
#define ROTARY_PIN_A 34
#define ROTARY_PIN_B 36
#define ROTARY_BUTTON 39
#define PIN_POT 35
#define PWRBUTTON 4
#define BWBUTTON 25
#define MODEBUTTON 26
#define CONTRASTPIN 2
#define STANDBYLED 19
#define SMETERPIN 27
#define BATTERYPIN 13
#define BWINTERVAL 200 // Refresh interval for certain UI elements
//#define ARS // uncomment for BGR type display (ARS version)
#ifdef ARS
#define VERSION "v1.22ARS"
#include "TFT_Colors.h"
TFT_eSPI tft = TFT_eSPI(320, 240);
#else
#define VERSION "v1.22"
TFT_eSPI tft = TFT_eSPI(240, 320);
#endif
RotaryEncoder encoder(ROTARY_PIN_A, ROTARY_PIN_B, ROTARY_BUTTON);
int16_t position = 0;
void ICACHE_RAM_ATTR encoderISR() {
encoder.readAB();
}
void ICACHE_RAM_ATTR encoderButtonISR() {
encoder.readPushButton();
}
bool tuned;
bool setupmode;
bool direction;
bool seek;
bool screenmute = false;
bool power = true;
bool change2;
bool BWreset;
bool menu;
bool menu2;
bool setupmenu;
bool menuopen = false;
bool menu2open = false;
bool LowLevelInit = false;
bool RDSstatusold;
bool SQ;
bool Stereostatusold;
bool StereoToggle = true;
bool store;
bool tunemode = false;
bool USBstatus = false;
bool WiFistatus = false;
bool wificonnect;
bool XDRMute;
bool wifisetupopen = false;
extern bool ProgramTP;
extern bool TPStatus;
byte band;
byte BWset;
byte ContrastSet;
byte displayflip;
byte freqoldcount;
byte rotarymode;
byte SStatusoldcount;
byte stepsize;
byte SignalUnits;
byte SNRold;
byte SNR;
byte LowLevelSet;
byte iMSset;
byte EQset;
byte iMSEQ;
byte TEF;
byte optenc;
byte RDSClear;
byte XDRGTKShutdown;
byte WiFiSwitch = EEPROM.readByte(63);
char buff[16];
char programServicePrevious[9];
char programTypePrevious[17];
char radioIdPrevious[4];
char radioTextPrevious[65];
int AGC;
int BWOld;
int ConverterSet;
int DeEmphasis;
int ForceMono;
int freqold;
int HighCutLevel;
int HighCutOffset;
int HighEdgeSet;
int LevelOffset;
int LowEdgeSet;
int menuoption = 30;
int menu2option = 30;
int MStatusold;
int OStatusold;
int peakholdold;
int peakholdtimer;
int lowsignaltimer;
int scanner_filter;
int Sqstatusold;
int Squelch;
int Squelchold;
int SStatusold;
int status = WL_IDLE_STATUS;
int StereoLevel;
int Stereostatus;
int VolSet;
int volume;
int XDRBWset;
int XDRBWsetold;
// Theme Engine Colors
int PrimaryColor;
int SecondaryColor;
int FrameColor;
int FrequencyColor;
int GreyoutColor;
int BackgroundColor;
int ActiveColor;
int OptimizerColor;
int RDSColor;
int StereoColor;
int CurrentTheme;
// Scrolling RT settings
int xPos = 6;
int yPos = 2;
int charWidth = tft.textWidth("AA");
uint16_t BW;
uint16_t MStatus;
int16_t OStatus;
int16_t SStatus;
uint16_t USN;
uint16_t WAM;
String ContrastString;
String ConverterString;
String CurrentThemeString;
String SignalUnitsString;
String cryptedpassword;
String HighCutLevelString;
String HighCutOffsetString;
String HighEdgeString;
String hostname = "ESP32 TEF6686 FM Tuner";
String LevelOffsetString;
String LowEdgeString;
String LowLevelString;
String password = "12345";
String salt;
String saltkey = " ";
String PIold;
String PSold;
String PTYold;
String RDSClearString;
String RTold;
String StereoLevelString;
String VolString;
String XDRGTKShutdownString;
String WiFiSwitchString;
// Extra settings strings
const char* SignalUnitsStrings[] = { "dBuV", "dBf" };
const char* RDSClearStrings[] = { "OFF", "ON" };
const char* XDRGTKShutdownStrings[] = { "OFF", "ON" };
const char* WiFiSwitchStrings[] = { "OFF", "ON" };
// Wi-Fi setup
const char* PARAM_INPUT_1 = "ssid";
const char* PARAM_INPUT_2 = "pass";
const char* PARAM_INPUT_3 = "ip";
const char* PARAM_INPUT_4 = "gateway";
String ssid;
String pass;
String ip;
String gateway;
const char* ssidPath = "/ssid.txt";
const char* passPath = "/pass.txt";
const char* ipPath = "/ip.txt";
const char* gatewayPath = "/gateway.txt";
IPAddress localIP;
IPAddress localGateway;
IPAddress subnet(255, 255, 255, 0);
unsigned long previousMillis = 0;
const long interval = 2500; // interval to wait for Wi-Fi connection (milliseconds)
uint16_t rdsB;
uint16_t rdsC;
uint16_t rdsD;
uint16_t rdsErr;
uint8_t buff_pos = 0;
uint8_t RDSstatus;
int16_t SAvg;
unsigned int change;
unsigned int freq_scan;
unsigned int frequency;
unsigned int frequency_AM;
unsigned int frequencyold;
unsigned int scanner_end;
unsigned int scanner_start;
unsigned int scanner_step;
unsigned long peakholdmillis;
unsigned long BWCLOCK = 0;
unsigned long RTCLOCK = 0;
unsigned long TPCLOCK = 0;
// Battery calculation
const int MAX_ANALOG_VAL = 4095;
const float MIN_BATTERY_VOLTAGE = 3.15;
const float MAX_BATTERY_VOLTAGE = 4.05;
int rawValue = analogRead(BATTERYPIN);
float voltageLevel = (rawValue / 4095.0) * 2 * 1.1 * 3.3 - 0.2; // calculate voltage level
float batteryFraction = (voltageLevel - MIN_BATTERY_VOLTAGE) / (MIN_BATTERY_VOLTAGE / MAX_BATTERY_VOLTAGE);
float batteryPercentage = batteryFraction * 100;
// Wi-Fi Setup Webpage HTML Code
const char index_html[] PROGMEM = "<!DOCTYPE html><html><head> <title>TEF6686 Wi-Fi Setup</title> <meta name='viewport' content='width=device-width, initial-scale=1'> <link rel='stylesheet' type='text/css' href='style.css'> <style> html { font-family: Arial, Helvetica, sans-serif; display: inline-block; text-align: center;}h1 { font-size: 1.8rem; color: #252525;}.topnav { overflow: hidden; background-color: #00ea8f; }body { margin: 0; background: #252525;}.content { padding: 5%;}.card { max-width: 800px; margin: 0 auto; background-color: white; padding: 5px 5% 5px 5%;}input[type=submit] { color: #252525; background-color: #00ea8f; border: 3px solid #252525; cursor: pointer; font-weight: bold; padding: 15px 15px; text-align: center; text-decoration: none; font-size: 16px; width: 160px; margin-top: 20px; margin-bottom: 10px; border-radius: 8px; transition: .3s ease-in-out; }input[type=submit]:hover { background-color: #252525; border: 3px solid #00ea8f; color: #00ea8f;}input[type=text], input[type=number], select { width: 100%; padding: 12px 20px; border: 1px solid #ccc; box-sizing: border-box;}a { text-decoration: none; color: #00ea8f;}p { font-size: 14px; text-transform: uppercase; text-align: left; margin: 0; margin-top: 15px;}.card p{ font-weight: bold;}.gateway-info{ text-transform: initial; text-align: center; color: white;} </style></head><body> <div class='topnav'> <h1>TEF6686 Wi-Fi SETUP</h1> </div> <div class='content'> <div class='card'> <form action='/' method='POST'> <p>SSID:</p> <input type='text' id ='ssid' name='ssid' placeholder='The exact name of your Wi-Fi network'><br> <p>Password:</p> <input type='text' id ='pass' name='pass' placeholder='Password to your Wi-Fi network'><br> <p>IP Address:</p> <input type='text' id ='ip' name='ip' placeholder='Example: 192.168.1.200'><br> <p>Gateway:</p> <input type='text' id ='gateway' name='gateway' placeholder='Example: 192.168.1.1'><br> <input type ='submit' value ='SUBMIT'> </form> </div> <p class='gateway-info'>If you're unsure, you can <a href='https://www.noip.com/support/knowledgebase/finding-your-default-gateway/' target='_blank'>look up how to get your gateway</a>.</p> </div></body></html>";
TEF6686 radio;
RdsInfo rdsInfo;
AsyncWebServer setupserver(80);
WiFiServer server(7373);
WiFiClient RemoteClient;
TFT_eSprite sprite = TFT_eSprite(&tft);
void initSPIFFS() {
if (!SPIFFS.begin(true)) {
Serial.println("An error has occurred while mounting SPIFFS");
}
}
// Read File from SPIFFS
String readFile(fs::FS& fs, const char* path) {
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if (!file || file.isDirectory()) {
Serial.println("- failed to open file for reading");
return String();
}
String fileContent;
while (file.available()) {
fileContent = file.readStringUntil('\n');
break;
}
return fileContent;
}
// Write file to SPIFFS
void writeFile(fs::FS& fs, const char* path, const char* message) {
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("- failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("- file written");
} else {
Serial.println("- frite failed");
}
}
bool initWiFi() {
if (ssid == "" || ip == "") {
Serial.println("Undefined SSID or IP address.");
return false;
}
if (ip != "") {
localIP.fromString(ip.c_str());
}
if (gateway != "") {
localGateway.fromString(gateway.c_str());
}
if (!WiFi.config(localIP, localGateway, subnet)) {
Serial.println("STA Failed to configure");
return false;
}
Serial.println(WiFi.localIP());
return true;
}
void setup() {
setupmode = true;
EEPROM.begin(64);
if (EEPROM.readByte(41) != 15) {
EEPROM.writeByte(2, 0);
EEPROM.writeByte(3, 0);
EEPROM.writeUInt(0, 10000);
EEPROM.writeInt(4, 0);
EEPROM.writeInt(8, 0);
EEPROM.writeInt(12, 87);
EEPROM.writeInt(16, 108);
EEPROM.writeInt(20, 50);
EEPROM.writeInt(24, 0);
EEPROM.writeInt(28, 0);
EEPROM.writeInt(32, 70);
EEPROM.writeInt(36, 0);
EEPROM.writeByte(41, 15);
EEPROM.writeByte(42, 0);
EEPROM.writeByte(43, 20);
EEPROM.writeByte(44, 1);
EEPROM.writeByte(45, 1);
EEPROM.writeByte(46, 0);
EEPROM.writeUInt(47, 828);
EEPROM.writeByte(52, 0);
EEPROM.writeByte(53, 0);
EEPROM.writeByte(54, 0);
EEPROM.writeByte(55, 0);
EEPROM.writeByte(56, 0);
EEPROM.writeByte(57, 0);
EEPROM.writeByte(58, 0);
EEPROM.writeInt(59, 0);
EEPROM.writeByte(63, 1);
EEPROM.commit();
}
frequency = EEPROM.readUInt(0);
VolSet = EEPROM.readInt(4);
ConverterSet = EEPROM.readInt(8);
LowEdgeSet = EEPROM.readInt(12);
HighEdgeSet = EEPROM.readInt(16);
ContrastSet = EEPROM.readInt(20);
LevelOffset = EEPROM.readInt(24);
StereoLevel = EEPROM.readInt(28);
HighCutLevel = EEPROM.readInt(32);
HighCutOffset = EEPROM.readInt(36);
stepsize = EEPROM.readByte(42);
LowLevelSet = EEPROM.readByte(43);
iMSset = EEPROM.readByte(44);
EQset = EEPROM.readByte(45);
band = EEPROM.readByte(46);
frequency_AM = EEPROM.readUInt(47);
rotarymode = EEPROM.readByte(52);
displayflip = EEPROM.readByte(53);
TEF = EEPROM.readByte(54);
optenc = EEPROM.readByte(55);
SignalUnits = EEPROM.readByte(56);
RDSClear = EEPROM.readByte(57);
XDRGTKShutdown = EEPROM.readByte(58);
CurrentTheme = EEPROM.readInt(59);
WiFiSwitch = EEPROM.readByte(63);
EEPROM.commit();
encoder.begin();
btStop();
Serial.begin(115200);
initSPIFFS();
ssid = readFile(SPIFFS, ssidPath);
pass = readFile(SPIFFS, passPath);
ip = readFile(SPIFFS, ipPath);
gateway = readFile(SPIFFS, gatewayPath);
WiFi.setHostname(hostname.c_str());
rawValue = analogRead(BATTERYPIN);
if (voltageLevel < 1.5) {
voltageLevel = voltageLevel * 3.6;
}
delay(50);
if (initWiFi()) {
if (WiFiSwitch == 0) {
WiFi.mode(WIFI_OFF);
} else {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), pass.c_str());
server.begin();
}
}
SignalUnitsString = SignalUnitsStrings[SignalUnits];
RDSClearString = RDSClearStrings[RDSClear];
XDRGTKShutdownString = XDRGTKShutdownStrings[XDRGTKShutdown];
WiFiSwitchString = WiFiSwitchStrings[WiFiSwitch];
if (iMSset == 1 && EQset == 1) {
iMSEQ = 2;
}
if (iMSset == 0 && EQset == 1) {
iMSEQ = 3;
}
if (iMSset == 1 && EQset == 0) {
iMSEQ = 4;
}
if (iMSset == 0 && EQset == 0) {
iMSEQ = 1;
}
tft.init();
if (displayflip == 0) {
#ifdef ARS
tft.setRotation(0);
#else
tft.setRotation(3);
#endif
} else {
#ifdef ARS
tft.setRotation(2);
#else
tft.setRotation(1);
#endif
}
TEF = EEPROM.readByte(54);
if (TEF != 101 && 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();
}
doTheme();
sprite.createSprite(313, 18);
analogWrite(CONTRASTPIN, ContrastSet * 2 + 27);
analogWrite(SMETERPIN, 0);
uint8_t version;
radio.getIdentification(device, hw, sw);
version = highByte(hw) * 100 + highByte(sw);
pinMode(MODEBUTTON, INPUT);
pinMode(BWBUTTON, INPUT);
pinMode(CONTRASTPIN, OUTPUT);
pinMode(SMETERPIN, OUTPUT);
pinMode(BATTERYPIN, INPUT);
if (digitalRead(BWBUTTON) == LOW) {
if (rotarymode == 0) {
rotarymode = 1;
} else {
rotarymode = 0;
}
EEPROM.writeByte(52, rotarymode);
EEPROM.commit();
tft.fillScreen(BackgroundColor);
tft.setTextColor(SecondaryColor);
tft.drawCentreString("Rotary direction changed", 150, 70, 4);
tft.drawCentreString("Please release button", 150, 100, 4);
while (digitalRead(BWBUTTON) == LOW) {
delay(5);
}
}
if (digitalRead(MODEBUTTON) == LOW) {
if (displayflip == 0) {
displayflip = 1;
tft.setRotation(1);
} else {
displayflip = 0;
tft.setRotation(3);
}
EEPROM.writeByte(53, displayflip);
EEPROM.commit();
tft.fillScreen(BackgroundColor);
tft.setTextColor(SecondaryColor);
tft.drawCentreString("Screen flipped", 150, 70, 4);
tft.drawCentreString("Please release button", 150, 100, 4);
while (digitalRead(MODEBUTTON) == LOW) {
delay(50);
}
}
if (digitalRead(PWRBUTTON) == LOW) {
analogWrite(SMETERPIN, 511);
tft.fillScreen(BackgroundColor);
tft.setTextColor(SecondaryColor);
tft.drawCentreString("Calibrate analog meter", 150, 70, 4);
tft.drawCentreString("Release button when ready", 150, 100, 4);
while (digitalRead(PWRBUTTON) == LOW) {
delay(50);
}
analogWrite(SMETERPIN, 0);
}
if (digitalRead(ROTARY_BUTTON) == LOW) {
tft.fillScreen(BackgroundColor);
tft.setTextColor(SecondaryColor);
if (optenc == 0) {
optenc = 1;
tft.drawCentreString("encoder set to optical", 150, 70, 4);
} else {
optenc = 0;
tft.drawCentreString("encoder set to standard", 150, 70, 4);
}
EEPROM.writeByte(55, optenc);
EEPROM.commit();
tft.drawCentreString("Please release button", 150, 100, 4);
while (digitalRead(ROTARY_BUTTON) == LOW) {
delay(5);
}
}
/* Splash screen */
tft.setSwapBytes(true);
tft.fillScreen(BackgroundColor);
tft.fillCircle(160, 70, 60, PrimaryColor);
tft.fillCircle(160, 70, 52, BackgroundColor);
tft.drawBitmap(130, 60, TEFLogo, 59, 23, ActiveColor);
tft.setTextColor(ActiveColor);
tft.drawCentreString("NXP", 160, 42, 2);
tft.drawCentreString("TEF6686", 160, 85, 2);
tft.drawCentreString("AM/FM Receiver", 160, 145, 4);
tft.setTextColor(PrimaryColor);
if (CurrentTheme == 0) {
tft.setTextColor(FrameColor);
} else {
tft.setTextColor(GreyoutColor);
}
tft.drawString(String(VERSION), 10, 220, 2);
tft.drawRightString("Modded by Noobish.eu", 310, 205, 2);
tft.drawRightString("Original software by PE5PVB.nl", 310, 220, 2);
tft.fillRect(120, 180, 16, 6, GreyoutColor);
tft.fillRect(152, 180, 16, 6, GreyoutColor);
tft.fillRect(184, 180, 16, 6, GreyoutColor);
if (lowByte(device) != 14 && lowByte(device) != 1 && lowByte(device) != 9 && lowByte(device) != 3) {
tft.setTextColor(TFT_RED);
tft.drawString("Tuner: !None! (TEF ERROR)", 160, 20, 2);
while (true)
;
for (;;)
;
}
tft.drawString("Patch: v" + String(TEF), 10, 205, 2);
delay(200);
tft.fillRect(120, 180, 16, 6, PrimaryColor);
delay(375);
tft.fillRect(152, 180, 16, 6, PrimaryColor);
delay(375);
tft.fillRect(184, 180, 16, 6, PrimaryColor);
delay(375);
radio.setVolume(VolSet);
radio.setOffset(LevelOffset);
radio.setStereoLevel(StereoLevel);
radio.setHighCutLevel(HighCutLevel);
radio.setHighCutOffset(HighCutOffset);
radio.clearRDS();
radio.setMute();
LowLevelInit = true;
if (ConverterSet >= 200) {
Wire.beginTransmission(0x12);
Wire.write(ConverterSet >> 8);
Wire.write(ConverterSet & (0xFF));
Wire.endTransmission();
}
SelectBand();
ShowSignalLevel();
ShowBW();
setupmode = false;
attachInterrupt(digitalPinToInterrupt(ROTARY_PIN_A), encoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_PIN_B), encoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_BUTTON), encoderButtonISR, FALLING);
radio.setUnMute();
if (VolSet > 10) {
VolSet = 10;
}
}
void loop() {
if (digitalRead(PWRBUTTON) == LOW && USBstatus == false) {
PWRButtonPress();
}
if (power == true) {
if (seek == true) {
Seek(direction);
}
if (SStatus / 10 > LowLevelSet && LowLevelInit == false && menu == false && menu2 == false && band == 0) {
if (RDSClear == 1) {
radio.clearRDS();
sprite.fillSprite(BackgroundColor);
sprite.pushSprite(6, 220);
}
if (screenmute == false) {
sprite.fillSprite(BackgroundColor);
sprite.pushSprite(6, 220);
tft.setTextColor(SecondaryColor);
tft.drawString("20", 20, 153, 1);
tft.drawString("40", 50, 153, 1);
tft.drawString("60", 80, 153, 1);
tft.drawString("80", 110, 153, 1);
tft.drawString("100", 134, 153, 1);
tft.drawString("120", 164, 153, 1);
tft.drawString("%", 196, 153, 1);
tft.drawString("M", 6, 136, 2);
tft.drawString("PI:", 216, 195, 2);
tft.drawString("PS:", 6, 195, 2);
tft.drawString("PTY:", 6, 168, 2);
tft.drawLine(20, 150, 200, 150, TFT_DARKGREY);
}
LowLevelInit = true;
}
if (SStatus / 10 <= LowLevelSet && band == 0) {
if (LowLevelInit == true && menu == false && menu2 == false) {
radio.clearRDS();
if (screenmute == false) {
tft.setTextColor(BackgroundColor);
if (RDSClear == 1) {
sprite.fillSprite(BackgroundColor);
sprite.pushSprite(6, 220);
tft.drawString(PSold, 38, 192, 4);
tft.drawString(PIold, 244, 192, 4);
tft.drawString(RTold, 6, 222, 2);
tft.drawString(PTYold, 38, 168, 2);
tft.drawString("TP", 190, 168, 2);
}
tft.fillRect(20, 139, 12, 8, GreyoutColor);
tft.fillRect(34, 139, 12, 8, GreyoutColor);
tft.fillRect(48, 139, 12, 8, GreyoutColor);
tft.fillRect(62, 139, 12, 8, GreyoutColor);
tft.fillRect(76, 139, 12, 8, GreyoutColor);
tft.fillRect(90, 139, 12, 8, GreyoutColor);
tft.fillRect(104, 139, 12, 8, GreyoutColor);
tft.fillRect(118, 139, 12, 8, GreyoutColor);
tft.fillRect(132, 139, 12, 8, GreyoutColor);
tft.fillRect(146, 139, 12, 8, GreyoutColor);
tft.fillRect(160, 139, 12, 8, GreyoutColor);
tft.fillRect(174, 139, 12, 8, GreyoutColor);
tft.fillRect(188, 139, 12, 8, GreyoutColor);
tft.setTextColor(GreyoutColor);
tft.drawString("20", 20, 153, 1);
tft.drawString("40", 50, 153, 1);
tft.drawString("60", 80, 153, 1);
tft.drawString("80", 110, 153, 1);
tft.drawString("100", 134, 153, 1);
tft.drawString("120", 164, 153, 1);
tft.drawString("%", 196, 153, 1);
tft.drawString("M", 6, 136, 2);
tft.drawString("PI:", 216, 195, 2);
tft.drawString("PS:", 6, 195, 2);
tft.drawString("PTY:", 6, 168, 2);
tft.drawLine(20, 150, 200, 150, GreyoutColor);
tft.drawBitmap(110, 5, RDSLogo, 67, 22, GreyoutColor);
}
LowLevelInit = false;
}
if (millis() >= lowsignaltimer + 300) {
lowsignaltimer = millis();
if (band == 0) {
radio.getStatus(SStatus, USN, WAM, OStatus, BW, MStatus);
} else {
radio.getStatus_AM(SStatus, USN, WAM, OStatus, BW, MStatus);
}
if (screenmute == true) {
readRds();
}
if (menu == false && menu2 == false) {
doSquelch();
}
}
} else {
if (band == 0) {
radio.getStatus(SStatus, USN, WAM, OStatus, BW, MStatus);
} else {
radio.getStatus_AM(SStatus, USN, WAM, OStatus, BW, MStatus);
}
if (menu == false && menu2 == false) {
doSquelch();
readRds();
if (screenmute == false) {
ShowModLevel();
}
}
}
if (menu == false && menu2 == false) {
if (screenmute == false) {
showPI();
showTP();
showPTY();
showPS();
showRadioText();
showPS();
ShowStereoStatus();
ShowOffset();
ShowSignalLevel();
ShowBW();
}
}
XDRGTKRoutine();
XDRCommunication();
XDRGTKWiFi();
if (encoder.getPushButton() == true) {
ButtonPress();
}
if (menu == true && menuopen == true && menuoption == 110) {
if (band == 0) {
radio.getStatus(SStatus, USN, WAM, OStatus, BW, MStatus);
} else {
radio.getStatus_AM(SStatus, USN, WAM, OStatus, BW, MStatus);
}
if (millis() >= lowsignaltimer + 500 || change2 == true) {
lowsignaltimer = millis();
change2 = false;
if (SStatus > SStatusold || SStatus < SStatusold) {
String count = String(SStatus / 10, DEC);
if (screenmute == false) {
tft.setTextColor(BackgroundColor);
if (SStatusold >= 0) {
if (SStatusoldcount <= 1) {
tft.setCursor(100, 140);
}
if (SStatusoldcount == 2) {
tft.setCursor(73, 140);
}
if (SStatusoldcount >= 3) {
tft.setCursor(46, 140);
}
} else {
if (SStatusoldcount <= 1) {
tft.setCursor(100, 140);
}
if (SStatusoldcount == 2) {
tft.setCursor(83, 140);
}
if (SStatusoldcount >= 3) {
tft.setCursor(56, 140);
}
}
tft.setTextFont(6);
tft.print(SStatusold / 10);
tft.print(".");
if (SStatusold < 0) {
String negative = String(SStatusold % 10, DEC);
if (SStatusold % 10 == 0) {
tft.print("0");
} else {
tft.print(negative[1]);
}
} else {
tft.print(SStatusold % 10);
}
tft.setTextColor(PrimaryColor, BackgroundColor);
if (SStatus >= 0) {
if (count.length() == 1) {
tft.setCursor(100, 140);
}
if (count.length() == 2) {
tft.setCursor(73, 140);
}
if (count.length() == 3) {
tft.setCursor(46, 140);
}
} else {
if (count.length() == 1) {
tft.setCursor(100, 140);
}
if (count.length() == 2) {
tft.setCursor(83, 140);
}
if (count.length() >= 3) {
tft.setCursor(56, 140);
}
}
tft.setTextFont(6);
tft.print(SStatus / 10);
tft.print(".");
if (SStatus < 0) {
String negative = String(SStatus % 10, DEC);
if (SStatus % 10 == 0) {
tft.print("0");
} else {
tft.print(negative[1]);
}
} else {
tft.print(SStatus % 10);
}
SStatusold = SStatus;
SStatusoldcount = count.length();
}
}
}
}
if (position < encoder.getPosition()) {
if (rotarymode == 0 && menu != true && menu2 != true) {
sprite.fillSprite(BackgroundColor);
sprite.pushSprite(6, 220);
radio.clearRDS();
tft.setTextColor(BackgroundColor);
tft.drawString(PSold, 38, 192, 4);
tft.drawString(PIold, 244, 192, 4);
tft.drawString(RTold, 6, 222, 2);
tft.drawString(PTYold, 38, 168, 2);
PSold = "";
PTYold = "";
PIold = "";
RTold = "";
KeyUp();
} else {
KeyDown();
}
}
if (position > encoder.getPosition()) {
if (rotarymode == 0 && menu != true && menu2 != true) {
sprite.fillSprite(BackgroundColor);
sprite.pushSprite(6, 220);
KeyDown();
radio.clearRDS();
tft.setTextColor(BackgroundColor);
tft.drawString(PSold, 38, 192, 4);
tft.drawString(PIold, 244, 192, 4);
tft.drawString(RTold, 6, 222, 2);
tft.drawString(PTYold, 38, 168, 2);
PSold = "";
PTYold = "";
PIold = "";
RTold = "";
} else {
KeyUp();
}
}
if (digitalRead(MODEBUTTON) == LOW && digitalRead(BWBUTTON) != LOW && screenmute == false) {
ModeButtonPress();
}
if (digitalRead(BWBUTTON) == LOW && digitalRead(MODEBUTTON) != LOW && screenmute == false) {
BWButtonPress();
}
if (store == true) {
change++;
}
if (change > 200 && store == true) {
detachInterrupt(digitalPinToInterrupt(ROTARY_PIN_A));
/* detachInterrupt(digitalPinToInterrupt(ROTARY_PIN_B));*/
detachInterrupt(digitalPinToInterrupt(ROTARY_BUTTON));
EEPROM.writeUInt(0, radio.getFrequency());
EEPROM.writeUInt(47, radio.getFrequency_AM());
EEPROM.writeByte(46, band);
EEPROM.commit();
store = false;
attachInterrupt(digitalPinToInterrupt(ROTARY_PIN_A), encoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_PIN_B), encoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_BUTTON), encoderButtonISR, FALLING);
}
}
}
void PWRButtonPress() {
if (menu == false && menu2 == false) {
unsigned long counterold = millis();
unsigned long counter = millis();
while (digitalRead(PWRBUTTON) == LOW && counter - counterold <= 1000) {
counter = millis();
}
if (counter - counterold < 1000) {
if (power == false) {
ESP.restart();
} else {
if (band == 0) {
band = 1;
} else {
band = 0;
}
StoreFrequency();
SelectBand();
}
} else {
if (power == false) {
ESP.restart();
} else {
BuildMenu2();
menu2 = true;
}
}
while (digitalRead(PWRBUTTON) == LOW) {
delay(50);
}
delay(100);
}
}
void StoreFrequency() {
EEPROM.writeUInt(0, radio.getFrequency());
EEPROM.writeUInt(47, radio.getFrequency_AM());
EEPROM.writeByte(46, band);
EEPROM.commit();
}
void SelectBand() {
if (band == 1) {
seek = false;
tunemode = false;
BWreset = true;
BWset = 2;
if (radio.getFrequency_AM() > 0) {
frequency_AM = radio.getFrequency_AM();
}
radio.setFrequency_AM(frequency_AM);
freqold = frequency_AM;
doBW;
radio.getStatus_AM(SStatus, USN, WAM, OStatus, BW, MStatus);
if (screenmute == false) {
BuildDisplay();
}
tft.drawString("PI:", 216, 195, 2);