#include "user_driver.h"
//#include "t_pwm.h"
#include "u_main.h"

//#define PWM_DEBUG_EN

#if 1
//#define PWM_CHANCE_R  	(8)
//#define PWM_CHANCE_G  	(9)
//#define PWM_CHANCE_B    (10) //bk

#define PWM_CHANCE_R  	(24)
#define PWM_CHANCE_G  	(0)
#define PWM_CHANCE_B    (25)

#define PWM_RT_PERIOD  	(3400)
#define PWM_PERIOD  	(797)


void light_set_color(uint32_t color_rgb)
{
	float clr_temp[3] = {0x00}, chan_0 = 0, chan_1 = 0, chan_2 = 0, rgb_num = 0;
  uint32_t pwm_chan_0 = 0, pwm_chan_1 = 0, pwm_chan_2 = 0;

	
	//����Ϊ����ռ�ձ�������� 
	clr_temp[1] = ((color_rgb & 0x0ff) * 3125 + 500) / 1000;
	clr_temp[0] = (((color_rgb >> 8) & 0x0ff) * 3125 + 500) / 1000;
	clr_temp[2] = (((color_rgb >> 16) & 0x0ff) * 3125 + 500) / 1000;
	  
	
	rgb_num = clr_temp[1] + clr_temp[0] + clr_temp[2];
	#if PWM_DEBUG_EN
	U_UART_PRINTF("RGB = 0x%06X %d \r\n", color_rgb,color_rgb);
	U_UART_PRINTF("RGB = %d, %d, %d \r\n", clr_temp[1], clr_temp[0], clr_temp[2]);
	U_UART_PRINTF("rgb_num = %d\r\n", rgb_num);
	#endif
	
	if (rgb_num <= 1594)
	{
	
	}
	else if (rgb_num >= 2300) //797*3
	{
		clr_temp[1] = 75 * clr_temp[1] / 100;
		clr_temp[0] = 75 * clr_temp[0] / 100;
		clr_temp[2] = 75 * clr_temp[2] / 100;
	}
	else if (rgb_num > 1594)
	{
		float rt_k = 1594.0 / (clr_temp[1] + clr_temp[0] + clr_temp[2]);
		
		clr_temp[1] = rt_k * clr_temp[1];
		clr_temp[0] = rt_k * clr_temp[0];
		clr_temp[2] = rt_k * clr_temp[2];
	}

//	chan_1 = clr_temp[g_light_para.rgb_order[1]];//bk 
//	chan_0 = clr_temp[g_light_para.rgb_order[0]];
//	chan_2 = clr_temp[g_light_para.rgb_order[2]];
	
//	chan_1 = (uint32_t)((float)(clr_temp[g_light_para.rgb_order[1]]*101/797.0)); 
//	chan_0 = (uint32_t)((float)(clr_temp[g_light_para.rgb_order[0]]*101/797.0));
//	chan_2 = (uint32_t)((float)(clr_temp[g_light_para.rgb_order[2]]*101/797.0));

//	chan_1 = ((float)(clr_temp[g_light_para.rgb_order[1]]*101/797.0)); 
//	chan_0 = ((float)(clr_temp[g_light_para.rgb_order[0]]*101/797.0));
//	chan_2 = ((float)(clr_temp[g_light_para.rgb_order[2]]*101/797.0));
	
	chan_1 = ((float)(clr_temp[g_light_para.rgb_order[1]]*100/797.0)); 
	chan_0 = ((float)(clr_temp[g_light_para.rgb_order[0]]*100/797.0));
	chan_2 = ((float)(clr_temp[g_light_para.rgb_order[2]]*100/797.0));
	
	pwm_chan_0 = (uint32_t)(chan_0+0.5);//С��ȡ��
	pwm_chan_1 = (uint32_t)(chan_1+0.5);
	pwm_chan_2 = (uint32_t)(chan_2+0.5);
	
//	clr_temp[0] = (uint8_t)((float)((color_rgb >> 8) & 0xff)/255.0)*99;
//	clr_temp[1] = (uint8_t)((float)(color_rgb & 0xff)/255.0)*99;
//	clr_temp[2] = (uint8_t)((float)((color_rgb >> 16) & 0xff)/255.0)*99;
	
//	chan_1 = clr_temp[g_light_para.rgb_order[1]];
//	chan_0 = clr_temp[g_light_para.rgb_order[0]];
//	chan_2 = clr_temp[g_light_para.rgb_order[2]];
  
//	UserSet_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_0,20000,chan_0);// G	
//	UserSet_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_1,20000,chan_2);//R		
//	UserSet_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_2,20000,chan_1);//B
	
//	UserSet_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_0,20000,pwm_chan_0);// G	
//	UserSet_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_1,20000,pwm_chan_2);//R		
//	UserSet_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_2,20000,pwm_chan_1);//B
	
	
		UserSet_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_0,20000,pwm_chan_0);// G	
	UserSet_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_1,20000,pwm_chan_2);//R		
	//UserSet_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_2,20000,pwm_chan_1);//B
	
