Basic demo showing how to use the touch screen present on the DISCO_F746NG board

Dependencies:   BSP_DISCO_F746NG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "stm32746g_discovery_lcd.h"
00003 #include "stm32746g_discovery_ts.h"
00004 
00005 int main()
00006 {
00007     TS_StateTypeDef TS_State;
00008     uint16_t x, y;
00009     uint8_t text[30];
00010     uint8_t status;
00011     uint8_t idx;
00012     uint8_t cleared = 0;
00013     uint8_t prev_nb_touches = 0;
00014 
00015     BSP_LCD_Init();
00016     BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
00017     BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
00018 
00019     BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
00020     HAL_Delay(1000);
00021 
00022     status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
00023     if (status != TS_OK) {
00024         BSP_LCD_Clear(LCD_COLOR_RED);
00025         BSP_LCD_SetBackColor(LCD_COLOR_RED);
00026         BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
00027         BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
00028     } else {
00029         BSP_LCD_Clear(LCD_COLOR_GREEN);
00030         BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
00031         BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
00032         BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
00033     }
00034 
00035     HAL_Delay(1000);
00036     BSP_LCD_SetFont(&Font12);
00037     BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
00038     BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
00039 
00040     while(1) {
00041 
00042         BSP_TS_GetState(&TS_State);
00043         if (TS_State.touchDetected) {
00044             // Clear lines corresponding to old touches coordinates
00045             if (TS_State.touchDetected < prev_nb_touches) {
00046                 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
00047                     BSP_LCD_ClearStringLine(idx);
00048                 }
00049             }
00050             prev_nb_touches = TS_State.touchDetected;
00051 
00052             cleared = 0;
00053 
00054             sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
00055             BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00056 
00057             for (idx = 0; idx < TS_State.touchDetected; idx++) {
00058                 x = TS_State.touchX[idx];
00059                 y = TS_State.touchY[idx];
00060                 sprintf((char*)text, "Touch %d: x=%d y=%d    ", idx+1, x, y);
00061                 BSP_LCD_DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
00062             }
00063 
00064             BSP_LCD_DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
00065         } else {
00066             if (!cleared) {
00067                 BSP_LCD_Clear(LCD_COLOR_BLUE);
00068                 sprintf((char*)text, "Touches: 0");
00069                 BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00070                 cleared = 1;
00071             }
00072         }
00073     }
00074 }