#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>


/* #define TFT_CS 15
  #define TFT_DC 2
  #define TFT_WR 23
  #define TFT_RD 19
  #define TFT_RESET 4 */

#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 23
#define TFT_CLK 18
#define TFT_RST 4
#define TFT_MISO 19

//Adafruit_ILI9341 tft = Adafruit_ILI9341(LCD_CS, LCD_DC, 23, 19, 4);
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

//Definicao de cores
#define BLACK           0x0000
#define BLUE            0x2015
#define BLUE2           0x0DDE
#define RED             0xF800
#define GREEN           0x07E0
#define GREEN2          0x43A2
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0
#define WHITE           0xFFFF
#define GRAY            0x31A6
#define PINK            0xF81F

void setup() {
  tft.begin();
  tft.fillScreen(WHITE);
  uint8_t x = tft.readcommand8(ILI9341_RDMODE);
  Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  //drawButtons();
}

void loop() {


}

void drawBox() {
  tft.drawFastHLine(0, 1, 240, WHITE);
  tft.drawFastVLine(0, 0, 320, WHITE);
  tft.drawFastVLine(238, 0, 320, WHITE);

  tft.drawFastHLine(0, 60, 240, WHITE);
  tft.drawFastVLine(90, 60, 52, WHITE);
  tft.drawFastVLine(180, 60, 52, WHITE);

  for (int y = 112; y <= 216; y += 52) {
    tft.drawFastHLine(0, y, 240, WHITE);
    tft.drawFastVLine(60, y, 52, WHITE);
    tft.drawFastVLine(120, y, 52, WHITE);
    tft.drawFastVLine(180, y, 52, WHITE);
  }

  tft.drawFastHLine(0, 268, 240, WHITE);
  tft.drawFastVLine(60, 268, 52, WHITE);
  tft.drawFastVLine(120, 268, 52, WHITE);
  tft.drawFastHLine(0, 319, 240, WHITE);
}

void drawButtons() {

  // x_start, y_start, altura, largura, cor
  tft.fillRect(0, 0, 240, 60, GRAY);
  //tft.fillRect(0, 0, 240, 60, WHITE);

  //Primeira linha
  tft.fillRect(0, 60, 90, 52, BLACK);

  tft.fillRect(90, 60, 90, 52, BLACK);

  tft.fillRect(180, 60, 60, 52, GRAY);

  for (int y = 112; y <= 216; y += 52) {
    tft.fillRect(0, y, 60, 52, BLACK);

    tft.fillRect(60, y, 60, 52, BLACK);

    tft.fillRect(120, y, 60, 52, BLACK);

    tft.fillRect(180, y, 60, 52, GRAY);
  }

  //Quinta linha
  tft.fillRect(0, 268, 60, 52, RED);

  tft.fillRect(60, 268, 60, 52, BLACK);

  tft.fillRect(120, 268, 120, 52, GREEN);

  drawBox();
  fillButtons();
}

void fillButtons() {
  tft.setCursor(38, 75);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("(");

  tft.setCursor(128, 75);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print(")");

  tft.setCursor(204, 77);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("/");

  tft.setCursor(204, 127);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("x");

  tft.setCursor(204, 180);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("-");

  tft.setCursor(204, 233);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("+");

  tft.setCursor(170, 280);
  tft.setTextColor(WHITE);
  tft.setTextSize(4);
  tft.print("=");

  tft.setCursor(22, 283);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("C");

  tft.setCursor(83, 283);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("0");

  tft.setCursor(22, 127);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("7");

  tft.setCursor(22, 180);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("4");

  tft.setCursor(22, 233);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("1");

  tft.setCursor(83, 127);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("8");

  tft.setCursor(83, 180);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("5");

  tft.setCursor(83, 233);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("2");

  tft.setCursor(143, 127);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("9");

  tft.setCursor(143, 180);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("6");

  tft.setCursor(143, 233);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("3");
}