ir_recv.c

来自「一个非常重要的红外遥控接收的软件程序」· C语言 代码 · 共 1,214 行 · 第 1/2 页

C
1,214
字号
      }
      else
      {
        _bIRBitCnt <<= 4;

#ifdef IR_USE_REPEAT
        if (_bIRStatus != IR_READ_REPEAT_CODE)
        {
          _bIRStatus = IR_READ_REPEAT_CODE;
          _bIRBitCnt = IR_REAPEAT_CNT_LENGTH + 1; // it will be minus one later
        }
        else // error condition
        {
          _bIRStatus = IR_WAIT_NEW_CODE;
          _bIRBitCnt = 0;
        }
#endif /* IR_USE_REPEAT */
      }

      /* put the received key */
      if (_bIRKeyBuf != IR_NONE)
      {
        /* assume IR interrupt priority is the highest */
#ifdef IR_USE_REPEAT
        if (_bIRStatus == IR_READ_REPEAT_CODE)
        {
          if (_bIRBitCnt == 0)
          {
            _fgIRKeyValid = TRUE;
  #ifdef IR_CHECK_KEY_LOCK
            sFlagIR.fgKeyLock = TRUE;
            _bIRKeyReleaseCnt = IR_KEY_RELEASE_TIMEOUT;
  #endif
            _bIRBitCnt = IR_REAPEAT_CNT_LENGTH;
          }
          else
          {
            _bIRBitCnt--;
          }
          break;
        }
        else
#endif // IR_USE_REPEAT
        {
          _fgIRKeyValid = TRUE;
          _bIRKey0 = _bIRKeyBuf;
        }
      } /* _bIRKeyBuf != IR_NONE */

      _bIRStatus = IR_WAIT_NEW_CODE;
#ifdef IR_CHECK_KEY_LOCK
      sFlagIR.fgKeyLock = FALSE;
      _bIRKeyPressCnt = 0;
      _bIRKeyReleaseCnt = 0;
#endif
      break;
    } /* IR_WAIT_LEAD_CODE */

    default:
      _bIRStatus = IR_WAIT_NEW_CODE;
#ifdef IR_CHECK_KEY_LOCK
      sFlagIR.fgKeyLock = FALSE;
      _bIRKeyPressCnt = 0;
      _bIRKeyReleaseCnt = 0;
#endif
      break;
  } /* switch (_bIRStatus) */

 
  /* before interrupt return, clear IR */
  WriteBIM(BIM_CRBSY, CRIR);
}
  #else /* NEC format */

#ifdef IR_USE_2X
  #define IR_SA_PERIOD           29   /* 560us/9.5us = 58, 58 / 2 = 29 */
  
  #define IR_OVERFLOW            142  /* 38 ms */
  #define IR_LEAD_CENTER         12   /* 3.37 ms */
  #define IR_REPEAT_CENTER       3    /* 2.25 ms */
  #define IR_REPEAT_MAX          8
  #define IR_REPEAT_MIN          6
  #define IR_DATA1               3
  #define IR_DATA0               2
  #define IR_END_LEN             IR_END_31
#else /* 1X sampling */
  //#define IR_SA_PERIOD           58  /* 560us/9.5us = 58 */
  #define IR_SA_PERIOD           50  /* 58 * 0.75 ~ 43.5 */
  
  #define IR_OVERFLOW            68  /* 38 ms */
  #define IR_LEAD_CENTER         6   /* 3.37 ms */
  #define IR_REPEAT_CENTER       3   /* 2.25 ms */
  #define IR_REPEAT_MAX          4
  #define IR_REPEAT_MIN          3
  #define IR_DATA1               2
  #define IR_DATA0               1
  #define IR_END_LEN             IR_END_15
#endif /* 1X sampling */

