code source
Dependencies: BSP_DISCO_F746NG SRF05
tactile.cpp@0:8f4c0fdd288b, 2020-06-24 (annotated)
- Committer:
- fisherlpsesam
- Date:
- Wed Jun 24 16:26:23 2020 +0000
- Revision:
- 0:8f4c0fdd288b
code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fisherlpsesam | 0:8f4c0fdd288b | 1 | #include "mbed.h" |
fisherlpsesam | 0:8f4c0fdd288b | 2 | #include "stm32746g_discovery_lcd.h" |
fisherlpsesam | 0:8f4c0fdd288b | 3 | #include "stm32746g_discovery_ts.h" |
fisherlpsesam | 0:8f4c0fdd288b | 4 | #include "SRF05.h" |
fisherlpsesam | 0:8f4c0fdd288b | 5 | #include "logo.h" |
fisherlpsesam | 0:8f4c0fdd288b | 6 | |
fisherlpsesam | 0:8f4c0fdd288b | 7 | |
fisherlpsesam | 0:8f4c0fdd288b | 8 | |
fisherlpsesam | 0:8f4c0fdd288b | 9 | /* |
fisherlpsesam | 0:8f4c0fdd288b | 10 | COULEURS CABLES |
fisherlpsesam | 0:8f4c0fdd288b | 11 | 5V : cable bleu |
fisherlpsesam | 0:8f4c0fdd288b | 12 | ECHO : cable jaune |
fisherlpsesam | 0:8f4c0fdd288b | 13 | TRIGGER : cable marron |
fisherlpsesam | 0:8f4c0fdd288b | 14 | -- NO CONNEXION -- |
fisherlpsesam | 0:8f4c0fdd288b | 15 | GND : cable marron |
fisherlpsesam | 0:8f4c0fdd288b | 16 | */ |
fisherlpsesam | 0:8f4c0fdd288b | 17 | |
fisherlpsesam | 0:8f4c0fdd288b | 18 | |
fisherlpsesam | 0:8f4c0fdd288b | 19 | SRF05 broches (PA_0, PF_9); // assignations des broches |
fisherlpsesam | 0:8f4c0fdd288b | 20 | DigitalOut led(LED1); |
fisherlpsesam | 0:8f4c0fdd288b | 21 | |
fisherlpsesam | 0:8f4c0fdd288b | 22 | void drawImageLogo(int offsetX, int offsetY); |
fisherlpsesam | 0:8f4c0fdd288b | 23 | |
fisherlpsesam | 0:8f4c0fdd288b | 24 | int main() |
fisherlpsesam | 0:8f4c0fdd288b | 25 | { |
fisherlpsesam | 0:8f4c0fdd288b | 26 | TS_StateTypeDef TS_State; |
fisherlpsesam | 0:8f4c0fdd288b | 27 | uint16_t x, y; |
fisherlpsesam | 0:8f4c0fdd288b | 28 | uint8_t text[30]; |
fisherlpsesam | 0:8f4c0fdd288b | 29 | uint8_t status; |
fisherlpsesam | 0:8f4c0fdd288b | 30 | uint8_t idx; |
fisherlpsesam | 0:8f4c0fdd288b | 31 | uint8_t cleared = 0; |
fisherlpsesam | 0:8f4c0fdd288b | 32 | uint8_t prev_nb_touches = 0; |
fisherlpsesam | 0:8f4c0fdd288b | 33 | float mesures, milli, feet; |
fisherlpsesam | 0:8f4c0fdd288b | 34 | |
fisherlpsesam | 0:8f4c0fdd288b | 35 | BSP_LCD_Init(); |
fisherlpsesam | 0:8f4c0fdd288b | 36 | BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS); |
fisherlpsesam | 0:8f4c0fdd288b | 37 | BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER); |
fisherlpsesam | 0:8f4c0fdd288b | 38 | |
fisherlpsesam | 0:8f4c0fdd288b | 39 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 40 | HAL_Delay(1000); |
fisherlpsesam | 0:8f4c0fdd288b | 41 | |
fisherlpsesam | 0:8f4c0fdd288b | 42 | status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize()); |
fisherlpsesam | 0:8f4c0fdd288b | 43 | if (status != TS_OK) { |
fisherlpsesam | 0:8f4c0fdd288b | 44 | BSP_LCD_Clear(LCD_COLOR_RED); |
fisherlpsesam | 0:8f4c0fdd288b | 45 | BSP_LCD_SetBackColor(LCD_COLOR_RED); |
fisherlpsesam | 0:8f4c0fdd288b | 46 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
fisherlpsesam | 0:8f4c0fdd288b | 47 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 48 | } else { |
fisherlpsesam | 0:8f4c0fdd288b | 49 | BSP_LCD_Clear(LCD_COLOR_GREEN); |
fisherlpsesam | 0:8f4c0fdd288b | 50 | BSP_LCD_SetBackColor(LCD_COLOR_GREEN); |
fisherlpsesam | 0:8f4c0fdd288b | 51 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
fisherlpsesam | 0:8f4c0fdd288b | 52 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 53 | } |
fisherlpsesam | 0:8f4c0fdd288b | 54 | |
fisherlpsesam | 0:8f4c0fdd288b | 55 | HAL_Delay(1000); |
fisherlpsesam | 0:8f4c0fdd288b | 56 | BSP_LCD_SetFont(&Font12); |
fisherlpsesam | 0:8f4c0fdd288b | 57 | //BSP_LCD_SetBackColor(LCD_COLOR_BLUE); |
fisherlpsesam | 0:8f4c0fdd288b | 58 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
fisherlpsesam | 0:8f4c0fdd288b | 59 | |
fisherlpsesam | 0:8f4c0fdd288b | 60 | |
fisherlpsesam | 0:8f4c0fdd288b | 61 | while(1) { |
fisherlpsesam | 0:8f4c0fdd288b | 62 | |
fisherlpsesam | 0:8f4c0fdd288b | 63 | // affichage des mesures dans le LCD |
fisherlpsesam | 0:8f4c0fdd288b | 64 | mesures = broches.read(); |
fisherlpsesam | 0:8f4c0fdd288b | 65 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
fisherlpsesam | 0:8f4c0fdd288b | 66 | BSP_LCD_SetFont(&Font20); |
fisherlpsesam | 0:8f4c0fdd288b | 67 | sprintf((char*)text, "valeur de la distance : %5.3f cm", mesures); |
fisherlpsesam | 0:8f4c0fdd288b | 68 | BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)&text, LEFT_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 69 | led = 0; |
fisherlpsesam | 0:8f4c0fdd288b | 70 | |
fisherlpsesam | 0:8f4c0fdd288b | 71 | //affichage des alertes |
fisherlpsesam | 0:8f4c0fdd288b | 72 | BSP_LCD_SetTextColor(LCD_COLOR_RED); |
fisherlpsesam | 0:8f4c0fdd288b | 73 | |
fisherlpsesam | 0:8f4c0fdd288b | 74 | if(mesures < 9.0){ |
fisherlpsesam | 0:8f4c0fdd288b | 75 | BSP_LCD_SetFont(&Font16); |
fisherlpsesam | 0:8f4c0fdd288b | 76 | sprintf((char*)text, "ATTENTION, RISQUE DE COLUSION"); |
fisherlpsesam | 0:8f4c0fdd288b | 77 | BSP_LCD_DisplayStringAt(0, LINE(9), (uint8_t *)&text, CENTER_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 78 | led = 1; // allume la led |
fisherlpsesam | 0:8f4c0fdd288b | 79 | }else if (mesures > 25.0) { |
fisherlpsesam | 0:8f4c0fdd288b | 80 | BSP_LCD_SetFont(&Font16); |
fisherlpsesam | 0:8f4c0fdd288b | 81 | sprintf((char*)text, "ATTENTION, ELOIGNEMENT "); |
fisherlpsesam | 0:8f4c0fdd288b | 82 | BSP_LCD_DisplayStringAt(0, LINE(9), (uint8_t *)&text, CENTER_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 83 | }else{ |
fisherlpsesam | 0:8f4c0fdd288b | 84 | BSP_LCD_SetFont(&Font16); |
fisherlpsesam | 0:8f4c0fdd288b | 85 | sprintf((char*)text, "TOUT VA BIEN "); |
fisherlpsesam | 0:8f4c0fdd288b | 86 | BSP_LCD_DisplayStringAt(0, LINE(9), (uint8_t *)&text, CENTER_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 87 | |
fisherlpsesam | 0:8f4c0fdd288b | 88 | } |
fisherlpsesam | 0:8f4c0fdd288b | 89 | |
fisherlpsesam | 0:8f4c0fdd288b | 90 | |
fisherlpsesam | 0:8f4c0fdd288b | 91 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
fisherlpsesam | 0:8f4c0fdd288b | 92 | BSP_TS_GetState(&TS_State); |
fisherlpsesam | 0:8f4c0fdd288b | 93 | if (TS_State.touchDetected) { |
fisherlpsesam | 0:8f4c0fdd288b | 94 | // Clear lines corresponding to old touches coordinates |
fisherlpsesam | 0:8f4c0fdd288b | 95 | if (TS_State.touchDetected < prev_nb_touches) { |
fisherlpsesam | 0:8f4c0fdd288b | 96 | for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) { |
fisherlpsesam | 0:8f4c0fdd288b | 97 | BSP_LCD_ClearStringLine(idx); |
fisherlpsesam | 0:8f4c0fdd288b | 98 | } |
fisherlpsesam | 0:8f4c0fdd288b | 99 | } |
fisherlpsesam | 0:8f4c0fdd288b | 100 | prev_nb_touches = TS_State.touchDetected; |
fisherlpsesam | 0:8f4c0fdd288b | 101 | |
fisherlpsesam | 0:8f4c0fdd288b | 102 | cleared = 0; |
fisherlpsesam | 0:8f4c0fdd288b | 103 | |
fisherlpsesam | 0:8f4c0fdd288b | 104 | sprintf((char*)text, "Touches: %d", TS_State.touchDetected); |
fisherlpsesam | 0:8f4c0fdd288b | 105 | BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 106 | |
fisherlpsesam | 0:8f4c0fdd288b | 107 | for (idx = 0; idx < TS_State.touchDetected; idx++) { |
fisherlpsesam | 0:8f4c0fdd288b | 108 | x = TS_State.touchX[idx]; |
fisherlpsesam | 0:8f4c0fdd288b | 109 | y = TS_State.touchY[idx]; |
fisherlpsesam | 0:8f4c0fdd288b | 110 | sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y); |
fisherlpsesam | 0:8f4c0fdd288b | 111 | BSP_LCD_DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 112 | } |
fisherlpsesam | 0:8f4c0fdd288b | 113 | |
fisherlpsesam | 0:8f4c0fdd288b | 114 | if ((x>=1&&x<=475)&&(y>=1&&y<=270)) |
fisherlpsesam | 0:8f4c0fdd288b | 115 | { |
fisherlpsesam | 0:8f4c0fdd288b | 116 | milli = mesures*10.0; |
fisherlpsesam | 0:8f4c0fdd288b | 117 | BSP_LCD_SetFont(&Font20); |
fisherlpsesam | 0:8f4c0fdd288b | 118 | sprintf((char*)text, "valeur en millimetres : %5.3f mm", milli); |
fisherlpsesam | 0:8f4c0fdd288b | 119 | BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)&text, LEFT_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 120 | feet = mesures / 30.48; |
fisherlpsesam | 0:8f4c0fdd288b | 121 | sprintf((char*)text, "valeur en feet : %5.3f ft", feet); |
fisherlpsesam | 0:8f4c0fdd288b | 122 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)&text, LEFT_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 123 | |
fisherlpsesam | 0:8f4c0fdd288b | 124 | } |
fisherlpsesam | 0:8f4c0fdd288b | 125 | |
fisherlpsesam | 0:8f4c0fdd288b | 126 | BSP_LCD_DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_RED); |
fisherlpsesam | 0:8f4c0fdd288b | 127 | } else { |
fisherlpsesam | 0:8f4c0fdd288b | 128 | if (!cleared) { |
fisherlpsesam | 0:8f4c0fdd288b | 129 | BSP_LCD_Clear(LCD_COLOR_GREEN); |
fisherlpsesam | 0:8f4c0fdd288b | 130 | drawImageLogo(320,0); // affichage du logo en haut à gauche |
fisherlpsesam | 0:8f4c0fdd288b | 131 | sprintf((char*)text, "Touches: 0"); |
fisherlpsesam | 0:8f4c0fdd288b | 132 | BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE); |
fisherlpsesam | 0:8f4c0fdd288b | 133 | cleared = 1; |
fisherlpsesam | 0:8f4c0fdd288b | 134 | } |
fisherlpsesam | 0:8f4c0fdd288b | 135 | } |
fisherlpsesam | 0:8f4c0fdd288b | 136 | } |
fisherlpsesam | 0:8f4c0fdd288b | 137 | } |
fisherlpsesam | 0:8f4c0fdd288b | 138 | |
fisherlpsesam | 0:8f4c0fdd288b | 139 | // fonction pour le logo |
fisherlpsesam | 0:8f4c0fdd288b | 140 | |
fisherlpsesam | 0:8f4c0fdd288b | 141 | void drawImageLogo(int offsetX, int offsetY) |
fisherlpsesam | 0:8f4c0fdd288b | 142 | { |
fisherlpsesam | 0:8f4c0fdd288b | 143 | int x = 0; |
fisherlpsesam | 0:8f4c0fdd288b | 144 | int y = 0; |
fisherlpsesam | 0:8f4c0fdd288b | 145 | uint32_t* dataPtr = (uint32_t*)logo.data; |
fisherlpsesam | 0:8f4c0fdd288b | 146 | while(y < logo.height) |
fisherlpsesam | 0:8f4c0fdd288b | 147 | { |
fisherlpsesam | 0:8f4c0fdd288b | 148 | while(x < logo.width) |
fisherlpsesam | 0:8f4c0fdd288b | 149 | { |
fisherlpsesam | 0:8f4c0fdd288b | 150 | BSP_LCD_DrawPixel(x + offsetX, y + offsetY, *dataPtr); |
fisherlpsesam | 0:8f4c0fdd288b | 151 | dataPtr++; |
fisherlpsesam | 0:8f4c0fdd288b | 152 | x++; |
fisherlpsesam | 0:8f4c0fdd288b | 153 | } |
fisherlpsesam | 0:8f4c0fdd288b | 154 | x = 0; |
fisherlpsesam | 0:8f4c0fdd288b | 155 | y++; |
fisherlpsesam | 0:8f4c0fdd288b | 156 | } |
fisherlpsesam | 0:8f4c0fdd288b | 157 | } |
fisherlpsesam | 0:8f4c0fdd288b | 158 |