
timer
Dependencies: BSP_DISCO_F746NG
main.cpp@0:0e4d7e992e68, 2020-12-09 (annotated)
- Committer:
- iliatumash
- Date:
- Wed Dec 09 11:23:29 2020 +0000
- Revision:
- 0:0e4d7e992e68
timer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iliatumash | 0:0e4d7e992e68 | 1 | #include "mbed.h" |
iliatumash | 0:0e4d7e992e68 | 2 | #include "stm32746g_discovery_lcd.h" |
iliatumash | 0:0e4d7e992e68 | 3 | #include "stm32746g_discovery_ts.h" |
iliatumash | 0:0e4d7e992e68 | 4 | |
iliatumash | 0:0e4d7e992e68 | 5 | int minutes= 2; |
iliatumash | 0:0e4d7e992e68 | 6 | int seconds = 0; |
iliatumash | 0:0e4d7e992e68 | 7 | |
iliatumash | 0:0e4d7e992e68 | 8 | Thread thread0; |
iliatumash | 0:0e4d7e992e68 | 9 | |
iliatumash | 0:0e4d7e992e68 | 10 | void display(){ |
iliatumash | 0:0e4d7e992e68 | 11 | BSP_LCD_Init(); |
iliatumash | 0:0e4d7e992e68 | 12 | BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS); |
iliatumash | 0:0e4d7e992e68 | 13 | BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER); |
iliatumash | 0:0e4d7e992e68 | 14 | BSP_LCD_Clear(LCD_COLOR_BLUE); |
iliatumash | 0:0e4d7e992e68 | 15 | BSP_LCD_SetFont(&LCD_DEFAULT_FONT); |
iliatumash | 0:0e4d7e992e68 | 16 | BSP_LCD_SetBackColor(LCD_COLOR_BLUE); |
iliatumash | 0:0e4d7e992e68 | 17 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
iliatumash | 0:0e4d7e992e68 | 18 | } |
iliatumash | 0:0e4d7e992e68 | 19 | |
iliatumash | 0:0e4d7e992e68 | 20 | void displayInterface(){ |
iliatumash | 0:0e4d7e992e68 | 21 | BSP_LCD_FillRect(20, 120, 80, 80); //tlacitko - |
iliatumash | 0:0e4d7e992e68 | 22 | BSP_LCD_FillRect(380, 120, 80, 80); //tlacito + |
iliatumash | 0:0e4d7e992e68 | 23 | BSP_LCD_FillRect(160, 120, 160, 80); //tlacitko set |
iliatumash | 0:0e4d7e992e68 | 24 | |
iliatumash | 0:0e4d7e992e68 | 25 | BSP_LCD_SetBackColor(LCD_COLOR_WHITE); |
iliatumash | 0:0e4d7e992e68 | 26 | BSP_LCD_SetTextColor(LCD_COLOR_BLACK); |
iliatumash | 0:0e4d7e992e68 | 27 | BSP_LCD_DisplayStringAt(-175, 150, (uint8_t *)"-", CENTER_MODE); |
iliatumash | 0:0e4d7e992e68 | 28 | BSP_LCD_DisplayStringAt(185, 150, (uint8_t *)"+", CENTER_MODE); |
iliatumash | 0:0e4d7e992e68 | 29 | BSP_LCD_DisplayStringAt(0, 150, (uint8_t *)"set", CENTER_MODE); |
iliatumash | 0:0e4d7e992e68 | 30 | } |
iliatumash | 0:0e4d7e992e68 | 31 | |
iliatumash | 0:0e4d7e992e68 | 32 | void printTime() { |
iliatumash | 0:0e4d7e992e68 | 33 | BSP_LCD_Clear(LCD_COLOR_BLUE); |
iliatumash | 0:0e4d7e992e68 | 34 | displayInterface(); |
iliatumash | 0:0e4d7e992e68 | 35 | char buffer[20]; |
iliatumash | 0:0e4d7e992e68 | 36 | sprintf(buffer, "%02d : %02d", minutes, seconds); |
iliatumash | 0:0e4d7e992e68 | 37 | BSP_LCD_SetBackColor(LCD_COLOR_BLUE); |
iliatumash | 0:0e4d7e992e68 | 38 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
iliatumash | 0:0e4d7e992e68 | 39 | BSP_LCD_DisplayStringAt(0, 5, (uint8_t *)buffer, CENTER_MODE); |
iliatumash | 0:0e4d7e992e68 | 40 | } |
iliatumash | 0:0e4d7e992e68 | 41 | |
iliatumash | 0:0e4d7e992e68 | 42 | void increaseSec (){ |
iliatumash | 0:0e4d7e992e68 | 43 | seconds += 10; |
iliatumash | 0:0e4d7e992e68 | 44 | if (seconds > 50){ |
iliatumash | 0:0e4d7e992e68 | 45 | seconds = 0; |
iliatumash | 0:0e4d7e992e68 | 46 | minutes++; |
iliatumash | 0:0e4d7e992e68 | 47 | } |
iliatumash | 0:0e4d7e992e68 | 48 | printTime(); |
iliatumash | 0:0e4d7e992e68 | 49 | } |
iliatumash | 0:0e4d7e992e68 | 50 | |
iliatumash | 0:0e4d7e992e68 | 51 | void decreaseSec (){ |
iliatumash | 0:0e4d7e992e68 | 52 | if (seconds == 0 && minutes != 0){ |
iliatumash | 0:0e4d7e992e68 | 53 | seconds = 50; |
iliatumash | 0:0e4d7e992e68 | 54 | minutes--; |
iliatumash | 0:0e4d7e992e68 | 55 | }else if (seconds > 0 ) { |
iliatumash | 0:0e4d7e992e68 | 56 | seconds -= 10; |
iliatumash | 0:0e4d7e992e68 | 57 | } |
iliatumash | 0:0e4d7e992e68 | 58 | printTime(); |
iliatumash | 0:0e4d7e992e68 | 59 | } |
iliatumash | 0:0e4d7e992e68 | 60 | |
iliatumash | 0:0e4d7e992e68 | 61 | void runTimer() { |
iliatumash | 0:0e4d7e992e68 | 62 | while(seconds + minutes > 0) { |
iliatumash | 0:0e4d7e992e68 | 63 | ThisThread::sleep_for(1000); |
iliatumash | 0:0e4d7e992e68 | 64 | if(seconds == 0) { |
iliatumash | 0:0e4d7e992e68 | 65 | seconds = 59; |
iliatumash | 0:0e4d7e992e68 | 66 | minutes--; |
iliatumash | 0:0e4d7e992e68 | 67 | } |
iliatumash | 0:0e4d7e992e68 | 68 | else { |
iliatumash | 0:0e4d7e992e68 | 69 | seconds--; |
iliatumash | 0:0e4d7e992e68 | 70 | } |
iliatumash | 0:0e4d7e992e68 | 71 | printTime(); |
iliatumash | 0:0e4d7e992e68 | 72 | } |
iliatumash | 0:0e4d7e992e68 | 73 | BSP_LCD_Clear(LCD_COLOR_RED); |
iliatumash | 0:0e4d7e992e68 | 74 | } |
iliatumash | 0:0e4d7e992e68 | 75 | |
iliatumash | 0:0e4d7e992e68 | 76 | void defineButtons(uint16_t x, uint16_t y,uint16_t last_x,uint16_t last_y){ |
iliatumash | 0:0e4d7e992e68 | 77 | if (x!=last_x && y!=last_y) { |
iliatumash | 0:0e4d7e992e68 | 78 | if(x >= 20 && x <= 100 && y >= 120 && y <= 200){decreaseSec();} |
iliatumash | 0:0e4d7e992e68 | 79 | if(x >= 380 && x <= 460 && y >= 120 && y <= 200){increaseSec();} |
iliatumash | 0:0e4d7e992e68 | 80 | if(x >= 160 && x <= 320 && y >= 120 && y <= 200){runTimer();} |
iliatumash | 0:0e4d7e992e68 | 81 | } |
iliatumash | 0:0e4d7e992e68 | 82 | } |
iliatumash | 0:0e4d7e992e68 | 83 | |
iliatumash | 0:0e4d7e992e68 | 84 | void touchScreen(){ |
iliatumash | 0:0e4d7e992e68 | 85 | TS_StateTypeDef TS_State; |
iliatumash | 0:0e4d7e992e68 | 86 | uint16_t x, y; |
iliatumash | 0:0e4d7e992e68 | 87 | uint16_t last_x=0, last_y=0; |
iliatumash | 0:0e4d7e992e68 | 88 | uint8_t status; |
iliatumash | 0:0e4d7e992e68 | 89 | status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize()); |
iliatumash | 0:0e4d7e992e68 | 90 | uint8_t idx; |
iliatumash | 0:0e4d7e992e68 | 91 | while(1) { |
iliatumash | 0:0e4d7e992e68 | 92 | BSP_TS_GetState(&TS_State); |
iliatumash | 0:0e4d7e992e68 | 93 | if (TS_State.touchDetected) { |
iliatumash | 0:0e4d7e992e68 | 94 | x = TS_State.touchX[0]; |
iliatumash | 0:0e4d7e992e68 | 95 | y = TS_State.touchY[0]; |
iliatumash | 0:0e4d7e992e68 | 96 | defineButtons(x,y,last_x,last_y); |
iliatumash | 0:0e4d7e992e68 | 97 | last_x=x; |
iliatumash | 0:0e4d7e992e68 | 98 | last_y=y; |
iliatumash | 0:0e4d7e992e68 | 99 | } |
iliatumash | 0:0e4d7e992e68 | 100 | } |
iliatumash | 0:0e4d7e992e68 | 101 | |
iliatumash | 0:0e4d7e992e68 | 102 | } |
iliatumash | 0:0e4d7e992e68 | 103 | |
iliatumash | 0:0e4d7e992e68 | 104 | int main() |
iliatumash | 0:0e4d7e992e68 | 105 | { |
iliatumash | 0:0e4d7e992e68 | 106 | display(); |
iliatumash | 0:0e4d7e992e68 | 107 | printTime(); |
iliatumash | 0:0e4d7e992e68 | 108 | thread0.start(touchScreen); |
iliatumash | 0:0e4d7e992e68 | 109 | while(1){} |
iliatumash | 0:0e4d7e992e68 | 110 | } |