#include "u_effect_rhythm.h"
#include "u_main.h"
#include "drv_mic.h"
#include "user_driver.h"
RhythmPara_t 		g_normal_rhythm 	= {0x00};	//ȫ���ɶ�
static uint8_t 		s_app_rhythm_val 	= 0x00;
extern void delay_ms(unsigned int tt);

void u_set_one_rhythm_color(void)
{
	//u_set_color(m_rhythm.vol_value, m_rhythm.mode_info[12].data[0], m_rhythm.mode_info[12].data[1], m_rhythm.mode_info[12].data[2]);
}

void app_rhythm_val_up(uint8_t rhythm_val)
{
	s_app_rhythm_val = rhythm_val;
	#ifdef DEBUG_DRV_APP
	U_UART_PRINTF("v = %d\n", s_app_rhythm_val);
	#endif
}

void rhythm_timer_cd(void)//app�����ɶ�ģʽ
{
	#define RHYTHM_SENSIT_K		(3)
	static uint8_t s_run_cnt = 0, vol_val = 0;
	uint8_t  adc_vol;
	//static uint8_t s_music_keep = 0;
	uint8_t rhythm_pvt = 10;
	if( g_light_para.mic == 0)
	{
	   adc_vol = s_app_rhythm_val;//rhythm_vol_get_cb();
	}
	else
	{
		if(g_light_para.mic_AppOrDriver == 1)
		{
			adc_vol = rhythm_vol_get_cb();
			//U_UART_PRINTF("i am driver mic \r\n");
		}else
		{
			adc_vol = s_app_rhythm_val;
			//U_UART_PRINTF("i am app mic \r\n");
		}
	}
	switch (g_light_para.rhythm_mode)
	{	
		case RHYTHM_MODE_JUMP_7:
		case RHYTHM_MODE_JUMP_3:
		{
			rhythm_pvt = g_light_para.rhythm_sensit + VOT_JUMP_VPT;
			//U_UART_PRINTF("     u_pvt = %d\n", rhythm_pvt);
			if (adc_vol > rhythm_pvt)
			{
				int16_t speed = (adc_vol - rhythm_pvt) / 8;
				if (speed >= 4)
					speed = 4;
				else if (speed <= 0)
					speed = 0;
			  //U_UART_PRINTF("speed = %d\n", speed);
				g_normal_rhythm.bright = MODE_JUMP_BRIGHT;
				 //U_UART_PRINTF("MODE_JUMP_BRIGHT\n");
				g_normal_rhythm.speed = 27 - speed * 5;
				//U_UART_PRINTF("f3_sd = %d\n", g_normal_rhythm.speed);
			}
			else
			{
				g_normal_rhythm.bright = LIGHT_BRIGHT_MIN;
				g_normal_rhythm.speed  = 0;
				//U_UART_PRINTF("MODE_JUMP_MIN_BRIGHT\n");
			}
			light_rhythm_mode_jump_timer_cb(&g_normal_rhythm);	
		}
		break;
		case RHYTHM_MODE_GRADUAL_7:
		{
			rhythm_pvt = 30;	//g_light_para.rhythm_sensit + 10;
			if (adc_vol > rhythm_pvt)
			{
				int16_t speed = (adc_vol - rhythm_pvt) / 14;
				if (speed >= 4)
					speed = 4;
				else if (speed <= 0)
					speed = 0;	
				g_normal_rhythm.bright = adc_vol;
				g_normal_rhythm.speed = speed;
			}
			else
			{
				g_normal_rhythm.bright = LIGHT_BRIGHT_MIN;
			}
			light_rhythm_mode_gradual_timer_cb(&g_normal_rhythm);	
		}
		break;
		case RHYTHM_MODE_FADE_7:
		{
			rhythm_pvt = g_light_para.rhythm_sensit + VOT_GRADUAL_7_VPT;
			if (adc_vol > rhythm_pvt )
			{
				//U_UART_PRINTF("     u_pvt = %d\n", rhythm_pvt);
				int16_t speed = (adc_vol - rhythm_pvt) / 8;
				if (speed >= 4)
					speed = 4;
				else if (speed <= 0)
					speed = 0;	
				g_normal_rhythm.speed = 27 - speed * 5;
				g_normal_rhythm.music_start = 1;
				//U_UART_PRINTF("f7_sd = %d\n", g_normal_rhythm.speed);
			}
			light_rhythm_mode_fade_timer_cb(&g_normal_rhythm);
		}
		break;
		case RHYTHM_MODE_FADE_3:
		{
			rhythm_pvt = g_light_para.rhythm_sensit + VOT_GRADUAL_3_VPT;
			if (adc_vol > rhythm_pvt )
			{
				//U_UART_PRINTF("     u_pvt = %d\n", rhythm_pvt);
				g_normal_rhythm.music_start = 1;	
				g_normal_rhythm.speed       = 15;
				//U_UART_PRINTF("f3_sd = %d\n", g_normal_rhythm.speed);
			}
			light_rhythm_mode_fade_timer_cb(&g_normal_rhythm);
		}
		break;
	}
}

