/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdlib.h> // Para gerar números aleatórios
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t sequence[4]; // Sequência secreta
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void display_write(uint8_t value);
void led_feedback(uint8_t posicao, uint8_t status);
void buzzer_beep(uint8_t tipo);
void generate_sequence(uint8_t fase);
void play_game(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
HAL_Init();
/* 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();
/* USER CODE BEGIN 2 */
play_game(); // Inicia o jogo
/* USER CODE END 2 */
/* 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 = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/* Configure GPIO pins : PA0 PA1 PA2 (Botões) */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Configure GPIO pins : PB0 PB1 PB10 PB11 (LEDs) */
GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5| GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Configure GPIO pin : PA3 (Buzzer) */
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Configure GPIO pins : PA4 PA5 PA6 (Display 74HC595) */
GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/**
* @brief Função para escrever no display 7 segmentos via 74HC595
* @param value O valor a ser exibido
* @retval None
*/
void display_write(uint8_t value)
{
// Mapeamento de segmentos para números e letras (0-9, A-F)
const uint8_t segment_map[] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F, // 9
0x77, // A
0x7C, // B
0x39, // C
0x5E, // D
0x79, // E
0x71 // F
};
// Escreve o valor serialmente no 74HC595
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); // LATCH baixo
for (int i = 7; i >= 0; i--) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Clock baixo
if ((segment_map[value] >> i) & 0x01) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // Data = 1
} else {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // Data = 0
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Clock alto
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET); // LATCH alto
}
/**
* @brief Função para controlar o feedback visual nos LEDs
* @param posicao Posição do LED
* @param status Estado do LED (0 = apagado, 1 = aceso, 2 = piscando rápido, 3 = piscando devagar)
* @retval None
*/
void led_feedback(uint8_t posicao, uint8_t status)
{
uint16_t led_pins[] = {GPIO_PIN_0, GPIO_PIN_1, GPIO_PIN_10, GPIO_PIN_11};
switch (status) {
case 0: // LED apagado
HAL_GPIO_WritePin(GPIOB, led_pins[posicao], GPIO_PIN_RESET);
break;
case 1: // LED aceso
HAL_GPIO_WritePin(GPIOB, led_pins[posicao], GPIO_PIN_SET);
break;
case 2: // LED piscando rápido
// Colocar código para piscar rápido
break;
case 3: // LED piscando devagar
// Colocar código para piscar devagar
break;
}
}
/**
* @brief Função para emitir um beep curto ou longo no buzzer
* @param tipo Tipo de buzzer (0 = curto, 1 = longo)
* @retval None
*/
void buzzer_beep(uint8_t tipo)
{
if (tipo == 0) { // Buzzer curto
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
} else if (tipo == 1) { // Buzzer longo
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
}
}
/**
* @brief Função para gerar a sequência secreta com base na fase
* @param fase A fase atual do jogo
* @retval None
*/
void generate_sequence(uint8_t fase)
{
for (int i = 0; i < fase; i++) {
sequence[i] = rand() % 16; // Números de 0 a 15 (0-F no display)
}
}
/**
* @brief Função principal do jogo
* @retval None
*/
void play_game(void)
{
uint8_t fase = 1;
uint8_t tentativa = 0;
while (fase <= 4) {
// Mostrar fase no display
display_write(fase - 1); // Exibe F1, F2, etc.
// Gerar a sequência secreta para a fase atual
generate_sequence(fase);
// Loop de tentativas
for (int i = 0; i < fase; i++) {
uint8_t digit = 0;
uint8_t attempts = 0;
while (attempts < 3) {
// Ajustar número com botões
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET) { digit++; }
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_SET) { digit--; }
// Exibir número atual
display_write(digit);
// Confirmar número com botão
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == GPIO_PIN_SET) {
if (digit == sequence[i]) {
led_feedback(i, 1); // LED aceso se correto
buzzer_beep(0); // Buzzer curto
break;
} else {
led_feedback(i, 2); // LED piscando
buzzer_beep(0); // Buzzer curto
attempts++;
}
}
}
if (attempts == 3) {
// Se não acertar em 3 tentativas, reinicia a fase
fase = 1;
break;
}
}
fase++; // Passa para a próxima fase
}
// Fim de jogo (vitória)
buzzer_beep(1); // Buzzer longo
display_write(0x7F); // Exibe sequência correta no display
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/* USER CODE BEGIN 5 */
/* USER CODE END 5 */
/* USER CODE BEGIN 6 */
/* USER CODE END 6 */