/*
 * 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 S1:
		HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);
		break;
	case S2:
		HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4);
		break;
	case S3:
		HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
		break;
	case S4:
		HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2);
		break;
	case S5:
		HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3);
		break;
	case S6:
		HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4);
		break;
	case S7:
		HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1);
		break;
	case S8:
		HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2);
		break;
	case S9:
		HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3);
		break;
	case S10:
		HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4);
		break;
	}
}

void PWMStart(uint8_t sortie) {
	switch (sortie) {
	case S1:
		HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
		break;
	case S2:
		HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
		break;
	case S3:
		HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
		break;
	case S4:
		HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
		break;
	case S5:
		HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
		break;
	case S6:
		HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
		break;
	case S7:
		HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
		break;
	case S8:
		HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);
		break;
	case S9:
		HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
		break;
	case S10:
		HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
		break;
	}
}

void PWMStop(uint8_t sortie) {
	switch (sortie) {
	case S1:
		HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
		break;
	case S2:
		HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4);
		break;
	case S3:
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
		break;
	case S4:
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2);
		break;
	case S5:
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_3);
		break;
	case S6:
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_4);
		break;
	case S7:
		HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_1);
		break;
	case S8:
		HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_2);
		break;
	case S9:
		HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
		break;
	case S10:
		HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
		break;
	}
}

uint16_t PourcentageToValue(uint8_t val) {
	float t = (val * PWM_MAX) / 100;
	return (uint16_t) t;
}

void GerePWM(uint16_t listeSortie, uint8_t pourcentage) {
	if (listeSortie & MASKS1) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S1, pourcentage);
			PWMStart(S1);
		} else
			PWMStop(S1);
	}

	if (listeSortie & MASKS2) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S2, pourcentage);
			PWMStart(S2);
		} else
			PWMStop(S2);

	}

	if (listeSortie & MASKS3) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S3, pourcentage);
			PWMStart(S3);
		} else
			PWMStop(S3);
	}

	if (listeSortie & MASKS4) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S4, pourcentage);
			PWMStart(S4);
		} else
			PWMStop(S4);
	}

	if (listeSortie & MASKS5) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S5, pourcentage);
			PWMStart(S5);
		} else
			PWMStop(S5);
	}

	if (listeSortie & MASKS6) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S6, pourcentage);
			PWMStart(S6);
		} else
			PWMStop(S6);
	}

	if (listeSortie & MASKS7) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S7, pourcentage);
			PWMStart(S7);
		} else
			PWMStop(S7);
	}

	if (listeSortie & MASKS8) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S8, pourcentage);
			PWMStart(S8);
		} else
			PWMStop(S8);
	}

	if (listeSortie & MASKS9) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S9, pourcentage);
			PWMStart(S9);
		} else
			PWMStop(S9);
	}

	if (listeSortie & MASKS10) {
		if (pourcentage != 0) {
			PWMSetDutyCycle(S10, pourcentage);
			PWMStart(S10);
		} else {
			PWMStop(S10);
		}

	}
}

uint8_t PWMPourcentage(Sortie *s, uint16_t heure) {
	if (s->Status == ENALLUME) {
		 int r = (float)s->PuissanceMax / ((float)s->Pente / (float)(heure - s->Demmarage));
		 return r;
	} else {
		return s->PuissanceMax / (s->Pente / (s->Extinxtion - heure));
	}
}
