#include "stm32f1xx_hal.h"

#include "led_pwm_hal.h"
#include "herramienta_hal.h"
#include "main.h"

TIM_HandleTypeDef htim2;
TIM_OC_InitTypeDef sConfigOC;

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{

  if(htim_base->Instance==TIM2)
  {
  /* USER CODE BEGIN TIM2_MspInit 0 */

  /* USER CODE END TIM2_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_TIM2_CLK_ENABLE();
  /* USER CODE BEGIN TIM2_MspInit 1 */

  /* USER CODE END TIM2_MspInit 1 */
  }

}

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(htim->Instance==TIM2)
  {
  /* USER CODE BEGIN TIM2_MspPostInit 0 */

  /* USER CODE END TIM2_MspPostInit 0 */
  
    /**TIM2 GPIO Configuration    
    PB10     ------> TIM2_CH3
    PA15     ------> TIM2_CH1
    PB3     ------> TIM2_CH2 
		PB11		------> TIM2_CH4
    */
    GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_10|GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    __HAL_AFIO_REMAP_TIM2_ENABLE();

  /* USER CODE BEGIN TIM2_MspPostInit 1 */

  /* USER CODE END TIM2_MspPostInit 1 */
  }

}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{

  if(htim_base->Instance==TIM2)
  {
  /* USER CODE BEGIN TIM2_MspDeInit 0 */

  /* USER CODE END TIM2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM2_CLK_DISABLE();
  }
  /* USER CODE BEGIN TIM2_MspDeInit 1 */

  /* USER CODE END TIM2_MspDeInit 1 */

}

void TIM2_Init(void)
{

  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_OC_InitTypeDef sConfigOC;
	
	 
	//__HAL_RCC_TIM2_CLK_ENABLE();//activar tim2
	//HAL_TIM_Base_MspInit(&htim2);
	
//time 64000000/64=1000000 HZ;
//1000000/256=3.906,25 HZ para pwm
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = defaultprescalar;//63+1;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = defaultperiodo;//255+1;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }

  if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
	
	HAL_TIM_OC_Init(&htim2);
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }

  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }

  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  {
    Error_Handler();
  }
	if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  {
    Error_Handler();
  }
	
 	HAL_TIM_MspPostInit(&htim2);
	
	//iniciado PWM en ch1 ch2 ch3
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_4);
	

}

void Pwm_Led(uint16_t duty,uint8_t ch){
	sConfigOC.Pulse=duty;
	if(duty>defaultperiodo){
		Error_Handler();
	}
	
	switch(ch){
		
		case 1:
			
			__HAL_TIM_GET_AUTORELOAD(&htim2); //gets the Period set for PWm
			__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, duty); //sets the PWM duty cycle 
			break;
		case 2:
			
			__HAL_TIM_GET_AUTORELOAD(&htim2); //gets the Period set for PWm
			__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, duty); //sets the PWM duty cycle 
			break;
		case 3:
			
			__HAL_TIM_GET_AUTORELOAD(&htim2); //gets the Period set for PWm
			__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, duty); //sets the PWM duty cycle 
			break;
		case 4:
			__HAL_TIM_GET_AUTORELOAD(&htim2); //gets the Period set for PWm
			__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, duty); //sets the PWM duty cycle 
			break;
		default:
				Error_Handler();
				break;
	}
			
	
}

void Pwm_Led_Porcent(float porcent,uint8_t ch){
	uint16_t duty=0;
	
	if(porcent>100 || porcent<0){
		//Error_Handler();
		porcent=0.0;
	}
	
	duty=(uint16_t)por_adc(porcent,0,(float_t)defaultperiodo);

	sConfigOC.Pulse=duty;
	
	
	switch(ch){
		
		case 1:
			
			__HAL_TIM_GET_AUTORELOAD(&htim2); //gets the Period set for PWm
			__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, duty); //sets the PWM duty cycle 
			break;
		case 2:
			
			__HAL_TIM_GET_AUTORELOAD(&htim2); //gets the Period set for PWm
			__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, duty); //sets the PWM duty cycle 
			break;
		case 3:
			
			__HAL_TIM_GET_AUTORELOAD(&htim2); //gets the Period set for PWm
			__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, duty); //sets the PWM duty cycle 
			break;
		case 4:
			__HAL_TIM_GET_AUTORELOAD(&htim2); //gets the Period set for PWm
			__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, duty); //sets the PWM duty cycle 
			break;
		default:
				Error_Handler();
				break;
	}
			
	
	
}

void Pwm_LedRGB(uint16_t red,uint16_t green,uint16_t blue){
	Pwm_Led(red,1);
	Pwm_Led(green,2);
	Pwm_Led(blue,3);
}

void Pwm_LedRGB_Porcent(float porcent_R,float porcent_G,float porcent_B){
	Pwm_Led_Porcent(porcent_R,1);
	Pwm_Led_Porcent(porcent_G,2);
	Pwm_Led_Porcent(porcent_B,3);		
}

void Pwm_LedUV(uint16_t uv){
	Pwm_Led(uv,4);
	
}
void Pwm_LedUV_Porcent(float porcent_UV){
	Pwm_Led_Porcent(porcent_UV,4);
	
}







