#include <Adafruit_GFX.h>       
#include <SPI.h>  
#include <Adafruit_ILI9341.h>   
#include <Adafruit_FT6206.h>
// #include <XPT2046_Touchscreen.h>
 
#define TFT_CS    10      
#define TFT_RST   9      
#define TFT_DC    8     


// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206(); // untuk simulasi wokwi
// XPT2046_Touchscreen ctp(CS_PIN);

// Ukuran layar: 240px x 320px
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);


// Warna 
// Website color picker: https://barth-dev.de/online/rgb565-color-picker/
#define GREY 0x52AA
#define DARK_GREY 0x4208
#define RED 0xF800
#define LIGHT_GREEN 0xC714
#define YELLOW 0xDF4F
#define YELLOW_2 0xFF8F
#define ORANGE 0xFC29
#define PINK 0xEB8E
#define LIGHT_RED 0xDB0B

// Menu
int menu = 0;

// Gender
String gender = "man";

// Age
int age = 0;

// Deklarasi menu
void starting_animation();
void main_menu();
void measurement_menu();
void input_age_gender_menu();
void results_menu();
 
void setup() {
  Serial.begin(9600);

  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");

  starting_animation();
}
 
 
void loop(void) {

  TS_Point p = ctp.getPoint();  

  // Serial.print("X: ");
  // Serial.println(p.x);
  // Serial.print("Y: ");
  // Serial.println(p.y);

  switch (menu) {
    case 0:
      main_menu();

      p.x = map(p.x, 0, 240, 240, 0);
      p.y = map(p.y, 0, 320, 320, 0);

      // Daerah tekan tombol start
      if(p.x >= 200 && p.x <= 235 && p.y >= 123 && p.y <= 190){
        menu = 1;
      } 
      break;
    case 1:
      measurement_menu();

      p.x = map(p.x, 0, 240, 240, 0);
      p.y = map(p.y, 0, 320, 320, 0);

      // Tombol Save
      if(p.x >= 206 && p.x <= 240 && p.y >= 13 && p.y <= 87){
        menu = 2;
      } else if(p.x >= 206 && p.x <= 240 && p.y >= 100 && p.y <= 171){ 
        //tombol reset
        // resetValue();
      }

      break;
    case 2:
      input_age_gender_menu();

      p.x = map(p.x, 0, 240, 240, 0);
      p.y = map(p.y, 0, 320, 320, 0);

      // Input gender
      if(p.x >= 115 && p.x <= 135 && p.y >= 285 && p.y <= 308){
        gender = "man";
      } else if(p.x >= 141 && p.x <= 161 && p.y >= 285 && p.y <= 308){
        gender = "woman";
      }

      // Input angka
      if(p.x >= 69 && p.x <= 103 && p.y >= 160 && p.y <= 192){
        // Angka 1
        age = age * 10 + 1;
      } else if(p.x >= 69 && p.x <= 103 && p.y >= 114 && p.y <= 145){
        // Angka 2
        age = age * 10 + 2;
      } else if(p.x >= 69 && p.x <= 103 && p.y >= 68 && p.y <= 102){
        // Angka 3
        age = age * 10 + 3;
      } else if(p.x >= 69 && p.x <= 103 && p.y >= 23 && p.y <= 56){
        // Angka 0
        age = age * 10;
      } else if(p.x >= 115 && p.x <= 142 && p.y >= 160 && p.y <= 192){
        // Angka 4
        age = age * 10 + 4;
      } else if(p.x >= 115 && p.x <= 142 && p.y >= 114 && p.y <= 145){
        // Angka 5
        age = age * 10 + 5;
      } else if(p.x >= 115 && p.x <= 142 && p.y >= 68 && p.y <= 102){
        // Angka 6
        age = age * 10 + 6;
      } else if(p.x >= 115 && p.x <= 142 && p.y >= 23 && p.y <= 56){
        // C
        age = 0;
      } else if(p.x >= 160 && p.x <= 193 && p.y >= 160 && p.y <= 192){
        // Angka 7
        age = age * 10 + 7;
      } else if(p.x >= 160 && p.x <= 193 && p.y >= 114 && p.y <= 145){
        // Angka 8
        age = age * 10 + 8;
      } else if(p.x >= 160 && p.x <= 193 && p.y >= 68 && p.y <= 102){
        // Angka 9
        age = age * 10 + 9;
      } else if(p.x >= 160 && p.x <= 193 && p.y >= 23 && p.y <= 56){
        // S
        // age = age * 10 + 1;
      }

      // Tombol Save
      if(p.x >= 208 && p.x <= 240 && p.y >= 14 && p.y <= 85){
        menu = 3;
      }

      break;
    case 3:
      results_menu();

      p.x = map(p.x, 0, 240, 240, 0);
      p.y = map(p.y, 0, 320, 320, 0);

      // Tombol Restart
      if(p.x >= 194 && p.x <= 226 && p.y >= 11 && p.y <= 112){
        menu = 0;
      } 
      break;
  }
  
}


