#include"config.h" #include"key.h" /* ********************************************************************************************************* * LOCAL CONSTANTS ********************************************************************************************************* */ #define KEY_STATE_UP 1 /* Key scanning states used in KeyScan()*/ #define KEY_STATE_DEBOUNCE 2 #define KEY_STATE_RPT_START_DLY 3 #define KEY_STATE_RPT_DLY 4 static uint8 KeyBuf[KEY_BUF_SIZE]; static uint8 KeyBufInIx; static uint8 KeyBufOutIx; static volatile uint8 KeyNRead; static uint16 KeyDownTmr; static uint8 KeyRptStartDlyCtr; static uint8 KeyRptDlyCtr; static uint8 KeyScanState; #if USE_RTOS static OS_STK KeyScanTaskStk[KEY_SCAN_TASK_STK_SIZE]; /* Keyboard scanning task stack */ static OS_EVENT *KeySemPtr; /* Pointer to keyboard semaphore */ static void KeyScanTask(void *data); /* Keyboard scanning task */ #else static volatile uint16 KeyWaitTime; #endif /* ********************************************************************************************************* * LOCAL FUNCTION PROTOTYPES ********************************************************************************************************* */ static void KeyBufIn(uint8 icode); /* Insert scan code into keyboard buffer */ static uint8 KeyBufOut(void); static uint8 KeyDecode(void); /* Get scan code from current key pressed */ static boolean KeyIsKeyDown(void); /* See if key has been pressed */ static void KeyBufIn(uint8 icode) { ENTRY_CRITICAL(); if (KeyNRead=KEY_BUF_SIZE) KeyBufInIx=0; EXIT_CRITICAL(); #if USE_RTOS OSSemPost(KeySemPtr); #endif } else { EXIT_CRITICAL(); } } static uint8 KeyDecode(void) { uint8 row; uint8 col; uint8 col_id; uint8 offset; uint8 msk; boolean done; row=0; done=false; while ((row0)?true:false); EXIT_CRITICAL(); return hit; } static uint8 KeyBufOut(void) { uint8 retkey=KEY_NO_KEY; ENTRY_CRITICAL(); if (KeyNRead>0) { KeyNRead--; retkey=KeyBuf[KeyBufOutIx++]; if (KeyBufOutIx>=KEY_BUF_SIZE) KeyBufOutIx=0; } EXIT_CRITICAL(); return retkey; } uint8 KeyGetKey(uint16 to) { #if USE_RTOS uint8 err; OSSemPend(KeySemPtr,to,&err); #else if (KeyNRead==0) { KeyWaitTime=to/KEY_SCAN_TASK_DLY+1; while ((KeyWaitTime>0)&&(KeyNRead==0)); } #endif return KeyBufOut(); } uint32 KeyGetKeyDownTime(void) { uint32 tmr; ENTRY_CRITICAL(); tmr=KeyDownTmr; EXIT_CRITICAL(); return (tmr*KEY_SCAN_TASK_DLY); } void KeyInit(void) { KeyScanState=KEY_STATE_UP; KeyBufOutIx=0; KeyBufInIx=0; KeyNRead=0; KeyDownTmr=0; #if USE_RTOS KeySemPtr=OSSemCreate(0); #if OS_STK_GROWTH>0 OSTaskCreate(KeyScanTask,(void*)0,&KeyScanTaskStk[KEY_SCAN_TASK_STK_SIZE],KEY_SCAN); #else OSTaskCreate(KeyScanTask,(void*)0,&KeyScanTaskStk[0],KEY_SCAN); #endif #else KeyWaitTime=0; #endif KeyInitPort(); KeySelRow(KEY_ALL_ROWS); } static boolean KeyIsKeyDown(void) { if (KeyGetCol()&((1<>KEY_MAX_ROWS); } void KeySelRow(uint8 row) /*选择键盘的某一行,作为选择某行输出 */ { if (row==KEY_ALL_ROWS) KEY_PORT_ROW=0x00; else KEY_PORT_ROW=~(1<