//	PWM_SetupIOL(BIT(PWM_CHANCE_R));
//	PWM_OPEN_CLOCK(PWM_CHANCE_R, PWM_CLKSEL_DPLL);
//	PWM_Setup(PWM_CHANCE_R, 1, PWM_PERIOD, chan_2);
//	PWM_Open(PWM_CHANCE_R); 
//	
//	PWM_SetupIOL(BIT(PWM_CHANCE_G));
//	PWM_OPEN_CLOCK(PWM_CHANCE_G, PWM_CLKSEL_DPLL);
//	PWM_Setup(PWM_CHANCE_G, 1, PWM_PERIOD, chan_0);
//	PWM_Open(PWM_CHANCE_G);
//	
//	PWM_SetupIOL(BIT(PWM_CHANCE_B));
//	PWM_OPEN_CLOCK(PWM_CHANCE_B, PWM_CLKSEL_DPLL);
//	PWM_Setup(PWM_CHANCE_B, 1, PWM_PERIOD, chan_1);
//	PWM_Open(PWM_CHANCE_B);	
  #if PWM_DEBUG_EN
  U_UART_PRINTF("chan_1 = %d\r\n", (int)(chan_1*10));
	U_UART_PRINTF("chan_0 = %d\r\n", (int)(chan_0*10));
	U_UART_PRINTF("chan_2 = %d\r\n", (int)(chan_2*10));
	U_UART_PRINTF("\r\n  rgb_order[0] = %d, rgb_order[1]=%d rgb_order[2]=%d\r\n", g_light_para.rgb_order[0], g_light_para.rgb_order[1], g_light_para.rgb_order[2]);
  #endif
	
}


//bk3431
//void light_set_color(uint32_t color_rgb)
//{
//	uint32_t clr_temp[3] = {0x00}, chan_0 = 0, chan_1 = 0, chan_2 = 0, rgb_num = 0;

//	//U_UART_PRINTF("RGB = %06X\n", color_rgb);
//	//U_UART_PRINTF("RGB = %d, %d, %d\n", clr_temp[1], clr_temp[0], clr_temp[2]);
//	
//	clr_temp[1] = ((color_rgb & 0x0ff) * 3125 + 500) / 1000;
//	clr_temp[0] = (((color_rgb >> 8) & 0x0ff) * 3125 + 500) / 1000;
//	clr_temp[2] = (((color_rgb >> 16) & 0x0ff) * 3125 + 500) / 1000;
//	
//	rgb_num = clr_temp[1] + clr_temp[0] + clr_temp[2];