void starting_animation(){
  tft.setRotation(1);
  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(10, 100);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(3);
  tft.print("Device Started");
  delay(500);

  tft.fillRect(10, 140, 50, 10, ILI9341_BLUE);
  delay(500);
  tft.fillRect(10, 140, 100, 10, ILI9341_BLUE);
  delay(500);
  tft.fillRect(10, 140, 150, 10, ILI9341_BLUE);
  delay(500);
  tft.fillRect(10, 140, 200, 10, ILI9341_BLUE);
  delay(500);
  tft.fillRect(10, 140, 250, 10, ILI9341_BLUE);
  delay(500);

  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(60, 100);
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(3);
  tft.print("Finish...");
  delay(500);

  tft.fillScreen(ILI9341_BLACK);
  tft.fillScreen(DARK_GREY);
  tft.fillRect(20, 10, 280, 70, ILI9341_BLACK);
}
 
void main_menu(){
  tft.setCursor(100, 20);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.print("Smart Body");
  tft.setCursor(100, 50);
  tft.print("Measurement");
  
  tft.setCursor(110, 90);
  tft.print("Group 2");

  tft.setCursor(60, 115);
  tft.print("Michael Theofilos");

  tft.setCursor(70, 135);
  tft.print("Evelyn Valencia");

  tft.setCursor(100, 155);
  tft.print("Viar Ghina");

  tft.setCursor(90, 175);
  tft.print("Hafizh Rifqi");

  // tombol start
  tft.fillRoundRect(130, 200, 70, 30, 10, RED);
  tft.setCursor(135, 210);
  tft.print("Start");
}

void measurement_menu(){
  tft.fillScreen(DARK_GREY);

  tft.fillRect(5, 0, 150, 200, ILI9341_BLACK);
  tft.fillRect(165, 0, 150, 200, ILI9341_BLACK);

  // Indikator Height
  tft.drawRect(15, 55, 125, 20, ILI9341_WHITE);
  tft.setTextSize(1);
  tft.setCursor(15, 45);
  tft.print("0");
  tft.setCursor(40, 45);
  tft.print("50");
  tft.setCursor(65, 45);
  tft.print("100");
  tft.setCursor(95, 45);
  tft.print("150");
  tft.setCursor(125, 45);
  tft.print("200");
  

   

  // Height
  tft.fillRect(15, 120, 130, 30, GREY);
  tft.setTextSize(2);
  tft.setCursor(40, 125);
  tft.print("Height");

  tft.fillRect(30, 160, 70, 30, GREY);
  tft.setCursor(35, 165);
  tft.print("163.5");

  tft.setCursor(115, 165);
  tft.print("CM");

  // Weight
  tft.fillRect(175, 120, 130, 30, GREY);
  tft.setCursor(200, 125);
  tft.print("Weight");

  tft.fillRect(190, 160, 70, 30, GREY);
  tft.setCursor(195, 165);
  tft.print("55");
  
  tft.setCursor(275, 165);
  tft.print("KG");

  // Tombol reset
  tft.fillRoundRect(150, 210, 70, 30, 10, RED);
  tft.setCursor(155, 215);
  tft.print("Reset");

  // Tombol save
  tft.fillRoundRect(235, 210, 70, 30, 10, RED);
  tft.setCursor(240, 215);
  tft.print("Save");

  delay(200000);

}

