Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * Copyright (c) 2018 STMicroelectronics International N.V.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted, provided that the following conditions are met:
- *
- * 1. Redistribution of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of other
- * contributors to this software may be used to endorse or promote products
- * derived from this software without specific written permission.
- * 4. This software, including modifications and/or derivative works of this
- * software, must execute solely and exclusively on microcontroller or
- * microprocessor devices manufactured by or for STMicroelectronics.
- * 5. Redistribution and use of this software other than as permitted under
- * this license is void and will automatically terminate your rights under
- * this license.
- *
- * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
- * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
- * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /** Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f1xx_hal.h"
- #include "cmsis_os.h"
- /* USER CODE BEGIN Includes */
- #include "atraso.h"
- #include "defPrincipais.h"
- #include "NOKIA5110_fb.h"
- #include "figuras.h"
- #include "PRNG_LFSR.h"
- #include "sound.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc1;
- DMA_HandleTypeDef hdma_adc1;
- TIM_HandleTypeDef htim2;
- TIM_HandleTypeDef htim3;
- osThreadId defaultTaskHandle;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /*typedef struct seta{
- int x, y;
- struct figura_t* figura;
- }seta;*/
- struct pontos_t pos_score = {0};
- struct pontos_t pos_coracao = {0};
- struct pontos_t pos_guindaste = {0};
- struct pontos_t pos_plataforma = {0};
- struct bloco_t pos_object = {0};
- volatile uint8_t flag_points = 0;
- volatile uint8_t pace = 0;
- volatile uint8_t score;
- volatile uint8_t life;
- volatile uint8_t vTask_Create = 0;
- volatile uint32_t rand_number_position;
- volatile uint32_t rand_number_object;
- volatile uint32_t joystick_pos[2];
- uint8_t aux1 = 0;
- uint8_t aux2 = 0;
- // variaveis para sincronia da musica
- uint32_t pos_musica;
- uint32_t pos_musica_play;
- uint32_t passo = 1;
- volatile uint32_t ADC_buffer[2];
- volatile uint32_t valor_ADC[2];
- uint32_t counter;
- //Handlers das tasks utilizadas
- TaskHandle_t PrintHandle;
- TaskHandle_t SoundHandle;
- TaskHandle_t RestartHandle;
- TaskHandle_t UpdateHandle;
- TaskHandle_t JoystickHandle; // Para excluir uma task(que não se auto exclui) é necessário o Handler
- xQueueHandle xSound;
- /* USER CODE END PV */
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_DMA_Init(void);
- static void MX_ADC1_Init(void);
- static void MX_TIM3_Init(void);
- static void MX_TIM2_Init(void);
- void StartDefaultTask(void const * argument);
- void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc);
- void change_note (TIM_HandleTypeDef *htim, int nota);
- //- Tasks do jogo
- void vTask_Create_Game (void *parameters);
- void vTask_Print (void * parameters);
- void vTask_Sound(void *parameters);
- void vTask_Joystick(void *parameters);
- void vTask_Restart(void *parameters);
- void vTask_Update(void *parameters);
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_ADC1_Init();
- MX_TIM3_Init();
- MX_TIM2_Init();
- /* USER CODE BEGIN 2 */
- //LCD 5110
- inic_LCD();
- limpa_LCD();
- //ADC Joystick
- HAL_ADC_Start_DMA(&hadc1,(uint32_t*)ADC_buffer,2);
- HAL_ADC_Start_IT(&hadc1);
- //PWM Buzzer
- HAL_TIM_Base_Start(&htim3);
- HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
- /* USER CODE END 2 */
- /* USER CODE BEGIN RTOS_MUTEX */
- /* add mutexes, ... */
- /* USER CODE END RTOS_MUTEX */
- /* USER CODE BEGIN RTOS_SEMAPHORES */
- //xSound = xQueueCreateCountingSemaphore(10, sizeof(long));
- // xSound = xQueueCreateCountingSemaphore(16,0);
- //xQueueCreateCountingSemaphore()
- xSound = xQueueCreate(16, sizeof(uint32_t));
- /* USER CODE END RTOS_SEMAPHORES */
- /* USER CODE BEGIN RTOS_TIMERS */
- /* start timers, add new ones, ... */
- /* USER CODE END RTOS_TIMERS */
- /* Create the thread(s) */
- /* definition and creation of defaultTask */
- osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
- defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
- /* USER CODE BEGIN RTOS_THREADS */
- /* add threads, ... */
- //Cria a primeira tarefa do jogo que vai criar o resto e depois inicia o RTOS
- uint32_t vTask_Create = 0;
- vTask_Create = xTaskCreate(vTask_Create_Game, "Create", 400, NULL, 3, NULL);
- if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
- limpa_LCD();
- string_LCD("There was an error on create game");
- imprime_LCD();
- while(1){
- HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
- HAL_Delay(500);
- }
- }
- /* USER CODE END RTOS_THREADS */
- /* USER CODE BEGIN RTOS_QUEUES */
- /* add queues, ... */
- /* USER CODE END RTOS_QUEUES */
- /* Start scheduler */
- osKernelStart();
- /* We should never get here as control is now taken by the scheduler */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
- PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
- }
- /* ADC1 init function */
- static void MX_ADC1_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
- /**Common config
- */
- hadc1.Instance = ADC1;
- hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
- hadc1.Init.ContinuousConvMode = ENABLE;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.NbrOfConversion = 2;
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_1;
- sConfig.Rank = ADC_REGULAR_RANK_1;
- sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_2;
- sConfig.Rank = ADC_REGULAR_RANK_2;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
- /* TIM2 init function */
- static void MX_TIM2_Init(void)
- {
- TIM_MasterConfigTypeDef sMasterConfig;
- TIM_OC_InitTypeDef sConfigOC;
- htim2.Instance = TIM2;
- htim2.Init.Prescaler = 0;
- htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim2.Init.Period = TIM_COUNT;
- htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 900;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- HAL_TIM_MspPostInit(&htim2);
- }
- /* TIM3 init function */
- static void MX_TIM3_Init(void)
- {
- TIM_MasterConfigTypeDef sMasterConfig;
- TIM_OC_InitTypeDef sConfigOC;
- htim3.Instance = TIM3;
- htim3.Init.Prescaler = 0;
- htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim3.Init.Period = TIM_COUNT;
- htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 900;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- HAL_TIM_MspPostInit(&htim3);
- }
- /**
- * Enable DMA controller clock
- */
- static void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
- /* DMA interrupt init */
- /* DMA1_Channel1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 5, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6
- |GPIO_PIN_7, GPIO_PIN_RESET);
- /*Configure GPIO pin : Botao_Pin */
- GPIO_InitStruct.Pin = Button_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(Button_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pins : PA3 PA4 PA5 PA6
- PA7 */
- GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6
- |GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- // Protótipo funções
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
- {
- if(hadc->Instance == ADC1)
- {
- valor_ADC[0]=ADC_buffer[0];
- valor_ADC[1]=ADC_buffer[1];
- }
- }
- void change_note (TIM_HandleTypeDef *htim, int nota){
- uint32_t prescaler;
- if (nota>0)
- prescaler = SystemCoreClock/(TIM_COUNT*nota);
- else
- prescaler =0;
- __HAL_TIM_SET_PRESCALER(htim, prescaler);
- }
- void vTask_Create_Game(void * parameters){
- uint32_t seed = 0;
- //Inicialização das posições das figuras basicas e variaveis
- score = 0;
- life = 3;
- pace = 100;
- flag_points = 0;
- pos_score.x1 = 72;
- pos_score.y1 = 8;
- pos_guindaste.x1 = 1;
- pos_guindaste.y1 = 4;
- pos_coracao.x1 = 73;
- pos_coracao.y1 = 18;
- pos_plataforma.x1 = 30;
- pos_plataforma.y1 = 44;
- //pos_bloco.x1 = 2;
- //pos_bloco.y1 = 10;
- //pos_bomba.x1 = 2;
- //pos_bomba.y1 = 8;
- //Tela inicial Game Start + botão de start
- while(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin)){ // enquando nao pressionar joystick fica travado
- seed++;
- limpa_LCD();
- escreve2fb(game_start_1);
- imprime_LCD();
- HAL_Delay(200);
- limpa_LCD();
- escreve2fb(game_start_2);
- imprime_LCD();
- HAL_Delay(200);
- limpa_LCD();
- escreve2fb(game_start_3);
- imprime_LCD();
- HAL_Delay(200);
- }
- init_LFSR(seed); // inicializacao para geracao de numeros pseudoaleatorios
- //Criacao das tarefas
- vTask_Create = xTaskCreate(vTask_Print, "Print", 200, NULL,1, &PrintHandle);
- if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
- limpa_LCD();
- string_LCD("There was an error on print");
- imprime_LCD();
- while(1){
- HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
- HAL_Delay(500);
- }
- }
- vTask_Create = xTaskCreate(vTask_Joystick, "Joystick", 128, NULL,1, &JoystickHandle);
- if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
- limpa_LCD();
- string_LCD("There was an error on joystick");
- imprime_LCD();
- while(1){
- HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
- HAL_Delay(500);
- }
- }
- vTask_Create = xTaskCreate(vTask_Update, "Update", 300, NULL,1, &UpdateHandle);
- if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
- limpa_LCD();
- string_LCD("There was an error on update");
- imprime_LCD();
- while(1){
- HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
- HAL_Delay(500);
- }
- }
- vTask_Create = xTaskCreate(vTask_Sound, "Sound", 128, NULL,1, &SoundHandle);
- if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
- limpa_LCD();
- string_LCD("There was an error on sound");
- imprime_LCD();
- while(1){
- HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
- HAL_Delay(500);
- }
- }
- //deleta essa task apos criar o jogo
- vTaskDelete(NULL);
- while(1){
- }
- }
- void vTask_Print (void * parameters){
- struct pontos_t aux = {0};
- while(1){
- limpa_LCD();
- escreve2fb(background); //desenho do fundo
- pos_coracao.x1 = 73; //posicionamento inicial e desenho das vidas
- pos_coracao.y1 = 18;
- for(int x=0; x < life; x++){
- desenha_fig(&pos_coracao, &coracao);
- pos_coracao.y1 = pos_coracao.y1 + 10;
- }
- desenha_fig(&pos_guindaste, &guindaste); //desenho das outras figuras e pontuacao
- desenha_fig(&pos_plataforma, &plataforma);
- if(pos_object.flag_alive == 1 && pos_object.flag_bomb == 1){ //se estiver vivo e flag_bomb for FALSE desenhar bloco
- aux.x1 = pos_object.x1;
- aux.y1 = pos_object.y1;
- desenha_fig(&aux, &bomba);
- }
- if(pos_object.flag_alive == 1 && pos_object.flag_bomb == 0){ //se estiver vivo e flag_bomb for FALSE desenhar bloco
- aux.x1 = pos_object.x1;
- aux.y1 = pos_object.y1;
- desenha_fig(&aux, &bloco);
- }
- escreve_Nr_Peq(pos_score.x1,pos_score.y1, score, 0);
- imprime_LCD();
- vTaskDelay(5);
- }
- }
- void vTask_Update(void *parameters){
- while(1){
- //numero randomico
- if(pos_object.flag_alive == 0 ){
- pos_guindaste.x1 = 1; //coloco o guindaste no inicio
- pos_guindaste.y1 = 4;
- rand_number_position = prng_LFSR() % 64;
- rand_number_object = prng_LFSR() % 2;
- if(rand_number_object == 1){
- pos_object.flag_bomb = 1;
- pos_object.x1 = 2;
- pos_object.y1 = 10;
- }
- else{
- pos_object.flag_bomb = 0;
- pos_object.x1 = 2;
- pos_object.y1 = 8;
- }
- pos_object.flag_alive = 1; //cria o objeto
- }
- //checagem do joystick
- if(joystick_pos[RIGHT] == TRUE){
- pos_plataforma.x1++; //move a plataforma
- if(pos_plataforma.x1 > 62){ //checa a colisão com o fim do trilho,
- pos_plataforma.x1 = 62;
- }
- }
- if(joystick_pos[LEFT] == TRUE){
- pos_plataforma.x1--; //move a plataforma
- if(pos_plataforma.x1 < 0){ //checa a colisão com o fim do trilho
- pos_plataforma.x1 = 0;
- }
- }
- //movimentação das partes do jogo
- pos_guindaste.x1++; //move o guindaste
- if(pos_guindaste.x1 > 62){ //checa a colisão com o fim do trilho
- pos_guindaste.x1 = 1;
- }
- pos_object.x1++;
- //pos_bloco.x1++;
- if(pos_object.x1 > rand_number_position){
- pos_object.x1 = rand_number_position;
- pos_object.y1++;
- if((pos_object.y1 == 43) && (pos_object.flag_bomb == 0)){
- pos_object.flag_alive = 0;
- life--;
- }
- if((pos_object.y1 == 39) && (pos_object.flag_bomb == 1)){
- pos_object.flag_alive = 0;
- }
- }
- 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)){
- pos_object.flag_alive = 0;
- life--;
- }
- 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)){
- score++;
- flag_points = 1;
- pos_object.flag_alive = 0;
- }
- //checagem das vidas
- if(life == 0){
- HAL_Delay(200); //pequeno delay antes de resetar
- vTask_Create = xTaskCreate(vTask_Restart, "Restart", 128, NULL,2, NULL);
- if (vTask_Create != pdPASS){ //Trava o jogo caso tenha algum problema
- limpa_LCD();
- string_LCD("There was an error on Restart");
- imprime_LCD();
- while(1){
- HAL_GPIO_TogglePin(Buzzer_GPIO_Port, Buzzer_Pin);
- HAL_Delay(500);
- }
- }
- }
- if(score == 5 && (flag_points == 1) ){
- pace = 50;
- vTaskDelay(200);
- flag_points =0;
- }
- if(score == 10 && (flag_points == 1)){
- pace = 25;
- vTaskDelay(200);
- flag_points =0;
- }
- if(score == 25 && (flag_points == 1)){
- pace = 10;
- vTaskDelay(200);
- flag_points =0;
- }
- vTaskDelay(pace);
- }
- }
- void vTask_Sound(void *parameters){
- /*
- int pos;
- while(1)
- {
- xQueueReceive(xSound,&pos,osWaitForever);
- //vTaskDelay(MS(60*(48/passo)));
- change_note(&htim3, song[pos]);
- vTaskDelay(MS(song[pos+1]));
- change_note(&htim3, 0);
- vTaskDelay(MS(3)); //
- }
- */
- while(1);
- }
- void vTask_Joystick(void *parameters){
- while(1){
- if (valor_ADC[0]<min_joystick){
- joystick_pos[RIGHT]= TRUE;
- } else{
- joystick_pos[RIGHT]= FALSE;
- }
- if (valor_ADC[0]>max_joystick){
- joystick_pos[LEFT]= TRUE;
- } else{
- joystick_pos[LEFT]= FALSE;
- }
- vTaskDelay(50);
- }
- }
- void vTask_Restart (void * parameters){
- HAL_Delay(500);
- limpa_LCD();
- escreve2fb(game_over_1);
- imprime_LCD();
- HAL_Delay(1000);
- limpa_LCD();
- escreve2fb(game_over_2);
- imprime_LCD();
- HAL_Delay(1000);
- limpa_LCD();
- escreve2fb(game_over_3);
- imprime_LCD();
- HAL_Delay(1000);
- limpa_LCD();
- escreve2fb(try_again_screen);
- pos_score.x1 = 58;
- pos_score.y1 = 13;
- escreve_Nr_Peq(pos_score.x1,pos_score.y1, score, 0);
- imprime_LCD();
- //exclui as tarefas criadas
- vTaskDelete(PrintHandle);
- vTaskDelete(UpdateHandle);
- vTaskDelete(SoundHandle);
- vTaskDelete(JoystickHandle);
- while(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin)){ // enquando nao pressionar joystick fica travado
- }
- HAL_Delay(1000);
- xTaskCreate(vTask_Create_Game,"Create",150, NULL, 4, NULL); vTaskDelete(NULL);
- while(1){
- }
- }
- /* USER CODE END 4 */
- /* StartDefaultTask function */
- void StartDefaultTask(void const * argument)
- {
- /* USER CODE BEGIN 5 */
- /* Infinite loop */
- for(;;)
- {
- osDelay(1);
- }
- /* USER CODE END 5 */
- }
- /**
- * @brief Period elapsed callback in non blocking mode
- * @note This function is called when TIM1 interrupt took place, inside
- * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
- * a global variable "uwTick" used as application time base.
- * @param htim : TIM handle
- * @retval None
- */
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- /* USER CODE BEGIN Callback 0 */
- /* USER CODE END Callback 0 */
- if (htim->Instance == TIM1) {
- HAL_IncTick();
- }
- /* USER CODE BEGIN Callback 1 */
- /* USER CODE END Callback 1 */
- }
- /**
- * @brief This function is executed in case of error occurrence.
- * @param file: The file name as string.
- * @param line: The line in file as a number.
- * @retval None
- */
- void _Error_Handler(char *file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement