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

📄 keyboard.c

📁 FREESCALE的基于802.15.4无线通讯原代码
💻 C
字号:
/************************************************************************************
* This is the source file for Keyboard Driver.
*
*
* (c) Copyright 2006, Freescale, Inc.  All rights reserved.
*
* Freescale Confidential Proprietary
* Digianswer Confidential
*
* No part of this document must be reproduced in any form - including copied,
* transcribed, printed or by any electronic means - without specific written
* permission from Freescale.
*
******************************************************************************/
#include "TMR_Interface.h"
#include "embeddedtypes.h"
#include "portconfig.h"
#include "keyboard.h"
#include "led.h"
#include "switch.h"
#include "AppToPlatformConfig.h"

#ifndef gMacStandAlone_d
#define gMacStandAlone_d 0
#endif /*gMacStandAlone_d*/
#ifndef gLpmIncluded_d
#define gLpmIncluded_d 0
#endif /*gLpmIncluded_d*/
#if (gMacStandAlone_d==0)
#include "AppZdoInterface.h"
#endif
#if (gLpmIncluded_d==1)
#include "PWRLib.h"
#endif

#if (gKeyBoardSupported_d == 1)
/******************************************************************************
*******************************************************************************
* Private macros
*******************************************************************************
******************************************************************************/
/* mKwySacnTimerID_c is used for internal timer ID. Do NOT change. */
#define mKeyScanTimerID_c gApplicationTimerIDOffset_c
/* Interval constant for callback function KeyScan. Do NOT change. */
#define mKeyScanInterval_c 20
/* Detect a key press minimum 5 times */
#define mKeyDetect_c 5
/* Minimum detect of no key press */
#define mNoPrel_c 100
/* constant used for LongKey press with event all time. Sent event after ( 15 * 20ms = 0.3s ) */
#define mLongKeyEventScan_c 15 

/* Keyboard Interrupt Status and Control register
 bit7-4:  Edge select for kb pins 7-4 (PTA 7-4)
 bit3:    KBIF Keyboard interrupt flag - read only
 bit2:    KBACK Keyboard interrupt acknowledge - write 1 to clear KBIF
 bit1:    KBIE Keyboard interrupt enable
 bit0:    Keyboard detection mode - 0= edge only , 1= edge and level */
#define mKBIE_BIT_c (1<<1)
#define mKBACK_BIT_c (1<<2)
#define mKBIF_BIT_c (1<<3)
#define mKBI_ENABLE_c KBISC |= mKBIE_BIT_c
#define mKBI_DISABLE_c KBISC &= ~mKBIE_BIT_c
#define mKBI_ACKNOWLEDGE_c KBISC |= mKBACK_BIT_c
#define mSWITCH_MASK_c gSWITCH1_MASK_c | gSWITCH2_MASK_c | gSWITCH3_MASK_c | gSWITCH4_MASK_c
/* Keyboard State */
enum {
  mStateKeyIdle,
  mStateKeyDetect,
  mStateKeyOK,
  mStateKeyOKLong,
  mStateKeyOKLongEvent,
  mStateKeyDelay,
  mStateStopTimer
};

/******************************************************************************
*******************************************************************************
* Private prototypes
*******************************************************************************
******************************************************************************/
static void KeyScan(uint8_t timerId);

/******************************************************************************
*******************************************************************************
* Private type definitions
*******************************************************************************
******************************************************************************/
/*None*/

/******************************************************************************
*******************************************************************************
* Private memory declarations
*******************************************************************************
******************************************************************************/
static uint8_t KeyState = mStateKeyIdle, mSwitch_SCAN, mLongKey = FALSE,
               mShortKey = FALSE, mSendEvent=0;
static uint16_t mKeyDetect=0;
static KBDFunction_t pFunction;

/******************************************************************************
*******************************************************************************
* Public memory declarations
*******************************************************************************/

/* Timers need to be reinitialized when coming out of sleep mode by means
   of keyboard . This flag should be raised before the application enters
   in sleep mode. */
bool_t gReinitTimersAfterSleep = FALSE;
/******************************************************************************
*******************************************************************************
* Public functions
*******************************************************************************/
/******************************************************************************
* This function stores the adr. for the callback function. 
* The function will be called every time a new key press is detected
*
* Interface assumptions:
*
*
* Return value:
* None
*
*
* Revison history:
*   date      Author    Comments
*   ------    ------    --------
*   22.02.06   MMA       Created
******************************************************************************/
void KBD_Init
  (
  KBDFunction_t pfCallBackAdr /* IN: Pointer to callback function */
  )
{
  pFunction = pfCallBackAdr;
  KBISC |= 0x02;
  KBIPE |= mSWITCH_MASK; 
}

/******************************************************************************
* This interrupt function is the ISR keyboard function
*
* Interface assumptions:
*
*
* Return value:
* None
*
*
* Revison history:
*   date      Author    Comments
*   ------    ------    --------
*   14.02.06   MMA       Created
******************************************************************************/
__interrupt void Switch_Press_ISR
  (
  )
{
  mKBI_DISABLE_c;
  mKBI_ACKNOWLEDGE_c;
  
  if (gReinitTimersAfterSleep)
  {
    TMR_Init();
  }
  TMR_StartInterval(mKeyScanTimerID_c, mKeyScanInterval_c, KeyScan);
  #if (gLpmIncluded_d==1)
  PWRLib_MCU_WakeupReason.Bits.FromKBI = TRUE;  
  #endif  
}

