#ifndef _IRC_C_
#define _IRC_C_
/*********************************************************************************************************************/
#include "include/ca51f_config.h"		
#include "include/ca51f2sfr.h"
#include "include/ca51f2xsfr.h"
#include "include/gpiodef_f2.h"
#include "include/sample.h"
#include "include/system_clock.h"
#include "include/uart.h"
#include "include/irc.h"
#include "include/key.h"
#include <intrins.h>
/*********************************************************************************************************************/
unsigned char IR_Code[4];			   	//遥控码值
bit IRC_SyncFlag;					   			//同步信号标志， 为1表示已接收到同步信号
bit IR_RxEndFlag;
unsigned char IRC_BreakTimer;
unsigned char IRC_LongKeyTimer;
bit IRC_MonitorFlag;
bit IRC_LongStartFlag;
unsigned int IRC_Msg;
unsigned char IR_BitCount;			  //遥控码值位指示
unsigned char OverFlowCount;		  //计数溢出计数器

void INT9_ISR (void) interrupt 14 	
{
	unsigned int PulseWidth;
	if(SMSTA & SMEF)
	{
		SMSTA |= SMEF;
	
		if(OverFlowCount == 0)
		{
			PulseWidth = (SMDATH&0x7F)*256 + SMDATL;	 //获取脉冲宽度
			if(!IRC_SyncFlag)			
			{
				if((PulseWidth > (SYN_WIDTH1-WIDTH*6)) && (PulseWidth < (SYN_WIDTH1+WIDTH*6))) //如果未同步， 判断当前脉冲是否同步信号
				{
					IRC_SyncFlag = 1;
					IR_BitCount = 0;
				}
			}
			else
			{
				if((PulseWidth > (DAT_1_WIDTH-WIDTH*2)) && (PulseWidth < (DAT_1_WIDTH+WIDTH*2)))	  //判断当前脉冲是否位1
				{
					IR_Code[IR_BitCount/8] |= (1<<(7-(IR_BitCount%8)));	
					IR_BitCount++;	
				}
				else if((PulseWidth > (DAT_0_WIDTH-WIDTH)) && (PulseWidth < (DAT_0_WIDTH+WIDTH)))	  //判断当前脉冲是否位0
				{
					IR_Code[IR_BitCount/8] &= ~(1<<(7-(IR_BitCount%8)));	
					IR_BitCount++;	
				}
				else
				{
					IRC_SyncFlag = 0;		  //如果不是位1或0的宽度， 则等待接收下一个同步信号
				}
				if((IR_BitCount == 32) && IRC_SyncFlag)	   //32位遥控码接收完成
				{
					IRC_SyncFlag = 0;
					IR_RxEndFlag = 1;

				}
			}
		}
		OverFlowCount=0;				 //复位溢出计数器
		IRC_BreakTimer = 13;
	}
	if(SMSTA & SMOF)
	{
		SMSTA |= SMOF;
		if(OverFlowCount < 0xFF) 
		{
			OverFlowCount++;			//计数溢出累加
		}
	}	
}

void Sample_init(void)
{										     	 	 
	GPIO_Init(P70F,P70_SAMPLE_SETTING);			  		//设置P70为采样管脚

	SMCON = SMEN_SYS_CLK | SMOE(1) | SMMD(1);	 		//初始化SAMPLE功能
	SMDIV = 6;								 	 					//设置SAMPLE时钟分频
	SMVTHL = (SYN_WIDTH1*2)%256;			 	 	//设置溢出时钟宽度
	SMVTHH = (SYN_WIDTH1*2)/256;
	SMCON |= SMIE(1);	
	INT9EN = 1; 				 				//使能SAMPLE中断		 
}

void Timer0_init(void)
{
	TMOD=(TMOD&0xFC)|0x01;
	TL0 = 0x00;
	TH0 = 0xF4;
	TR0 = 1;
	ET0 = 1;	
}
void TIMER0_ISR (void) interrupt 1 		 //10ms 
{
	TL0 = 0x00;
	TH0 = 0xF4;

	if(IRC_BreakTimer) IRC_BreakTimer--;
	if(IRC_LongKeyTimer) IRC_LongKeyTimer--;

}
void IRC_init(void)
{
	Sample_init();
	IR_RxEndFlag = 0;
	OverFlowCount=0;	

	IRC_SyncFlag = 0;
	IRC_BreakTimer = 0;
	IRC_MonitorFlag = 0;
	IRC_LongStartFlag = 0;
	IRC_Msg = KEY_INVALID;
}
unsigned char IRC_GetKeyCode(unsigned char irc_code)		
{
	unsigned char i;
	code unsigned char IRKEY_TABLE[][2]=
	{
		{K1,162},	
		{K2,98},
		{K3,226},
		{K4,34},
		{K5,2},
		{K6,194},
		{K7,224},
		{K8,168},
		{K9,144},	 
		{K10,152},
	};
	for(i=0;i<sizeof(IRKEY_TABLE)/2;i++)
	{
		if(IRKEY_TABLE[i][1] == irc_code)
		{
			return IRKEY_TABLE[i][0];
		}
	}
	return 0;
}
unsigned int IRC_GetMsg(void)
{
	static unsigned int KeyBak;
	unsigned int Msg = KEY_INVALID;
	if(!IRC_MonitorFlag)
	{
		if(IR_RxEndFlag)
		{
			IR_RxEndFlag = 0;
#ifdef PRINT_EN
			uart_printf("code1 = 0x%x,code2 = 0x%x,code3 = 0x%x,code4 = 0x%x \n",(unsigned int)IR_Code[0],(unsigned int)IR_Code[1],(unsigned int)IR_Code[2],(unsigned int)IR_Code[3]);	
#endif			
			if((IR_Code[0] == USER_CODE1) && (IR_Code[1] == USER_CODE2) && (IR_Code[2] == ~IR_Code[3]))
			{
				Msg = (unsigned int)IRC_GetKeyCode(IR_Code[2]);		//得到遥控按键按下的信息
				KeyBak = Msg;						
				IRC_MonitorFlag = 1;
				IRC_LongStartFlag = 0;
				IRC_LongKeyTimer = IRC_LONG_START_TIME;			
			}
		}				
	}	
	else
	{
		if(!IRC_BreakTimer)
		{
			if(!IRC_LongStartFlag)
			{
				Msg = KeyBak | KEY_BREAK;		//得到遥控按键短按释放信息
			}
			else
			{
				Msg = KeyBak | KEY_LONG_BREAK;	//得到遥控按键长按释放信息
			}
			IRC_MonitorFlag = 0;			
		}
		else
		{
			if(!IRC_LongKeyTimer)
			{
				if(!IRC_LongStartFlag)
				{
					IRC_LongStartFlag = 1;
					Msg = KeyBak | KEY_LONG_START;			//得到遥控按键长按开始的信息， 按键按下后大概1秒后产生长按信息
				}
				else
				{
					Msg = KeyBak | KEY_LONG;		//得到长按信息， 长按开始后，大概每300ms产生一次长按信息
				}
				IRC_LongKeyTimer = IRC_LONG_TIME;
			}			
		}
	}
	return Msg;
}
void IRC_MainLoop(void)
{
		IRC_init();
		Timer0_init();
		while(1)
		{
			IRC_Msg = IRC_GetMsg();
			if(IRC_Msg != KEY_INVALID)
			{
#ifdef PRINT_EN
				uart_printf("IRC_Msg = 0x%x\n",IRC_Msg);	
#endif		
			}
		}
}
#endif
