#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>
#include <IRremote.h>
#include <EEPROM.h>
#define IR_RECEIVER_PIN 3 // Pin connected to the IR receiver module
// 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 9
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Define button dimensions and colors
#define NumButton_width ((tft.width())/5)
#define NumButton_height ((tft.width())/5)
#define BUTTON_COLOR ILI9341_BLUE;
#define BUTTON_TEXT_COLOR ILI9341_WHITE
#define MAX_ARRAY_SIZE 17 // Maximum size of the array
String current_page = "Dial";
bool screen_needs_update = true;
struct NumButton {
int16_t x, y;
uint16_t width, height;
uint16_t back_color;
uint16_t content_color;
char label[50];
void (*callback)();
};
NumButton button_num1 = {(((tft.width())*(0.3))*0) + ((tft.width())/10), (((tft.height())/4)*0) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "1", NULL};
NumButton button_num2 = {(((tft.width())*(0.3))*1) + ((tft.width())/10), (((tft.height())/4)*0) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "2", NULL};
NumButton button_num3 = {(((tft.width())*(0.3))*2) + ((tft.width())/10), (((tft.height())/4)*0) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "3", NULL};
NumButton button_num4 = {(((tft.width())*(0.3))*0) + ((tft.width())/10), (((tft.height())/4)*1) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "4", NULL};
NumButton button_num5 = {(((tft.width())*(0.3))*1) + ((tft.width())/10), (((tft.height())/4)*1) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "5", NULL};
NumButton button_num6 = {(((tft.width())*(0.3))*2) + ((tft.width())/10), (((tft.height())/4)*1) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "6", NULL};
NumButton button_num7 = {(((tft.width())*(0.3))*0) + ((tft.width())/10), (((tft.height())/4)*2) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "7", NULL};
NumButton button_num8 = {(((tft.width())*(0.3))*1) + ((tft.width())/10), (((tft.height())/4)*2) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "8", NULL};
NumButton button_num9 = {(((tft.width())*(0.3))*2) + ((tft.width())/10), (((tft.height())/4)*2) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "9", NULL};
NumButton button_num0 = {(((tft.width())*(0.3))*1) + ((tft.width())/10), (((tft.height())/4)*3) + ((tft.height())/10), NumButton_width, NumButton_height, 0xFFFF, 0x0000, "0", NULL};
void setup(void) {
while (!Serial); // used for leonardo debugging
Serial.begin(9600);
Serial.println(F("Cap Touch Paint!"));
pinMode(IR_RECEIVER_PIN, INPUT);
IrReceiver.begin(IR_RECEIVER_PIN);
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.fillScreen(ILI9341_BLACK);
}
void loop() {
TS_Point* pointPtr; // Declare a pointer to TS_Point
TS_Point p = ctp.getPoint(); // Retrieve a point
pointPtr = &p; // Assign the address of p to the pointer
unsigned long incomm = IrReceiver.decodedIRData.command;
if (IrReceiver.decode()) {
receiveIR(&incomm);
storeIR(incomm);
}
if (screen_needs_update == true) {
if (current_page == "Dial") {
draw_DialPage();
screen_needs_update = false;
}
}
// Wait for a touch
if (! ctp.touched()) {
return;
}
get_coordinates(&p);
//Check if the touch point is within the bounds of any button
if (isTouchWithinButton(&p, button_num1)) {
// Perform action for button 1
Serial.println("Button 1 pressed");
receiveIR(incomm);
storeIR(incomm);
}
else if (isTouchWithinButton(&p, button_num2)) {
// Perform action for button 2
Serial.println("Button 2 pressed");
Serial.println(incomm);
}
}
void draw_NumButton(NumButton button) {
uint8_t content_size = 3;
uint16_t content_width = strlen(button.label) * 5 * content_size; // Rough estimation of text width
uint16_t content_height = 8 * content_size;
tft.fillRect(button.x, button.y, button.width, button.height, button.back_color);
tft.setCursor(button.x + ( (NumButton_width - content_width) /2), button.y + ( (NumButton_height - content_height) /2) );
tft.setTextColor(button.content_color);
tft.setTextSize(3);
tft.print(button.label);
}
void draw_DialPage() {
draw_NumButton(button_num1);
draw_NumButton(button_num2);
draw_NumButton(button_num3);
draw_NumButton(button_num4);
draw_NumButton(button_num5);
draw_NumButton(button_num6);
draw_NumButton(button_num7);
draw_NumButton(button_num8);
//draw_NumButton(button_num9);
//draw_NumButton(button_num0);
}
void get_coordinates(TS_Point *pointPtr) {
// Print out raw data from screen touch controller
//Serial.print("X = "); Serial.print(pointPtr->x);
//Serial.print("\tY = "); Serial.print(pointPtr->y);
//Serial.print(" -> ");
// flip it around to match the screen.
pointPtr->x = map(pointPtr->x, 0, 240, 240, 0);
pointPtr->y = map(pointPtr->y, 0, 320, 320, 0);
if (pointPtr->z > 0) { // Touch detected
int16_t x = map(pointPtr->x, 0, 240, 0, tft.width());
int16_t y = map(pointPtr->y, 0, 320, 0, tft.height());
if (x >= 10 && x <= 70 && y >= 10 && y <= 40) {
Serial.println("Button 1 pressed!");
}
else if (x >= 90 && x <= 150 && y >= 10 && y <= 40) {
Serial.println("Button 2 pressed!");
}
}
// Print out the remapped (rotated) coordinates
//Serial.print("("); Serial.print(pointPtr->x);
//Serial.print(", "); Serial.print(pointPtr->y);
//Serial.println(")");
}
// Function to check if a touch point is within a button
bool isTouchWithinButton(TS_Point *point, NumButton button) {
return (point->x >= button.x && point->x <= (button.x + button.width) &&
point->y >= button.y && point->y <= (button.y + button.height));
}
unsigned long receiveIR(unsigned long *incomm) {
if (IrReceiver.decode()) {
Serial.println(*incomm);
IrReceiver.resume();
return *incomm;
}
}
void storeIR(unsigned long data) {
// Write data to EEPROM
for (uint8_t i = 0; i < sizeof(data); i++) {
EEPROM.write(i, (data >> (8 * i)) & 0xFF);
}
}Loading
ili9341-cap-touch
ili9341-cap-touch