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

Dependencies:   BSP_DISCO_F746NG

Touch screen example

Committer:
bcostm
Date:
Wed Oct 07 15:53:19 2015 +0000
Revision:
0:9933f7db9a9b
Child:
3:9f66aabe7b3b
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:9933f7db9a9b 1 #include "mbed.h"
bcostm 0:9933f7db9a9b 2 #include "TS_DISCO_F746NG.h"
bcostm 0:9933f7db9a9b 3 #include "LCD_DISCO_F746NG.h"
bcostm 0:9933f7db9a9b 4
bcostm 0:9933f7db9a9b 5 LCD_DISCO_F746NG lcd;
bcostm 0:9933f7db9a9b 6 TS_DISCO_F746NG ts;
bcostm 0:9933f7db9a9b 7
bcostm 0:9933f7db9a9b 8 int main()
bcostm 0:9933f7db9a9b 9 {
bcostm 0:9933f7db9a9b 10 TS_StateTypeDef TS_State;
bcostm 0:9933f7db9a9b 11 uint16_t x, y;
bcostm 0:9933f7db9a9b 12 uint8_t text[30];
bcostm 0:9933f7db9a9b 13 uint8_t status;
bcostm 0:9933f7db9a9b 14 uint8_t idx;
bcostm 0:9933f7db9a9b 15 uint8_t cleared = 0;
bcostm 0:9933f7db9a9b 16 uint8_t prev_nb_touches = 0;
bcostm 0:9933f7db9a9b 17
bcostm 0:9933f7db9a9b 18 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
bcostm 0:9933f7db9a9b 19 wait(1);
bcostm 0:9933f7db9a9b 20
bcostm 0:9933f7db9a9b 21 status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
bcostm 0:9933f7db9a9b 22 if (status != TS_OK) {
bcostm 0:9933f7db9a9b 23 lcd.Clear(LCD_COLOR_RED);
bcostm 0:9933f7db9a9b 24 lcd.SetBackColor(LCD_COLOR_RED);
bcostm 0:9933f7db9a9b 25 lcd.SetTextColor(LCD_COLOR_WHITE);
bcostm 0:9933f7db9a9b 26 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
bcostm 0:9933f7db9a9b 27 } else {
bcostm 0:9933f7db9a9b 28 lcd.Clear(LCD_COLOR_GREEN);
bcostm 0:9933f7db9a9b 29 lcd.SetBackColor(LCD_COLOR_GREEN);
bcostm 0:9933f7db9a9b 30 lcd.SetTextColor(LCD_COLOR_WHITE);
bcostm 0:9933f7db9a9b 31 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
bcostm 0:9933f7db9a9b 32 }
bcostm 0:9933f7db9a9b 33
bcostm 0:9933f7db9a9b 34 wait(1);
bcostm 0:9933f7db9a9b 35 lcd.SetFont(&Font12);
bcostm 0:9933f7db9a9b 36 lcd.SetBackColor(LCD_COLOR_BLUE);
bcostm 0:9933f7db9a9b 37 lcd.SetTextColor(LCD_COLOR_WHITE);
bcostm 0:9933f7db9a9b 38
bcostm 0:9933f7db9a9b 39 while(1) {
bcostm 0:9933f7db9a9b 40
bcostm 0:9933f7db9a9b 41 ts.GetState(&TS_State);
bcostm 0:9933f7db9a9b 42 if (TS_State.touchDetected) {
bcostm 0:9933f7db9a9b 43 // Clear lines corresponding to old touches coordinates
bcostm 0:9933f7db9a9b 44 if (TS_State.touchDetected < prev_nb_touches) {
bcostm 0:9933f7db9a9b 45 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
bcostm 0:9933f7db9a9b 46 lcd.ClearStringLine(idx);
bcostm 0:9933f7db9a9b 47 }
bcostm 0:9933f7db9a9b 48 }
bcostm 0:9933f7db9a9b 49 prev_nb_touches = TS_State.touchDetected;
bcostm 0:9933f7db9a9b 50
bcostm 0:9933f7db9a9b 51 cleared = 0;
bcostm 0:9933f7db9a9b 52
bcostm 0:9933f7db9a9b 53 sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
bcostm 0:9933f7db9a9b 54 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
bcostm 0:9933f7db9a9b 55
bcostm 0:9933f7db9a9b 56 for (idx = 0; idx < TS_State.touchDetected; idx++) {
bcostm 0:9933f7db9a9b 57 x = TS_State.touchX[idx];
bcostm 0:9933f7db9a9b 58 y = TS_State.touchY[idx];
bcostm 0:9933f7db9a9b 59 sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y);
bcostm 0:9933f7db9a9b 60 lcd.DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
bcostm 0:9933f7db9a9b 61 }
bcostm 0:9933f7db9a9b 62
bcostm 0:9933f7db9a9b 63 lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
bcostm 0:9933f7db9a9b 64 } else {
bcostm 0:9933f7db9a9b 65 if (!cleared) {
bcostm 0:9933f7db9a9b 66 lcd.Clear(LCD_COLOR_BLUE);
bcostm 0:9933f7db9a9b 67 sprintf((char*)text, "Touches: 0");
bcostm 0:9933f7db9a9b 68 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
bcostm 0:9933f7db9a9b 69 cleared = 1;
bcostm 0:9933f7db9a9b 70 }
bcostm 0:9933f7db9a9b 71 }
bcostm 0:9933f7db9a9b 72 }
bcostm 0:9933f7db9a9b 73 }