Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_DISCO_F746NG
main.cpp
00001 #include "mbed.h" 00002 #include "stm32746g_discovery_lcd.h" 00003 #include "stm32746g_discovery_ts.h" 00004 #include "Monster1.h" 00005 #include "upbutton.h" 00006 #include "downbutton.h" 00007 00008 #define LCD_FB1_START_ADDRESS (LCD_FB_START_ADDRESS + (480 * 272) * 4) 00009 #define LCD_MEM_ADDR (LCD_FB1_START_ADDRESS + 4 * 480 * 272) 00010 00011 TIM_HandleTypeDef htim3; 00012 DMA2D_HandleTypeDef hdma2d; 00013 00014 uint32_t ramBuffer[ (192 * 32 * 2) / 4]; 00015 00016 int y_offset = 0; 00017 int old_x_offset = 0, old_y_offset = 0; 00018 00019 extern "C" void Init_TimerSensor(void) { 00020 uint32_t prescalerValue = 0; 00021 00022 __HAL_RCC_TIM3_CLK_ENABLE(); 00023 00024 prescalerValue = (uint32_t)(SystemCoreClock / 2 / 10000)-1; 00025 htim3.Instance = TIM3; 00026 htim3.Init.Prescaler = prescalerValue; 00027 htim3.Init.Period = 100 - 1; 00028 htim3.Init.ClockDivision = 0; 00029 htim3.Init.RepetitionCounter = 0; 00030 htim3.Init.CounterMode = TIM_COUNTERMODE_UP; 00031 HAL_TIM_Base_Init(&htim3); 00032 00033 HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0); 00034 00035 HAL_NVIC_EnableIRQ(TIM3_IRQn); 00036 00037 HAL_TIM_Base_Start_IT(&htim3); 00038 } 00039 00040 extern "C" void TIM3_IRQHandler(void) { 00041 HAL_TIM_IRQHandler(&htim3); 00042 } 00043 00044 extern "C" void EXTI15_10_IRQHandler(void) { 00045 HAL_GPIO_EXTI_IRQHandler(TS_INT_PIN); 00046 } 00047 00048 void DMA2D_Config(void); 00049 void FillRectWithColor(uint32_t color, uint32_t dst_addr, int x, int y, int width, int height); 00050 void MoveTileToMemory(uint32_t source_addr, uint32_t dest_addr, int width, int height); 00051 00052 int main() 00053 { 00054 HAL_Init(); 00055 00056 TS_StateTypeDef TS_State; 00057 uint16_t x, y; 00058 uint8_t text[30]; 00059 uint8_t status; 00060 uint8_t idx; 00061 uint8_t cleared = 0; 00062 uint8_t prev_nb_touches = 0; 00063 00064 BSP_LCD_Init(); 00065 BSP_LCD_LayerDefaultInit(0, LCD_FB1_START_ADDRESS); // init background layer address 00066 00067 BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS); 00068 BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER); 00069 BSP_LCD_SetColorKeying(1, 0xffffffff); 00070 00071 BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE); 00072 HAL_Delay(1000); 00073 00074 status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize()); 00075 if (status != TS_OK) { 00076 BSP_LCD_Clear(LCD_COLOR_RED); 00077 BSP_LCD_SetBackColor(LCD_COLOR_RED); 00078 BSP_LCD_SetTextColor(LCD_COLOR_WHITE); 00079 BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); 00080 } else { 00081 //BSP_LCD_Clear(LCD_COLOR_GREEN); 00082 BSP_LCD_SetBackColor(LCD_COLOR_GREEN); 00083 BSP_LCD_SetTextColor(LCD_COLOR_WHITE); 00084 //BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); 00085 } 00086 00087 if (BSP_TS_ITConfig() != TS_OK) { 00088 BSP_LCD_Clear(LCD_COLOR_RED); 00089 BSP_LCD_SetBackColor(LCD_COLOR_RED); 00090 BSP_LCD_SetTextColor(LCD_COLOR_WHITE); 00091 BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INTERRUPT FAIL", CENTER_MODE); 00092 } 00093 00094 HAL_Delay(1000); 00095 BSP_LCD_SetFont(&Font12); 00096 BSP_LCD_SetBackColor(LCD_COLOR_BLUE); 00097 BSP_LCD_SetTextColor(LCD_COLOR_WHITE); 00098 00099 //Init_TimerSensor(); 00100 00101 // load image into memory 00102 DMA2D_Config(); 00103 HAL_DMA2D_Start(&hdma2d, (uint32_t)&pic_Dude_Monster_Walk_6_bmp, (uint32_t)&ramBuffer, 192, 32); 00104 while(HAL_DMA2D_PollForTransfer(&hdma2d, 1000) != HAL_OK); 00105 00106 HAL_DMA2D_Start(&hdma2d, (uint32_t)&down_button_image_array, LCD_MEM_ADDR, 64, 64); 00107 while(HAL_DMA2D_PollForTransfer(&hdma2d, 1000) != HAL_OK); 00108 HAL_DMA2D_Start(&hdma2d, (uint32_t)&up_button_image_array, LCD_MEM_ADDR + 64*64*2, 64, 64); 00109 while(HAL_DMA2D_PollForTransfer(&hdma2d, 1000) != HAL_OK); 00110 00111 FillRectWithColor(0xff00ff00, LCD_FB_START_ADDRESS, 0, 0, 480, 272); 00112 HAL_DMA2D_Start(&hdma2d, 0xff00ff00, LCD_FB1_START_ADDRESS, 480, 272); 00113 while(HAL_DMA2D_PollForTransfer(&hdma2d, 1000) != HAL_OK); 00114 00115 00116 hdma2d.Init.Mode = DMA2D_M2M_PFC; 00117 hdma2d.Init.ColorMode = DMA2D_ARGB8888; 00118 hdma2d.Init.OutputOffset = 480 - 64; 00119 hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA; 00120 hdma2d.LayerCfg[1].InputAlpha = 0xff; 00121 hdma2d.LayerCfg[1].InputColorMode = CM_RGB565; 00122 hdma2d.LayerCfg[1].InputOffset = 0; 00123 HAL_DMA2D_Init(&hdma2d); 00124 HAL_DMA2D_ConfigLayer(&hdma2d, 1); 00125 HAL_DMA2D_Start(&hdma2d, LCD_MEM_ADDR, LCD_FB_START_ADDRESS + 240 * 4 + 480 * 200 * 4, 64, 64); 00126 while(HAL_DMA2D_PollForTransfer(&hdma2d, 1000) != HAL_OK); 00127 HAL_DMA2D_Start(&hdma2d, LCD_MEM_ADDR + 64*64*2, LCD_FB_START_ADDRESS + 240 * 4 + 480 * 136 * 4, 64, 64); 00128 while(HAL_DMA2D_PollForTransfer(&hdma2d, 1000) != HAL_OK); 00129 00130 int counter = 0; 00131 int xpos_offset = 0; 00132 int dir = 0; 00133 00134 while(1) { 00135 HAL_Delay(50); 00136 // fill all area 00137 FillRectWithColor(0xff00ff00, LCD_FB_START_ADDRESS, 0, 0, 480, 100); 00138 00139 hdma2d.Init.Mode = DMA2D_M2M_PFC; 00140 hdma2d.Init.ColorMode = DMA2D_ARGB8888; 00141 hdma2d.Init.OutputOffset = 480 - 32; 00142 hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA; 00143 hdma2d.LayerCfg[1].InputAlpha = 0xff; 00144 hdma2d.LayerCfg[1].InputColorMode = CM_RGB565; 00145 hdma2d.LayerCfg[1].InputOffset = 192 - 32; 00146 HAL_DMA2D_Init(&hdma2d); 00147 HAL_DMA2D_ConfigLayer(&hdma2d, 1); 00148 //HAL_DMA2D_Start(&hdma2d, (uint32_t)&ramBuffer, LCD_FB_START_ADDRESS + offset + xpos_offset, 150, 150); 00149 HAL_DMA2D_Start(&hdma2d, ((uint32_t)&ramBuffer) + counter * 64, LCD_FB_START_ADDRESS + xpos_offset * 4 + y_offset * 480 * 4, 32, 32); 00150 while(HAL_DMA2D_PollForTransfer(&hdma2d, 1000) != HAL_OK); 00151 if (dir == 0) { 00152 old_x_offset = xpos_offset; 00153 xpos_offset++; 00154 if (xpos_offset == 480-32) dir = 1; 00155 } 00156 else { 00157 old_x_offset = xpos_offset; 00158 xpos_offset--; 00159 if (xpos_offset == 0) dir = 0; 00160 } 00161 counter++; 00162 if (counter == 6) 00163 counter = 0; 00164 00165 /*BSP_TS_GetState(&TS_State); 00166 if (TS_State.touchDetected) { 00167 // Clear lines corresponding to old touches coordinates 00168 if (TS_State.touchDetected < prev_nb_touches) { 00169 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) { 00170 BSP_LCD_ClearStringLine(idx); 00171 } 00172 } 00173 prev_nb_touches = TS_State.touchDetected; 00174 00175 cleared = 0; 00176 00177 sprintf((char*)text, "Touches: %d", TS_State.touchDetected); 00178 BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE); 00179 00180 for (idx = 0; idx < TS_State.touchDetected; idx++) { 00181 x = TS_State.touchX[idx]; 00182 y = TS_State.touchY[idx]; 00183 sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y); 00184 BSP_LCD_DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE); 00185 } 00186 00187 BSP_LCD_DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE); 00188 } else { 00189 if (!cleared) { 00190 BSP_LCD_Clear(LCD_COLOR_BLUE); 00191 sprintf((char*)text, "Touches: 0"); 00192 BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE); 00193 cleared = 1; 00194 } 00195 }*/ 00196 } 00197 } 00198 00199 extern "C" void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { 00200 TS_StateTypeDef ts; 00201 BSP_TS_GetState(&ts); 00202 if (ts.touchDetected) { 00203 int x = ts.touchX[0]; 00204 int y = ts.touchY[0]; 00205 if (x > 240 && y > 136 && x < 304 && y < 200 && y_offset > 0) { 00206 y_offset -= 2; 00207 } 00208 else if (x > 240 && y > 200 && x < 304 && y < 264 && y_offset < 72) { 00209 y_offset += 2; 00210 } 00211 } 00212 } 00213 00214 extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { 00215 TS_StateTypeDef ts; 00216 BSP_TS_GetState(&ts); 00217 if (ts.touchDetected) { 00218 int x = ts.touchX[0]; 00219 int y = ts.touchY[0]; 00220 if (x > 240 && y > 136 && x < 304 && y < 200 && y_offset > 0) { 00221 old_y_offset = y_offset; 00222 y_offset -= 2; 00223 } 00224 else if (x > 240 && y > 200 && x < 304 && y < 264 && y_offset < 72) { 00225 old_y_offset = y_offset; 00226 y_offset += 2; 00227 } 00228 } 00229 } 00230 00231 void DMA2D_Config(void) { 00232 hdma2d.Instance = DMA2D; 00233 hdma2d.Init.Mode = DMA2D_M2M; 00234 hdma2d.Init.OutputOffset = 0; 00235 hdma2d.Init.ColorMode = DMA2D_RGB565; 00236 00237 hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA; 00238 hdma2d.LayerCfg[1].InputAlpha = 0xff; 00239 hdma2d.LayerCfg[1].InputOffset = 0; 00240 hdma2d.LayerCfg[1].InputColorMode = CM_RGB565; 00241 00242 HAL_DMA2D_Init(&hdma2d); 00243 HAL_DMA2D_ConfigLayer(&hdma2d, 1); 00244 } 00245 00246 void FillRectWithColor(uint32_t color, uint32_t dst_addr, int x, int y, int width, int height) { 00247 hdma2d.Init.Mode = DMA2D_R2M; 00248 hdma2d.Init.ColorMode = DMA2D_ARGB8888; 00249 hdma2d.Init.OutputOffset = 480 - width; 00250 HAL_DMA2D_Init(&hdma2d); 00251 HAL_DMA2D_Start(&hdma2d, color, dst_addr + x * 4 + y * 480 * 4, width, height); 00252 while(HAL_DMA2D_PollForTransfer(&hdma2d, 1000) != HAL_OK); 00253 } 00254 00255 void MoveTileToMemory(uint32_t source_addr, uint32_t dest_addr, int width, int height) { 00256 // apply necessary changes 00257 //HAL_DMA2D_Start(&hdma2d, (uint32_t)&ramBuffer, LCD_FB_START_ADDRESS, 32, 32); 00258 } 00259
Generated on Thu Sep 29 2022 10:24:14 by
1.7.2