How to use the LCD touchscreen

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.

How to do it…

Perform the following steps to use the LCD touchscreen:

  1. Return to touchScreenDemo_c2v0 and open the project.
  2. Use the RTE manager to add Software ComponentBoard Support for the Graphic LCD (in addition to the Touchscreen). Click Resolve and then OK.
  3. Open 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"
  4. Define the following macros, global variables, and function prototypes:
    // 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);
  5. Extend the 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);   
      }
    }
  6. Add the 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   :");
    }
  7. Add the 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);
    }
  8. Add the 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, "        ");
    }
  9. Add the 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);
    }
  10. Check the Use MicroLIB project option.
  11. Build the project, download it, and run the program. The GLCD will display the LCD touchscreen and screen coordinates when touched (refer to the following screenshot):
    How to do it…

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.85