#line 1 "C:/Users/Lenovo/Desktop/irrecv/irct.c"
sbit ir_in at RA1_bit;
sbit ir_in_direction at TRISA1_bit;

unsigned char readbuff[64] absolute 0x500;
unsigned char writebuff[64] absolute 0x540;



unsigned int retint;
unsigned char ir_result[6];
char _dat[100];
unsigned char modifier = 0b00000000;
unsigned char volctrl = 0b00000000;
unsigned char key;
unsigned char ID;
int Repeat = 1;


int get_mark(void);
int get_space(void);
void nec_read(void);
unsigned char get_nec_byte(void);
void SendKeyCode(unsigned char _device, unsigned char _modifier, unsigned char _volctrl, unsigned char _key);
void SendKeyCode_func(int _repeat, unsigned char _device, unsigned char _modifier, unsigned char _volctrl, unsigned char _key);

void Interrupt(){
 USB_Interrupt_Proc();

 if (INT1IF_bit){
 INT1IF_bit = 0;
 INT1IE_bit = 0;
 ir_result[0] = 0;
 ir_result[1] = 0;
 ir_result[2] = 0;
 ir_result[3] = 0 ;
 nec_read();
 }
}


void main() {

 ANCON0 = 0x1F;
 ANCON1 = 0x1F;

 PLLEN_bit = 1;
 Delay_ms(150);

 HID_Enable(&readbuff,&writebuff);

 writebuff[3]=0;
 writebuff[4]=0;
 writebuff[5]=0;
 writebuff[6]=0;
 writebuff[7]=0;

 ir_in_direction = 1;

 T0CON = 0x85;
 TMR0L = 96;
 TMR0H = 0xff;


 Unlock_IOLOCK();
 PPS_Mapping_NoLock(1, _INPUT , _INT1);
 Lock_IOLOCK();


 INT1IF_bit = 0;
 INT1IE_bit = 1;
 INTEDG1_bit = 0;
 GIE_bit = 1;
 delay_ms(10);


 while (1);
}


void nec_read(void){
 signed int temp_nec;
 int count_byte;

 temp_nec = get_mark();
 if ((temp_nec > 1150) && (temp_nec < 1840)){
 temp_nec = get_space();
 if ((temp_nec > 460) && (temp_nec < 920)){
 for(count_byte = 0 ; count_byte < 4 ; count_byte++ ){
 ir_result[count_byte] = get_nec_byte();
 }
 }

 switch(ir_result[2]){

 case 2:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x51); break;
 case 5:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x52); break;
 case 9:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x2a); break;
 case 10:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x50); break;
 case 12:SendKeyCode_func(0,0x02,0b00001000,0b00000000,0x07); break;
 case 13:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x4b); break;
 case 16:SendKeyCode_func(1,0x01,0b00000000,0b00000010,0x00); break;
 case 17:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x4e); break;
 case 18:SendKeyCode_func(0,0x02,0b00000000,0b00000000,0x29); break;
 case 21:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x4d); break;
 case 22:SendKeyCode_func(0,0x01,0b00000000,0b00000100,0x00); break;
 case 23:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x4c); break;
 case 25:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x4a); break;
 case 27:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x49); break;
 case 28:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x28); break;
 case 29:SendKeyCode_func(0,0x02,0b00000000,0b00000000,0x2b); break;
 case 30:SendKeyCode_func(1,0x02,0b00000000,0b00000000,0x4f); break;
 case 31:SendKeyCode_func(0,0x02,0b00000000,0b00000000,0x65); break;
 case 64:SendKeyCode_func(0,0x02,0b00000100,0b00000000,0x28); break;
 case 76:SendKeyCode_func(1,0x01,0b00000000,0b00000001,0x00); break;
 case 77:SendKeyCode_func(0,0x02,0b00000100,0b00000000,0x3d); break;
 case 84:SendKeyCode_func(0,0x02,0b00001000,0b00000000,0x46); break;
 case 0:if(Repeat)SendKeyCode(ID, modifier, volctrl, key);delay_ms(10); break;

 default:Repeat = 0;ID = 0x02;modifier = 0b00000000;volctrl = 0b00000000;key = 0x00;
 }
 Delay_ms(10);
 }
 INT1IE_bit = 1;
}


unsigned char get_nec_byte(void){
char n;
unsigned char ir_byte;
signed int temp_ir;

 for(n=0 ; n<8 ; n++){
 temp_ir = get_mark();
 temp_ir = get_space();
 ir_byte >>= 1;
 if (temp_ir > 160){
 ir_byte += 0x80;
 }
 }
 return ir_byte;
}



int get_mark(void){
 TMR0H = 0;
 TMR0L = 0;
 while(!ir_in);
 retint = TMR0L;
 retint += (TMR0H<<8);
 return retint;
}


int get_space(void){
 TMR0H = 0;
 TMR0L = 0;
 while(ir_in);
 retint = TMR0L;
 retint += (TMR0H<<8);
 return retint;
}

void SendKeyCode(unsigned char _device, unsigned char _modifier, unsigned char _volctrl, unsigned char _key){
 writebuff[0] = _device;
 if (_device == 0x01){
 writebuff[1] = _volctrl;
 while(!HID_Write(writebuff,2));
 delay_ms(10);
 writebuff[1] = 0b00000000;
 while(!HID_Write(writebuff,2));
 delay_ms(10);
 }else if(_device == 0x02){
 writebuff[1] = _modifier;
 writebuff[2] = _key;
 while(!HID_Write(writebuff,8));
 delay_ms(10);
 writebuff[1] = 0b00000000;
 writebuff[2] = 0x00;
 while(!HID_Write(writebuff,8));
 delay_ms(10);
 }
}

void SendKeyCode_func(int _repeat, unsigned char _device, unsigned char _modifier, unsigned char _volctrl, unsigned char _key){
 Repeat = _repeat;
 ID = _device;
 modifier = _modifier;
 volctrl = _volctrl;
 key = _key;
 SendKeyCode(ID, modifier, volctrl, key);
 if(Repeat)delay_ms(200);else delay_ms(10);
}
