//3.3 for esp, 5v for external voltage soruces 5-14V
//CS to arduino is 10+, connected to esp16 here
//D/C command pin 9+ on arduino esp 17 here
//scl 22, sda 21
//cs,dc, sclk any just define
//does dc and cs have to be 15 and 2? tried 17 16
//size of screen is 240x320
#include "SPI.h"
#include <TFT_eSPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Wire.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>
#define TFT_DC 16
#define TFT_CS 17
// #define TFT_MOSI 23
// #define TFT_SCLK 18
#define BUTTON_SIZE 40
#define BUTTON_SPACING 5
#define SCREEN_X 240
#define SCREEN_Y 320
//NEED TO TRY ESPI VARIANT
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// TFT_eSPI tft = TFT_eSPI();
Adafruit_FT6206 ctp = Adafruit_FT6206();
//button class?
typedef struct {
int id;
int x;
int y;
}Button;
//dynamic memory allocation?
#define MAX_BUTTONS 40
Button buttons[MAX_BUTTONS];
int buttonCount = 0;
void setup() {
tft.begin();
Serial.begin(115200);
Serial.println(F("Cap Touch Paint!"));
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
//drawing some text:
//----------------------------------
// tft.setCursor(26, 120);
// tft.setTextColor(ILI9341_RED);
// tft.setTextSize(3);
// tft.println("Hello, TFT!");
// tft.setCursor(20, 160);
// tft.setTextColor(ILI9341_GREEN);
// tft.setTextSize(2);
// tft.println("I can has colors?");
//-----------------------------------
drawButtons(BUTTON_SIZE, BUTTON_SPACING);
Serial.println("HELLO");
}
void loop() {
if (!ctp.touched()) {
delay(10); // Speeds up the simulation
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(")");
for(int i = 0; i < buttonCount; ++i){
if (p.x >= buttons[i].x && p.x <= buttons[i].x + BUTTON_SIZE &&
p.y >= buttons[i].y && p.y <= buttons[i].y + BUTTON_SIZE) {
Serial.print("Button "); Serial.print(i); Serial.println(" pressed!");
// displayButtonNumber(i);
break;
}
}
}
void drawButtons(int buttonSize, int buttonSpacing){
int x = BUTTON_SPACING;
int y = 0;
buttonCount = 0;
while(1){
//checking if we need to add a new row
if(x + BUTTON_SIZE > SCREEN_X){
Serial.println("new row!");
x = BUTTON_SPACING;
y = y + BUTTON_SPACING + BUTTON_SIZE;
}
//checking if all buttons have been drawn - cannot draw next row
if(y + BUTTON_SIZE > SCREEN_Y){
Serial.println("no more rows!");
break;
}
//drawing a button
tft.fillRect(x, y, BUTTON_SIZE, BUTTON_SIZE, ILI9341_RED);
buttons[buttonCount].x = x;
buttons[buttonCount].y = y;
buttons[buttonCount].id = buttonCount;
buttonCount++;
x = x + BUTTON_SPACING + BUTTON_SIZE;
}
}