/** 
	**************************************************************************
	* @file general_gpio.h
	* @brief Заголовочный файл для модуля инициализации портов.
	*************************************************************************/
#ifndef __GPIO_GENERAL_H_
#define __GPIO_GENERAL_H_

#include "mylibs_defs.h"


#define SPI_Alternate_Mapping(INSTANCE) 			 ((((INSTANCE) == TIM1) || ((INSTANCE) == TIM2))? GPIO_AF1_TIM1: 																										\
																								(((INSTANCE) == TIM3) || ((INSTANCE) == TIM4) || ((INSTANCE) == TIM5))? GPIO_AF2_TIM3:  													\
																								(((INSTANCE) == TIM8) || ((INSTANCE) == TIM9) || ((INSTANCE) == TIM10) || ((INSTANCE) == TIM11))? GPIO_AF3_TIM8:  \
																								(((INSTANCE) == TIM12) || ((INSTANCE) == TIM13) || ((INSTANCE) == TIM14))? GPIO_AF9_TIM12: 												\
																								(0))


#define TIM_Alternate_Mapping(INSTANCE) 			 ((((INSTANCE) == TIM1) || ((INSTANCE) == TIM2))? GPIO_AF1_TIM1: 																										\
																								(((INSTANCE) == TIM3) || ((INSTANCE) == TIM4) || ((INSTANCE) == TIM5))? GPIO_AF2_TIM3:  													\
																								(((INSTANCE) == TIM8) || ((INSTANCE) == TIM9) || ((INSTANCE) == TIM10) || ((INSTANCE) == TIM11))? GPIO_AF3_TIM8:  \
																								(((INSTANCE) == TIM12) || ((INSTANCE) == TIM13) || ((INSTANCE) == TIM14))? GPIO_AF9_TIM12: 												\
																								(0))


typedef enum
{
  LED_IS_OFF = 0,
  LED_IS_ON = 1,
  LED_IS_BLINKING = 2,
  LED_IS_FADING = 3,
}GPIO_LEDStateTypeDef;

typedef struct
{
  GPIO_LEDStateTypeDef state;
  
  GPIO_TypeDef  *LED_Port;
  uint32_t      LED_Pin;
  
  uint32_t      LED_Period;
  uint32_t      tickprev;
}GPIO_LEDTypeDef;

typedef struct
{
  GPIO_TypeDef  *Sw_Port;
  uint32_t      Sw_Pin;
  
  uint32_t      Sw_PrevState;
  uint32_t      Sw_FilterDelay;
  uint32_t      tickprev;
}GPIO_SwitchTypeDef;

/////////////////////////////////////////////////////////////////////
///////////////////////////---FUNCTIONS---///////////////////////////
HAL_StatusTypeDef GPIO_Clock_Enable(GPIO_TypeDef *GPIOx);

/* Считать состоянии кнопки запуска */
uint8_t GPIO_Read_Swich(GPIO_SwitchTypeDef *swstart);
/* Включить светодиод */
void GPIO_LED_On(GPIO_LEDTypeDef *led);
/* Выключить светодиод */
void GPIO_LED_Off(GPIO_LEDTypeDef *led);
/* Активировать моргание светодиодом */
void GPIO_LED_Blink_Start(GPIO_LEDTypeDef *led, uint32_t period);
/* Моргание светодиодом */
void GPIO_LED_Blink_Handle(GPIO_LEDTypeDef *led);
///////////////////////////---FUNCTIONS---///////////////////////////


#ifndef LED_ON
#define LED_ON 0
#endif

#ifndef LED_0FF
#define LED_OFF 1
#endif

#ifndef SW_ON
#define SW_ON 0
#endif

#ifndef SW_0FF
#define SW_OFF 1
#endif

#endif // __GPIO_GENERAL_H_