void driver_rhythm_timer_cd(void)//�豸mic�ɶ�ģʽ
{
	#define RHYTHM_SENSIT_K		(3)
	static uint8_t s_run_cnt = 0, vol_val = 0;
	uint8_t  adc_vol=0;
	uint8_t rhythm_pvt = 10;
	if( g_light_para.mic ==0)
	{
	   adc_vol = s_app_rhythm_val;//rhythm_vol_get_cb();
	}
	else
	{
		if(g_light_para.mic_AppOrDriver == 1)
		{
			adc_vol = rhythm_vol_get_cb();
		}else if(g_light_para.mic_AppOrDriver == 0)
		{
			adc_vol = s_app_rhythm_val;
		}
	}
	switch (g_light_para.rhythm_mode)
	{	
		case RHYTHM_MODE_JUMP_7:
		case RHYTHM_MODE_JUMP_3:
		{
			rhythm_pvt = g_light_para.rhythm_sensit * 100 / 125 + VOT_JUMP_VPT;//rhythm_pvt ���Ϊ50 
			if (adc_vol > rhythm_pvt)
			{
				int16_t speed = (adc_vol - rhythm_pvt) / 26; //�����ٶ�
				if (speed >= 4)
					speed = 4;
				else if (speed <= 0)
					speed = 0;
			  //U_UART_PRINTF("speed = %d\n", speed);
				g_normal_rhythm.bright = MODE_JUMP_BRIGHT; //�ɶ�ģʽ
				 //U_UART_PRINTF("MODE_JUMP_BRIGHT\n");
				g_normal_rhythm.speed = 27 - speed * 5;    //�ɶ��ٶ�
				//U_UART_PRINTF("f3_sd = %d\n", g_normal_rhythm.speed);
			}
			else
			{
				g_normal_rhythm.bright = LIGHT_BRIGHT_MIN;
				g_normal_rhythm.speed  = 0;
				//U_UART_PRINTF("MODE_JUMP_MIN_BRIGHT\n");
			}
			light_rhythm_mode_jump_timer_cb(&g_normal_rhythm);	
		}
		break;
		case RHYTHM_MODE_GRADUAL_7://�����ɶ�ģʽ
		{
			rhythm_pvt = 30;	//g_light_para.rhythm_sensit + 10;
			if (adc_vol > rhythm_pvt)
			{
				int16_t speed = (adc_vol - rhythm_pvt) / 14;
				if (speed >= 4)
					speed = 4;
				else if (speed <= 0)
					speed = 0;	
				g_normal_rhythm.bright = adc_vol;
				g_normal_rhythm.speed = speed;
			}
			else
			{
				g_normal_rhythm.bright = 0;
			}
			light_rhythm_mode_gradual_timer_cb(&g_normal_rhythm);	
		}
		break;
		case RHYTHM_MODE_FADE_7:
		{
			rhythm_pvt = g_light_para.rhythm_sensit * 100 / 125 + VOT_GRADUAL_7_VPT;
			if (adc_vol > rhythm_pvt )
			{
				//U_UART_PRINTF("     u_pvt = %d\n", rhythm_pvt);
				int16_t speed = (adc_vol - rhythm_pvt) / 26;
				if (speed >= 4)
					speed = 4;
				else if (speed <= 0)
					speed = 0;		
				g_normal_rhythm.speed = 27 - speed * 5;
				g_normal_rhythm.music_start = 1;
				//U_UART_PRINTF("f7_sd = %d\n", g_normal_rhythm.speed);
			}
			light_rhythm_mode_fade_timer_cb(&g_normal_rhythm);
		}
		break;
		case RHYTHM_MODE_FADE_3:
		{
			rhythm_pvt = g_light_para.rhythm_sensit + VOT_GRADUAL_3_VPT;
			if (adc_vol > rhythm_pvt )
			{
				//U_UART_PRINTF("     u_pvt = %d\n", rhythm_pvt);
				g_normal_rhythm.music_start = 1;	
				g_normal_rhythm.speed = 15;
				//U_UART_PRINTF("f3_sd = %d\n", g_normal_rhythm.speed);
			}
			light_rhythm_mode_fade_timer_cb(&g_normal_rhythm);
		}
		break;
	}	
}
/****************************** mic_adc **********************************/
static void bubble_sort(uint8_t *sample_data,uint16_t len)   //ð��
{
	uint16_t i,j;
	uint16_t temp;
	for(j = 0; j < (len - 1); j++)
	{
		for(i=0;i < (len - 1 - j);i++)
		{
			if(sample_data[i] > sample_data[i+1])
			{
				temp = sample_data[i];
				sample_data[i] = sample_data[i+1];
				sample_data[i+1] = temp;
			}
		} 
	}
}
#if 0
uint8_t rhythm_vol_get_cb(void)
{
	static uint8_t buff_ind = 0, vol_value = 0;
	static uint8_t adc_sample[10] = {0};

	//adc_temp = ADC_Get_Value();
	//��ʼ��ADC
//	ADC_Deinit(2);
	ADC_Init(1);
	//delay_ms(2);
	ADC_Get_Value();
	adc_sample[buff_ind] = ADC_Get_Value();
	//U_UART_PRINTF("adc_sample = %d\n", adc_sample[buff_ind]);
	buff_ind++;
	if (buff_ind  >= 10)
	{
		int i = 0;
		uint16_t sample_num = 0;
		buff_ind = 0;
		bubble_sort(adc_sample, 10);
		for (i = 0; i < 5; i++)
		{
			sample_num += adc_sample[i+5];
		}
		vol_value = sample_num / 5;
		//U_UART_PRINTF("%d\n", vol_value);
	}
	return vol_value;
}
#endif

