⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ir_recv.c

📁 一个非常重要的红外遥控接收的软件程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************    MTK CONFIDENTIAL & COPYRIGHTED     ****************/
/***************                                       ****************/
/***************  $Modtime:: 03/01/13 4:49p    $       ****************/
/***************  $Revision:: 27               $       ****************/
/***************                                       ****************/
/*************** Description : IR receive module       ****************/
/***************                                       ****************/
/***************     Company : MediaTek Inc.           ****************/
/***************  Programmer :                         ****************/
/**********************************************************************/

#define _IR_RECV_C_

#pragma NOAREGS
#include "general.h"

#ifndef PLAYER_NO_IR

#include "./custom/demo/irkey.c"
#ifdef DVD_RECEIVER
extern xdata BYTE _bSysMode;
extern xdata BYTE  _bKeyTimCnt;

 xdata BYTE  _bkeyPressCount;
 
 
#include "./custom/demo/receiver/tuner.h"
#define VFD_DELAY_KEY_TIME 60
#endif
bit is_panel_key;
//extern void vOsdShowHex(BYTE bnum1,BYTE bnum2,BYTE bnum3,BYTE bnum4 ) large;
// *********************************************************************
// Function : void vIrInit(void)
// Description : Called when system initial, to reset IR and UI
// Parameter : None
// Return    : None
// *********************************************************************
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

#ifdef IR_USE_RC6
  /* clear IR */
  WriteBIM(BIM_CRBSY, CRIR);
#endif

  return;
}

#define IR_WAIT_NEW_CODE        0
#define IR_WAIT_LEAD_CODE       1
#define IR_READ_CUSTOM_CODE     2
#define IR_READ_SUB_CUSTOM_CODE 3
#define IR_READ_DATA_CODE       4
#define IR_READ_DATA_CODE_INV   5
#define IR_READ_REPEAT_CODE     6

#define IR_CUSTOM_LENGHT        8
#define IR_SUB_CUSTOM_LENGHT    8
#define IR_DATA_LENGTH          8

#ifndef IR_CHECK_KEY_LOCK
  #define IR_REAPEAT_CNT_LENGTH   2
#else
  #define IR_REAPEAT_CNT_LENGTH   1

  #define IR_KEY_PRESS_TIMEOUT    100 // 20ms base, 2 seconds
  #define IR_KEY_RELEASE_TIMEOUT  25  // 20ms base, 500ms
#endif

#ifndef IR_USE_HW /* original routine */

#define IR_OVERFLOW            250  /* 38 ms */
#define IR_LEAD_CENTER         23   /* 3.37 ms */
#define IR_REPEAT_CENTER       14   /* 2.25 ms */
#define IR_DATA_CENTER         8    /* 1.21 ms */

#define IR_LEAD_MAX            30
#define IR_LEAD_MIN            28
#define IR_DATA1_MAX           12
#define IR_DATA1_MIN           10
#define IR_DATA0_MAX            6
#define IR_DATA0_MIN            2
#define IR_REPEAT_MIN          13
#define IR_REPEAT_MAX          15

/************************************************************************
     Function : 
  Description : 
    Parameter : 
    Return    : 
************************************************************************/
void vHwIrInit(void)
{
  BYTE bTmp;

  /* setup H/W IR */
  WriteBIM(BIM_IR_CFGH, IR_END_15 + IR_IGSYN + IR_HW);
  
  /* clear IR */
  WriteBIM(BIM_CRBSY, CRIR);

  /* setup S/W IR */
  WriteBIM(BIM_IR_CFGH, 0);

  /* read clear IR */
  bTmp = bReadBIM(BIM_IRH);
}

