#include <Adafruit_GFX.h>    // Core graphics library
#include <SPI.h>       // this is needed for display
#include <Adafruit_ILI9341.h>
#include <Wire.h>      // this is needed for FT6206
#include <Adafruit_FT6206.h>

// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();

// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

// Size of the color selection boxes and the paintbrush size
#define BOXSIZE 40
int currentcolor;
int oldcolor;
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

void setup(void) {
  while (!Serial);    
 Serial.begin(115200);
  tft.begin();
if (! ctp.begin(40)) { 
    Serial.println("Couldn't start FT6206 touchscreen controller");
    while (1);
  }
Serial.println("Capacitive touchscreen started");

  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.fillScreen(BLACK);
  tft.fillRect(65, 70, 120, 60, GREEN);
  tft.fillRect(65, 150, 120, 60, GREEN);
   tft.fillRect(65, 230, 120, 60, GREEN);
  tft.setCursor(80, 90);
  tft.println("Feedback");
  tft.setCursor(90, 160);
  tft.println("Cycles");
  tft.setCursor(80, 180);
  tft.println("Feedback");
  tft.setCursor(90, 250);
  tft.println("Cycles");
  

}

void loop() {
  // Wait for a touch
  if (! ctp.touched()) {
    return;
  }

  // Retrieve a point  
  TS_Point p = ctp.getPoint();
  

  // flip it around to match the screen.
  p.x = map(p.x, 0, 240, 240, 0);
  p.y = map(p.y, 0, 320, 320, 0);

  // Print out the remapped (rotated) coordinates
  Serial.print("("); Serial.print(p.x);
  Serial.print(", "); Serial.print(p.y);
  Serial.println(")");
  

  if ((p.y>70) && (p.y < 130 ) {

     if (p.x > 65 ) && (p.x < 185 ) { 
     mode=1;     
  }
  if ((p.y>150) && (p.y < 210 ) {

     if (p.x > 65 ) && (p.x < 185 ) { 
    mode=2;  
  }
  if ((p.y>230) && (p.y < 290 ) {

     if (p.x > 65 ) && (p.x < 185 ) { 
     mode=3;    
  }

}