/*
 * PWM.c
 *
 *  Created on: 14 janv. 2019
 *      Author: e_rcluze
 */

#include "PWM.h"
#include "tim.h"
#include "gpio.h"


void PWMSetDutyCycle(uint8_t sortie, uint8_t pourcentage) {
	TIM_OC_InitTypeDef sConfigOC;

	sConfigOC.OCMode = TIM_OCMODE_PWM1;
	sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
	sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
	sConfigOC.Pulse = PourcentageToValue(pourcentage);
	HAL_Delay(1);
	switch (sortie) {
	case 1:
		HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);
		break;
	case 2:
		HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4);
		break;
	case 3:
		HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
		break;
	case 4:
		HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2);
		break;
	case 5:
		HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3);
		break;
	case 6:
		HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4);
		break;
	case 7:
		HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1);
		break;
	case 8:
		HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2);
		break;
	case 9:
		HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3);
		break;
	case 10:
		HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4);
		break;
	}
}

void PWMStart(uint8_t sortie) {
	switch (sortie) {
	case 1:
		HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
		break;
	case 2:
		HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
		break;
	case 3:
		HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
		break;
	case 4:
		HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
		break;
	case 5:
		HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
		break;
	case 6:
		HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
		break;
	case 7:
		HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
		break;
	case 8:
		HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);
		break;
	case 9:
		HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
		break;
	case 10:
		HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
		break;
	}
}

void PWMStop(uint8_t sortie) {
	switch (sortie) {
	case 1:
		HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
		break;
	case 2:
		HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4);
		break;
	case 3:
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
		break;
	case 4:
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2);
		break;
	case 5:
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_3);
		break;
	case 6:
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_4);
		break;
	case 7:
		HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_1);
		break;
	case 8:
		HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_2);
		break;
	case 9:
		HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
		break;
	case 10:
		HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
		break;
	}
}

uint16_t PourcentageToValue(uint8_t val) {
	float t = ((100-val) * PWM_MAX) / 100; //Logic inverse (si 0 alors allumé si 1 alors eteint)
	return (uint16_t) t;
}

uint8_t POWMAX(uint8_t sortie){
	switch (sortie) {
	case 1:
		return 50;
		break;
	case 2:
		return 50;
		break;
	case 3:
		break;
	case 4:
		return 50;
		break;
	case 5:
		return 10;
		break;
	case 6:
		return 10;
		break;
	case 7:
		return 0;
		break;
	case 8:
		return 0;
		break;
	case 9:
		return 0;
		break;
	case 10:
		return 0;
		break;
	default :
		return 0;
		break;
	}

}
