/**
  First demo for FT6206 Capactive Touch Screen on Wokwi. Enjoy!

  https://wokwi.com/arduino/projects/311598148845830720
 */

/***************************************************
  This is our touchscreen painting example for the Adafruit ILI9341
  captouch shield
  ----> http://www.adafruit.com/products/1947

  Check out the links above for our tutorials and wiring diagrams

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************/


#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>

class Button {
  public:
  int leftBound;
  int topBound;

  int width;
  int height;

  int rightBound;
  int bottomBound;

  bool selected;
  
  Button(int x, int y, int width, int height) {
    this->leftBound = x;
    this->topBound = y;
    this->width = width;
    this->height = height;

    rightBound = leftBound + this->width;
    bottomBound = topBound + this->height;

    selected = false;
  }
};

// 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 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

// -----------------------------
#define TABHEIGHT 30
#define TABWIDTH 70

#define INNERX 10
#define INNERY 20
#define INNERWIDTH 300
#define INNERHEIGHT 200

#define MENUX (INNERX+TABWIDTH)
#define MENUY (INNERY+TABHEIGHT)
#define MENUWIDTH (INNERWIDTH-TABWIDTH-2)
#define MENUHEIGHT (INNERHEIGHT-TABHEIGHT-2)

Button keysButton = Button(INNERX,INNERY,TABWIDTH,TABHEIGHT);
Button gpsButton = Button(INNERX,INNERY+TABHEIGHT-2,TABWIDTH,TABHEIGHT);
Button openButton = Button(INNERX,INNERY+(TABHEIGHT*2)-4,TABWIDTH,TABHEIGHT);

bool key1 = false;
bool key2 = true;
bool key3 = false;
bool key4 = true;

void setup(void) {
  while (!Serial);     // used for leonardo debugging
 
  Serial.begin(115200);
  
  tft.begin();

  if (! ctp.begin(40)) {  // pass in 'sensitivity' coefficient
    Serial.println("Couldn't start FT6206 touchscreen controller");
    while (1);
  }

  Serial.println("Capacitive touchscreen started");
  
  tft.setRotation(1);

  DrawInnerFrame();
  DrawTabs();
}

void DrawTabs() {
  tft.drawRect(INNERX,INNERY,TABWIDTH,TABHEIGHT, ILI9341_GREEN); //keys
  tft.drawRect(INNERX+1,INNERY+1,TABWIDTH-2,TABHEIGHT-2, ILI9341_GREEN);
  tft.setCursor(INNERX+10,INNERY+8);
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(2);
  tft.println("Keys");

  tft.drawRect(INNERX,INNERY+TABHEIGHT-2,TABWIDTH,TABHEIGHT, ILI9341_GREEN); //GPS
  tft.drawRect(INNERX+1,INNERY+TABHEIGHT-1,TABWIDTH-2,TABHEIGHT-2, ILI9341_GREEN);
  tft.setCursor(INNERX+16,INNERY+TABHEIGHT+6);
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(2);
  tft.println("GPS");

  tft.drawRect(INNERX,INNERY+(TABHEIGHT*2)-4,TABWIDTH,TABHEIGHT, ILI9341_GREEN); //Unlock
  tft.drawRect(INNERX+1,INNERY+(TABHEIGHT*2)-3,TABWIDTH-2,TABHEIGHT-2, ILI9341_GREEN);
  tft.setCursor(INNERX+12,INNERY+(TABHEIGHT*2)+4);
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(2);
  tft.println("Open");
}

void DrawInnerFrame() {
  // Outside border
  tft.fillScreen(ILI9341_BLACK);
  tft.drawRect(0,0,tft.width(),tft.height(), ILI9341_GREEN);
  tft.drawRect(1,1,tft.width()-2,tft.height()-2, ILI9341_GREEN);

  // Inside frame
  tft.drawRect(INNERX,INNERY, INNERWIDTH,INNERHEIGHT, ILI9341_GREEN); //frame
  tft.drawRect(INNERX+1,INNERY+1, INNERWIDTH-2,INNERHEIGHT-2, ILI9341_GREEN);

  tft.drawRect(INNERX,INNERY,MENUX-INNERX,INNERHEIGHT, ILI9341_GREEN); //sidebar
  tft.drawRect(INNERX+1,INNERY+1,MENUX-INNERX-2,INNERHEIGHT-2, ILI9341_GREEN);

  tft.drawRect(MENUX-2,INNERY, INNERWIDTH-TABWIDTH+2,TABHEIGHT, ILI9341_GREEN); //titlebar
  tft.drawRect(MENUX-1,INNERY+1, INNERWIDTH-TABWIDTH,TABHEIGHT-2, ILI9341_GREEN);
}

bool CheckButtonBounds(TS_Point& p, Button& b) {
  if(p.x > b.leftBound && p.x < b.rightBound && p.y > b.topBound && p.y < b.bottomBound) {
    return true;
  }
  else {
    return false;
  }
}

