
First example-based program. RTOS + MBED + TS + LCD + Led
Dependencies: BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed
main.cpp@0:44fcaf1b7f23, 2018-10-08 (annotated)
- Committer:
- MrManlu
- Date:
- Mon Oct 08 10:08:06 2018 +0000
- Revision:
- 0:44fcaf1b7f23
Working test!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MrManlu | 0:44fcaf1b7f23 | 1 | #include "mbed.h" |
MrManlu | 0:44fcaf1b7f23 | 2 | #include "rtos.h" |
MrManlu | 0:44fcaf1b7f23 | 3 | #include "TS_DISCO_F746NG.h" |
MrManlu | 0:44fcaf1b7f23 | 4 | #include "LCD_DISCO_F746NG.h" |
MrManlu | 0:44fcaf1b7f23 | 5 | |
MrManlu | 0:44fcaf1b7f23 | 6 | DigitalOut led1(LED1); |
MrManlu | 0:44fcaf1b7f23 | 7 | LCD_DISCO_F746NG lcd; |
MrManlu | 0:44fcaf1b7f23 | 8 | TS_DISCO_F746NG ts; |
MrManlu | 0:44fcaf1b7f23 | 9 | Thread thread; |
MrManlu | 0:44fcaf1b7f23 | 10 | |
MrManlu | 0:44fcaf1b7f23 | 11 | void led1_thread() { |
MrManlu | 0:44fcaf1b7f23 | 12 | while (true) { |
MrManlu | 0:44fcaf1b7f23 | 13 | led1 = !led1; |
MrManlu | 0:44fcaf1b7f23 | 14 | Thread::wait(1000); |
MrManlu | 0:44fcaf1b7f23 | 15 | } |
MrManlu | 0:44fcaf1b7f23 | 16 | } |
MrManlu | 0:44fcaf1b7f23 | 17 | |
MrManlu | 0:44fcaf1b7f23 | 18 | int main() { |
MrManlu | 0:44fcaf1b7f23 | 19 | thread.start(led1_thread); |
MrManlu | 0:44fcaf1b7f23 | 20 | |
MrManlu | 0:44fcaf1b7f23 | 21 | TS_StateTypeDef TS_State; |
MrManlu | 0:44fcaf1b7f23 | 22 | uint16_t x, y; |
MrManlu | 0:44fcaf1b7f23 | 23 | uint8_t text[30]; |
MrManlu | 0:44fcaf1b7f23 | 24 | uint8_t status; |
MrManlu | 0:44fcaf1b7f23 | 25 | uint8_t idx; |
MrManlu | 0:44fcaf1b7f23 | 26 | uint8_t cleared = 0; |
MrManlu | 0:44fcaf1b7f23 | 27 | uint8_t prev_nb_touches = 0; |
MrManlu | 0:44fcaf1b7f23 | 28 | |
MrManlu | 0:44fcaf1b7f23 | 29 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE); |
MrManlu | 0:44fcaf1b7f23 | 30 | wait(1); |
MrManlu | 0:44fcaf1b7f23 | 31 | |
MrManlu | 0:44fcaf1b7f23 | 32 | status = ts.Init(lcd.GetXSize(), lcd.GetYSize()); |
MrManlu | 0:44fcaf1b7f23 | 33 | if (status != TS_OK) { |
MrManlu | 0:44fcaf1b7f23 | 34 | lcd.Clear(LCD_COLOR_RED); |
MrManlu | 0:44fcaf1b7f23 | 35 | lcd.SetBackColor(LCD_COLOR_RED); |
MrManlu | 0:44fcaf1b7f23 | 36 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MrManlu | 0:44fcaf1b7f23 | 37 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); |
MrManlu | 0:44fcaf1b7f23 | 38 | } else { |
MrManlu | 0:44fcaf1b7f23 | 39 | lcd.Clear(LCD_COLOR_GREEN); |
MrManlu | 0:44fcaf1b7f23 | 40 | lcd.SetBackColor(LCD_COLOR_GREEN); |
MrManlu | 0:44fcaf1b7f23 | 41 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MrManlu | 0:44fcaf1b7f23 | 42 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); |
MrManlu | 0:44fcaf1b7f23 | 43 | } |
MrManlu | 0:44fcaf1b7f23 | 44 | |
MrManlu | 0:44fcaf1b7f23 | 45 | wait(1); |
MrManlu | 0:44fcaf1b7f23 | 46 | lcd.SetFont(&Font12); |
MrManlu | 0:44fcaf1b7f23 | 47 | lcd.SetBackColor(LCD_COLOR_BLUE); |
MrManlu | 0:44fcaf1b7f23 | 48 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MrManlu | 0:44fcaf1b7f23 | 49 | |
MrManlu | 0:44fcaf1b7f23 | 50 | while(1) { |
MrManlu | 0:44fcaf1b7f23 | 51 | |
MrManlu | 0:44fcaf1b7f23 | 52 | ts.GetState(&TS_State); |
MrManlu | 0:44fcaf1b7f23 | 53 | if (TS_State.touchDetected) { |
MrManlu | 0:44fcaf1b7f23 | 54 | // Clear lines corresponding to old touches coordinates |
MrManlu | 0:44fcaf1b7f23 | 55 | if (TS_State.touchDetected < prev_nb_touches) { |
MrManlu | 0:44fcaf1b7f23 | 56 | for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) { |
MrManlu | 0:44fcaf1b7f23 | 57 | lcd.ClearStringLine(idx); |
MrManlu | 0:44fcaf1b7f23 | 58 | } |
MrManlu | 0:44fcaf1b7f23 | 59 | } |
MrManlu | 0:44fcaf1b7f23 | 60 | prev_nb_touches = TS_State.touchDetected; |
MrManlu | 0:44fcaf1b7f23 | 61 | |
MrManlu | 0:44fcaf1b7f23 | 62 | cleared = 0; |
MrManlu | 0:44fcaf1b7f23 | 63 | |
MrManlu | 0:44fcaf1b7f23 | 64 | sprintf((char*)text, "Touches: %d", TS_State.touchDetected); |
MrManlu | 0:44fcaf1b7f23 | 65 | lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE); |
MrManlu | 0:44fcaf1b7f23 | 66 | |
MrManlu | 0:44fcaf1b7f23 | 67 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"LONG LIFE OPEN SOURCE", CENTER_MODE); |
MrManlu | 0:44fcaf1b7f23 | 68 | |
MrManlu | 0:44fcaf1b7f23 | 69 | for (idx = 0; idx < TS_State.touchDetected; idx++) { |
MrManlu | 0:44fcaf1b7f23 | 70 | x = TS_State.touchX[idx]; |
MrManlu | 0:44fcaf1b7f23 | 71 | y = TS_State.touchY[idx]; |
MrManlu | 0:44fcaf1b7f23 | 72 | sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y); |
MrManlu | 0:44fcaf1b7f23 | 73 | lcd.DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE); |
MrManlu | 0:44fcaf1b7f23 | 74 | } |
MrManlu | 0:44fcaf1b7f23 | 75 | |
MrManlu | 0:44fcaf1b7f23 | 76 | lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE); |
MrManlu | 0:44fcaf1b7f23 | 77 | } else { |
MrManlu | 0:44fcaf1b7f23 | 78 | if (!cleared) { |
MrManlu | 0:44fcaf1b7f23 | 79 | lcd.Clear(LCD_COLOR_BLUE); |
MrManlu | 0:44fcaf1b7f23 | 80 | sprintf((char*)text, "Touches: 0"); |
MrManlu | 0:44fcaf1b7f23 | 81 | lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE); |
MrManlu | 0:44fcaf1b7f23 | 82 | cleared = 1; |
MrManlu | 0:44fcaf1b7f23 | 83 | } |
MrManlu | 0:44fcaf1b7f23 | 84 | } |
MrManlu | 0:44fcaf1b7f23 | 85 | } |
MrManlu | 0:44fcaf1b7f23 | 86 | } |