//	if (rgb_num <= 1594)
//	{
//	
//	}
//	else if (rgb_num >= 2300) //797*3
//	{
//		clr_temp[1] = 75 * clr_temp[1] / 100;
//		clr_temp[0] = 75 * clr_temp[0] / 100;
//		clr_temp[2] = 75 * clr_temp[2] / 100;
//	}
//	else if (rgb_num > 1594)
//	{
//		float rt_k = 1594.0 / (clr_temp[1] + clr_temp[0] + clr_temp[2]);
//		
//		clr_temp[1] = rt_k * clr_temp[1];
//		clr_temp[0] = rt_k * clr_temp[0];
//		clr_temp[2] = rt_k * clr_temp[2];
//	}
//	
//	chan_1 = clr_temp[g_light_para.rgb_order[1]];
//	chan_0 = clr_temp[g_light_para.rgb_order[0]];
//	chan_2 = clr_temp[g_light_para.rgb_order[2]];
//	
//	PWM_SetupIOL(BIT(PWM_CHANCE_R));
//	PWM_OPEN_CLOCK(PWM_CHANCE_R, PWM_CLKSEL_DPLL);
//	PWM_Setup(PWM_CHANCE_R, 1, PWM_PERIOD, chan_2);
//	PWM_Open(PWM_CHANCE_R);
//	
//	PWM_SetupIOL(BIT(PWM_CHANCE_G));
//	PWM_OPEN_CLOCK(PWM_CHANCE_G, PWM_CLKSEL_DPLL);
//	PWM_Setup(PWM_CHANCE_G, 1, PWM_PERIOD, chan_0);
//	PWM_Open(PWM_CHANCE_G);
//	
//	PWM_SetupIOL(BIT(PWM_CHANCE_B));
//	PWM_OPEN_CLOCK(PWM_CHANCE_B, PWM_CLKSEL_DPLL);
//	PWM_Setup(PWM_CHANCE_B, 1, PWM_PERIOD, chan_1);
//	PWM_Open(PWM_CHANCE_B);	
//	
//	//U_UART_PRINTF("period = %d, rgb = %d, %d %d\n", period, clr_temp[1], clr_temp[0], clr_temp[2]);
//}
#else
#define PWM_CHANCE_R  	(8)
#define PWM_CHANCE_G  	(9)
#define PWM_CHANCE_B    (10)
#define PWM_RT_PERIOD  	(3400)
#define PWM_PERIOD  	(2550)

void light_set_color(uint32_t color_rgb)
{
	uint16_t color_r = 0, color_g = 0, color_b = 0, rgb_num = 0, period = 0;
	
	color_r = ((color_rgb >> 8) & 0x0ff) * 10;
	color_g = (color_rgb & 0x0ff) * 10;
	color_b = ((color_rgb >> 16) & 0x0ff) * 10;
	rgb_num = color_r + color_g + color_b;
	
	//U_UART_PRINTF("RGB = %d, %d, %d\n", color_r, color_g, color_b);
	
	if (rgb_num >= 7650)
	{
		period = 3400;
	}
	else if (rgb_num > 5100)
	{
		float rt_k = 5100.0 / (color_r + color_g + color_b);
		period = 2550;
		
		color_r = rt_k * color_r;
		color_g = rt_k * color_g;
		color_b = rt_k * color_b;
	}
	else
	{
		period = 2550;
	}
	
	PWM_SetupIOL(BIT(PWM_CHANCE_R));
	PWM_OPEN_CLOCK(PWM_CHANCE_R, PWM_CLKSEL_DPLL);
	PWM_Setup(PWM_CHANCE_R, 1, period, color_r);
	PWM_Open(PWM_CHANCE_R);
	
	PWM_SetupIOL(BIT(PWM_CHANCE_G));
	PWM_OPEN_CLOCK(PWM_CHANCE_G, PWM_CLKSEL_DPLL);
	PWM_Setup(PWM_CHANCE_G, 1, period, color_g);
	PWM_Open(PWM_CHANCE_G);
	
	PWM_SetupIOL(BIT(PWM_CHANCE_B));
	PWM_OPEN_CLOCK(PWM_CHANCE_B, PWM_CLKSEL_DPLL);
	PWM_Setup(PWM_CHANCE_B, 1, period, color_b);
	PWM_Open(PWM_CHANCE_B);	
	
	//U_UART_PRINTF("period = %d, rgb = %d, %d %d\n", period, color_r, color_g, color_b);
}
#endif