/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

/* 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 ---------------------------------------------------------*/
ADC_HandleTypeDef hadc3;

SPI_HandleTypeDef hspi4;

TIM_HandleTypeDef htim6;

/* USER CODE BEGIN PV */
uint8_t LEDMode = 0;
bool LEDDesign_PendingChange = false;
uint8_t LEDData[64][3] = { 0 }; // 64 LEDs for a 8x8 WS2812B array, in GRB format
uint8_t LEDData_WS2812B[66][3][3] = { 0 }; // For the LEDData Arrays, index 64 and 65
                                           // will always be zeroes, in order to have
                                           // SPI transmit a RESET signal of 76.8 μs,
                                           // which is greater than the minimum 50 μs
                                           // needed for the WS2812B ICs to signify
                                           // the end of the data
uint32_t WS2812BConvertedData;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI4_Init(void);
static void MX_TIM6_Init(void);
static void MX_ADC3_Init(void);
/* USER CODE BEGIN PFP */

uint8_t getSoundLevel(void);
void LEDDesign_Off(void);
void LEDDesign_ColorWhite(void);
void LEDDesign_ColorBlue(void);
void LEDDesign_ColorGreen(void);
void LEDDesign_ColorRed(void);
void LEDDesign_Crazy(void);
void LEDDesign_Smile(void);
void LEDDesign_SuperCrazy(void);
void updateWS2812BData(void);

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int _write(int32_t file, uint8_t *ptr, int32_t len)
{
    for (int i = 0; i < len; i++)
    {
        ITM_SendChar(*ptr++);
    }
    return len;
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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_SPI4_Init();
  MX_TIM6_Init();
  MX_ADC3_Init();
  /* USER CODE BEGIN 2 */

    updateWS2812BData();
    HAL_SPI_Transmit_IT(&hspi4, (uint8_t*) &LEDData, (uint16_t) 66 * 3 * 3);
    HAL_ADC_Start(&hadc3);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
    while (1) {
        if(LEDDesign_PendingChange){
            LEDDesign_Off();
        }else{
            switch (LEDMode) {
            case 0:
                LEDDesign_Smile();
                break;
            case 1:
                LEDDesign_Crazy();
                break;
            case 2:
                LEDDesign_SuperCrazy();
                break;
            case 3:
                LEDDesign_ColorWhite();
                break;
            case 4:
                LEDDesign_ColorRed();
                break;
            case 5:
                LEDDesign_ColorGreen();
                break;
            case 6:
                LEDDesign_ColorBlue();
                break;
            default:
                LEDDesign_Off();
            }
        }
        updateWS2812BData();
    /* 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};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 160;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  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_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief ADC3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_ADC3_Init(void)
{

  /* USER CODE BEGIN ADC3_Init 0 */

  /* USER CODE END ADC3_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC3_Init 1 */

  /* USER CODE END ADC3_Init 1 */
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
  hadc3.Instance = ADC3;
  hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc3.Init.Resolution = ADC_RESOLUTION_8B;
  hadc3.Init.ScanConvMode = DISABLE;
  hadc3.Init.ContinuousConvMode = DISABLE;
  hadc3.Init.DiscontinuousConvMode = DISABLE;
  hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc3.Init.NbrOfConversion = 1;
  hadc3.Init.DMAContinuousRequests = DISABLE;
  hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  if (HAL_ADC_Init(&hadc3) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
  sConfig.Channel = ADC_CHANNEL_4;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC3_Init 2 */

  /* USER CODE END ADC3_Init 2 */

}

/**
  * @brief SPI4 Initialization Function
  * @param None
  * @retval None
  */
static void MX_SPI4_Init(void)
{

  /* USER CODE BEGIN SPI4_Init 0 */

  /* USER CODE END SPI4_Init 0 */

  /* USER CODE BEGIN SPI4_Init 1 */

  /* USER CODE END SPI4_Init 1 */
  /* SPI4 parameter configuration*/
  hspi4.Instance = SPI4;
  hspi4.Init.Mode = SPI_MODE_MASTER;
  hspi4.Init.Direction = SPI_DIRECTION_2LINES;
  hspi4.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi4.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi4.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi4.Init.NSS = SPI_NSS_SOFT;
  hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi4.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi4.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi4) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI4_Init 2 */

  /* USER CODE END SPI4_Init 2 */

}

/**
  * @brief TIM6 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM6_Init(void)
{

  /* USER CODE BEGIN TIM6_Init 0 */

  /* USER CODE END TIM6_Init 0 */

  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM6_Init 1 */

  /* USER CODE END TIM6_Init 1 */
  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 4000;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 10000;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_OnePulse_Init(&htim6, TIM_OPMODE_SINGLE) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM6_Init 2 */

  /* USER CODE END TIM6_Init 2 */

}