/******************************************************************************
*******************************************************************************
* Private functions
*******************************************************************************
******************************************************************************/
/******************************************************************************
* This function handle the keyboard routine. 
* Every time the mKeyScanTimerID_c expires this function is called  
*
* Interface assumptions:
*
*
* Return value:
* None
*
*
* Revison history:
*   date      Author    Comments
*   ------    ------    --------
*   14.02.06   MMA       Created
******************************************************************************/
static void KeyScan
  (
  uint8_t timerId
  )
{
  uint8_t mSwitch_SCAN_Prel;
  uint8_t mprel=0;
  switch( KeyState ) {
/*----------------------------------------------------------------------------*/
    case mStateKeyIdle:
      mSwitch_SCAN = SwitchPortGet;
      /* If Key press */
      if(mSwitch_SCAN) {
      KeyState = mStateKeyDetect;
      } 
      else {
        KeyState = mStateStopTimer;
      }
      break;
/*----------------------------------------------------------------------------*/
    case mStateKeyDetect:
      mSwitch_SCAN_Prel = SwitchPortGet;
      if( mSwitch_SCAN_Prel == mSwitch_SCAN ) {
        mKeyDetect++;
      }
      else {
      /* No key press detected */
      KeyState = mStateKeyDelay;
      }
      /* Key press detected 5 times */
      if(mKeyDetect == mKeyDetect_c) {
        KeyState = mStateKeyOK;
      }
      break;
/*----------------------------------------------------------------------------*/
    case mStateKeyOK:
      mSwitch_SCAN_Prel = SwitchPortGet;
      if( mSwitch_SCAN_Prel == mSwitch_SCAN ) {
        mKeyDetect++;
        KeyState = mStateKeyOK;
        /* LongKey with event all the time enable */
        #if mLongEventEnable
          if(mSwitch_SCAN & mLongEventEnable && mKeyDetect >= mLongKeyEventScan_c ) {
            KeyState = mStateKeyOKLongEvent;
          }
        #endif
      }
      else {
          /* ShortKey */
          KeyState = mStateKeyDelay;
          mShortKey = TRUE;
      }
      if( mKeyDetect >= mLongKeySCAN_c ) {
        /* LongKey with event after timeout */
        KeyState = mStateKeyOKLong;
      }
      break;
/*----------------------------------------------------------------------------*/
    case mStateKeyDelay:
      /* Avoid bouncing */
      mSwitch_SCAN_Prel = SwitchPortGet;
      if( (mSwitch_SCAN_Prel & mSWITCH_MASK_c) && mLongKey == TRUE) {
        KeyState = mStateKeyDelay;  
      }
      else {
        do {
          mSwitch_SCAN_Prel = SwitchPortGet;
          if( mSwitch_SCAN_Prel == FALSE) {
            mprel++;
          }
        }while (mprel < mNoPrel_c);
        mprel=0;
        mLongKey = FALSE;
        mKeyDetect=0;
        KeyState = mStateStopTimer;
      }
      break;
/*----------------------------------------------------------------------------*/
    case mStateKeyOKLong:
      KeyState = mStateStopTimer;
      mLongKey = TRUE;
      mKeyDetect=0;
      break;
/*----------------------------------------------------------------------------*/
    case mStateKeyOKLongEvent:
      if(mSendEvent >= mSendEvent_c) {
        /* Detect which key is pressed and sent event to callback function */
        switch ( mSwitch_SCAN ) {
            case gSWITCH1_MASK_c:
              pFunction(gKBD_EventLongSW1_c);
              break;

  			    case gSWITCH2_MASK_c:
              pFunction(gKBD_EventLongSW2_c);
              break;

            case gSWITCH3_MASK_c:
              pFunction(gKBD_EventLongSW3_c);
              break;

            case gSWITCH4_MASK_c:
              pFunction(gKBD_EventLongSW4_c);
              break;
        }
        mSwitch_SCAN_Prel = SwitchPortGet;
          if( mSwitch_SCAN_Prel == FALSE) {
            KeyState = mStateKeyDelay;
            mLongKey = FALSE;
            mShortKey = FALSE;
            mKeyDetect=0;
          }
        mSendEvent=0;
      }
      else {
        mSendEvent++;
      }

      break;
/*----------------------------------------------------------------------------*/
    case mStateStopTimer:
      if(mLongKey) {
        /* Detect which LongKey is pressed and sent event to callback function */
        switch ( mSwitch_SCAN ) {
          case gSWITCH1_MASK_c:
            pFunction(gKBD_EventLongSW1_c);
            break;

			    case gSWITCH2_MASK_c:
            pFunction(gKBD_EventLongSW2_c);
            break;

          case gSWITCH3_MASK_c:
            pFunction(gKBD_EventLongSW3_c);
            break;

          case gSWITCH4_MASK_c:
            pFunction(gKBD_EventLongSW4_c);
            break;
        }
        mShortKey = FALSE;
        KeyState = mStateKeyDelay;
      }
      else {
        TMR_StopInterval(timerId);
        if(mShortKey) {
          /* Detect which ShortKey is pressed and sent event to callback function */
          switch ( mSwitch_SCAN ) {
            case gSWITCH1_MASK_c:
              pFunction(gKBD_EventSW1_c);
              break;
			      case gSWITCH2_MASK_c:
              pFunction(gKBD_EventSW2_c);
              break;
					  case gSWITCH3_MASK_c:
              pFunction(gKBD_EventSW3_c);
              break;
						case gSWITCH4_MASK_c:
              pFunction(gKBD_EventSW4_c);
              break;
          }
          mShortKey = FALSE;
        }
      KeyState = mStateKeyIdle;
      mKBI_ACKNOWLEDGE_c;
      mKBI_ENABLE_c;
      }
      break;
    default:
      break;
  }
}

/******************************************************************************
*******************************************************************************
* Private Debug stuff
*******************************************************************************
******************************************************************************/
#endif /* gKeyBoardSupported_d */

/*None*/

⌨️ 快捷键说明

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