The LCD touchscreen used by the MCBSTM32F400 evaluation board is a resistive film giving a resolution of 4000 × 4000 (that is, far greater than the GLCD). This recipe extends touchScreenDemo_c2v0
and illustrates how to use the LCD touchscreen.
Perform the following steps to use the LCD touchscreen:
touchScreenDemo_c2v0
and open the project.touchScreenDemo.c
, and include the following headers:#include <stdio.h> #include "stm32f4xx_hal.h" #include "cmsis_os.h" #include "Driver_I2C.h" #include "Board_GLCD.h" #include "GLCD_Config.h" #include "Board_Touch.h"
// The size of the touch-screen co-ordinates system. #define SCREEN_TS_WIDTH 4000 #define SCREEN_TS_HEIGHT 4000 #define wait_delay HAL_Delay /* Globals */ extern GLCD_FONT GLCD_Font_16x24; /* Function Prototypes */ void screenTransformTS(TOUCH_STATE *ts); void SystemClock_Config(void); void setDisplay(void); void updateDisplay(TOUCH_STATE *tsc_state); void clearDisplay(void);
main()
function:/*-------------------------------------------------- Main function *--------------------------------------------------*/ int main (void) { TOUCH_STATE tsc_state; HAL_Init (); /* Init Hardware Abstraction Layer */ SystemClock_Config (); /* Config Clocks */ Touch_Initialize(); /* Touchscrn Controller Init */ GLCD_Initialize(); /* Graphical Display Init */ setDisplay(); /* Draw GLCD Display */ while (1) { Touch_GetState (&tsc_state); /* Get touch state */ if (tsc_state.pressed) updateDisplay(&tsc_state); else clearDisplay(); wait_delay(100); } }
setDisplay()
function to touchScreenDemo.c
file:/*-------------------------------------------------- setDisplay *--------------------------------------------------*/ void setDisplay( ) { GLCD_SetBackgroundColor (GLCD_COLOR_WHITE); GLCD_ClearScreen (); /* clear the GLCD */ GLCD_SetBackgroundColor (GLCD_COLOR_BLUE); GLCD_SetForegroundColor (GLCD_COLOR_WHITE); GLCD_SetFont (&GLCD_Font_16x24); GLCD_DrawString (0, 0*24, " CORTEX-M4 COOKBOOK "); GLCD_DrawString (0, 1*24, " PACKT Publishing "); GLCD_SetBackgroundColor (GLCD_COLOR_WHITE); GLCD_SetForegroundColor (GLCD_COLOR_BLACK); GLCD_DrawString (0, 3*24, "Touch:"); GLCD_DrawString (0, 4*24, "x :"); GLCD_DrawString (0, 5*24, "y :"); GLCD_DrawString (0, 6*24, "xt :"); GLCD_DrawString (0, 7*24, "yt :"); }
updateDisplay()
function to file touchScreenDemo.c
:/*-------------------------------------------------- updateDisplay *--------------------------------------------------*/ void updateDisplay(TOUCH_STATE *tsc_state) { char buffer[128]; GLCD_SetForegroundColor (GLCD_COLOR_BLACK); GLCD_DrawString (7*16, 3*24, "DETECTED"); sprintf(buffer, "%i ", tsc_state->x); /* raw x_coord */ GLCD_DrawString (7*16, 4*24, buffer); sprintf(buffer, "%i ", tsc_state->y); /* raw y_coord */ GLCD_DrawString (7*16, 5*24, buffer); screenTransformTS(tsc_state); sprintf(buffer, "%i ", tsc_state->x); GLCD_DrawString (7*16, 6*24, buffer); sprintf(buffer, "%i ", tsc_state->y); GLCD_DrawString (7*16, 7*24, buffer); }
clearDisplay()
function to file touchScreenDemo.c
:/*-------------------------------------------------- clearDisplay *--------------------------------------------------*/ void clearDisplay() { GLCD_SetForegroundColor (GLCD_COLOR_LIGHT_GREY); GLCD_DrawString (7*16, 3*24, "DETECTED"); GLCD_DrawString (7*16, 4*24, " "); GLCD_DrawString (7*16, 5*24, " "); GLCD_DrawString (7*16, 6*24, " "); GLCD_DrawString (7*16, 7*24, " "); }
screenTransformTS()
function to file touchScreenDemo.c
:/*-------------------------------------------------- Touch Screen Transform *--------------------------------------------------*/ void screenTransformTS(TOUCH_STATE *ts) { int y = ts->y; int x = ts->x; // Note: co-ordinates are inverted if (x > 0) ts->y = GLCD_HEIGHT - (int)(((double)x / (double)SCREEN_TS_HEIGHT)*(double)GLCD_HEIGHT); if (y > 0) ts->x = (int)(((double)y / (double)SCREEN_TS_WIDTH)*(double)GLCD_WIDTH); }
The Touch_GetState()
function updates the tsc_state
variable, which stores the status of the LCD touchscreen and coordinates. These are stored as a structure that is defined by a typedef
keyword in the Board_Touch.h
file:
/* Touch state */ typedef struct _TOUCH_STATE { int16_t x; ///< Position X int16_t y; ///< Position Y uint8_t pressed; ///< Pressed flag } TOUCH_STATE;
The LCD touchscreen and GLCD coordinate systems are different in resolution and origin. The screenTransformTS( )
function maps between GLCD and touchscreen coordinate systems. Notice how we pass a pointer to the tsc_state
variable and access specific fields such as ts->y
, and so on.
3.145.179.85