//char show_buffer[10]={0};
//#include "stdio.h"
uint8_t rhythm_vol_get_cb(void)
{
	static uint8_t buff_ind = 0, vol_value = 0;
	static uint8_t adc_sample[10] = {0};
	adc_sample[buff_ind] = Get_Audio_val();
	//U_UART_PRINTF("adc_sample = %d\n", adc_sample[buff_ind]);
	buff_ind++;
	if (buff_ind  >= 10)
	{
		int i = 0;
		uint16_t sample_num = 0;
		buff_ind = 0;
		bubble_sort(adc_sample, 10);
		for (i = 0; i < 5; i++)
		{
			sample_num += adc_sample[i+5];
		}
		vol_value = sample_num / 5;
				
//		U_UART_PRINTF("adc_val:");
//		for(i=0;i<10;i++)
//		{
//		   U_UART_PRINTF(" %d ", (int)adc_sample[i]);
//		}
//		U_UART_PRINTF("\r\n");
//		sprintf(show_buffer,"v:%3d ",vol_value);
//		u_ble_data_send(show_buffer,strlen(show_buffer));
		//U_UART_PRINTF("%d\n", vol_value);
	}
//  vol_value = Get_Audio_val(); 
	return vol_value;
}
/****************************** mic_adc end **********************************/
/*
sensit_step ����ң�ص��������ȼӼ�  
rhythm_sensit app����������
*/
void rhythm_sensit_ctrl(const int8_t sensit_step, const uint8_t rhythm_sensit)// ����mic������
{
	uint8_t dri_rhythm_sensit = LIGHT_SENSIT_MAX - rhythm_sensit / APP_SENSIT_K;
	if (dri_rhythm_sensit > LIGHT_SENSIT_MAX)
	{
		return;
	}
	if (sensit_step != 0)
	{
		int8_t sensit = 0;

		sensit = g_light_para.rhythm_sensit + sensit_step;	
		if (sensit >= LIGHT_SENSIT_MIN && sensit <= LIGHT_SENSIT_MAX)
		{
			g_light_para.rhythm_sensit = sensit;
		}
		else if (sensit <= LIGHT_SENSIT_MIN)
		{
			g_light_para.rhythm_sensit = LIGHT_SENSIT_MIN;
		}
		else if (sensit >= LIGHT_SENSIT_MAX)
		{
			g_light_para.rhythm_sensit = LIGHT_SENSIT_MAX;
		}
	}
	else
	{
		g_light_para.rhythm_sensit = dri_rhythm_sensit;
	}
//	sprintf(show_buffer,"s:%3d ",(int)g_light_para.rhythm_sensit);
//	u_ble_data_send(show_buffer,strlen(show_buffer));
	#ifdef DEBUG_DRV_APP
	U_UART_PRINTF("sensit = [%d] [%d] \n", g_light_para.rhythm_sensit, g_light_para.rhythm_sensit);
	#endif
}