void input_age_gender_menu(){
  tft.fillScreen(DARK_GREY);

  tft.fillRect(5, 5, 310, 200, ILI9341_BLACK);
  tft.setCursor(30, 15);
  tft.setTextSize(2);
  tft.print("Insert Age and Gender");

  tft.fillRect(35, 45, 60, 50, GREY);
  tft.setCursor(50, 60);
  tft.setTextSize(3);
  tft.print(age);
  
  // Gender
  if(gender == "man"){
    tft.fillCircle(25, 125, 10, GREY);
    tft.fillCircle(25, 125, 5, ILI9341_WHITE);
    tft.setCursor(45, 116);
    tft.setTextSize(2);
    tft.print("MAN");

    tft.fillCircle(25, 150, 10, GREY);
    tft.setCursor(45, 141);
    tft.print("WOMAN");
  } else {
    tft.fillCircle(25, 125, 10, GREY);
    tft.setCursor(45, 116);
    tft.setTextSize(2);
    tft.print("MAN");

    tft.fillCircle(25, 150, 10, GREY);
    tft.fillCircle(25, 150, 5, ILI9341_WHITE);
    tft.setCursor(45, 141);
    tft.print("WOMAN");
  }

  // Number pad
  // Angka 1, 4, 7
  tft.fillRect(130, 70, 30, 30, GREY);
  tft.setCursor(140, 80);
  tft.print("1");

  tft.fillRect(130, 115, 30, 30, GREY);
  tft.setCursor(140, 125);
  tft.print("4");

  tft.fillRect(130, 160, 30, 30, GREY);
  tft.setCursor(140, 170);
  tft.print("7");

  // Angka 2, 5, 8
  tft.fillRect(175, 70, 30, 30, GREY);
  tft.setCursor(185, 80);
  tft.print("2");

  tft.fillRect(175, 115, 30, 30, GREY);
  tft.setCursor(185, 125);
  tft.print("5");

  tft.fillRect(175, 160, 30, 30, GREY);
  tft.setCursor(185, 170);
  tft.print("8");

  // Angka 3, 6, 9
  tft.fillRect(220, 70, 30, 30, GREY);
  tft.setCursor(230, 80);
  tft.print("3");

  tft.fillRect(220, 115, 30, 30, GREY);
  tft.setCursor(230, 125);
  tft.print("6");

  tft.fillRect(220, 160, 30, 30, GREY);
  tft.setCursor(230, 170);
  tft.print("9");

  // 0, C, S
  tft.fillRect(265, 70, 30, 30, GREY);
  tft.setCursor(275, 80);
  tft.print("0");

  tft.fillRect(265, 115, 30, 30, ILI9341_GREEN);
  tft.setCursor(275, 125);
  tft.print("C");

  tft.fillRect(265, 160, 30, 30, RED);
  tft.setCursor(275, 170);
  tft.print("S");

  // Tombol save
  tft.fillRoundRect(235, 210, 70, 30, 10, RED);
  tft.setCursor(240, 215);
  tft.print("Save");

}

void results_menu(){
  tft.fillScreen(DARK_GREY);
  
  tft.setCursor(114, 15);
  tft.setTextSize(2);
  tft.print("RESULTS");

  // BMI
  tft.fillRect(5, 35, 95, 150, ILI9341_BLACK);
  tft.setCursor(30, 45);
  tft.setTextSize(2);
  tft.print("BMI");

  tft.fillRect(15, 90, 75, 75, GREY);
  tft.setCursor(30, 110);
  tft.setTextSize(4);
  tft.print("XX");

  // FMI
  tft.fillRect(110, 35, 95, 150, ILI9341_BLACK);
  tft.setCursor(135, 45);
  tft.setTextSize(2);
  tft.print("FMI");

  tft.fillRect(120, 90, 75, 75, GREY);
  tft.setCursor(135, 110);
  tft.setTextSize(4);
  tft.print("XX");

  // TBW
  tft.fillRect(215, 35, 95, 150, ILI9341_BLACK);
  tft.setCursor(240, 45);
  tft.setTextSize(2);
  tft.print("TBW");

  tft.fillRect(225, 90, 75, 75, GREY);
  tft.setCursor(240, 110);
  tft.setTextSize(4);
  tft.print("XX");

  // Tombol restart
  tft.fillRoundRect(210, 195, 100, 30, 10, RED);
  tft.setCursor(215, 205);
  tft.setTextSize(2);
  tft.print("Restart");

}
Loading
ili9341-cap-touch