In this article we will finally add touch capabilities to our project.
Before You start with this tutorial, You need to finish previous four parts. You can see it here part1, part2, part3 and part4.
We already included the needed .h file:
1 |
#include "stm32746g_discovery_ts.h" |
Next thing we need to do is initialize touch screen. We use function BSP_TS_Init:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* USER CODE BEGIN 2 */ BSP_SDRAM_Init(); /* Initializes the SDRAM device */ if (BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize()) != TS_OK) { BSP_LCD_SetBackColor(LCD_COLOR_WHITE); BSP_LCD_SetTextColor(LCD_COLOR_RED); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 95, (uint8_t *) "ERROR", CENTER_MODE); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 80, (uint8_t *) "Touchscreen cannot be initialized", CENTER_MODE); while (1); } __HAL_RCC_CRC_CLK_ENABLE(); |
In first line we Initialize touch and check if everything is OK. If it is not, the program displays “ERROR” “Touchscreen cannot be initialized” and waits at While loop, othervise it just continues to the next command.
BSP_LCD_GetXSize() and BSP_LCD_GetYSize() are routines to get number of pixels on the LCD.
We also need some kind of variable to store touch data:
1 2 3 |
/* USER CODE BEGIN 1 */ TS_StateTypeDef TS_State; /* USER CODE END 1 */ |
We will to add BSP_TS_ResetTouchData(&TS_State); somewhere to reset touch data. We will this later too, to clear touch data to prevent multiple touch sensing when we only pres LCD once. <- This is my way on solving something like “debounce”.
1 2 3 4 5 6 7 |
GUI_SetColor(GUI_BLACK); GUI_SetPenSize(3); GUI_DrawGradientRoundedV(100, 100, 140, 140, 10, GUI_LIGHTGRAY, GUI_GRAY); GUI_SetPenSize(3); GUI_AA_DrawRoundedRect(100, 100, 140, 140, 10); GUI_DispStringHCenterAt("OK", 120, 110); BSP_TS_ResetTouchData(&TS_State); //we added this line for reseting touch data, all above is just to show where it should be |
To sense touch we need to call routine called BSP_TS_GetState(&TS_State); to get touch data. When I tried just this routine to check for touch I got over 100 touches at one fingertouch, so I needed to figure something out. After reading a lot of documentation I saw that there is a variable inside TS_Touch called touchEventID and one of it’s values is TOUCH_EVENT_PRESS_DOWN and this is the one I used for “debouncing”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ BSP_TS_GetState(&TS_State); if(TS_State.touchEventId[0] == TOUCH_EVENT_PRESS_DOWN) { GUI_Clear(); GUI_SetFont(&GUI_FontComic24B_ASCII); GUI_SetColor(GUI_BLACK); GUI_DispDecAt(TS_State.touchX[0], 120, 20, 5); GUI_DispDecAt(TS_State.touchY[0], 120, 120, 5); BSP_TS_ResetTouchData(&TS_State); HAL_Delay(10); } } /* USER CODE END 3 */ |
If we compile and upload this program, we can now detect touches on LCD. It will write X and Y coordinates of touch. I get 1..479 values for X and 1..271 values for Y. From here we can add command acording to coordinates on display.
This concludes this series of tutorials about STEmWin with Touch Screen on STM32F746G Discovery. And here is the link to zipped complete project.
P.S.: Don’t forget to check the BONUS article!!!
Hi,
I am trying to follow your tutorial to create the project, unfortunately i am not able to make this work. Can you share the whole project?
Hi,
sure, no problem!
Please let me know where is the problem with your project, so that I can add solution here for anyone else having same problem.
Hello,
Thank you for the series, now I have understood the STemWin. Can you make a series about the Ethernet of the F746-DISCO? I am thinking about a MQTT using mbedtls and MQTT Paho library with the F7-DISCO but it seems I haven’t known the use of the Ethernet.
Thank you again,
Best regards,
Duy
Hello!
I haven’t done anything with ethernet on the STM32 yet, so it might take some time, before I do…
Regards!
Hi Slemi, glad that you say so. I’ll be waiting for it.
Best regards!
Hi Slemi,
I used STemWin to display an image (JPG) but it failed, I used the software they provided to convert the image in to C file but the image color when bad on the LCD. Can you help me?
The full discussion is here https://community.st.com/message/181362-re-stemwin-display-jpg?commentID=181362&et=watches.email.thread#comment-181362
Thank you,
Best regards,
I am not yet using JPGs, so I can’t really help now. Hope the forum will have more knowledge.
Regards!
Hi can this work for stm32f769I-discovery board?
You would have to make all the changes for different display and it MIGHT work.