#pragma once

#include <Arduino.h>
#include <TFT_eSPI.h>
#include "TEF6686.h"
#include "constants.h"
#include "change_detector.h"
#include "language.h"
#include <WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include "WiFiConnect.h"
#include "WiFiConnectParam.h"
#include "ESP32Time.h"
#include "scrolling_text.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

extern bool RDSstatus;
extern bool RDSstatusold;
extern TFT_eSPI tft;
extern bool dynamicspi;
extern bool Data_Accelerator;
extern bool advancedRDS, afmethodBold, afpage;
extern bool afscreen, aftest;
extern bool autoDST, autolog, autologged;
extern bool autosquelch, batterydetect, beepresetstart;
extern bool beepresetstop, BWreset, bwtouchtune;
extern bool BWtune, change, clockampm;
extern bool direction, dropout;
extern bool dynamicPTYold, edgebeep, externaltune;
extern bool findMemoryAF;
extern bool firstTouchHandled;
extern bool flashing;
extern bool fmsi, fullsearchrds;
extern bool hasafold, haseonold;
extern bool initdxscan, invertdisplay, leave;
extern bool LowLevelInit;
extern bool memorystore;
extern bool memreset, memtune;
extern bool menu, menuopen;
extern bool mwstepsize;
extern bool NTPupdated;
extern bool optenc;
extern bool rdsflagreset;
extern bool rdsreset;
extern bool rdsstatscreen;
extern bool RDSSPYTCP, RDSSPYUSB;
extern bool rotaryaccelerate;
extern bool rtcset;
extern bool scandxmode;
extern bool scanholdflag;
extern bool scanholdonsignal;
extern bool scanmem;
extern bool scanmute;
extern bool screenmute;
extern bool screensavertriggered;
extern bool seek;
extern bool seekinit;
extern bool setextendbw;
extern bool setupmode;
extern bool showclock;
extern bool showlongps;
extern bool usesquelch;
extern bool softmuteam;
extern bool softmutefm;
extern bool SQ;
extern bool Stereostatusold;
extern bool StereoToggle;
extern bool store;
extern bool touchrepeat;
extern bool touch_detect;
extern bool tuned;
extern bool USBmode;
extern bool XDRGTKdata;
extern bool XDRGTKMuteScreen;
extern bool XDRGTKTCP, XDRGTKUSB;
extern bool XDRMute, XDRScan;
extern bool wifi, wificonnected;
extern byte af_counterold;
extern byte aid_counterold;
extern byte af;
extern byte afpagenr;
extern byte amagc;
extern byte amnb;
extern byte amscansens;
extern byte audiomode;
extern byte band;
extern byte bandAM;
extern byte bandFM;
extern byte bandforbidden;
extern byte battery;
extern byte batteryold;
extern byte batteryoptions;
extern byte BWset;
extern byte BWsettemp;
extern byte BWsetAM;
extern byte BWsetFM;
extern byte BWsetRecall;
extern byte BWtemp;
extern byte charwidth;
extern byte chipmodel;
extern byte hardwaremodel;
extern byte ContrastSet;
extern byte CurrentSkin;
extern byte CurrentTheme;
extern byte displayflip;
extern byte eonptyold[20];
extern byte EQset;
extern byte fmagc;
extern byte fmscansens;
extern byte fmdefaultstepsize;
extern byte fmnb;
extern byte fmdeemphasis;
extern byte freqfont;
extern byte amcodect;
extern byte amcodectcount;
extern byte amgain;
extern byte freqoldcount;
extern byte HighCutLevel;
extern byte HighCutOffset;
extern byte items[10];
extern byte iMSEQ;
extern byte iMSset;
extern byte language;
extern byte longbandpress;
extern byte memdoublepi;
extern byte memorypos;
extern byte memoryposold;
extern byte memoryposstatus;
extern byte mempionly;
extern byte memstartpos;
extern byte memstoppos;
extern byte menuitem;
extern byte menupage;
extern byte poweroptions;
extern byte rdsblockold;
extern byte rdsqualityold;
extern byte rotarymode;
extern byte touchrotating;
extern byte scancancel;
extern byte scanstart;
extern byte scanstop;
extern byte scanhold;
extern byte scanmodeold;
extern byte screensaverOptions[5];
extern byte screensaverset;
extern byte showmodulation;
extern byte showrdserrors;
extern byte showSWMIBand;
extern byte submenu;
extern byte stationlistid;
extern byte nowToggleSWMIBand;
extern byte stepsize;
extern byte StereoLevel;
extern byte subnetclient;
extern byte TEF, tot, tunemode;
extern byte unit, spispeed;
extern char buff[16];
extern char eonpicodeold[20][6];
extern const uint8_t* currentFont;
extern float vPerold;
extern int ActiveColor;
extern int ActiveColorSmooth;
extern int AGC;
extern int AMLevelOffset;
extern int BackgroundColor;
extern int BackgroundColor1;
extern int BackgroundColor2;
extern int BackgroundColor3;
extern int BackgroundColor4;
extern int BackgroundColor5;
extern int BarSignificantColor;
extern int BarInsignificantColor;
extern int BatteryValueColor;
extern int BatteryValueColorSmooth;
extern int batupdatetimer;
extern int berPercentold;
extern int BWAutoColor;
extern int BWAutoColorSmooth;
extern int BWOld;
extern int bwupdatetimer;
extern int DisplayedSegments;
extern int ForceMono;
extern int FrameColor;
extern int FreqColor;
extern int FreqColorSmooth;
extern int freq_in;
extern int freqold;
extern int GreyoutColor;
extern int InsignificantColor;
extern int InsignificantColorSmooth;
extern int menuoption;
extern int ModBarInsignificantColor;
extern int ModBarSignificantColor;
extern int offsetupdatetimer;
extern int OStatusold;
extern int peakholdold;
extern int peakholdtimer;
extern int PrimaryColor;
extern int PrimaryColorSmooth;
extern int RDSColor, RDSColorSmooth;
extern int RDSDropoutColor, RDSDropoutColorSmooth;
extern int SignificantColor;
extern int SignificantColorSmooth;
extern int StereoColor;
extern int StereoColorSmooth;
extern int WifiColorHigh;
extern int WifiColorLow;
extern int SquelchShow;
extern int rotary;
extern int rotarycounter;
extern int rotarycounteraccelerator;
extern int rssi;
extern int rssiold;
extern int scanner_filter;
extern int SecondaryColor;
extern int SecondaryColorSmooth;
extern int SNRupdatetimer;
extern int Sqstatusold;
extern int Squelch;
extern int Squelchold;
extern int SStatusold;
extern int Stereostatus;
extern int XDRBWset;
extern int xPos;
extern int xPos2;
extern int16_t OStatus;
extern int16_t SAvg;
extern int16_t SAvg2;
extern int16_t SAvg3;
extern int16_t SAvg4;
extern int16_t SAvg5;
extern int16_t SStatus;
extern int16_t MP;
extern int16_t US;
extern int8_t MPold;
extern int8_t USold;
extern int8_t LevelOffset;
extern int8_t LowLevelSet;
extern int8_t NTPoffset;
extern int8_t CN;
extern int8_t CNold;
extern int8_t VolSet;
extern float batteryVold;
extern IPAddress remoteip;
extern String AIDString;
extern String cryptedpassword;
extern String ECColdString;
extern String ECCString;
extern String eonpsold[20];
extern String PIold;
extern String PSold;
extern String PTYold;
extern String rds_clock;
extern String rds_clockold;
extern String rds_date;
extern String rds_dateold;
extern String RDSSPYRDS;
extern String RDSSPYRDSold;
extern String RTold;
extern String salt;
extern String saltkey;
extern String stationIDold;
extern String stationStateold;
extern String StereoStatusCommand;
extern String StereoStatusCommandold;
extern String SWMIBandstring;
extern String SWMIBandstringold;
extern String XDRGTK_key;
extern Detector<String, 1> XDRGTKRDS;
extern uint16_t BW;
extern uint16_t MStatus;
extern uint16_t SWMIBandPos;
extern uint16_t SWMIBandPosold;
extern uint16_t TouchCalData[5];
extern uint16_t USN;
extern uint16_t WAM;
extern uint8_t buff_pos;
extern unsigned int ConverterSet;
extern unsigned int freq_scan;
extern unsigned int frequency;
extern unsigned int frequency_OIRT;
extern unsigned int frequency_AM;
extern unsigned int frequency_LW;
extern unsigned int frequency_MIBand_11M;
extern unsigned int frequency_MIBand_120M;
extern unsigned int frequency_MIBand_13M;
extern unsigned int frequency_MIBand_15M;
extern unsigned int frequency_MIBand_160M;
extern unsigned int frequency_MIBand_16M;
extern unsigned int frequency_MIBand_19M;
extern unsigned int frequency_MIBand_22M;
extern unsigned int frequency_MIBand_25M;
extern unsigned int frequency_MIBand_31M;
extern unsigned int frequency_MIBand_41M;
extern unsigned int frequency_MIBand_49M;
extern unsigned int frequency_MIBand_60M;
extern unsigned int frequency_MIBand_75M;
extern unsigned int frequency_MIBand_90M;
extern unsigned int frequency_MW;
extern unsigned int frequency_SW;
extern unsigned int HighEdgeOIRTSet;
extern unsigned int HighEdgeSet;
extern unsigned int LowEdgeOIRTSet;
extern unsigned int logcounter;
extern unsigned int LowEdgeSet;
extern unsigned int LWHighEdgeSet;
extern unsigned int LWLowEdgeSet;
extern unsigned int mappedfreqold[20];
extern unsigned int mappedfreqold2[20];
extern unsigned int mappedfreqold3[20];
extern unsigned int memstartfreq;
extern unsigned int memstopfreq;
extern unsigned int MWHighEdgeSet;
extern unsigned int MWLowEdgeSet;
extern unsigned int scanner_end;
extern unsigned int scanner_start;
extern unsigned int scanner_step;
extern unsigned int SWHighEdgeSet;
extern unsigned int SWLowEdgeSet;
extern unsigned long aftimer;
extern unsigned long autosquelchtimer;
extern unsigned long blockcounterold[33];
extern unsigned long eonticker;
extern unsigned long flashingtimer;
extern unsigned long keypadtimer;
extern unsigned long lastTouchTime;
extern unsigned long lowsignaltimer;
extern unsigned long ModulationpreviousMillis;
extern unsigned long ModulationpeakPreviousMillis;
extern unsigned long NTPtimer;
extern unsigned long peakholdmillis;
extern unsigned long processed_rdsblocksold[33];
extern unsigned long pslongticker;
extern unsigned long pslongtickerhold;
extern unsigned long rtticker;
extern unsigned long rttickerhold;
extern unsigned long rotarytimer;
extern unsigned long scantimer;
extern unsigned long screensavertimer;
extern unsigned long signalstatustimer;
extern unsigned long tottimer;
extern unsigned long tuningtimer;
extern unsigned long udplogtimer;
extern unsigned long udptimer;
extern const size_t language_totalnumber;
extern const size_t language_entrynumber;

extern mem presets[EE_PRESETS_CNT];
extern TEF6686 radio;
extern ESP32Time rtc;

extern TFT_eSprite FrequencySprite;
extern TFT_eSprite RDSSprite;
extern TFT_eSprite SquelchSprite;
extern TFT_eSprite FullLineSprite;
extern TFT_eSprite OneBigLineSprite;
extern TFT_eSprite SignalSprite;
extern TFT_eSprite PSSprite;

extern WiFiConnect wc;
extern WiFiServer Server;
extern WiFiClient RemoteClient;
extern WiFiUDP Udp;
extern WebServer webserver;

extern ScrollingTextDisplay rtplusDisplay;
extern ScrollingTextDisplay eonDisplay;
extern ScrollingTextDisplay eccDisplay;
extern ScrollingTextDisplay rtDisplay;