2026-05-15 6:35 AM
Hello all,
I'm working on an embedded project based on an STM32F030K6T6 microcontroller.
The MCU is connected to an Adafruit TFT display with an integrated resistive touchscreen. The display is driven via SPI using the ILI9341 controller, while the touchscreen is interfaced using four wires (X+, X−, Y+, Y−).
The resistive touchscreen is read by dynamically configuring the GPIO pins and performing ADC measurements to retrieve raw X and Y coordinates. These raw values are then calibrated and mapped into screen coordinates to detect user interaction with the UI.
The graphical interface consists of four blocks:
- RELAY_1_ON
- RELAY_2_ON
- RELAY_3_ON
- RELAY_4_ON
Each block behaves like a button. When the user touches a block:
- the corresponding GPIO is set HIGH to drive a MOSFET connected to a relay.
When the same block is touched again:
- the GPIO is set LOW, turning the relay OFF.
To handle touch interaction correctly, a simple state machine is implemented to detect press and release events, ensuring that each touch triggers only one toggle action.
Additionally, instead of continuously polling the touchscreen, an external interrupt (EXTI) is used to detect touch events. This allows the MCU to respond efficiently to user input without wasting CPU cycles.
The project is structured into multiple logical blocks for modularity and readability.
I would like to know if this architecture is correct or if there are improvements or issues I should address ( For now i did this part, consider that im a newbie so its not going to be that good )
#include "main.h"
#include <stdint.h>
volatile uint8_t touch_pending = 0; // the program is waiting for the finger to touch the display, i declared it as volatile because obviously its something that can change //
volatile uint8_t touch_active = 0; // if the finger is still on the display //
uint8_t relay_state[4] = {0,0,0,0}; // here i defined the state of 4 relays, since they are multiple i used an array, their state its initially OFF, so i set all to 0 //
typedef struct {
uint16_t x_raw;
uint16_t y_raw;
uint16_t x;
uint16_t y;
} touch_point_t;
touch_point_t tp;
// this structure is used to store touch information //
// x_raw and y_raw contain the ADC values read directly from the touchscreen //
// x and y contain the mapped coordinates relative to the display resolution //
// this allows me to separate raw data from processed data //
uint16_t X_MIN = 200, X_MAX = 3800;
uint16_t Y_MIN = 250, Y_MAX = 3750;
// For the display calibration i had in mind about doing something like this: touching the display at the corners and printing the values which i will set as max and min //
// ======================== //
// UI STATE MACHINE
// ======================== //
typedef enum { UI_IDLE, UI_PRESSED } ui_state_t;
ui_state_t ui_state = UI_IDLE;
int pressed_btn = -1;
// here i defined 2 states for my UI: //
// UI_IDLE means no touch is happening (waiting state) //
// here i initialize the UI state as IDLE //
// so the system starts waiting for a touch //
// UI_PRESSED means the user is pressing the screen //
// this variable stores which button has been pressed //
// -1 means no button is pressed //
// 0 = relay 1 and so on //
----------------------------------------------------------------------------------------
```
// ========================
// PIN DEFINITIONS
// ========================
// TFT
#define TFT_CS_PORT GPIOA
#define TFT_CS_PIN GPIO_PIN_4
#define TFT_DC_PORT GPIOA
#define TFT_DC_PIN GPIO_PIN_2
// TOUCH
#define TP_YP_PORT GPIOA
#define TP_YP_PIN GPIO_PIN_0
#define TP_YM_PORT GPIOA
#define TP_YM_PIN GPIO_PIN_1
#define TP_XP_PORT GPIOB
#define TP_XP_PIN GPIO_PIN_0
#define TP_XM_PORT GPIOB
#define TP_XM_PIN GPIO_PIN_1
// RELAY
#define R_PORT GPIOB
#define R1_PIN GPIO_PIN_4
#define R2_PIN GPIO_PIN_5
#define R3_PIN GPIO_PIN_6
#define R4_PIN GPIO_PIN_7
```
2026-05-15 6:43 AM
Welcome to the forum.
I guess this is the display you're referring to: https://www.adafruit.com/product/2478 ?
Always best to give a link to 3rd-party stuff so that there's no doubt or guessing ...
2026-05-15 6:56 AM
Thanks a lot, yeah exactly this!