/************************************************************************
     Function : 
  Description : 
    Parameter : 
    Return    : 
************************************************************************/
void vHwIrInit(void)
{
  /* setup H/W IR */
#ifdef IR_BIT_REVERSE
  WriteBIM(BIM_IR_CFGH, IR_END_LEN + IR_ORDI + IR_IGSYN + IR_HW);
#else
  WriteBIM(BIM_IR_CFGH, IR_END_LEN + IR_IGSYN + IR_HW);
#endif
  
  /* sampling frequency */
  WriteBIM(BIM_IR_CFGL, IR_SA_PERIOD);
  
  /* IR 0/1 threshold */
  WriteBIM(BIM_IRTHD, IR_DATA1 - 1); /* greater than */
  
  /* clear IR */
  WriteBIM(BIM_CRBSY, CRIR);
}

/************************************************************************
     Function : 
  Description : 
    Parameter : bIRH: the first pulse width
    Return    : 
************************************************************************/
#ifdef TEST_VFD
extern xdata BYTE _bCon_Key_flag;
#endif
void vIrRecv(BYTE bIRVal)
{ 
  if (bIRVal > IR_LEAD_CENTER)
  {
#ifdef IR_USE_REPEAT
    if ((bIRVal > IR_OVERFLOW) && (_bIRStatus == IR_READ_REPEAT_CODE))
    {
      /* only allow overflow when read repeat code */
      /* before interrupt return, clear IR */
      WriteBIM(BIM_CRBSY, CRIR);
      return;
    }
    else
#endif
    {
      _bIRStatus = IR_WAIT_LEAD_CODE;
#ifdef IR_CHECK_KEY_LOCK
      sFlagIR.fgKeyLock = FALSE;
      _bIRKeyPressCnt = 0;
      _bIRKeyReleaseCnt = 0;
#endif
    }
  }

  switch (_bIRStatus)
  {
    case IR_WAIT_NEW_CODE:
    {
      /* dummy case */
      break;
    }

    case IR_WAIT_LEAD_CODE:
    {
      /* check bit count first */
      if (bReadBIM(BIM_IR_CNT) != 33) /* include lead code */
      {
        break;
      }
      /* read out 2nd pulse width */
      bIRVal = bReadBIM(BIM_IR_VAL);
      /* custome code */
      bIRVal = bReadBIM(BIM_IR_VAL);
#ifdef TEST_VFD//Custom code
     if(_bCon_Key_flag)
          DBGLogB(0xf1, 0xf1, bIRVal, bIRVal) ; 
#endif
#ifndef TEST_VFD
       if (bIRVal == IR_CUSTOM_CODE || bIRVal == IR_PANEL_CUSTOM_CODE) 
      {
       if(bIRVal == IR_CUSTOM_CODE)
           is_panel_key=0;
        else
           is_panel_key=1;

      }
      else
      {
        _bIRStatus = IR_WAIT_NEW_CODE;
        break;
      }
 #endif
      /* sub-custome code */
      bIRVal = bReadBIM(BIM_IR_VAL);
#ifdef TEST_VFD  //Custom sub code
      if(_bCon_Key_flag)
          DBGLogB(0xf2, 0xf2, bIRVal, bIRVal) ; 
#endif

#ifndef TEST_VFD  //Custom sub code
       if (bIRVal == IR_CUSTOM_SUB_CODE || bIRVal == IR_PANEL_CUSTOM_SUB_CODE) 
      {
       if(bIRVal == IR_CUSTOM_SUB_CODE)
           is_panel_key=0;
        else
           is_panel_key=1;

      }
      else
      {
        _bIRStatus = IR_WAIT_NEW_CODE;
        break;
      }
#endif
      /* read data code */
      _bIRKeyBuf = bReadBIM(BIM_IR_VAL);
#ifdef TEST_VFD //Custom key code
      if(_bCon_Key_flag)
             DBGLogB(0xf3, 0xf3, _bIRKeyBuf, _bIRKeyBuf) ; 
#endif
      /* read data code inverse */
      bIRVal = bReadBIM(BIM_IR_VAL);
      /* check data code inverse */
      if (bIRVal != (~_bIRKeyBuf))
      {
        _bIRStatus = IR_WAIT_NEW_CODE;
        break;
      }
      /* all bit transfered, convert the IR key to internal key */
#if (IR_KEY_GAP_SIZE > 0)
      if (_bIRKeyBuf >= IR_KEY_GAP_START)
      {
        _bIRKeyBuf -= IR_KEY_GAP_SIZE;
      }
#endif

      if (_bIRKeyBuf < IR_KEY_TBL_SIZE)
      {
        /* look up the mapping table */
        if(is_panel_key)
        _bIRKeyBuf = _pbPanelKeyTbl[_bIRKeyBuf];
        else
        _bIRKeyBuf = _pbIRKeyTbl[_bIRKeyBuf];

#ifdef IR_SUPPORT_SHIFT_KEY
        if (_fgIRShiftKey)
        {
          if (_bIRKeyBuf <= IR_SHIFT_KEY_MAX)
          {
            _bIRKeyBuf = _pbShiftIRKeyTbl[_bIRKeyBuf];
          }
        }
#endif
//vOsdShowHex(_bIRKeyBuf,_bIRKeyBuf,0,0);

        /* put the received key */
        if (_bIRKeyBuf != IR_NONE)
        {
        
                #ifdef DVD_RECEIVER
   if(_bSysMode==SYS_TUNER)
   {     if(is_panel_key)
        {
        if((_bIRKeyBuf==IR_ENTER)  )
            _bIRKeyBuf=IR_TUNER_AUTO_UP;

        if((_bIRKeyBuf==IR_PLAY_PAUSE)  )
            _bIRKeyBuf=IR_MEMO;

        if((_bIRKeyBuf==IR_VOCAL_ASSIST)  )
            _bIRKeyBuf=IR_FM_MODE;

        if((_bIRKeyBuf==IR_STOP)  )
            _bIRKeyBuf=IR_PRE_TUNER;

        if((_bIRKeyBuf==IR_FF) )
            _bIRKeyBuf=IR_RIGHT;

        if((_bIRKeyBuf==IR_FR)  )
            _bIRKeyBuf=IR_LEFT;

        if((_bIRKeyBuf==IR_NEXT)  )
            _bIRKeyBuf=IR_UP;

        if((_bIRKeyBuf==IR_PREV) )
            _bIRKeyBuf=IR_DOWN;

        }
       else
       {
      if(_bIRKeyBuf==IR_CLEAR)
            _bIRKeyBuf=IR_TUNER;

      if(_bIRKeyBuf==IR_PAL_NTSC)
            _bIRKeyBuf=IR_FM_MODE;

      if(_bIRKeyBuf==IR_PROGRAM)
            _bIRKeyBuf=IR_PRE_TUNER;
       	
       	
       	
       	}
  } 
    
 	#endif

         /* assume IR interrupt priority is the highest */
          _fgIRKeyValid = TRUE;
          _bIRKey0 = _bIRKeyBuf;


#ifdef IR_USE_REPEAT
  #ifdef IR_CHECK_KEY_LOCK
          sFlagIR.fgKeyLock = TRUE;
          _bIRKeyPressCnt = IR_KEY_PRESS_TIMEOUT;
          _bIRKeyReleaseCnt = IR_KEY_RELEASE_TIMEOUT;
          _bIRStatus = IR_READ_REPEAT_CODE;
          _bIRBitCnt = IR_REAPEAT_CNT_LENGTH;
          break;
  #else // not define IR_CHECK_KEY_LOCK
    #if 0
          _bIRStatus = IR_READ_REPEAT_CODE;
          _bIRBitCnt = IR_REAPEAT_CNT_LENGTH;
          break;
    #else
          if ((_bIRKey0 == IR_VOLUME_UP) || (_bIRKey0 == IR_VOLUME_DOWN))
          {
            _bIRStatus = IR_READ_REPEAT_CODE;
            _bIRBitCnt = IR_REAPEAT_CNT_LENGTH;
            break;
          }
         #ifdef DVD_RECEIVER
           else if((_bSysMode==SYS_TUNER)&&((_bIRKey0==IR_LEFT)||(_bIRKey0==IR_RIGHT)))
          {
            _bIRStatus = IR_READ_REPEAT_CODE;
            _bIRBitCnt = IR_REAPEAT_CNT_LENGTH;
           _bKeyTimCnt=VFD_DELAY_KEY_TIME;
           _bkeyPressCount=0;
            break;
          }
         #endif           
    #endif
  #endif // not define IR_CHECK_KEY_LOCK
#endif
        }
      } /* valid key */

      _bIRStatus = IR_WAIT_NEW_CODE;
#ifdef IR_CHECK_KEY_LOCK
      sFlagIR.fgKeyLock = FALSE;
      _bIRKeyPressCnt = 0;
      _bIRKeyReleaseCnt = 0;
#endif
      break;
    } /* IR_WAIT_LEAD_CODE */

#ifdef IR_USE_REPEAT
    case IR_READ_REPEAT_CODE:
    {
       /* check bit count first */
      if (bReadBIM(BIM_IR_CNT) != 1)
      {
        _bIRStatus = IR_WAIT_NEW_CODE;
  #ifdef IR_CHECK_KEY_LOCK
        sFlagIR.fgKeyLock = FALSE;
        _bIRKeyPressCnt = 0;
        _bIRKeyReleaseCnt = 0;
  #endif
        break;
      }

  #if 0
      if (bIRVal == IR_REPEAT_CENTER)
  #else
      if ((bIRVal <= IR_REPEAT_MAX) && (bIRVal >= IR_REPEAT_MIN))
  #endif
      {
        if (_bIRKey0 != IR_NONE)
        {
          if (_bIRBitCnt == 0)
          {
             #ifndef DVD_RECEIVER
            _fgIRKeyValid = TRUE;
            #else
			if(_bSysMode==SYS_TUNER && ((_bIRKey0==IR_RIGHT) ||(_bIRKey0==IR_LEFT) ))
			{
            		     /*	if(_bKeyTimCnt<1)*/
            		 if(_bkeyPressCount>2)	  
            			{
					if(_bIRKey0==IR_RIGHT)
					{
				        	_bIRKey0=IR_TUNER_AUTO_UP; //turn to auto search
					       // _bkeyPressCount=0;                             
					       _bKeyTimCnt=0;
					       _bIRStatus = IR_WAIT_NEW_CODE;
				 	      _fgIRKeyValid = TRUE;
				 	      break;
				 	      } 
					

					if(_bIRKey0==IR_LEFT)
					{
					
				       		_bIRKey0=IR_TUNER_AUTO_DOWN; //turn to auto search
					      // _bkeyPressCount=0;                              
					       _bKeyTimCnt=0;
					       _bIRStatus = IR_WAIT_NEW_CODE;
				 	      _fgIRKeyValid = TRUE;
				 	      break;
				 	  
					}
					
            			}
            			else
            			 {
            			  _bkeyPressCount++;
            			  _fgIRKeyValid = TRUE;
            			 } 
			}
			else
			{
			_fgIRKeyValid = TRUE;
				
            		
			}
            
            #endif
  #ifdef IR_CHECK_KEY_LOCK
            sFlagIR.fgKeyLock = TRUE;
            _bIRKeyReleaseCnt = IR_KEY_RELEASE_TIMEOUT;
  #endif
            _bIRBitCnt = IR_REAPEAT_CNT_LENGTH;
          }
          else
          {
            _bIRBitCnt--;
          }
          break;
        }
      }

      _bIRStatus = IR_WAIT_NEW_CODE;
  #ifdef IR_CHECK_KEY_LOCK
      sFlagIR.fgKeyLock = FALSE;
      _bIRKeyPressCnt = 0;
      _bIRKeyReleaseCnt = 0;
  #endif
      break;
    }
#endif /* IR_USE_REPEAT */

    default:
      _bIRStatus = IR_WAIT_NEW_CODE;
#ifdef IR_CHECK_KEY_LOCK
      sFlagIR.fgKeyLock = FALSE;
      _bIRKeyPressCnt = 0;
      _bIRKeyReleaseCnt = 0;
#endif
      break;
  } /* switch (_bIRStatus) */

 
  /* before interrupt return, clear IR */
  WriteBIM(BIM_CRBSY, CRIR);
}
  #endif /* NEC format */