/**
  * @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_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOG_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pins : A0_Pin A1_Pin A2_Pin A3_Pin
                           A4_Pin A5_Pin SDNRAS_Pin A6_Pin
                           A7_Pin A8_Pin A9_Pin */
  GPIO_InitStruct.Pin = A0_Pin|A1_Pin|A2_Pin|A3_Pin
                          |A4_Pin|A5_Pin|SDNRAS_Pin|A6_Pin
                          |A7_Pin|A8_Pin|A9_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

  /*Configure GPIO pins : SPI5_SCK_Pin SPI5_MISO_Pin SPI5_MOSI_Pin */
  GPIO_InitStruct.Pin = SPI5_SCK_Pin|SPI5_MISO_Pin|SPI5_MOSI_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI5;
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

  /*Configure GPIO pin : ENABLE_Pin */
  GPIO_InitStruct.Pin = ENABLE_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
  HAL_GPIO_Init(ENABLE_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : SDNWE_Pin */
  GPIO_InitStruct.Pin = SDNWE_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
  HAL_GPIO_Init(SDNWE_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
  GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pin : B1_Pin */
  GPIO_InitStruct.Pin = B1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
  GPIO_InitStruct.Pin = MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : B5_Pin VSYNC_Pin G2_Pin R4_Pin
                           R5_Pin */
  GPIO_InitStruct.Pin = B5_Pin|VSYNC_Pin|G2_Pin|R4_Pin
                          |R5_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : ACP_RST_Pin */
  GPIO_InitStruct.Pin = ACP_RST_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : OTG_FS_OC_Pin */
  GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : R3_Pin R6_Pin */
  GPIO_InitStruct.Pin = R3_Pin|R6_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF9_LTDC;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pin : BOOT1_Pin */
  GPIO_InitStruct.Pin = BOOT1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : A10_Pin A11_Pin BA0_Pin BA1_Pin
                           SDCLK_Pin SDNCAS_Pin */
  GPIO_InitStruct.Pin = A10_Pin|A11_Pin|BA0_Pin|BA1_Pin
                          |SDCLK_Pin|SDNCAS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  /*Configure GPIO pins : D4_Pin D5_Pin D6_Pin D7_Pin
                           D8_Pin D9_Pin D10_Pin D11_Pin
                           D12_Pin NBL0_Pin NBL1_Pin */
  GPIO_InitStruct.Pin = D4_Pin|D5_Pin|D6_Pin|D7_Pin
                          |D8_Pin|D9_Pin|D10_Pin|D11_Pin
                          |D12_Pin|NBL0_Pin|NBL1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

  /*Configure GPIO pins : G4_Pin G5_Pin B6_Pin B7_Pin */
  GPIO_InitStruct.Pin = G4_Pin|G5_Pin|B6_Pin|B7_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pins : OTG_HS_ID_Pin OTG_HS_DM_Pin OTG_HS_DP_Pin */
  GPIO_InitStruct.Pin = OTG_HS_ID_Pin|OTG_HS_DM_Pin|OTG_HS_DP_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF12_OTG_HS_FS;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pin : VBUS_HS_Pin */
  GPIO_InitStruct.Pin = VBUS_HS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(VBUS_HS_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : D13_Pin D14_Pin D15_Pin D0_Pin
                           D1_Pin D2_Pin D3_Pin */
  GPIO_InitStruct.Pin = D13_Pin|D14_Pin|D15_Pin|D0_Pin
                          |D1_Pin|D2_Pin|D3_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /*Configure GPIO pin : TE_Pin */
  GPIO_InitStruct.Pin = TE_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
  GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /*Configure GPIO pins : R7_Pin DOTCLK_Pin B3_Pin */
  GPIO_InitStruct.Pin = R7_Pin|DOTCLK_Pin|B3_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  /*Configure GPIO pins : HSYNC_Pin G6_Pin R2_Pin */
  GPIO_InitStruct.Pin = HSYNC_Pin|G6_Pin|R2_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pin : I2C3_SDA_Pin */
  GPIO_InitStruct.Pin = I2C3_SDA_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
  HAL_GPIO_Init(I2C3_SDA_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : I2C3_SCL_Pin */
  GPIO_InitStruct.Pin = I2C3_SCL_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
  HAL_GPIO_Init(I2C3_SCL_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : STLINK_RX_Pin STLINK_TX_Pin */
  GPIO_InitStruct.Pin = STLINK_RX_Pin|STLINK_TX_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : G7_Pin B2_Pin */
  GPIO_InitStruct.Pin = G7_Pin|B2_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /*Configure GPIO pins : G3_Pin B4_Pin */
  GPIO_InitStruct.Pin = G3_Pin|B4_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF9_LTDC;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  /*Configure GPIO pins : LD3_Pin LD4_Pin */
  GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  /*Configure GPIO pins : SDCKE1_Pin SDNE1_Pin */
  GPIO_InitStruct.Pin = SDCKE1_Pin|SDNE1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI0_IRQn);

}

/* USER CODE BEGIN 4 */
uint8_t getSoundLevel(void){
    // Adjustment / Calibration
    static uint8_t divisor = 2;
    static uint8_t postscale_DownShift = 0;
    static uint8_t prescale_DownShift = 180;
    static uint8_t multiplier = 3;

    static uint16_t averagedDifferences = 0;
    uint16_t returnValue;
    uint8_t samples[16];
    uint16_t sumOfDifferences = 0;

    for(uint8_t i = 0; i < sizeof(samples); ++i){
        HAL_Delay(1);
        HAL_ADC_Start(&hadc3);
        HAL_ADC_PollForConversion(&hadc3, (uint32_t) 20);
        samples[i] = HAL_ADC_GetValue(&hadc3);
    }

    for(uint8_t i = 0; i < sizeof(samples) - 1; ++i){
        sumOfDifferences += abs(samples[i] - samples[i + 1]);
    }

    averagedDifferences += ((int32_t) sumOfDifferences - (int32_t) averagedDifferences) / 4;

    if(averagedDifferences < prescale_DownShift){returnValue = 0;}else{returnValue = averagedDifferences - prescale_DownShift;}
    returnValue = (returnValue / divisor) * multiplier;
    if(returnValue < postscale_DownShift){returnValue = 0;}else{returnValue -= postscale_DownShift;}

    if(returnValue >= 0xFF){return 0xFF;} else{return returnValue;}
}
void LEDDesign_Off(void){
    for(uint8_t i = 0; i < 64; ++i){
        for(uint8_t j = 0; j < 3; ++j){
            LEDData[i][j] = 0x00;
        }
    }
}
void LEDDesign_ColorWhite(void){
    for(uint8_t i = 0; i < 64; ++i){
        for(uint8_t j = 0; j < 3; ++j){
            LEDData[i][j] = 0xFF;
        }
    }
}
void LEDDesign_ColorBlue(void){
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][0] = 0x00;
    }
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][1] = 0x00;
    }
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][2] = 0xFF;
    }
}
void LEDDesign_ColorGreen(void){
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][0] = 0xFF;
    }
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][1] = 0x00;
    }
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][2] = 0x00;
    }
}
void LEDDesign_ColorRed(void){
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][0] = 0x00;
    }
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][1] = 0xFF;
    }
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][2] = 0x00;
    }
}
void LEDDesign_Crazy(void){
    HAL_Delay(50);
    for(uint8_t i = 0; i < 64; ++i){
        for(uint8_t j = 0; j < 3; ++j){
            LEDData[i][j] = (uint8_t) (0xFF * (((float) rand()) / RAND_MAX));
        }
    }
}
void LEDDesign_Smile(void){
    uint8_t currentSoundLevel = getSoundLevel();
    LEDData[0][0] = 0x00;
    LEDData[0][1] = 0x00;
    LEDData[0][2] = 0x00;
    LEDData[1][0] = 0x00;
    LEDData[1][1] = 0x00;
    LEDData[1][2] = 0x00;
    LEDData[2][0] = 0x00;
    LEDData[2][1] = 0x00;
    LEDData[2][2] = 0x00;
    LEDData[3][0] = 0x00;
    LEDData[3][1] = 0x00;
    LEDData[3][2] = 0x00;
    LEDData[4][0] = 0x00;
    LEDData[4][1] = 0x00;
    LEDData[4][2] = 0x7F;
    LEDData[5][0] = 0x00;
    LEDData[5][1] = 0x00;
    LEDData[5][2] = 0x00;
    LEDData[6][0] = 0x00;
    LEDData[6][1] = 0x00;
    LEDData[6][2] = 0x00;
    LEDData[7][0] = 0x00;
    LEDData[7][1] = 0x00;
    LEDData[7][2] = 0x00;
    LEDData[8][0] = 0x00;
    LEDData[8][1] = 0x00;
    LEDData[8][2] = 0x00;
    LEDData[9][0] = 0x00;
    LEDData[9][1] = 0x00;
    LEDData[9][2] = 0x00;
    LEDData[10][0] = 0x00;
    LEDData[10][1] = 0x00;
    LEDData[10][2] = 0x7F;
    LEDData[11][0] = 0x00;
    LEDData[11][1] = 0x00;
    LEDData[11][2] = 0x00;
    LEDData[12][0] = 0x00;
    LEDData[12][1] = 0x00;
    LEDData[12][2] = 0x00;
    LEDData[13][0] = 0x00;
    LEDData[13][1] = 0x00;
    LEDData[13][2] = 0x00;
    LEDData[14][0] = 0x00;
    LEDData[14][1] = 0x00;
    LEDData[14][2] = 0x00;
    LEDData[15][0] = 0x00;
    LEDData[15][1] = 0x00;
    LEDData[15][2] = 0x00;
    LEDData[16][0] = 0x00;
    LEDData[16][1] = 0x00;
    LEDData[16][2] = 0x00;
    LEDData[17][0] = 0x00;
    LEDData[17][1] = 0x00;
    LEDData[17][2] = 0x00;
    LEDData[18][0] = 0x00;
    LEDData[18][1] = 0x00;
    LEDData[18][2] = 0x00;
    LEDData[19][0] = 0x00;
    LEDData[19][1] = 0x00;
    LEDData[19][2] = 0x00;
    LEDData[20][0] = 0x00;
    LEDData[20][1] = 0x00;
    LEDData[20][2] = 0x00;
    LEDData[21][0] = 0x00;
    LEDData[21][1] = 0x00;
    LEDData[21][2] = 0x7F;
    LEDData[22][0] = 0x00;
    LEDData[22][1] = 0x00;
    LEDData[22][2] = 0x00;
    LEDData[23][0] = 0x00;
    LEDData[23][1] = 0x00;
    LEDData[23][2] = 0x00;
    LEDData[24][0] = 0x00;
    LEDData[24][1] = 0x00;
    LEDData[24][2] = 0x00;
    LEDData[25][0] = 0x00;
    LEDData[25][1] = 0x00;
    LEDData[25][2] = 0x00;
    LEDData[26][0] = 0x00;
    LEDData[26][1] = 0x00;
    LEDData[26][2] = 0x7F;
    LEDData[27][0] = 0x00;
    LEDData[27][1] = 0x00;
    LEDData[27][2] = 0x00;
    LEDData[28][0] = 0x00;
    LEDData[28][1] = 0x00;
    LEDData[28][2] = 0x00;
    LEDData[29][0] = 0x00;
    LEDData[29][1] = 0x00;
    LEDData[29][2] = 0x00;
    LEDData[30][0] = 0x00;
    LEDData[30][1] = 0x00;
    LEDData[30][2] = 0x00;
    LEDData[31][0] = 0x00;
    LEDData[31][1] = 0x00;
    LEDData[31][2] = 0x00;
    LEDData[32][0] = 0x00;
    LEDData[32][1] = 0x00;
    LEDData[32][2] = 0x00;
    LEDData[33][0] = 0x00;
    LEDData[33][1] = 0x00;
    LEDData[33][2] = 0x00;
    LEDData[34][0] = 0x00;
    LEDData[34][1] = 0x00;
    LEDData[34][2] = 0x00;
    LEDData[35][0] = 0x00;
    LEDData[35][1] = 0x00;
    LEDData[35][2] = 0x00;
    LEDData[36][0] = 0x00;
    LEDData[36][1] = 0x00;
    LEDData[36][2] = 0x00;
    LEDData[37][0] = 0x00;
    LEDData[37][1] = 0x00;
    LEDData[37][2] = 0x7F;
    LEDData[38][0] = 0x00;
    LEDData[38][1] = 0x00;
    LEDData[38][2] = 0x00;
    LEDData[39][0] = 0x00;
    LEDData[39][1] = 0x00;
    LEDData[39][2] = 0x00;
    LEDData[40][0] = 0x00;
    LEDData[40][1] = 0x00;
    LEDData[40][2] = 0x00;
    LEDData[41][0] = 0x00;
    LEDData[41][1] = 0x00;
    LEDData[41][2] = 0x00;
    LEDData[42][0] = 0x00;
    LEDData[42][1] = 0x00;
    LEDData[42][2] = 0x7F;
    LEDData[43][0] = 0x00;
    LEDData[43][1] = 0x00;
    LEDData[43][2] = 0x00;
    LEDData[44][0] = 0x00;
    LEDData[44][1] = 0x00;
    LEDData[44][2] = 0x00;
    LEDData[45][0] = 0x00;
    LEDData[45][1] = 0x00;
    LEDData[45][2] = 0x00;
    LEDData[46][0] = 0x00;
    LEDData[46][1] = 0x00;
    LEDData[46][2] = 0x00;
    LEDData[47][0] = 0x00;
    LEDData[47][1] = 0x00;
    LEDData[47][2] = 0x00;
    LEDData[48][0] = 0x00;
    LEDData[48][1] = 0x00;
    LEDData[48][2] = 0x00;
    LEDData[49][0] = 0x00;
    LEDData[49][1] = 0x00;
    LEDData[49][2] = 0x00;
    LEDData[50][0] = 0x00;
    LEDData[50][1] = 0x00;
    LEDData[50][2] = 0x00;
    LEDData[51][0] = 0x00;
    LEDData[51][1] = 0x00;
    LEDData[51][2] = 0x00;
    LEDData[52][0] = 0x00;
    LEDData[52][1] = 0x00;
    LEDData[52][2] = 0x00;
    LEDData[53][0] = 0x00;
    LEDData[53][1] = 0x00;
    LEDData[53][2] = 0x7F;
    LEDData[54][0] = 0x00;
    LEDData[54][1] = 0x00;
    LEDData[54][2] = 0x00;
    LEDData[55][0] = 0x00;
    LEDData[55][1] = 0x00;
    LEDData[55][2] = 0x00;
    LEDData[56][0] = 0x00;
    LEDData[56][1] = 0x00;
    LEDData[56][2] = 0x00;
    LEDData[57][0] = 0x00;
    LEDData[57][1] = 0x00;
    LEDData[57][2] = 0x00;
    LEDData[58][0] = 0x00;
    LEDData[58][1] = 0x00;
    LEDData[58][2] = 0x00;
    LEDData[59][0] = 0x00;
    LEDData[59][1] = 0x00;
    LEDData[59][2] = 0x7F;
    LEDData[60][0] = 0x00;
    LEDData[60][1] = 0x00;
    LEDData[60][2] = 0x00;
    LEDData[61][0] = 0x00;
    LEDData[61][1] = 0x00;
    LEDData[61][2] = 0x00;
    LEDData[62][0] = 0x00;
    LEDData[62][1] = 0x00;
    LEDData[62][2] = 0x00;
    LEDData[63][0] = 0x00;
    LEDData[63][1] = 0x00;
    LEDData[63][2] = 0x00;
}
void LEDDesign_SuperCrazy(void){
    HAL_Delay(50);
    uint8_t randomByte = (uint8_t) (0xFF * (((float) rand()) / RAND_MAX));
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][0] = randomByte;
    }
    randomByte = (uint8_t) (0xFF * (((float) rand()) / RAND_MAX));
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][1] = randomByte;
    }
    randomByte = (uint8_t) (0xFF * (((float) rand()) / RAND_MAX));
    for(uint8_t i = 0; i < 64; ++i){
        LEDData[i][2] = randomByte;
    }
}
void updateWS2812BData(void){
    uint8_t byteToConvert;
    for (uint8_t i = 0; i < 64; ++i) {
        for (uint8_t j = 0; j < 3; ++j) {
            byteToConvert = LEDData[i][j];
            switch((byteToConvert & 0xF0) >> 4){
            case 0x00:
                WS2812BConvertedData = 0x00924000;
                break;
            case 0x01:
                WS2812BConvertedData = 0x00926000;
                break;
            case 0x02:
                WS2812BConvertedData = 0x00934000;
                break;
            case 0x03:
                WS2812BConvertedData = 0x00936000;
                break;
            case 0x04:
                WS2812BConvertedData = 0x009A4000;
                break;
            case 0x05:
                WS2812BConvertedData = 0x009A6000;
                break;
            case 0x06:
                WS2812BConvertedData = 0x009B4000;
                break;
            case 0x07:
                WS2812BConvertedData = 0x009B6000;
                break;
            case 0x08:
                WS2812BConvertedData = 0x00D24000;
                break;
            case 0x09:
                WS2812BConvertedData = 0x00D26000;
                break;
            case 0x0A:
                WS2812BConvertedData = 0x00D34000;
                break;
            case 0x0B:
                WS2812BConvertedData = 0x00D36000;
                break;
            case 0x0C:
                WS2812BConvertedData = 0x00DA4000;
                break;
            case 0x0D:
                WS2812BConvertedData = 0x00DA6000;
                break;
            case 0x0E:
                WS2812BConvertedData = 0x00DB4000;
                break;
            default: // 0x0F
                WS2812BConvertedData = 0x00DB6000;

            }
            switch(byteToConvert & 0x0F){
            case 0x00:
                WS2812BConvertedData |= 0x00000924;
                break;
            case 0x01:
                WS2812BConvertedData |= 0x00000926;
                break;
            case 0x02:
                WS2812BConvertedData |= 0x00000934;
                break;
            case 0x03:
                WS2812BConvertedData |= 0x00000936;
                break;
            case 0x04:
                WS2812BConvertedData |= 0x000009A4;
                break;
            case 0x05:
                WS2812BConvertedData |= 0x000009A6;
                break;
            case 0x06:
                WS2812BConvertedData |= 0x000009B4;
                break;
            case 0x07:
                WS2812BConvertedData |= 0x000009B6;
                break;
            case 0x08:
                WS2812BConvertedData |= 0x00000D24;
                break;
            case 0x09:
                WS2812BConvertedData |= 0x00000D26;
                break;
            case 0x0A:
                WS2812BConvertedData |= 0x00000D34;
                break;
            case 0x0B:
                WS2812BConvertedData |= 0x00000D36;
                break;
            case 0x0C:
                WS2812BConvertedData |= 0x00000DA4;
                break;
            case 0x0D:
                WS2812BConvertedData |= 0x00000DA6;
                break;
            case 0x0E:
                WS2812BConvertedData |= 0x00000DB4;
                break;
            default: // 0x0F
                WS2812BConvertedData |= 0x00000DB6;

            }
            LEDData_WS2812B[i][j][0] = (WS2812BConvertedData & 0x00FF0000) >> 16;
            LEDData_WS2812B[i][j][1] = (WS2812BConvertedData & 0x0000FF00) >> 8;
            LEDData_WS2812B[i][j][2] = WS2812BConvertedData & 0x000000FF;
        }
    }
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */

  /* 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****/
