/** 
	**************************************************************************
	* @file general_gpio.c
	* @brief Модуль для инициализации портов.
	**************************************************************************
		@verbatim
		//-------------------Функции-------------------//
		Functions: users
			- GPIO_Clock_Enable							Инициализация тактирования порта
		@endverbatim
***************************************************************************/
#include "general_gpio.h"

//-------------------------------------------------------------------
//------------------------GPIO INIT FUNCTIONS------------------------

HAL_StatusTypeDef GPIO_Clock_Enable(GPIO_TypeDef *GPIOx)
{	
	HAL_StatusTypeDef status = HAL_OK;
	// choose port for enable clock
	if (GPIOx==GPIOA)
		__HAL_RCC_GPIOA_CLK_ENABLE();
	else if (GPIOx==GPIOB)
		__HAL_RCC_GPIOB_CLK_ENABLE();
	else if (GPIOx==GPIOC)
		__HAL_RCC_GPIOC_CLK_ENABLE();
#ifdef GPIOD
	else if (GPIOx==GPIOD)
		__HAL_RCC_GPIOD_CLK_ENABLE();
#endif
#ifdef GPIOE
	else if (GPIOx==GPIOE)
		__HAL_RCC_GPIOE_CLK_ENABLE();
#endif
	else
		status = HAL_ERROR;
	
	return status;
}

//------------------------GPIO INIT FUNCTIONS------------------------
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//------------------------GPIO LED FUNCTIONS-------------------------

/**
  * @brief  Включить светодиод 
  */
void GPIO_LED_On(GPIO_LEDTypeDef *led)
{
  led->state = LED_IS_ON;
  HAL_GPIO_WritePin(led->LED_Port, led->LED_Pin, LED_ON);
}
/**
  * @brief  Выключить светодиод 
  */
void GPIO_LED_Off(GPIO_LEDTypeDef *led)
{
  led->state = LED_IS_OFF;
  HAL_GPIO_WritePin(led->LED_Port, led->LED_Pin, LED_OFF);
}

/**
  * @brief Активировать моргание светодиодом
	*/
void GPIO_LED_Blink_Start(GPIO_LEDTypeDef *led, uint32_t period)
{
  led->state = LED_IS_BLINKING;
  led->LED_Period = period;
}
  
/**
  * @brief Моргание светодиодом
	*/
void GPIO_LED_Blink_Handle(GPIO_LEDTypeDef *led)
{
  if(led->state == LED_IS_BLINKING)
  {
    uint32_t tickcurrent = HAL_GetTick();
    if((tickcurrent - led->tickprev) > led->LED_Period)
    {
      HAL_GPIO_TogglePin(led->LED_Port, led->LED_Pin);
      led->tickprev = tickcurrent;
    }
  }
}
//------------------------GPIO LED FUNCTIONS-------------------------
//-------------------------------------------------------------------

//-------------------------------------------------------------------
//------------------------GPIO SW FUNCTIONS-------------------------

/**
  * @brief Считать состоянии кнопки запуска 
  */
uint8_t GPIO_Read_Swich(GPIO_SwitchTypeDef *sw)
{

  if(HAL_GPIO_ReadPin(sw->Sw_Port, sw->Sw_Pin) == SW_ON)
  {
    sw->Sw_PrevState = 1;
    
    if(sw->tickprev == 0)
      sw->tickprev = HAL_GetTick();
    
    if((HAL_GetTick() - sw->tickprev) > sw->Sw_FilterDelay)
    {
      if(HAL_GPIO_ReadPin(sw->Sw_Port, sw->Sw_Pin) == SW_ON)
      {
        return 1;
      }
      else
      {
        sw->tickprev = 0;
        return 0;
      }
    }
  }
  else
  {
    sw->Sw_PrevState = 0;
  }
  return 0;
}
//------------------------GPIO SW FUNCTIONS-------------------------
//-------------------------------------------------------------------