void HandleTouch(TS_Point& p) {
  // if a touch is inside the tabs, handle it by changing the screen
}
void HandleTouchRelease(TS_Point& p) {
  // if a touch is inside the tabs, handle it by changing the screen

  Serial.print("Got touch release at (");
  Serial.print(p.x);
  Serial.print(", ");
  Serial.print(p.y);
  Serial.println(")");

  if(CheckButtonBounds(p, keysButton)) {
    Serial.println("Keys Button");
    KeysScreen();
  }
  else if(CheckButtonBounds(p, gpsButton)) {
    Serial.println("GPS Button");
    GPSScreen();
  }
  else if(CheckButtonBounds(p, openButton)) {
    Serial.println("Open Button");
    OpenScreen();
  }
}

void KeysScreen() {
  // Unhighlight other tabs and make Keys tab highlighted
  tft.fillRect(keysButton.leftBound+2,keysButton.topBound+2,keysButton.width-4,keysButton.height-4, ILI9341_BLACK);
  tft.fillRect(gpsButton.leftBound+2,gpsButton.topBound+2,gpsButton.width-4,gpsButton.height-4, ILI9341_BLACK);
  tft.fillRect(openButton.leftBound+2,openButton.topBound+2,openButton.width-4,openButton.height-4, ILI9341_BLACK);
  DrawTabs();

  tft.fillRect(keysButton.leftBound+3,keysButton.topBound+3,keysButton.width-6,keysButton.height-6, ILI9341_GREEN);
  tft.setCursor(INNERX+10,INNERY+8);
  tft.setTextColor(ILI9341_BLACK);
  tft.setTextSize(2);
  tft.println("Keys");

  // Update the title bar
  tft.fillRect(MENUX, INNERY+2, INNERWIDTH-TABWIDTH-2, TABHEIGHT-4, ILI9341_BLACK);
  tft.setCursor(MENUX+38, INNERY+8);
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(2);
  tft.println("Required Keys");

  // Display keys screen
  tft.fillRect(MENUX,MENUY, MENUWIDTH, MENUHEIGHT, ILI9341_BLACK);
  // Draw keys
  if(key1) tft.fillRect(MENUX+10,MENUY+29, 50, 80, ILI9341_DARKGREEN); //key1
  tft.drawRect(MENUX+10,MENUY+29, 50, 80, ILI9341_GREEN);
  if(key1) tft.fillRect(MENUX+10,MENUY+10, 25, 20, ILI9341_DARKGREEN);
  tft.drawRect(MENUX+10,MENUY+10, 25, 20, ILI9341_GREEN);

  if(key2) tft.fillRect(MENUX+63,MENUY+29, 50, 80, ILI9341_DARKGREEN); //key2
  tft.drawRect(MENUX+63,MENUY+29, 50, 80, ILI9341_GREEN);
  if(key2) tft.fillRect(MENUX+63,MENUY+10, 25, 20, ILI9341_DARKGREEN);
  tft.drawRect(MENUX+63,MENUY+10, 25, 20, ILI9341_GREEN);

  if(key3) tft.fillRect(MENUX+116,MENUY+29, 50, 80, ILI9341_DARKGREEN); //key3
  tft.drawRect(MENUX+116,MENUY+29, 50, 80, ILI9341_GREEN);
  if(key3) tft.fillRect(MENUX+116,MENUY+10, 25, 20, ILI9341_DARKGREEN);
  tft.drawRect(MENUX+116,MENUY+10, 25, 20, ILI9341_GREEN);

  if(key4) tft.fillRect(MENUX+169,MENUY+29, 50, 80, ILI9341_DARKGREEN); //key4
  tft.drawRect(MENUX+169,MENUY+29, 50, 80, ILI9341_GREEN);
  if(key4) tft.fillRect(MENUX+169,MENUY+10, 25, 20, ILI9341_DARKGREEN);
  tft.drawRect(MENUX+169,MENUY+10, 25, 20, ILI9341_GREEN);

  // Draw decryption percentage
  tft.setCursor(MENUX+10, MENUY+120);
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(2);

  // Draw progress bar
  int keyTotal = 0;
  if(key1) keyTotal++;
  if(key2) keyTotal++;
  if(key3) keyTotal++;
  if(key4) keyTotal++;

  if(keyTotal == 1) {
    tft.fillRect(MENUX+11, MENUY+141, 52, 13, ILI9341_DARKGREEN);
    tft.drawLine(MENUX+63, MENUY+141, MENUX+63, MENUY+154, ILI9341_GREEN);
    tft.println("Decryption: 25%");
  }
  else if(keyTotal == 2) {
    tft.fillRect(MENUX+11, MENUY+141, 104, 13, ILI9341_DARKGREEN);
    tft.drawLine(MENUX+115, MENUY+141, MENUX+115, MENUY+154, ILI9341_GREEN);
    tft.println("Decryption: 50%");
  }
  else if(keyTotal == 3) {
    tft.fillRect(MENUX+11, MENUY+141, 157, 13, ILI9341_DARKGREEN);
    tft.drawLine(MENUX+168, MENUY+141, MENUX+168, MENUY+154, ILI9341_GREEN);
    tft.println("Decryption: 75%");
  }
  else if(keyTotal == 4) {
    tft.fillRect(MENUX+11, MENUY+141, 207, 13, ILI9341_DARKGREEN);
    tft.println("Decryption: 100%");
  }
  else {
    tft.println("Decryption: 0%");
  }
  tft.drawRect(MENUX+10, MENUY+140, 209, 15, ILI9341_GREEN);
}

