#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#define TFT_CS PA4
#define TFT_DC PA2
#define TFT_RST PA3
#define TOUCH_CS PB0
#define TOUCH_IRQ PB1
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
XPT2046_Touchscreen ts(TOUCH_CS, TOUCH_IRQ);
void drawButton(int x, int y, int w, int h, uint16_t color, const char* text) {
tft.fillRoundRect(x, y, w, h, 10, color);
tft.drawRoundRect(x, y, w, h, 10, ILI9341_WHITE);
tft.setCursor(x + 25, y + 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print(text);
}
void setup() {
Serial.begin(115200);
tft.begin();
ts.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_CYAN);
tft.setTextSize(3);
tft.setCursor(40, 10);
tft.print("STM32 HMI");
drawButton(40, 70, 160, 60, ILI9341_BLUE, "START");
drawButton(40, 150, 160, 60, ILI9341_RED, "STOP");
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
int x = map(p.x, 200, 3800, 0, 320);
int y = map(p.y, 200, 3800, 0, 240);
if (x > 40 && x < 200 && y > 70 && y < 130) {
tft.fillScreen(ILI9341_GREEN);
delay(500);
setup();
}
if (x > 40 && x < 200 && y > 150 && y < 210) {
tft.fillScreen(ILI9341_BLACK);
delay(500);
setup();
}
}
}