#endif /* IR_USE_HW */

#else /* PLAYER_NO_IR */

// *********************************************************************
// Function : void vIrInit(void)
// Description : Called when system initial, to reset IR and UI
// Parameter : None
// Return    : None
// *********************************************************************
code const BYTE _pbIRSeg[1] = { 0 };

void IrInit(void) large
{
  // reset IR related variable
  _fgIRKeyValid = FALSE;
  _bIRStatus = 0; /* IR_WAIT_NEW_CODE */
  _bIRBitCnt = 0;
  _bIRKeyBuf = 0;

#ifdef PLAYER_POWERDOWN
  #ifdef PLAYER_ACON_STANDBY
  if (_testbit_(_fgPowerOffTrayIn))
  {
    _bPlayPostKey = IR_POWER;
  }
  else
  #endif /* PLAYER_ACON_STANDBY */
#endif
  {
    _bPlayPostKey = IR_NONE;
  }

  _bIRKey =  IR_NONE;
  _bIRKey0 = IR_NONE;

#ifdef IR_SUPPORT_SHIFT_KEY
  _fgIRShiftKey = FALSE;
#endif

#ifdef IR_CHECK_KEY_LOCK
  sFlagIR.fgKeyLock = FALSE;
  _bIRKeyPressCnt = 0;
  _bIRKeyReleaseCnt = 0;
#endif

  _bIRKeyBuf = _pbIRSeg[0];

  if (_bIRKeyBuf == 0)
  {
    return;
  }
  else
  {
    _bIRKeyBuf = 0;
  }

  return;
}