void GPSScreen() {
  // Unhighlight other tabs and make GPS tab highlighted
  tft.fillRect(keysButton.leftBound+2,keysButton.topBound+2,keysButton.width-4,keysButton.height-4, ILI9341_BLACK);
  tft.fillRect(gpsButton.leftBound+2,gpsButton.topBound+2,gpsButton.width-4,gpsButton.height-4, ILI9341_BLACK);
  tft.fillRect(openButton.leftBound+2,openButton.topBound+2,openButton.width-4,openButton.height-4, ILI9341_BLACK);
  DrawTabs();

  tft.fillRect(gpsButton.leftBound+3,gpsButton.topBound+3,gpsButton.width-6,gpsButton.height-6, ILI9341_GREEN);
  tft.setCursor(INNERX+16,INNERY+TABHEIGHT+6);
  tft.setTextColor(ILI9341_BLACK);
  tft.setTextSize(2);
  tft.println("GPS");

  // Update the title bar
  tft.fillRect(MENUX, INNERY+2, INNERWIDTH-TABWIDTH-2, TABHEIGHT-4, ILI9341_BLACK);
  tft.setCursor(MENUX+42, INNERY+8);
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(2);
  tft.println("GPS Tracking");

  // Display GPS screen
  tft.fillRect(MENUX,MENUY, MENUWIDTH, MENUHEIGHT, ILI9341_BLACK);

  // Draw grid

  #define HSPACING ((MENUWIDTH+0.5f)/10)
  for(int i=1; i<10; i++) { // vertical lines
    tft.drawLine(MENUX+HSPACING*i,MENUY,MENUX+HSPACING*i,MENUY+MENUHEIGHT-1, ILI9341_DARKGREEN);
  }
  #define VSPACING ((MENUHEIGHT+0.5f)/10)
  //#define VSPACING 17
  for(int i=1; i<10; i++) { // horizontal lines
    tft.drawLine(MENUX, MENUY+VSPACING*i, MENUX+MENUWIDTH-1, MENUY+VSPACING*i, ILI9341_DARKGREEN);
  }

  tft.fillCircle(MENUX+(MENUWIDTH/2),MENUY+(MENUHEIGHT/2), 3, ILI9341_CYAN); // user dot

  Serial.print("Menu Size: ");
  Serial.print(MENUWIDTH);
  Serial.print(", ");
  Serial.println(MENUHEIGHT);

}

void OpenScreen() {
  // Unhighlight other tabs and make Open tab highlighted
  tft.fillRect(keysButton.leftBound+2,keysButton.topBound+2,keysButton.width-4,keysButton.height-4, ILI9341_BLACK);
  tft.fillRect(gpsButton.leftBound+2,gpsButton.topBound+2,gpsButton.width-4,gpsButton.height-4, ILI9341_BLACK);
  tft.fillRect(openButton.leftBound+2,openButton.topBound+2,openButton.width-4,openButton.height-4, ILI9341_BLACK);
  DrawTabs();

  tft.fillRect(openButton.leftBound+3,openButton.topBound+3,openButton.width-6,openButton.height-6, ILI9341_GREEN);
  tft.setCursor(INNERX+12,INNERY+(TABHEIGHT*2)+4);
  tft.setTextColor(ILI9341_BLACK);
  tft.setTextSize(2);
  tft.println("Open");
  
  // Update the title bar
  tft.fillRect(MENUX, INNERY+2, INNERWIDTH-TABWIDTH-2, TABHEIGHT-4, ILI9341_BLACK);
  tft.setCursor(MENUX+25, INNERY+8);
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(2);
  tft.println("Unlock Datacell");

  // Display Open screen
  tft.fillRect(MENUX,MENUY, INNERWIDTH-TABWIDTH-2, INNERHEIGHT-TABHEIGHT-2, ILI9341_BLACK);
}

bool touchDown = false;
TS_Point lastPoint;

void loop() {
  // Wait for a touch
  if (ctp.touched()) {
    touchDown = true;
    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);
    p.y = 320 - p.y;
    int tmpX = p.x;
    p.x = p.y;
    p.y = tmpX;

    if(p.x != 0 && p.y != 240) {
      lastPoint = TS_Point(p.x, p.y, 0);
    }

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

    HandleTouch(p);
  }
  else if(touchDown == true) {
    touchDown = false;
    HandleTouchRelease(lastPoint);
  }
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
Loading
ili9341-cap-touch