/************************************************************************
     Function : 
  Description : 
    Parameter : 
    Return    : 
************************************************************************/
void vIrRecv(BYTE bIRH)
{
  if (bIRH > IR_LEAD_CENTER)
  {
#ifdef IR_USE_REPEAT
    if ((bIRH > IR_OVERFLOW) && (_bIRStatus == IR_READ_REPEAT_CODE))
    {
      /* only allow overflow when read repeat code */
      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:
    {
      /* why do not check the lead code length? */
      _bIRStatus = IR_READ_CUSTOM_CODE;
      _bIRBitCnt = IR_CUSTOM_LENGHT;
      _bIRKeyBuf = 0;
#ifdef IR_CHECK_DATA_CODE_INV
      _bIRKeyBufInv = 0;
#endif
      break;
    }

    case IR_READ_CUSTOM_CODE:
    {
#ifdef IR_STRICT_TIMING
      if ((bIRH >= IR_DATA1_MIN) && (bIRH <= IR_DATA1_MAX))
      {
  #ifdef IR_BIT_REVERSE
        _bIRKeyBuf |= 0x01;
  #else
        _bIRKeyBuf |= 0x80;
  #endif
      }
      else if ((bIRH < IR_DATA0_MIN) || (bIRH > IR_DATA0_MAX))
      {
        /* not 1 neither 0, sequence fail! */
        _bIRStatus = IR_WAIT_NEW_CODE;
        break;
      }
#else
      if (bIRH > IR_DATA_CENTER)
      {
  #ifdef IR_BIT_REVERSE
        _bIRKeyBuf |= 0x01;
  #else
        _bIRKeyBuf |= 0x80;
  #endif
      }
#endif

      _bIRBitCnt--;

      if (_bIRBitCnt == 0)
      {
        if (_bIRKeyBuf != IR_CUSTOM_CODE)
        {
          _bIRStatus = IR_WAIT_NEW_CODE;
        }
        else
        {
          _bIRStatus = IR_READ_SUB_CUSTOM_CODE;
          _bIRBitCnt = IR_SUB_CUSTOM_LENGHT;
          _bIRKeyBuf = 0;
          break;
        }
      }
      else
      {
#ifdef IR_BIT_REVERSE
         _bIRKeyBuf <<= 1;
#else
         _bIRKeyBuf >>= 1;
#endif
      }

      break;
    }

    case IR_READ_SUB_CUSTOM_CODE:
    {
#ifdef IR_STRICT_TIMING
      if ((bIRH >= IR_DATA1_MIN) && (bIRH <= IR_DATA1_MAX))
      {
  #ifdef IR_BIT_REVERSE
        _bIRKeyBuf |= 0x01;
  #else
        _bIRKeyBuf |= 0x80;
  #endif
      }
      else if ((bIRH < IR_DATA0_MIN) || (bIRH > IR_DATA0_MAX))
      {
        /* not 1 neither 0, sequence fail! */
        _bIRStatus = IR_WAIT_NEW_CODE;
        break;
      }
#else
      if (bIRH > IR_DATA_CENTER)
      {
  #ifdef IR_BIT_REVERSE
        _bIRKeyBuf |= 0x01;
  #else
        _bIRKeyBuf |= 0x80;
  #endif
      }
#endif

      _bIRBitCnt--;

      if (_bIRBitCnt == 0)
      {
        if (_bIRKeyBuf != IR_CUSTOM_SUB_CODE)
        {
          _bIRStatus = IR_WAIT_NEW_CODE;
        }
        else
        {
          _bIRStatus = IR_READ_DATA_CODE;
          _bIRBitCnt = IR_DATA_LENGTH;
          _bIRKeyBuf = 0;
        }
      }
      else
      {
#ifdef IR_BIT_REVERSE
        _bIRKeyBuf <<= 1;
#else
        _bIRKeyBuf >>= 1;
#endif
      }

      break;
    }

    case IR_READ_DATA_CODE:
    {
#ifdef IR_STRICT_TIMING
      if ((bIRH >= IR_DATA1_MIN) && (bIRH <= IR_DATA1_MAX))
      {
  #ifdef IR_BIT_REVERSE
        _bIRKeyBuf |= 0x01;
  #else
        _bIRKeyBuf |= 0x80;
  #endif
      }
      else if ((bIRH < IR_DATA0_MIN) || (bIRH > IR_DATA0_MAX))
      {
        /* not 1 neither 0, sequence fail! */
        _bIRStatus = IR_WAIT_NEW_CODE;
        break;
      }
#else
      if (bIRH > IR_DATA_CENTER)
      {
  #ifdef IR_BIT_REVERSE
        _bIRKeyBuf |= 0x01;
  #else
        _bIRKeyBuf |= 0x80;
  #endif
      }
#endif

      _bIRBitCnt--;

      if (_bIRBitCnt == 0)
      {
        _bIRStatus = IR_READ_DATA_CODE_INV;
        _bIRBitCnt = IR_DATA_LENGTH;
        /* don't clear _bIRKeyBuf */
      }
      else
      {
#ifdef IR_BIT_REVERSE
        _bIRKeyBuf <<= 1;
#else
        _bIRKeyBuf >>= 1;
#endif
      }

      break;
    }

    case IR_READ_DATA_CODE_INV:
    {
#ifdef IR_CHECK_DATA_CODE_INV
  #ifdef IR_STRICT_TIMING
      if ((bIRH >= IR_DATA1_MIN) && (bIRH <= IR_DATA1_MAX))
      {
    #ifdef IR_BIT_REVERSE
        _bIRKeyBufInv |= 0x01;
    #else
        _bIRKeyBufInv |= 0x80;
    #endif
      }
      else if ((bIRH < IR_DATA0_MIN) || (bIRH > IR_DATA0_MAX))
      {
        /* not 1 neither 0, sequence fail! */
        _bIRStatus = IR_WAIT_NEW_CODE;
        break;
      }
  #else
      if (bIRH > IR_DATA_CENTER)
      {
    #ifdef IR_BIT_REVERSE
        _bIRKeyBufInv |= 0x01;
    #else
        _bIRKeyBufInv |= 0x80;
    #endif
      }
  #endif
#endif

      _bIRBitCnt--;

      /* all bit transfered, convert the IR key to internal key */
      if (_bIRBitCnt == 0)
      {
#ifdef IR_CHECK_DATA_CODE_INV
        if (_bIRKeyBufInv != (~_bIRKeyBuf))
        {
          _bIRKeyBufInv = 0;
          _bIRStatus = IR_WAIT_NEW_CODE;
          break;
        }
        else
        {
          _bIRKeyBufInv = 0;
        }
#endif
#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 */
          _bIRKeyBuf = _pbIRKeyTbl[_bIRKeyBuf];

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

          /* put the received key */
          if (_bIRKeyBuf != IR_NONE)
          {
            /* 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;
  #endif
            _bIRStatus = IR_READ_REPEAT_CODE;
            _bIRBitCnt = IR_REAPEAT_CNT_LENGTH;
            break;
#endif
          }
        } /* valid key */

        _bIRStatus = IR_WAIT_NEW_CODE;
      }
#ifdef IR_CHECK_DATA_CODE_INV
      else
      {
  #ifdef IR_BIT_REVERSE
        _bIRKeyBufInv <<= 1;
  #else
        _bIRKeyBufInv >>= 1;
  #endif
      }
#endif

      break;
    }

#ifdef IR_USE_REPEAT
    case IR_READ_REPEAT_CODE:
    {
  #if 0
      if (bIRH == IR_REPEAT_CENTER)
  #else
      if ((bIRH >= IR_REPEAT_MIN) && (bIRH <= IR_REPEAT_MAX))
  #endif
      {
        if (_bIRKey0 != IR_NONE)
        {
          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;
        }
      }

      _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) */
}

#else /* IR_USE_HW */
  #ifdef IR_USE_RC6

#define IR_SA_PERIOD           46  /* 446us/9.5us = 46 */

#define IR_BIT_CNT             30 /* for IR_END_15, 22+8=30 */

/************************************************************************
     Function : 
  Description : 
    Parameter : 
    Return    : 
************************************************************************/
void vHwIrInit(void)
{
  /* setup H/W IR */
  WriteBIM(BIM_IR_CFGH, IR_END_15 + IR_IGSYN + IR_ORDI + IR_RC5 + IR_HW);
  
  /* sampling frequency */
  WriteBIM(BIM_IR_CFGL, IR_SA_PERIOD);
  
  /* IR 0/1 threshold */
  WriteBIM(BIM_IRTHD, 1); /* greater than */
  
  /* clear IR */
  WriteBIM(BIM_CRBSY, CRIR);
}

/************************************************************************
     Function : 
  Description : 
    Parameter : bIRH: the first pulse width
    Return    : 
************************************************************************/
void vIrRecv(BYTE bIRVal)
{ 
  /* we don't care first pulse */

  switch (_bIRStatus)
  {
    case IR_WAIT_NEW_CODE:
    case IR_WAIT_LEAD_CODE:
#ifdef IR_USE_REPEAT
    case IR_READ_REPEAT_CODE:
#endif
    {
      /* check bit count first */
      if (bReadBIM(BIM_IR_CNT) != IR_BIT_CNT)
      {
        break;
      }

      /* read out 2nd pulse width */
      bIRVal = bReadBIM(BIM_IR_VAL);

      /* check leading code */
      bIRVal = bReadBIM(BIM_IR_VAL);
      _bIRKeyBuf = (bIRVal & 0x03);
      bIRVal &= 0xFC;
      if ((bIRVal != 0x84) && (bIRVal != 0x88))
      {
        _bIRStatus = IR_WAIT_NEW_CODE;
        break;        
      }

      /* get control code */
      _bIRBitCnt |= (bIRVal & 0x0f);

      /* check system code */
      bIRVal = bReadBIM(BIM_IR_VAL);
      _bIRKeyBuf <<= 6;
      _bIRKeyBuf |= (bIRVal >> 2);
      if (_bIRKeyBuf != IR_CUSTOM_CODE)
      {
        _bIRStatus = IR_WAIT_NEW_CODE;
        break;
      }

      /* get key code */
      _bIRKeyBuf = (bIRVal << 6);
      bIRVal = bReadBIM(BIM_IR_VAL);
      _bIRKeyBuf |= (bIRVal >> 2);

      /* look up the mapping table */
      _bIRKeyBuf = _pbIRKeyTbl[_bIRKeyBuf];

      /* check previous key and control bit */
      if ((_bIRBitCnt & 0x0f) == (_bIRBitCnt >> 4))
      {
        _bIRBitCnt <<= 4;

#ifdef IR_USE_REPEAT
        if ((_bIRStatus != IR_READ_REPEAT_CODE) ||
            (_bIRKey0 != _bIRKeyBuf))
        {
          // error condition
          _bIRStatus = IR_WAIT_NEW_CODE;
          _bIRBitCnt = 0;
          break;
        }
#else /* not define IR_USE_REPEAT */
        if (_bIRKey0 == _bIRKeyBuf)
        {
          _bIRStatus = IR_WAIT_NEW_CODE;
          break;
        }
#endif /* not define IR_USE_REPEAT */

⌨️ 快捷键说明

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