#define ILI9341_DRIVER

#define TFT_MISO 19  // (leave TFT SDO disconnected if other SPI devices share MISO)
#define TFT_MOSI 23
#define TFT_SCLK 18
#define TFT_CS   15  // Chip select control pin
#define TFT_DC    2  // Data Command control pin
#define TFT_RST   4  // Reset pin (could connect to RST pin)

// // Optional touch screen chip select
// //#define TOUCH_CS 5 // Chip select pin (T_CS) of touch screen

// #define LOAD_GLCD    // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
// #define LOAD_FONT2   // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
// #define LOAD_FONT4   // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
// #define LOAD_FONT6   // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
// #define LOAD_FONT7   // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:.
// #define LOAD_FONT8   // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
// #define LOAD_GFXFF   // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts

// #define SMOOTH_FONT
// #define SPI_FREQUENCY  40000000
// #define SPI_READ_FREQUENCY  16000000
// #define SPI_TOUCH_FREQUENCY  2500000
#define LOAD_GFXFF
#define M_FONT &FreeSans9pt7b
#include <TFT_eSPI.h>     // Hardware-specific library
//TFT_eSPI tft;
TFT_eSPI tft;
TFT_eSprite sprite = TFT_eSprite(&tft);
//'Arrow_w22xh20', 21x21px
const unsigned char  bmpArrow_w22xh20 []  = {
	0xff, 0xe3, 0x1f, 0xff, 0xcb, 0x1f, 0xff, 0x8b, 0x1f, 0xff, 0x0b, 0x1f, 0x01, 0x08, 0x1e, 0xfe, 
	0x07, 0x1c, 0x02, 0x00, 0x18, 0x02, 0x00, 0x10, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x02, 0x00, 
	0x00, 0x02, 0x00, 0x00, 0x02, 0x00, 0x10, 0x02, 0x00, 0x18, 0x01, 0x00, 0x1c, 0x03, 0x00, 0x1e, 
	0xff, 0x03, 0x1f, 0xff, 0x83, 0x1f, 0xff, 0xc3, 0x1f, 0xff, 0xe7, 0x1f, 0x00, 0x00, 0x00
};
// 'arrow_21x21', 21x21px
const unsigned char  bmpArrow_21x21  []  = {
	0xff, 0xfb, 0x1f, 0xff, 0xf3, 0x1f, 0xff, 0xe3, 0x1f, 0xff, 0xc3, 0x1f, 0xff, 0x83, 0x1f, 0x01, 
	0x00, 0x1f, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x18, 0x00, 0x00, 0x10, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x14, 0x02, 0x00, 0x1a, 0x02, 0x00, 0x1d, 0xfc, 0x83, 0x1e, 0x01, 0x40, 0x1f, 
	0xff, 0xa3, 0x1f, 0xff, 0xd3, 0x1f, 0xff, 0xeb, 0x1f, 0xff, 0xf3, 0x1f, 0xff, 0xfb, 0x1f
};
#define SIZE(a) sizeof(a) / sizeof(a[0])
#define IsWithin(x, a, b) ((x>=a)&&(x<=b))
#define IN_RANGE(x, low, high) (((x) >= (low)) && ((x) <= (high)))
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
//#include <Arduino.h>   

#include "fastLed_timers.h"


#define BTN1_PIN 13
#define BTN2_PIN 14

bool encRotFlag =false;
int8_t encDelta = 0;

bool tftChanged = false;

void setup(void) 
{
  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLUE); 
  Serial.begin(115200); // For debug
  pinMode(BTN1_PIN, INPUT_PULLUP);
  pinMode(BTN2_PIN, INPUT_PULLUP);
  showMainMenu(true);

}


void loop() 
{
if (!encRotFlag && digitalRead(BTN1_PIN) == LOW) {encDelta+=1;encRotFlag=true;}
if (!encRotFlag && digitalRead(BTN2_PIN) == LOW) {encDelta-=1;encRotFlag=true;}
EVERY_N_MILLIS(1000){ randomCount();}
showMainMenu();
}
int8_t getEncDelta(){
  int8_t result = 0;
  if(encRotFlag){  encRotFlag=false;  result = encDelta;  encDelta = 0;}
  return result;
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
lcd1:VCC
lcd1:GND
lcd1:CS
lcd1:RST
lcd1:D/C
lcd1:MOSI
lcd1:SCK
lcd1:LED
lcd1:MISO
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r