/************************************************************************/
/* Copyright(c) 2003, ShenZhen, All Rights Reserved */
/* Created By DuirkWang, 08.21.2003 */
/* File: Ir.c */
/* Ver: 1.0.1 */
/* Project: */
/* Description: NEC format IR process routine */
/************************************************************************/
#include "Config.h"
#include "Reg52.h"
#include "global.h"
#include "Extern.h"
#include "Event.h"
#include "tools.h"
#include "i2c.h"
#include "ir.h"
#include "Avswitch.h"
#ifdef CD_CHANGER
#include "Cdc.h"
#endif
#include "command.h"
#include "ir_lut.h"
// Hoe many repeat message will ignore before send it to the FSM's
#define REPEAT_DELAY 5
enum {
IR_IDLE,
IR_HEADER,
IR_CUSTOM
};
BYTE idata IrState = IR_IDLE;
Uint16 idata IrData = 0;
BYTE xdata IrDataBitCnt = 0;
WORD xdata ir_code=0x0ff;
WORD xdata custom_code; // Hold the custom (remote ID) code
BYTE xdata last_ir_code=0x0ff; // Hold the ir code
BYTE xdata repeat_delay = REPEAT_DELAY; // Repeat code counter
BYTE xdata valid_repeat = 0;
extern BIT dac_key_flag;
Uint8 idata eject_flag_cnt = 0;
extern BIT open_require_flag;
extern BIT close_require_flag;
#define IR_LEADER 27000
#define DATA_0 2240
#define DATA_1 4480
#define IR_REPEAT_LEADER 23000
#define LEADER_MARGIN 800
#define IR_MARGIN 300
void Require_flag_set();
void Close_flag_set();
void InitInt0()
{
IT0=1; //down edge interrupt
PX0=1;
IE0=0;
EX0=1; //open INT0
EA=1;
}
void IR_init()
{
TMOD&=0x0F;
TMOD|=0x10;
TH1 = 0;
TL1 = 0;
TR1 = 0;
}
static void IR_decode()
{
WORD width;
BIT IrError = 0;
HIBYTE(width) = TH1;
LOBYTE(width) = TL1;
if (TF1 == 1) {
IrState = IR_IDLE;
TF1 = 0;
}
switch (IrState) {
case IR_IDLE:
IrState = IR_HEADER;
break;
case IR_HEADER:
if ((width >= IR_LEADER - LEADER_MARGIN) && (width <= IR_LEADER +LEADER_MARGIN)) {
IrState = IR_CUSTOM;
IrData = IrDataBitCnt = 0;
}else if((width >= IR_REPEAT_LEADER - LEADER_MARGIN) &&
(width <= IR_REPEAT_LEADER +LEADER_MARGIN))
{
if (repeat_delay)
{
eject_flag_cnt = 3;
// Delay before sendnig the first repeat
repeat_delay--;
}
else
{ // repeat last ir_code
if (valid_repeat)
{
if(last_ir_code == IRKC_SETUP)
{
dac_key_flag = 1;
valid_repeat = 0;
Send_Event( IE_UI_REMOTE_INPUT);
}
#ifndef AVC500_FUTE
else if((last_ir_code == IRKC_EJECT2) || (last_ir_code == IRKC_EJECT))
{
Close_flag_set();
Send_Event( IE_UI_REMOTE_INPUT);
valid_repeat = 0;
}
#endif
else
{
ir_code=last_ir_code;
Send_Event( IE_UI_REMOTE_INPUT);
if((ir_code == IRKC_SEARCH_NEXT) || (ir_code == IRKC_SEARCH_PREV)
|| (ir_code == IRKC_SEARCH_NEXT2) || (ir_code == IRKC_SEARCH_PREV2)
#ifdef BLACK_VOLTAGE
|| (ir_code == IRKC_BLACK_SCREEN)
#endif
)
valid_repeat = 0;
}
}
}
IrState = IR_HEADER;
}
else
IrError = 1;
break;
case IR_CUSTOM:
IrData <<= 1;
if ((width >= DATA_1 - IR_MARGIN ) && (width <= DATA_1 + IR_MARGIN))
IrData |= 1;
else if ((width >= DATA_0 - IR_MARGIN) && (width <= DATA_0 + IR_MARGIN))
;
else
IrError = 1;
IrDataBitCnt ++;
if (IrDataBitCnt == 16) {
if((IrData != g_ir_system_code) &&
(IrData != g_ir_system_code2))
IrError = 1;
else
custom_code = IrData ;
} else if (IrDataBitCnt == 32) {
{
ir_code = (IrData >> 8) & 0xff;
last_ir_code=ir_code;
if(custom_code ==g_ir_system_code)
{
switch(ir_code)
{
case IRKC_LEFT:
case IRKC_RIGHT:
case IRKC_UP:
case IRKC_DOWN:
valid_repeat = 1;
repeat_delay=REPEAT_DELAY;
break;
case IRKC_FASTR:
case IRKC_FASTF:
if((AV_ID == FM_ID) || (AV_ID == TV_ID))
{
if(ir_code == IRKC_FASTR)
last_ir_code = IRKC_SEARCH_PREV;
else if(ir_code == IRKC_FASTF)
last_ir_code = IRKC_SEARCH_NEXT;
valid_repeat = 1;
repeat_delay=REPEAT_DELAY;
}
else
valid_repeat = 0;
break;
case IRKC_SETUP:
valid_repeat = 1;
repeat_delay=4 * REPEAT_DELAY;
break;
#ifndef AVC500_FUTE
case IRKC_EJECT:
last_ir_code = IRKC_EJECT;
valid_repeat = 1;
repeat_delay=REPEAT_DELAY;
Require_flag_set();
eject_flag_cnt = 3;
break;
#endif
default:
valid_repeat=0;
break;
}
}
else if(custom_code == g_ir_system_code2)
{
switch(ir_code)
{
case IRKC_LEFT2:
case IRKC_RIGHT2:
case IRKC_UP2:
case IRKC_DOWN2:
valid_repeat = 1;
repeat_delay=REPEAT_DELAY;
break;
#ifndef AVC500_FUTE
case IRKC_EJECT2:
last_ir_code = IRKC_EJECT2;
valid_repeat = 1;
repeat_delay=REPEAT_DELAY;
Require_flag_set();
eject_flag_cnt = 3;
break;
#endif
#ifdef BLACK_VOLTAGE
case IRKC_TIME2:
last_ir_code = IRKC_BLACK_SCREEN;
valid_repeat = 1;
repeat_delay=REPEAT_DELAY;
break;
#endif
default:
valid_repeat=0;
break;
}
}
Send_Event(IE_UI_REMOTE_INPUT);
}
IrState = IR_IDLE;
}
break;
}
if (IrError)
{
valid_repeat = 0;
IrState = IR_IDLE;
}
}
void IR_interrupt_server() interrupt 0
{
Disable_TIME1;
IR_decode();
Clear_TIME1;
Enable_TIME1;
}