#include "PWM_control.h"
#include <stdint.h>

uint8_t powerground_status;
uint8_t battery_cooling_status;
uint8_t powerground_percent;
uint8_t battery_cooling_percent;
//uint32_t powerground1_CCR, powerground2_CCR, battery_cooling_CCR;

TIM_HandleTypeDef powerground1, powerground2, battery_cooling;

/*
  Pulse width modulation mode allows for generating a signal with a frequency determined by 
  the value of the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register. 
*/

void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powerground2, TIM_HandleTypeDef battery_cooling){
  powerground_status = 0;
  battery_cooling_status = 0;

  HAL_TIM_PWM_Start(&powerground1, 1);        //TIM15CH1
  HAL_TIM_PWM_Start(&powerground2, 2);        //TIM15CH2
  HAL_TIM_PWM_Start(&battery_cooling, 3);     //TIM1CH3

  powerground_percent = 0;
  battery_cooling_percent = 0;
}              

/*
  controls the duty cycle of the fans by setting the CCR of the channel percent/100 = x/ARR
*/
void PWM_powerground_control(uint8_t percent){
  if (percent > 100 || percent < 0)
    return;

  powerground_percent = percent/100;
  TIM15->CCR1 = (POWERGROUND_MAX_DUTY_CYCLE-POWERGROUND_MIN_DUTY_CYCLE) * (percent/100) + POWERGROUND_MIN_DUTY_CYCLE;
  TIM15->CCR2 = TIM15->CCR1; /* *1.01 or *0.99  if the speeds of the fans are different*/
}

void PWM_battery_cooling_control(uint8_t percent){

}