/************************************************************************
     Function : 
  Description : 
    Parameter : 
    Return    : 
************************************************************************/
void vIrRecv(BYTE bIRH)
{
  /* before interrupt return, clear IR */
  //WriteBIM(BIM_CRBSY, CRIR);
}

/************************************************************************
     Function : 
  Description : 
    Parameter : 
    Return    : 
************************************************************************/
void vHwIrInit(void)
{
}

#endif /* PLAYER_NO_IR */

#ifdef IR_CHECK_KEY_LOCK
/************************************************************************
     Function : 
  Description : 
    Parameter : 
    Return    : 
************************************************************************/
#pragma disable
void vIrKeyPressTimeoutCheck(void)
{
  if (_bIRKeyPressCnt > 0)
  {
    _bIRKeyPressCnt--;
    
    if (_bIRKeyPressCnt == 0)
    {
      _bIRKeyReleaseCnt = 0;
      return;
    }
  }

  if (_bIRKeyReleaseCnt > 0)
  {
    _bIRKeyReleaseCnt--;

    if (_bIRKeyReleaseCnt == 0)
    {
      sFlagIR.fgKeyLock = FALSE;
      _bIRKeyPressCnt = 0;
      _bIRKeyReleaseCnt = 0;
      return;
    }
  }
}

/************************************************************************
     Function : 
  Description : 
    Parameter : 
    Return    : 
************************************************************************/
#pragma disable
void vIrKeyForceRelease(void)
{
  sFlagIR.fgKeyLock = FALSE;
  _bIRKeyPressCnt = 0;

  if (_bIRStatus == IR_READ_REPEAT_CODE)
  {
    _bIRStatus = IR_WAIT_NEW_CODE;
  }
}
#endif /* IR_CHECK_KEY_LOCK */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?