#include <TFT_eSPI.h>
#include <SPI.h>       // this is needed for display
#include <Button.h>

// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 15
#define TFT_DC 2
#define TFT_SCLK 18
#define TFT_MOSI 23


TFT_eSPI tft = TFT_eSPI();

#define SerialOne Serial1

#define SCR_WD   tft.width() // width
#define SCR_HT   tft.height() // height


// Color definitions
#define BLACK       0x0000      /*   0,   0,   0 */
#define NAVY        0x000F      /*   0,   0, 128 */
#define DARKGREEN   0x03E0      /*   0, 128,   0 */
#define DARKCYAN    0x03EF      /*   0, 128, 128 */
#define MAROON      0xB2C0      /* 153, 102,  51 */
#define PURPLE      0x780F      /* 128,   0, 128 */
#define OLIVE       0x7BE0      /* 128, 128,   0 */
#define LIGHTGREY   0xC618      /* 192, 192, 192 */
#define DARKGREY    0x7BEF      /* 128, 128, 128 */
#define BLUE        0x001F      /*   0,   0, 255 */
#define GREEN       0x07E0      /*   0, 255,   0 */
#define CYAN        0x2E9A      /*   0, 255, 255 */
#define RED         0xF800      /* 255,   0,   0 */
#define MAGENTA     0xF81F      /* 255,   0, 255 */
#define YELLOW      0xFFE0      /* 255, 255,   0 */
#define WHITE       0xFFFF      /* 255, 255, 255 */
#define ORANGE      0xFD20      /* 255, 165,   0 */
#define GREENYELLOW 0xAFE5      /* 173, 255,  47 */
#define PINK        0xF81F      /* 255,   0, 255 */
#define SPECIAL     0xFF56      /* 170, 255,  86 */

#define BROWN      0x5140 //0x5960
#define SKY_BLUE   0x02B5 //0x0318 //0x039B //0x34BF
#define DARK_RED   0x8000
#define DARK_GREY  0x39C7


int i;
int j;



