/**
  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
 ****************************************************/
 /*
Tests the updated floating point plot function
The datum is set to the centre of the 320 x 240 screen
so numbers are printed the middle.

The last test shows the datum point as a red dot.

Normally strings are printed relative to the top left corner but this can be
changed with the setTextDatum() function. The library has #defines for:

TL_DATUM  0 //Top left
TC_DATUM  1 //Top centre
TR_DATUM  2 //Top right
ML_DATUM  3 //Middle left
MC_DATUM  4 //Middle centre
MR_DATUM  5 //Middle right
BL_DATUM  6 //Bottom left
BC_DATUM  7 //Bottom centre
BR_DATUM  8 //Bottom right

 
 Needs fonts 2 and 6

 Make sure all the display driver and pin connections are correct by
 editing the User_Setup.h file in the TFT_eSPI library folder.

 #########################################################################
 ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
 #########################################################################
 */

#include <TFT_eSPI.h>
#include <SPI.h>       // this is needed for display
#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 15
#define TFT_DC 2
#define TFT_MOSI 23
#define TFT_SCLK 18
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library with default width and height

//Fonts
#define AA_FONT_SMALL &FreeSansBold16pt7b
// #define AA_FONT_LARGE &FreeSansBold72pt7b
#define AA_FONT_LARGE &FreeSerifBoldItalic24pt7b


//Colors
uint16_t ConvertRGB( byte R, byte G, byte B)
{
  return ( ((R & 0xF8) << 8) | ((G & 0xFC) << 3) | (B >> 3) );
}
#define EVA_ORANGE ConvertRGB(255, 150, 30)

#define DISPLAY_WIDTH 240
#define DISPLAY_HEIGHT 135

#define COL_W 170
#define COL_H 107
#define LINE_H 25
#define COL_GAP 3

float formatTo1Decimal(float input) {
  return round(input * 10.0) / 10.0;
}

void drawTemp(float cooler_temp, float trg_temp) {
  float c_temp = formatTo1Decimal(cooler_temp);
  float t_temp = formatTo1Decimal(trg_temp);
  tft.drawRect(0, 0, COL_W, COL_H, EVA_ORANGE);
  
  tft.loadFont(AA_FONT_LARGE); // Must load the font first into the sprite class
  tft.setTextColor(TFT_YELLOW, EVA_ORANGE); // Set the sprite font colour and the background colour
  tft.setCursor(0, LINE_H);          // Set the tft cursor position, yes tft position!
  tft.println(c_temp);    // Prints to tft cursor position, tft cursor NOT moved
  tft.unloadFont(); // Remove the font from sprite class to recover memory used
}

void drawInternal() {
  int width = DISPLAY_WIDTH - COL_W - COL_GAP;
  int height = COL_H;
  tft.drawRect(COL_W + COL_GAP, 0,  width, height, EVA_ORANGE);
}

void drawPower() {
  int width = DISPLAY_WIDTH;
  int height =DISPLAY_HEIGHT - COL_GAP - COL_H;
  tft.drawRect(0, 110, DISPLAY_WIDTH, height, EVA_ORANGE);
}

void drawTitle() {
  tft.drawLine(0, LINE_H, COL_W, LINE_H, EVA_ORANGE);
}


void setup(void) {
  Serial.begin(115200);
  
  tft.begin();

  if (! ctp.begin(240)) {  // pass in 'sensitivity' coefficient
    Serial.println("Couldn't start FT6206 touchscreen controller");
    while (1);
  }
  
  tft.fillScreen(ILI9341_BLACK);
  
  // Set rectangle color (16-bit RGB565 format)
  uint16_t rectColor = TFT_RED;

  // Draw the rectangle
  tft.drawRect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, rectColor);
  drawTemp(13.23, 13.42);
  drawTitle();
  drawPower();
  drawInternal();
}

void loop() {
  // Wait for a touch
  
}