Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.58 KB | None | 0 0
  1.  
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2018 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /** Includes ------------------------------------------------------------------*/
  50.  
  51. #include "main.h"
  52. #include "stm32f1xx_hal.h"
  53. #include "cmsis_os.h"
  54.  
  55. /* USER CODE BEGIN Includes */
  56. #include "atraso.h"
  57. #include "defPrincipais.h"
  58. #include "NOKIA5110_fb.h"
  59. #include "figuras.h"
  60. #include "PRNG_LFSR.h"
  61. #include "sound.h"
  62. /* USER CODE END Includes */
  63.  
  64.  
  65.  
  66. /* Private variables ---------------------------------------------------------*/
  67. ADC_HandleTypeDef hadc1;
  68. DMA_HandleTypeDef hdma_adc1;
  69.  
  70. TIM_HandleTypeDef htim2;
  71. TIM_HandleTypeDef htim3;
  72.  
  73. osThreadId defaultTaskHandle;
  74.  
  75. /* USER CODE BEGIN PV */
  76. /* Private variables ---------------------------------------------------------*/
  77.  
  78. /*typedef struct seta{
  79. int x, y;
  80. struct figura_t* figura;
  81.  
  82. }seta;*/
  83.  
  84.  
  85. struct pontos_t pos_score = {0};
  86. struct pontos_t pos_coracao = {0};
  87. struct pontos_t pos_guindaste = {0};
  88. struct pontos_t pos_plataforma = {0};
  89. struct bloco_t pos_object = {0};
  90. volatile uint8_t flag_points = 0;
  91. volatile uint8_t pace = 0;
  92. volatile uint8_t score;
  93. volatile uint8_t life;
  94. volatile uint8_t vTask_Create = 0;
  95. volatile uint32_t rand_number_position;
  96. volatile uint32_t rand_number_object;
  97. volatile uint32_t joystick_pos[2];
  98. uint8_t aux1 = 0;
  99. uint8_t aux2 = 0;
  100. // variaveis para sincronia da musica
  101. uint32_t pos_musica;
  102. uint32_t pos_musica_play;
  103. uint32_t passo = 1;
  104.  
  105.  
  106.  
  107. volatile uint32_t ADC_buffer[2];
  108. volatile uint32_t valor_ADC[2];
  109. uint32_t counter;
  110.  
  111. //Handlers das tasks utilizadas
  112.  
  113. TaskHandle_t PrintHandle;
  114. TaskHandle_t SoundHandle;
  115. TaskHandle_t RestartHandle;
  116. TaskHandle_t UpdateHandle;
  117. TaskHandle_t JoystickHandle; // Para excluir uma task(que não se auto exclui) é necessário o Handler
  118.  
  119.  
  120. xQueueHandle xSound;
  121.  
  122.  
  123. /* USER CODE END PV */
  124.  
  125. /* Private variables ---------------------------------------------------------*/
  126.  
  127. /* Private function prototypes -----------------------------------------------*/
  128. void SystemClock_Config(void);
  129. static void MX_GPIO_Init(void);
  130. static void MX_DMA_Init(void);
  131. static void MX_ADC1_Init(void);
  132. static void MX_TIM3_Init(void);
  133. static void MX_TIM2_Init(void);
  134. void StartDefaultTask(void const * argument);
  135.  
  136. void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
  137.  
  138.  
  139.  
  140. /* USER CODE BEGIN PFP */
  141. /* Private function prototypes -----------------------------------------------*/
  142.  
  143.  
  144. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc);
  145. void change_note (TIM_HandleTypeDef *htim, int nota);
  146.  
  147. //- Tasks do jogo
  148.  
  149. void vTask_Create_Game (void *parameters);
  150. void vTask_Print (void * parameters);
  151. void vTask_Sound(void *parameters);
  152. void vTask_Joystick(void *parameters);
  153. void vTask_Restart(void *parameters);
  154. void vTask_Update(void *parameters);
  155.  
  156. /* USER CODE END PFP */
  157.  
  158. /* USER CODE BEGIN 0 */
  159.  
  160. /* USER CODE END 0 */
  161.  
  162. /**
  163. * @brief The application entry point.
  164. *
  165. * @retval None
  166. */
  167. int main(void)
  168. {
  169. /* USER CODE BEGIN 1 */
  170.  
  171. /* USER CODE END 1 */
  172.  
  173. /* MCU Configuration----------------------------------------------------------*/
  174.  
  175. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  176. HAL_Init();
  177.  
  178. /* USER CODE BEGIN Init */
  179.  
  180. /* USER CODE END Init */
  181.  
  182. /* Configure the system clock */
  183. SystemClock_Config();
  184.  
  185. /* USER CODE BEGIN SysInit */
  186.  
  187. /* USER CODE END SysInit */
  188.  
  189. /* Initialize all configured peripherals */
  190. MX_GPIO_Init();
  191. MX_DMA_Init();
  192. MX_ADC1_Init();
  193. MX_TIM3_Init();
  194. MX_TIM2_Init();
  195. /* USER CODE BEGIN 2 */
  196.  
  197. //LCD 5110
  198. inic_LCD();
  199. limpa_LCD();
  200.  
  201. //ADC Joystick
  202. HAL_ADC_Start_DMA(&hadc1,(uint32_t*)ADC_buffer,2);
  203. HAL_ADC_Start_IT(&hadc1);
  204.  
  205. //PWM Buzzer
  206. HAL_TIM_Base_Start(&htim3);
  207. HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
  208.  
  209.  
  210. /* USER CODE END 2 */
  211.  
  212. /* USER CODE BEGIN RTOS_MUTEX */
  213. /* add mutexes, ... */
  214. /* USER CODE END RTOS_MUTEX */
  215.  
  216. /* USER CODE BEGIN RTOS_SEMAPHORES */
  217. //xSound = xQueueCreateCountingSemaphore(10, sizeof(long));
  218. // xSound = xQueueCreateCountingSemaphore(16,0);
  219. //xQueueCreateCountingSemaphore()
  220.  
  221. xSound = xQueueCreate(16, sizeof(uint32_t));
  222.  
  223.  
  224.  
  225. /* USER CODE END RTOS_SEMAPHORES */
  226.  
  227. /* USER CODE BEGIN RTOS_TIMERS */
  228. /* start timers, add new ones, ... */
  229. /* USER CODE END RTOS_TIMERS */
  230.  
  231. /* Create the thread(s) */
  232. /* definition and creation of defaultTask */
  233. osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  234. defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
  235.  
  236. /* USER CODE BEGIN RTOS_THREADS */
  237. /* add threads, ... */
  238.  
  239. //Cria a primeira tarefa do jogo que vai criar o resto e depois inicia o RTOS
  240.  
  241. uint32_t vTask_Create = 0;
  242. vTask_Create = xTaskCreate(vTask_Create_Game, "Create", 400, NULL, 3, NULL);
  243.  
  244. if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
  245. limpa_LCD();
  246. string_LCD("There was an error on create game");
  247. imprime_LCD();
  248. while(1){
  249. HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
  250. HAL_Delay(500);
  251. }
  252. }
  253.  
  254. /* USER CODE END RTOS_THREADS */
  255.  
  256. /* USER CODE BEGIN RTOS_QUEUES */
  257. /* add queues, ... */
  258.  
  259.  
  260.  
  261.  
  262. /* USER CODE END RTOS_QUEUES */
  263.  
  264.  
  265. /* Start scheduler */
  266. osKernelStart();
  267.  
  268. /* We should never get here as control is now taken by the scheduler */
  269.  
  270. /* Infinite loop */
  271. /* USER CODE BEGIN WHILE */
  272. while (1)
  273. {
  274.  
  275. /* USER CODE END WHILE */
  276.  
  277. /* USER CODE BEGIN 3 */
  278.  
  279. }
  280. /* USER CODE END 3 */
  281.  
  282. }
  283.  
  284. /**
  285. * @brief System Clock Configuration
  286. * @retval None
  287. */
  288. void SystemClock_Config(void)
  289. {
  290.  
  291. RCC_OscInitTypeDef RCC_OscInitStruct;
  292. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  293. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  294.  
  295. /**Initializes the CPU, AHB and APB busses clocks
  296. */
  297. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  298. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  299. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  300. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  301. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  302. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  303. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  304. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  305. {
  306. _Error_Handler(__FILE__, __LINE__);
  307. }
  308.  
  309. /**Initializes the CPU, AHB and APB busses clocks
  310. */
  311. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  312. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  313. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  314. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  315. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  316. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  317.  
  318. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  319. {
  320. _Error_Handler(__FILE__, __LINE__);
  321. }
  322.  
  323. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  324. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  325. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  326. {
  327. _Error_Handler(__FILE__, __LINE__);
  328. }
  329.  
  330. /**Configure the Systick interrupt time
  331. */
  332. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  333.  
  334. /**Configure the Systick
  335. */
  336. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  337.  
  338. /* SysTick_IRQn interrupt configuration */
  339. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  340. }
  341.  
  342. /* ADC1 init function */
  343. static void MX_ADC1_Init(void)
  344. {
  345.  
  346. ADC_ChannelConfTypeDef sConfig;
  347.  
  348. /**Common config
  349. */
  350. hadc1.Instance = ADC1;
  351. hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  352. hadc1.Init.ContinuousConvMode = ENABLE;
  353. hadc1.Init.DiscontinuousConvMode = DISABLE;
  354. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  355. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  356. hadc1.Init.NbrOfConversion = 2;
  357. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  358. {
  359. _Error_Handler(__FILE__, __LINE__);
  360. }
  361.  
  362. /**Configure Regular Channel
  363. */
  364. sConfig.Channel = ADC_CHANNEL_1;
  365. sConfig.Rank = ADC_REGULAR_RANK_1;
  366. sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
  367. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  368. {
  369. _Error_Handler(__FILE__, __LINE__);
  370. }
  371.  
  372. /**Configure Regular Channel
  373. */
  374. sConfig.Channel = ADC_CHANNEL_2;
  375. sConfig.Rank = ADC_REGULAR_RANK_2;
  376. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  377. {
  378. _Error_Handler(__FILE__, __LINE__);
  379. }
  380.  
  381. }
  382.  
  383. /* TIM2 init function */
  384. static void MX_TIM2_Init(void)
  385. {
  386.  
  387. TIM_MasterConfigTypeDef sMasterConfig;
  388. TIM_OC_InitTypeDef sConfigOC;
  389.  
  390. htim2.Instance = TIM2;
  391. htim2.Init.Prescaler = 0;
  392. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  393. htim2.Init.Period = TIM_COUNT;
  394. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  395. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  396. if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  397. {
  398. _Error_Handler(__FILE__, __LINE__);
  399. }
  400.  
  401. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  402. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  403. if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  404. {
  405. _Error_Handler(__FILE__, __LINE__);
  406. }
  407.  
  408. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  409. sConfigOC.Pulse = 900;
  410. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  411. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  412. if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  413. {
  414. _Error_Handler(__FILE__, __LINE__);
  415. }
  416.  
  417. HAL_TIM_MspPostInit(&htim2);
  418.  
  419. }
  420.  
  421. /* TIM3 init function */
  422. static void MX_TIM3_Init(void)
  423. {
  424.  
  425. TIM_MasterConfigTypeDef sMasterConfig;
  426. TIM_OC_InitTypeDef sConfigOC;
  427.  
  428. htim3.Instance = TIM3;
  429. htim3.Init.Prescaler = 0;
  430. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  431. htim3.Init.Period = TIM_COUNT;
  432. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  433. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  434. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  435. {
  436. _Error_Handler(__FILE__, __LINE__);
  437. }
  438.  
  439. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  440. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  441. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  442. {
  443. _Error_Handler(__FILE__, __LINE__);
  444. }
  445.  
  446. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  447. sConfigOC.Pulse = 900;
  448. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  449. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  450. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  451. {
  452. _Error_Handler(__FILE__, __LINE__);
  453. }
  454.  
  455. HAL_TIM_MspPostInit(&htim3);
  456.  
  457. }
  458.  
  459. /**
  460. * Enable DMA controller clock
  461. */
  462. static void MX_DMA_Init(void)
  463. {
  464. /* DMA controller clock enable */
  465. __HAL_RCC_DMA1_CLK_ENABLE();
  466.  
  467. /* DMA interrupt init */
  468. /* DMA1_Channel1_IRQn interrupt configuration */
  469. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 5, 0);
  470. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  471.  
  472. }
  473.  
  474. /** Configure pins as
  475. * Analog
  476. * Input
  477. * Output
  478. * EVENT_OUT
  479. * EXTI
  480. */
  481. static void MX_GPIO_Init(void)
  482. {
  483.  
  484. GPIO_InitTypeDef GPIO_InitStruct;
  485.  
  486. /* GPIO Ports Clock Enable */
  487. __HAL_RCC_GPIOC_CLK_ENABLE();
  488. __HAL_RCC_GPIOD_CLK_ENABLE();
  489. __HAL_RCC_GPIOA_CLK_ENABLE();
  490. __HAL_RCC_GPIOB_CLK_ENABLE();
  491.  
  492. /*Configure GPIO pin Output Level */
  493. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6
  494. |GPIO_PIN_7, GPIO_PIN_RESET);
  495.  
  496. /*Configure GPIO pin : Botao_Pin */
  497. GPIO_InitStruct.Pin = Button_Pin;
  498. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  499. GPIO_InitStruct.Pull = GPIO_PULLUP;
  500. HAL_GPIO_Init(Button_GPIO_Port, &GPIO_InitStruct);
  501.  
  502. /*Configure GPIO pins : PA3 PA4 PA5 PA6
  503. PA7 */
  504. GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6
  505. |GPIO_PIN_7;
  506. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  507. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  508. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  509.  
  510. }
  511.  
  512. /* USER CODE BEGIN 4 */
  513.  
  514.  
  515. // Protótipo funções
  516. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  517. {
  518. if(hadc->Instance == ADC1)
  519. {
  520. valor_ADC[0]=ADC_buffer[0];
  521. valor_ADC[1]=ADC_buffer[1];
  522. }
  523. }
  524. void change_note (TIM_HandleTypeDef *htim, int nota){
  525. uint32_t prescaler;
  526. if (nota>0)
  527. prescaler = SystemCoreClock/(TIM_COUNT*nota);
  528. else
  529. prescaler =0;
  530. __HAL_TIM_SET_PRESCALER(htim, prescaler);
  531. }
  532.  
  533.  
  534. void vTask_Create_Game(void * parameters){
  535. uint32_t seed = 0;
  536. //Inicialização das posições das figuras basicas e variaveis
  537. score = 0;
  538. life = 3;
  539. pace = 100;
  540. flag_points = 0;
  541. pos_score.x1 = 72;
  542. pos_score.y1 = 8;
  543. pos_guindaste.x1 = 1;
  544. pos_guindaste.y1 = 4;
  545. pos_coracao.x1 = 73;
  546. pos_coracao.y1 = 18;
  547. pos_plataforma.x1 = 30;
  548. pos_plataforma.y1 = 44;
  549. //pos_bloco.x1 = 2;
  550. //pos_bloco.y1 = 10;
  551. //pos_bomba.x1 = 2;
  552. //pos_bomba.y1 = 8;
  553.  
  554. //Tela inicial Game Start + botão de start
  555. while(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin)){ // enquando nao pressionar joystick fica travado
  556. seed++;
  557. limpa_LCD();
  558. escreve2fb(game_start_1);
  559. imprime_LCD();
  560. HAL_Delay(200);
  561. limpa_LCD();
  562. escreve2fb(game_start_2);
  563. imprime_LCD();
  564. HAL_Delay(200);
  565. limpa_LCD();
  566. escreve2fb(game_start_3);
  567. imprime_LCD();
  568. HAL_Delay(200);
  569. }
  570. init_LFSR(seed); // inicializacao para geracao de numeros pseudoaleatorios
  571.  
  572. //Criacao das tarefas
  573. vTask_Create = xTaskCreate(vTask_Print, "Print", 200, NULL,1, &PrintHandle);
  574. if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
  575. limpa_LCD();
  576. string_LCD("There was an error on print");
  577. imprime_LCD();
  578. while(1){
  579. HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
  580. HAL_Delay(500);
  581. }
  582. }
  583. vTask_Create = xTaskCreate(vTask_Joystick, "Joystick", 128, NULL,1, &JoystickHandle);
  584. if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
  585. limpa_LCD();
  586. string_LCD("There was an error on joystick");
  587. imprime_LCD();
  588. while(1){
  589. HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
  590. HAL_Delay(500);
  591. }
  592. }
  593.  
  594. vTask_Create = xTaskCreate(vTask_Update, "Update", 300, NULL,1, &UpdateHandle);
  595. if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
  596. limpa_LCD();
  597. string_LCD("There was an error on update");
  598. imprime_LCD();
  599. while(1){
  600. HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
  601. HAL_Delay(500);
  602. }
  603. }
  604. vTask_Create = xTaskCreate(vTask_Sound, "Sound", 128, NULL,1, &SoundHandle);
  605. if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
  606. limpa_LCD();
  607. string_LCD("There was an error on sound");
  608. imprime_LCD();
  609. while(1){
  610. HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
  611. HAL_Delay(500);
  612. }
  613. }
  614.  
  615. //deleta essa task apos criar o jogo
  616. vTaskDelete(NULL);
  617. while(1){
  618. }
  619. }
  620.  
  621.  
  622. void vTask_Print (void * parameters){
  623. struct pontos_t aux = {0};
  624. while(1){
  625. limpa_LCD();
  626. escreve2fb(background); //desenho do fundo
  627. pos_coracao.x1 = 73; //posicionamento inicial e desenho das vidas
  628. pos_coracao.y1 = 18;
  629. for(int x=0; x < life; x++){
  630. desenha_fig(&pos_coracao, &coracao);
  631. pos_coracao.y1 = pos_coracao.y1 + 10;
  632. }
  633. desenha_fig(&pos_guindaste, &guindaste); //desenho das outras figuras e pontuacao
  634. desenha_fig(&pos_plataforma, &plataforma);
  635.  
  636. if(pos_object.flag_alive == 1 && pos_object.flag_bomb == 1){ //se estiver vivo e flag_bomb for FALSE desenhar bloco
  637. aux.x1 = pos_object.x1;
  638. aux.y1 = pos_object.y1;
  639. desenha_fig(&aux, &bomba);
  640. }
  641. if(pos_object.flag_alive == 1 && pos_object.flag_bomb == 0){ //se estiver vivo e flag_bomb for FALSE desenhar bloco
  642. aux.x1 = pos_object.x1;
  643. aux.y1 = pos_object.y1;
  644. desenha_fig(&aux, &bloco);
  645. }
  646. escreve_Nr_Peq(pos_score.x1,pos_score.y1, score, 0);
  647. imprime_LCD();
  648. vTaskDelay(5);
  649. }
  650. }
  651.  
  652.  
  653. void vTask_Update(void *parameters){
  654. while(1){
  655. //numero randomico
  656.  
  657. if(pos_object.flag_alive == 0 ){
  658. pos_guindaste.x1 = 1; //coloco o guindaste no inicio
  659. pos_guindaste.y1 = 4;
  660.  
  661. rand_number_position = prng_LFSR() % 64;
  662. rand_number_object = prng_LFSR() % 2;
  663. if(rand_number_object == 1){
  664. pos_object.flag_bomb = 1;
  665. pos_object.x1 = 2;
  666. pos_object.y1 = 10;
  667. }
  668. else{
  669. pos_object.flag_bomb = 0;
  670. pos_object.x1 = 2;
  671. pos_object.y1 = 8;
  672. }
  673. pos_object.flag_alive = 1; //cria o objeto
  674. }
  675.  
  676. //checagem do joystick
  677. if(joystick_pos[RIGHT] == TRUE){
  678. pos_plataforma.x1++; //move a plataforma
  679. if(pos_plataforma.x1 > 62){ //checa a colisão com o fim do trilho,
  680. pos_plataforma.x1 = 62;
  681. }
  682. }
  683. if(joystick_pos[LEFT] == TRUE){
  684. pos_plataforma.x1--; //move a plataforma
  685. if(pos_plataforma.x1 < 0){ //checa a colisão com o fim do trilho
  686. pos_plataforma.x1 = 0;
  687. }
  688. }
  689.  
  690. //movimentação das partes do jogo
  691. pos_guindaste.x1++; //move o guindaste
  692. if(pos_guindaste.x1 > 62){ //checa a colisão com o fim do trilho
  693. pos_guindaste.x1 = 1;
  694. }
  695. pos_object.x1++;
  696. //pos_bloco.x1++;
  697. if(pos_object.x1 > rand_number_position){
  698. pos_object.x1 = rand_number_position;
  699. pos_object.y1++;
  700. if((pos_object.y1 == 43) && (pos_object.flag_bomb == 0)){
  701. pos_object.flag_alive = 0;
  702. life--;
  703. }
  704. if((pos_object.y1 == 39) && (pos_object.flag_bomb == 1)){
  705. pos_object.flag_alive = 0;
  706. }
  707.  
  708. }
  709. if((pos_object.y1) < (pos_plataforma.y1+3) && ((pos_object.x1) < (pos_plataforma.x1+5)) && ((pos_plataforma.y1) < (pos_object.y1+8)) && ((pos_plataforma.x1) < (pos_object.x1+7)) && (pos_object.flag_bomb == 1)){
  710. pos_object.flag_alive = 0;
  711. life--;
  712. }
  713. if((pos_object.y1) < (pos_plataforma.y1+3) && ((pos_object.x1) < (pos_plataforma.x1+5)) && ((pos_plataforma.y1) < (pos_object.y1+4)) && ((pos_plataforma.x1) < (pos_object.x1+5)) && (pos_object.flag_bomb == 0)){
  714. score++;
  715. flag_points = 1;
  716. pos_object.flag_alive = 0;
  717. }
  718.  
  719. //checagem das vidas
  720. if(life == 0){
  721. HAL_Delay(200); //pequeno delay antes de resetar
  722. vTask_Create = xTaskCreate(vTask_Restart, "Restart", 128, NULL,2, NULL);
  723. if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
  724. limpa_LCD();
  725. string_LCD("There was an error on Restart");
  726. imprime_LCD();
  727. while(1){
  728. HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
  729. HAL_Delay(500);
  730. }
  731. }
  732. }
  733. if(score == 5 && (flag_points == 1) ){
  734. pace = 50;
  735. vTaskDelay(200);
  736. flag_points =0;
  737. }
  738. if(score == 10 && (flag_points == 1)){
  739. pace = 25;
  740. vTaskDelay(200);
  741. flag_points =0;
  742. }
  743. if(score == 25 && (flag_points == 1)){
  744. pace = 10;
  745. vTaskDelay(200);
  746. flag_points =0;
  747. }
  748. vTaskDelay(pace);
  749. }
  750.  
  751. }
  752.  
  753.  
  754. void vTask_Sound(void *parameters){
  755. /*
  756. int pos;
  757.  
  758. while(1)
  759. {
  760. xQueueReceive(xSound,&pos,osWaitForever);
  761.  
  762.  
  763. //vTaskDelay(MS(60*(48/passo)));
  764.  
  765. change_note(&htim3, song[pos]);
  766.  
  767. vTaskDelay(MS(song[pos+1]));
  768.  
  769. change_note(&htim3, 0);
  770.  
  771. vTaskDelay(MS(3)); //
  772.  
  773. }
  774. */
  775. while(1);
  776. }
  777.  
  778.  
  779. void vTask_Joystick(void *parameters){
  780. while(1){
  781.  
  782.  
  783. if (valor_ADC[0]<min_joystick){
  784. joystick_pos[RIGHT]= TRUE;
  785. } else{
  786. joystick_pos[RIGHT]= FALSE;
  787. }
  788. if (valor_ADC[0]>max_joystick){
  789. joystick_pos[LEFT]= TRUE;
  790. } else{
  791. joystick_pos[LEFT]= FALSE;
  792. }
  793. vTaskDelay(50);
  794. }
  795.  
  796. }
  797.  
  798. void vTask_Restart (void * parameters){
  799.  
  800.  
  801. HAL_Delay(500);
  802. limpa_LCD();
  803. escreve2fb(game_over_1);
  804. imprime_LCD();
  805. HAL_Delay(1000);
  806. limpa_LCD();
  807. escreve2fb(game_over_2);
  808. imprime_LCD();
  809. HAL_Delay(1000);
  810. limpa_LCD();
  811. escreve2fb(game_over_3);
  812. imprime_LCD();
  813. HAL_Delay(1000);
  814. limpa_LCD();
  815. escreve2fb(try_again_screen);
  816. pos_score.x1 = 58;
  817. pos_score.y1 = 13;
  818. escreve_Nr_Peq(pos_score.x1,pos_score.y1, score, 0);
  819. imprime_LCD();
  820. //exclui as tarefas criadas
  821. vTaskDelete(PrintHandle);
  822. vTaskDelete(UpdateHandle);
  823. vTaskDelete(SoundHandle);
  824. vTaskDelete(JoystickHandle);
  825.  
  826. while(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin)){ // enquando nao pressionar joystick fica travado
  827.  
  828.  
  829. }
  830. HAL_Delay(1000);
  831. xTaskCreate(vTask_Create_Game,"Create",150, NULL, 4, NULL); vTaskDelete(NULL);
  832. while(1){
  833. }
  834. }
  835.  
  836. /* USER CODE END 4 */
  837.  
  838. /* StartDefaultTask function */
  839. void StartDefaultTask(void const * argument)
  840. {
  841.  
  842. /* USER CODE BEGIN 5 */
  843. /* Infinite loop */
  844. for(;;)
  845. {
  846. osDelay(1);
  847. }
  848. /* USER CODE END 5 */
  849. }
  850.  
  851. /**
  852. * @brief Period elapsed callback in non blocking mode
  853. * @note This function is called when TIM1 interrupt took place, inside
  854. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  855. * a global variable "uwTick" used as application time base.
  856. * @param htim : TIM handle
  857. * @retval None
  858. */
  859. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  860. {
  861. /* USER CODE BEGIN Callback 0 */
  862.  
  863. /* USER CODE END Callback 0 */
  864. if (htim->Instance == TIM1) {
  865. HAL_IncTick();
  866. }
  867. /* USER CODE BEGIN Callback 1 */
  868.  
  869. /* USER CODE END Callback 1 */
  870. }
  871.  
  872. /**
  873. * @brief This function is executed in case of error occurrence.
  874. * @param file: The file name as string.
  875. * @param line: The line in file as a number.
  876. * @retval None
  877. */
  878. void _Error_Handler(char *file, int line)
  879. {
  880. /* USER CODE BEGIN Error_Handler_Debug */
  881. /* User can add his own implementation to report the HAL error return state */
  882. while(1)
  883. {
  884. }
  885. /* USER CODE END Error_Handler_Debug */
  886. }
  887.  
  888. #ifdef USE_FULL_ASSERT
  889. /**
  890. * @brief Reports the name of the source file and the source line number
  891. * where the assert_param error has occurred.
  892. * @param file: pointer to the source file name
  893. * @param line: assert_param error line source number
  894. * @retval None
  895. */
  896. void assert_failed(uint8_t* file, uint32_t line)
  897. {
  898. /* USER CODE BEGIN 6 */
  899. /* User can add his own implementation to report the file name and line number,
  900. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  901. /* USER CODE END 6 */
  902. }
  903. #endif /* USE_FULL_ASSERT */
  904.  
  905. /**
  906. * @}
  907. */
  908.  
  909. /**
  910. * @}
  911. */
  912.  
  913. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement