#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#define BEEP_PIN 25
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
bool distressActivated = false;
unsigned long distressStartTime = 0;
bool locationShared = false;
String enteredPin = "";
void setup() {
pinMode(BEEP_PIN, OUTPUT);
Serial.begin(115200);
tft.begin();
ts.begin(40);
tft.setRotation(0);
tft.fillScreen(ILI9341_BLACK);
displayTimeDate();
drawDistressButton();
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
if (!locationShared && isDistressButtonPressed(p.x, p.y)) {
handleDistressButton();
} else if (locationShared && isPinEntryAreaPressed(p.x, p.y)) {
handlePinEntry(p.x, p.y);
}
}
if (distressActivated && (millis() - distressStartTime > 5000)) {
locationShared = true;
distressActivated = false;
tone(BEEP_PIN, 1000, 100);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(60, 70);
tft.print("Live Location");
tft.setCursor(60, 100);
tft.print("Shared");
drawPinEntryArea();
Serial.println("Live location shared. Enter PIN to stop.");
}
}
void displayTimeDate() {
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(75, 20);
tft.print("27-08-24");
tft.setCursor(75, 50);
tft.print("11:11 PM");
tft.setCursor(75, 80);
tft.print("Tuesday");
}
void drawDistressButton() {
tft.fillRect(70, 150, 100, 40, ILI9341_RED);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(75, 160);
tft.print("Distress");
}
bool isDistressButtonPressed(int x, int y) {
return x >= 70 && x <= 170 && y >= 150 && y <= 190;
}
void handleDistressButton() {
if (!distressActivated) {
distressActivated = true;
distressStartTime = millis();
tone(BEEP_PIN, 1000, 100);
tft.fillRect(70, 150, 100, 40, ILI9341_GREEN);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(75, 160);
tft.print("Distress");
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(20, 200);
tft.print("Distress Activated");
tft.setCursor(20, 230);
tft.print("Press Again");
tft.setCursor(20, 260);
tft.print("to Cancel");
Serial.println("Distress activated. Press again to cancel.");
} else {
distressActivated = false;
tone(BEEP_PIN, 1000, 50);
delay(100);
tone(BEEP_PIN, 1000, 50);
tft.fillRect(70, 150, 100, 40, ILI9341_RED);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(80, 160);
tft.print("Distress");
tft.fillRect(0, 200, SCREEN_WIDTH, SCREEN_HEIGHT - 200, ILI9341_BLACK);
Serial.println("Distress signal cancelled.");
}
}
void drawPinEntryArea() {
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(55, 150);
tft.print("Retrieve PIN:");
tft.fillRect(70, 180, 100, 40, ILI9341_WHITE);
tft.drawRect(70, 180, 100, 40, ILI9341_BLACK);
drawNumberPad();
}
bool isPinEntryAreaPressed(int x, int y) {
return x >= 70 && x <= 170 && y >= 180 && y <= 220;
}
void handlePinEntry(int x, int y) {
int pinDigit = getPinDigitFromPad(x, y);
if (pinDigit >= 0 && enteredPin.length() < 4) {
enteredPin += String(pinDigit);
tft.setCursor(75 + enteredPin.length() * 25, 190);
tft.setTextColor(ILI9341_BLACK);
tft.print("*");
if (enteredPin.length() == 4) {
if (enteredPin == "1031") {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.setCursor(20, 100);
tft.print("Location Sharing");
tft.setCursor(20, 130);
tft.print("Stopped.");
Serial.println("Location sharing stopped.");
} else {
tft.setTextColor(ILI9341_RED);
tft.setCursor(20, 230);
tft.print("Wrong PIN!");
tft.setCursor(20, 260);
tft.print("Try Again.");
enteredPin = "";
drawPinEntryArea();
}
}
}
}
void drawNumberPad() {
tft.setTextSize(1);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int num = i * 3 + j + 1;
int x = 40 + j * 60;
int y = 230 + i * 30;
tft.drawRect(x, y, 50, 30, ILI9341_WHITE);
tft.setCursor(x + 20, y + 10);
tft.setTextColor(ILI9341_WHITE);
tft.print(num);
}
}
tft.drawRect(100, 290, 50, 30, ILI9341_WHITE);
tft.setCursor(120, 300);
tft.setTextColor(ILI9341_WHITE);
tft.print("0");
}
int getPinDigitFromPad(int x, int y) {
if (y >= 230 && y < 260) {
if (x >= 40 && x < 90) return 1;
if (x >= 100 && x < 150) return 2;
if (x >= 160 && x < 210) return 3;
} else if (y >= 260 && y < 290) {
if (x >= 40 && x < 90) return 4;
if (x >= 100 && x < 150) return 5;
if (x >= 160 && x < 210) return 6;
} else if (y >= 290 && y < 320) {
if (x >= 40 && x < 90) return 7;
if (x >= 100 && x < 150) return 8;
if (x >= 160 && x < 210) return 9;
} else if (y >= 320 && y < 350) {
if (x >= 100 && x < 150) return 0;
}
return -1;
}
Loading
ili9341-cap-touch
ili9341-cap-touch