/*
void rhythm_speed_ctrl(const uint8_t rhythm_mode, const int8_t speed_step, uint8_t rhythm_speed)
{
	uint8_t rhythm_speed_ind = 0;
	
	if (rhythm_speed > LIGHT_SPEED_MAX)
	{
		rhythm_speed = LIGHT_SPEED_MAX;
	}
	else if (rhythm_speed < LIGHT_SPEED_MIN)
	{
		rhythm_speed = LIGHT_SPEED_MIN;
	}
	
	if (rhythm_mode > RHYTHM_MODE_FULL_MAX)
	{
		rhythm_speed_ind = RHYTHM_SPEED_IND_DREAM;
	}
	else
	{
		rhythm_speed_ind = RHYTHM_SPEED_IND_FULL;
	}
	
	if (speed_step != 0)
	{
		int8_t speed = 0;

		speed = g_light_para.rhythm_speed[rhythm_speed_ind] + speed_step;
		
		if (speed >= LIGHT_SPEED_MIN && speed <= LIGHT_SPEED_MAX)
		{
			g_light_para.rhythm_speed[rhythm_speed_ind] = speed;
		}
		else if (speed <= LIGHT_SPEED_MIN)
		{
			g_light_para.rhythm_speed[rhythm_speed_ind] = LIGHT_SPEED_MIN;
		}
		else if (speed >= LIGHT_SPEED_MAX)
		{
			g_light_para.rhythm_speed[rhythm_speed_ind] = LIGHT_SPEED_MAX;
		}
	}
	else
	{
		g_light_para.rhythm_speed[rhythm_speed_ind] = rhythm_speed;
	}
	
	U_UART_PRINTF("speed = [%d] [%d] \n", g_light_para.rhythm_speed[0], g_light_para.rhythm_speed[1]);
}
*/