unsigned long res[16];
void setup(void)
{
  SerialOne.begin(115200);
  tft.init();
  tft.setRotation(2);
  tft.fillScreen(BLACK);
  tft.setCursor(0, 0);


  //draw blue and orange box with blue fading

  tft.fillRoundRect(55, 60, 120, 20, 8, tft.color565(70, 70, 255));

  for (int i = 68, j = 60 ; (i < 110) && (j < 1000); i = i + 4, j = j + 12 ) {
    tft.fillRoundRect(55, i, 120, 8, 0, tft.color565(j, j, 255));
  }

  tft.fillRoundRect(55, 115, 120, 46, 0, tft.color565(200,  80, 0));
  tft.fillRoundRect(55, 150, 120, 20, 8, tft.color565(200,  80, 0));

  //draw little black box
  tft.fillRoundRect(105, 156, 25, 12, 0, tft.color565(0,  0, 0));

  // grey boxs
  tft.fillRoundRect(185, 30, 30, 180, 0, tft.color565(30, 30, 30));
  tft.fillRoundRect(10, 30, 30, 180, 0, tft.color565(30, 30, 30));
  tft.fillRoundRect(50, 0, 140, 15, 0, tft.color565(30, 30, 30));
  tft.fillRoundRect(220, 65, 35, 100, 0, tft.color565(30, 30, 320));

  tft.fillTriangle(235, 65,   240, 65,   240, 100,  BLACK);
  tft.fillTriangle(235, 165,   240, 165,  240, 130,  BLACK);

  tft.fillTriangle(220, 90,   220, 105,   225, 105,  BLACK );
  tft.fillTriangle(220, 125,   225, 125,  220, 140,  BLACK);
  tft.fillRoundRect(220, 106, 6, 19, 0, BLACK);


  //(2, 5, 6, 44, 55, 66);
  tft.fillCircle(115, 290, 80, tft.color565(30, 30, 30));

  //yellow line


  // white text
  tft.setCursor(112, 158);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.println("22");

  //white circles

  tft.drawCircle(180, 70, 2, tft.color565(255, 255, 255));
  tft.drawCircle(180, 90, 2, tft.color565(255, 255, 255));
  tft.drawRect(178, 114, 6, 1, tft.color565(255, 255, 255));
  tft.drawCircle(180, 130, 2, tft.color565(255, 255, 255));
  tft.drawCircle(180, 150, 2, tft.color565(255, 255, 255));

  //white tick marks  RH panel

  tft.drawRect(228, 161, 2, 1, tft.color565(255, 255, 255));
  tft.drawRect(228, 155, 2, 1, tft.color565(255, 255, 255));
  tft.drawRect(228, 145, 2, 1, tft.color565(255, 255, 255));
  tft.drawRect(228, 135, 2, 1, tft.color565(255, 255, 255));
  tft.drawRect(228, 125, 2, 1, tft.color565(255, 255, 255));
  tft.drawRect(232, 113, 4, 2, tft.color565(255, 0, 255));
  tft.drawRect(228, 115, 4, 1, tft.color565(255, 255, 255));
  tft.drawRect(232, 117, 4, 2, tft.color565(255, 0, 255));
  tft.drawRect(228, 105, 2, 1, tft.color565(255, 255, 255));
  tft.drawRect(228, 95, 2, 1, tft.color565(255, 255, 255));
  tft.drawRect(228, 85, 2, 1, tft.color565(255, 255, 255));
  tft.drawRect(228, 75, 2, 1, tft.color565(255, 255, 255));
  tft.drawRect(228, 65, 2, 1, tft.color565(255, 255, 255));

  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.setCursor(220, 132);
  tft.println("1");
  tft.setCursor(220, 152);
  tft.println("2");
  tft.setCursor(220, 92);
  tft.println("1");
  tft.setCursor(220, 72);
  tft.println("2");
  tft.setCursor(14, 210);
  tft.println(".327");



  //white tick marks  RH large panel

  tft.drawRect(185, 35, 6, 1, tft.color565(255, 255, 255));
  tft.drawRect(185, 55, 6, 1, tft.color565(255, 255, 255));
  tft.drawRect(185, 75, 6, 1, tft.color565(255, 255, 255));
  tft.drawRect(185, 95, 6, 1, tft.color565(255, 255, 255));
  tft.drawRect(185, 115, 6, 1, tft.color565(255, 255, 255));
  tft.drawRect(185, 135, 6, 1, tft.color565(255, 255, 255));
  tft.drawRect(185, 155, 6, 1, tft.color565(255, 255, 255));
  tft.drawRect(185, 175, 6, 1, tft.color565(255, 255, 255));
  tft.drawRect(185, 195, 6, 1, tft.color565(255, 255, 255));

  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.setCursor(195, 32);
  tft.println("400");
  tft.setCursor(195, 72);
  tft.println("200");
  tft.setCursor(195, 152);
  tft.println("-200");
  tft.setCursor(195, 192);
  tft.println("-400");


  // pointer

  tft.drawLine(240, 105, 225, 100, WHITE);
  tft.drawLine(240, 104, 225, 99, WHITE);
  tft.drawLine(240, 103, 225, 98, WHITE);

  // yellow hatch
  tft.drawLine(188, 134, 184, 130, YELLOW);
  tft.drawLine(192, 134, 184, 126, YELLOW);
  tft.drawLine(196, 134, 184, 122, YELLOW);
  tft.drawLine(200, 134, 188, 122, YELLOW);
  tft.drawLine(204, 134, 192, 122, YELLOW);
  tft.drawLine(208, 134, 196, 122, YELLOW);
  tft.drawLine(212, 134, 200, 122, YELLOW);
  tft.drawLine(214, 132, 204, 122, YELLOW);

  // yellow line
  tft.drawLine(185, 35, 185, 120, YELLOW);
  tft.drawLine(185, 120, 190, 120, YELLOW);


  //white bars top box
  tft.drawLine(90, 0, 90, 15, WHITE);
  tft.drawLine(145, 0, 145, 15, WHITE);

  //white tick marks  LH large panel

  tft.drawRect(32, 35, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 50, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 65, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 80, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 95, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 110, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 125, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 140, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 155, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 170, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 185, 8, 1, tft.color565(255, 255, 255));
  tft.drawRect(32, 200, 8, 1, tft.color565(255, 255, 255));

  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.setCursor(15, 32);
  tft.println("280");
  tft.setCursor(15, 62);
  tft.println("260");
  tft.setCursor(15, 92);
  tft.println("240");
  tft.setCursor(15, 152);
  tft.println("200");
  tft.setCursor(15, 182);
  tft.println("180");

  //precision box lh
  tft.fillRoundRect(4, 100, 27, 27, 0, BLACK);
  tft.drawRect(4, 100, 27, 27, WHITE);
  tft.setCursor(9, 112);
  tft.println("219");
  tft.setCursor(9, 102);
  tft.println("  0");
  tft.setCursor(9, 122);
  tft.println("  8");
  tft.fillTriangle(30, 104,   30, 118,   40, 111,  WHITE );
  tft.fillTriangle(28, 104,   28, 118,   38, 111,  BLACK );

  //precision box rh

  tft.fillRoundRect(190, 100, 35, 27, 0, BLACK);
  tft.drawRect(190, 100, 35, 27, WHITE);
  tft.setCursor(200, 112);
  tft.println("0040");
  tft.setCursor(200, 102);
  tft.println("  60");
  tft.setCursor(200, 122);
  tft.println("  20");
  tft.fillRoundRect(192, 111, 6, 9, 0, GREEN);
  //tft.fillRoundRect(185, 112, 5, 27, 0, tft.color565(153, 153, 0));
  //tft.fillRoundRect(185, 127, 40, 12, 0, tft.color565(153, 153, 0));

  tft.fillTriangle(190, 104,   190, 118,   180, 111,  WHITE );
  tft.fillTriangle(192, 104,   192, 118,   182, 111,  BLACK );





  // green text
  tft.setCursor(90, 50);
  tft.setTextColor(GREEN);  tft.setTextSize(1);
  tft.print("FLT  DIR");

  tft.setCursor(95, 5);
  tft.print("HGD HLD   TO/GA");

  tft.setCursor(50, 20);
  tft.print("108.9/207");

  tft.setCursor(50, 30);
  tft.print("DME   1.6");

  tft.setCursor(145, 190);
  tft.print("RADIO");

  tft.setCursor(150, 205);
  tft.print("200");

  tft.setCursor(185, 210);
  tft.print("29.92");

  //green line

  tft.drawLine(194, 65, 213, 65, GREEN);

  tft.drawTriangle(182, 58,   182, 72,   194, 65,  GREEN );

  //Purple text

  tft.setCursor(188, 17);
  tft.setTextColor(MAGENTA);  tft.setTextSize(2);
  tft.print("2");
  tft.setCursor(198, 20);
  tft.setTextColor(MAGENTA);  tft.setTextSize(1);
  tft.print("000");

  tft.setCursor(20, 20);
  tft.print("225");

  // purple line

  tft.drawLine(182, 35, 182, 40, MAGENTA);
  tft.drawLine(182, 40, 192, 40, MAGENTA);
  tft.drawLine(192, 40, 192, 30, MAGENTA);

  //Purple diamond

  tft.fillTriangle(114, 177,   110, 181,   118, 181,  MAGENTA );
  tft.fillTriangle(114, 185,   110, 181,   118, 181,  MAGENTA );

  //rounded square boxes white

  tft.drawRoundRect(85, 178, 7, 7, 2, WHITE);
  tft.drawRoundRect(135, 178, 7, 7, 2, WHITE);



  //line detail in AH panel
  //flight directors
  tft.drawRect(114, 70, 2, 90, MAGENTA);
  tft.drawRect(70, 115, 90, 2, MAGENTA);
  //ATT indicator
  tft.drawRect(112, 113, 6, 6, WHITE);//squre in center
  tft.fillRect(113, 114, 4, 4, BLACK);//squre in center
  //if change line order - problem with display
  tft.fillRect(88, 112, 6, 11, WHITE);//left part
  tft.fillRect(89, 113, 4, 9, BLACK);//left part
  tft.fillRect(70, 112, 18, 6, WHITE);//left part
  tft.fillRect(71, 113, 18, 4, BLACK);//left part

  tft.fillRect(138, 112, 6, 11, WHITE);//right part
  tft.fillRect(139, 113, 4, 9, BLACK);//right part
  tft.fillRect(144, 112, 18, 6, WHITE);//right part
  tft.fillRect(143, 113, 18, 4, BLACK);//right part


  tft.drawLine(97, 83, 133, 83, WHITE);
  tft.drawLine(110, 87, 118, 87, WHITE);
  tft.drawLine(103, 91, 125, 91, WHITE);
  tft.drawLine(110, 95, 118, 95, WHITE);
  tft.drawLine(97, 99, 131, 99, WHITE);
  tft.drawLine(110, 103, 118, 103, WHITE);
  tft.drawLine(103, 107, 125, 107, WHITE);
  tft.drawLine(110, 111, 118, 111, WHITE);
  //tft.drawLine(0, 115, 241, 115, GREEN);
  tft.drawLine(110, 119, 118, 119, WHITE);
  tft.drawLine(103, 123, 125, 123, WHITE);
  tft.drawLine(110, 126, 118, 126, WHITE);
  tft.drawLine(97, 129, 131, 129, WHITE);
  tft.drawLine(110, 132, 118, 132, WHITE);
  tft.drawLine(103, 136, 125, 136, WHITE);
  tft.drawLine(110, 140, 118, 140, WHITE);
  tft.drawLine(97, 144, 131, 144, WHITE);



  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.setCursor(134, 80);
  tft.print("20");
  tft.setCursor(85, 80);
  tft.print("20");
  tft.setCursor(134, 95);
  tft.print("10");
  tft.setCursor(85, 95);
  tft.print("10");

  tft.setCursor(132, 153);
  tft.print("20");
  tft.setCursor(85, 153);
  tft.print("20");
  tft.setCursor(132, 133);
  tft.print("10");
  tft.setCursor(85, 133);
  tft.print("10");

  tft.fillTriangle(111, 60,   117, 60,   114, 63,  WHITE );
  tft.drawTriangle(109, 70,   119, 70,   114, 65,  WHITE );
  tft.drawRect(109, 72,   12, 3,  WHITE );


  tft.drawLine(90, 63, 91, 65, WHITE);
  tft.drawLine(75, 68, 79, 76, WHITE);
  tft.drawLine(63, 78, 65, 80, WHITE);
  tft.drawLine(121, 59, 121, 61, WHITE);
  tft.drawLine(136, 62, 134, 69, WHITE);
  tft.drawLine(151, 68, 150, 71, WHITE);
  tft.drawLine(165, 78, 161, 84, WHITE);


  //details in round grey box
  tft.drawLine(115, 210, 115, 240, WHITE);
  tft.drawTriangle(115, 210,   111, 203,   119, 203,  WHITE );
  tft.setTextColor(MAGENTA);  tft.setTextSize(1);
  tft.setCursor(85, 233);
  tft.print("022h");
  tft.setTextColor(GREEN);  tft.setTextSize(1);
  tft.setCursor(127, 233);
  tft.print("MAG");
  tft.drawLine(105, 211, 106, 219, WHITE);
  tft.drawLine(90, 215, 91, 217, WHITE);
  tft.drawLine(75, 220, 79, 228, WHITE);
  tft.drawLine(63, 230, 65, 232, WHITE);
  tft.drawLine(121, 211, 121, 213, WHITE);
  tft.drawLine(136, 214, 134, 221, WHITE);
  tft.drawLine(151, 220, 150, 223, WHITE);
  tft.drawLine(165, 230, 161, 236, WHITE);

}


void loop() {
}