#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>
#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
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Define button dimensions and colors
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 40
#define BUTTON_COLOR ILI9341_BLUE
#define BUTTON_TEXT_COLOR ILI9341_WHITE
#define MAX_SIZE 17 // Maximum size of the array
struct Button {
int16_t x, y;
uint16_t width, height;
uint16_t color;
char label[20];
void (*callback)();
};
Button button1 = {20, 20, 100, 40, ILI9341_RED, "Button 1", NULL};
Button button2 = {20, 80, 100, 40, ILI9341_BLUE, "Button 2", NULL};
int IR_duration[17]; // Define an array of integers
int currentIndex = 0; // Current index to track the position in the array
void setup(void) {
while (!Serial); // used for leonardo debugging
Serial.begin(100000);
Serial.println(F("Hai Yo!"));
pinMode(IR_RECEIVER_PIN, INPUT);
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);
drawButton(button1);
drawButton(button2);
}
void loop() {
unsigned long irDuration = pulseIn(IR_RECEIVER_PIN, HIGH); // Measure the duration of the IR pulse
// Check if an IR pulse was detected
if (irDuration > 0) {
Serial.print("IR pulse duration: ");
Serial.println(irDuration);
appendToduration(irDuration);
//Serial.println(IR_duration);
}
// Wait for a touch
if (! ctp.touched()) {
return;
}
// Retrieve a point
TS_Point p = ctp.getPoint();
// Print out raw data from screen touch controller
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print(" -> ");
// 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);
if (p.z > 0) { // Touch detected
int16_t x = map(p.x, 0, 240, 0, tft.width());
int16_t y = map(p.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(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
//Check if the touch point is within the bounds of any button
if (isTouchWithinButton(p, button1)) {
// Perform action for button 1
Serial.println("Button 1 pressed");
}
else if (isTouchWithinButton(p, button2)) {
// Perform action for button 2
Serial.println("Button 2 pressed");
}
}
void appendToduration(int value) {
if (currentIndex < MAX_SIZE) {
IR_duration[currentIndex] = value; // Insert the new value at the current index
currentIndex++; // Increment the current index for the next insertion
} else {
currentIndex = 0;
}
}
// Function to check if a touch point is within a button
bool isTouchWithinButton(TS_Point point, Button button) {
return (point.x >= button.x && point.x <= (button.x + button.width) &&
point.y >= button.y && point.y <= (button.y + button.height));
}
void drawButton(Button button) {
tft.fillRect(button.x, button.y, button.width, button.height, button.color);
tft.setCursor(button.x + 10, button.y + 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print(button.label);
}