#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h> // Capacitive touch screen library
#include <ArduinoJson.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206(); // Capacitive touch screen object
int buttons = -1;
int params = 7;
int buttonTextSize = 2;
void setup() {
Serial.begin(9600);
// Initialize the TFT screen
tft.begin();
Serial.println("WAS IST DAS");
// Initialize the capacitive touch screen
if (!ts.begin(40)) {
Serial.println("Couldn't start touchscreen controller");
while (1);
}
// Draw buttons on the screen
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
loadConfig();
Serial.println("Data Collected!");
}
void loop() {
// Check for touch input
if (ts.touched()) {
TS_Point p = ts.getPoint(); // Get touch point
int xcor = 240 - p.x;
int ycor = 320 - p.y;
// Serial.println(xcor);
if(xcor>15 && ycor>15 && xcor<15+60 && ycor<15+30){
Serial.println("ON pressed");
}
delay(200); // Debounce delay
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void loadConfig() {
bool dataCollected = false;
while (!dataCollected) {
if (Serial.available()) {
String input = Serial.readString();
input.replace("\n", "");
Serial.print("received: ");
Serial.println(input);
if (!input.startsWith("Datasets: ")) {
return;
}
int sets = getValue(input, ':', 1).toInt();
Serial.print("Datasets: ");
Serial.println(sets);
String datasets[sets][params];
int minY = 10;
for (int i=1; i <= sets; i++) {
Serial.print("Set ");
Serial.println(i);
// Declare Data
String name = "";
String action = "none";
int minX = 10;
String color = "green";
bool icon = false;
// Collect data
while (
name == "" ||
action == "" ||
minX == -1 ||
minY == -1 ||
color == ""
) {
while (!Serial.available()) {}
String input = Serial.readString();
input.replace("\n", "");
if (input.startsWith("name: ")) {
name = getValue(input, ':', 1);
name.trim();
Serial.print("Saved name: ");
Serial.println(name);
icon = false;
} else if (input.startsWith("action: ")) {
action = getValue(input, ':', 1);
action.trim();
Serial.print("Saved action: ");
Serial.println(action);
} else if (input.startsWith("icon: ")) {
name = getValue(input, ':', 1);
name.trim();
icon = true;
} else if (input.startsWith("minX: ")) {
minX = getValue(input, ':', 1).toInt();
Serial.print("Saved minX: ");
Serial.println(minX);
} else if (input.startsWith("minY: ")) {
minY = getValue(input, ':', 1).toInt();
Serial.print("Saved minY: ");
Serial.println(minY);
} else if (input.startsWith("color: ")) {
color = getValue(input, ':', 1);
color.trim();
Serial.print("Saved color: ");
Serial.println(color);
} else {
String prop = getValue(input, ':', 0);
Serial.print("unknown property: ");
Serial.println(prop);
}
}
// add Button
addButton(name, minX, minY, color, icon);
minY = minY + 10 + 20*buttonTextSize;
}
dataCollected = true;
}
}
}
void addButton(String text, int minX, int minY, String buttonColor, bool icon)
{
int width = 8*buttonTextSize*text.length();
int height = 20*buttonTextSize;
Serial.println(height);
tft.fillRect(minX, minY, width, height, ILI9341_GREEN); // Start button
int cursorX = minX+(width - (buttonTextSize * 6 * text.length())) / 2;
int cursorY = minY+(height - (buttonTextSize * 8)) / 2;
tft.setCursor(cursorX, cursorY);
tft.setTextSize(buttonTextSize);
tft.print(text);
}Loading
ili9341-cap-touch
ili9341-cap-touch