#include <TFT_eSPI.h> 
#include <SPI.h>
#include<esp32-hal-ledc.h>
#include<Wire.h>
#include<Adafruit_Sensor.h>
#include<Adafruit_I2CDevice.h>
#include <AHT20.h>
AHT20 aht20;
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
#include<MPU9250.h>
MPU9250 mpu;
#include <Smoothed.h>         // Include the library





Smoothed<int> sl;
Smoothed<float> sh;
Smoothed<float> st;
Smoothed<float> sy;
Smoothed<float> sm;

int fps = 0;
int initHighFlag = 0;



const int SENSOR_PIN = 8;
int slight;
float High;
float sHigh;
float HighOrg;
float bTemp;
float sTemp;

float bm;
float sM;

int mYaw;
int sYaw;
float humi;

//MPU variables

int16_t pitch, roll, yaw; //from mpu6050light
int16_t Pitch, Roll, Yaw; //from mpu6050light

//  variables to store bmp280 readings to be sent
float temperature;
float pressure;
float startAltitude;

// Mavlink carry over variables
int16_t heading = 0;
float alt ;
//float volt = 0;
float curr = 0;
int16_t rssi = 0;
//float climb = 0;
float aspd = 0;
int16_t mode = 0;

static float volt = 12.3;
static char voltstr[15];

static float climb = 12.3;
static char climbstr[15];

// String modestr = "MODE";
char modestr[15] = "MODE";

int freq = 2000;     // 频率
int channel = 0;     // 通道
int resolution = 8;  // 分辨率

const int led = 38;


TFT_eSPI tft = TFT_eSPI();       
TFT_eSprite face = TFT_eSprite(&tft);
TFT_eSprite Hsprite = TFT_eSprite(&tft);
TFT_eSprite VTsprite = TFT_eSprite(&tft);
TFT_eSprite VHsprite = TFT_eSprite(&tft);
TFT_eSprite DashUp = TFT_eSprite(&tft);
TFT_eSprite DashDown = TFT_eSprite(&tft);


unsigned long drawTime = 0;


const int Y_TFT_height = 90;
const int X_TFT_width = 244;

// Invoke library, pins defined in User_Setup.h  https://github.com/Bodmer/

#define REDRAW_DELAY 50 // minimum delay in milliseconds between display updates

//V1 #define HOR 250    // Horizon circle outside radius (205 is corner to corner
/*V2*/
#define HOR 400    // Horizon vector line length    

#define BROWN      0x5140 //0x5960
//#define SKY_BLUE   0x02B5 //0x0318 //0x039B //0x34BF
//#define SKY_BLUE   0x34BF //0x0318 //0x039B //0x34BF
#define SKY_BLUE   0x03EF //0x0318 //0x039B //0x34BF
#define DARK_RED   0x8000
#define DARK_GREY  0x39C7
#define TFT_GREY 0xBDF7
#define LTBLUE 0xB6DF
#define LTTEAL 0xBF5F
#define LTGREEN 0xBFF7
#define LTCYAN 0xC7FF
#define LTRED 0xFD34
#define LTMAGENTA 0xFD5F
#define LTYELLOW 0xFFF8
#define LTORANGE 0xFE73
#define LTPINK 0xFDDF
#define LTPURPLE 0xCCFF
#define LTGREY 0xE71C

#define BLUE 0x001F
#define TEAL 0x0438
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define ORANGE 0xFC00
#define PINK 0xF81F
#define PURPLE 0x8010
#define GREY 0xC618
#define WHITE 0xdeb7
#define Fubo 0xEDAD
#define BLACK 0x0000
#define NAVY 0x000F

#define DKBLUE 0x000D
#define DKTEAL 0x020C
#define DKGREEN 0x03E0
#define DKCYAN 0x03EF
#define DKRED 0x6000
#define DKMAGENTA 0x8008
#define DKYELLOW 0x8400
#define DKORANGE 0x8200
#define DKPINK 0x9009
#define DKPURPLE 0x4010
#define DKGREY 0x4A49
#define DPGREY 0x4A4A
#define LPGREY 0x7D7D

#define CLOCK_FG BLACK
#define CLOCK_BG DKGREY
#define SECCOND_BG DKRED
#define SECCOND_FG RED
#define LABEL_FG GREY



#define XC  X_TFT_width/2 // x coord of centre of horizon
#define YC Y_TFT_height/2 // y coord of centre of horizon

#define ANGLE_INC 1 // Angle increment for arc segments, 1 will give finer resolution, 2 or more gives faster rotation

#define DEG2RAD 0.0174532925

int roll_angle = 180; // These must be initialed to 180 so updateHorizon(0); in setup() draws

int last_roll = 0; // the whole horizon graphic
int last_pitch = 0;

int roll_delta = 90;  // This is used to set arc drawing direction, must be set to 90 here


// Variables for test only
//int test_angle = 0;
//int delta = ANGLE_INC;

unsigned long redrawTime = 0;