📄 key.c
字号:
}
/*
*********************************************************************************************************
* KEYBOARD INITIALIZATION
*
* Description: Keyboard initialization function. KeyInit() must be called before calling any other of
* the user accessible functions.
* Arguments : none
* Returns : none
*********************************************************************************************************
*/
void KeyInit (void)
{
KeyScanState = KEY_STATE_UP; /* Keyboard should not have a key pressed */
KeyNRead = 0; /* Clear the number of keys read */
KeyDownTmr = 0;
KeyBufInIx = 0; /* Key codes inserted at the beginning of the buffer */
KeyBufOutIx = 0; /* Key codes removed from the beginning of the buffer */
#if MULTITASK == 1
KeySemPtr = xOSSemBCreate(0,0); /* Initialize the keyboard semaphore */
#endif
KeyInitPort(); /* Initialize I/O ports used in keyboard driver */
#if MULTITASK == 1
OSTaskCreate(KeyScanTask, (void *)0, &KeyScanTaskStk[0], KEY_SCAN_TASK_PRIO);
#endif
}
/*
*********************************************************************************************************
* SEE IF KEY PRESSED
*
* Description : This function checks to see if a key is pressed
* Arguments : none
* Returns : TRUE if a key is pressed
* FALSE if a key is not pressed
* Note : (1 << KEY_MAX_COLS) - 1 is used as a mask to isolate the column inputs (i.e. mask off
* the SHIFT keys).
*********************************************************************************************************
*/
static BOOLEAN KeyIsKeyDown (void)
{
if (KeyGetPort() & ((1 << KEY_PORT_MAX_BITS) - 1)) { /* Key not pressed if 0 */
OS_ENTER_CRITICAL();
KeyDownTmr++; /* Update key down counter */
OS_EXIT_CRITICAL();
return (TRUE);
} else {
return (FALSE);
}
}
/*
*********************************************************************************************************
* KEYBOARD SCANNING TASK
*
* Description : This function contains the body of the keyboard scanning task. The task should be
* assigned a low priority. The scanning period is determined by KEY_SCAN_TASK_DLY.
* Arguments : 'data' is a pointer to data passed to task when task is created (NOT USED).
* Returns : KeyScanTask() never returns.
* Notes : - An auto repeat of the key pressed will be executed after the key has been pressed for
* more than KEY_RPT_START_DLY scan times. Once the auto repeat has started, the key will
* be repeated every KEY_RPT_DLY scan times as long as the key is pressed. For example,
* if the scanning of the keyboard occurs every 50 mS and KEY_RPT_START_DLY is set to 40
* and KEY_RPT_DLY is set to 2, then the auto repeat function will engage after 2 seconds
* and will repeat every 100 mS (10 times per second).
*********************************************************************************************************
*/
#if MULTITASK == 1
static void KeyScanTask (void *data)
{
INT8U code;
data = data; /* Avoid compiler warning (uC/OS-II req.) */
for (;;) {
xOSTimeDlyHMSM(0, 0, 0, KEY_SCAN_TASK_DLY); /* Delay between keyboard scans */
switch (KeyScanState) {
case KEY_STATE_UP: /* See if need to look for a key pressed */
if (KeyIsKeyDown()) { /* See if key is pressed */
KeyScanState = KEY_STATE_DEBOUNCE; /* Next call we will have debounced the key */
KeyDownTmr = 0; /* Reset key down timer */
}
break;
case KEY_STATE_DEBOUNCE: /* Key pressed, get scan code and buffer */
if (KeyIsKeyDown()) { /* See if key is pressed */
code = KeyDecode(); /* Determine the key scan code */
KeyBufIn(code); /* Input scan code in buffer */
KeyRptStartDlyCtr = KEY_RPT_START_DLY;/* Start delay to auto-repeat function */
KeyScanState = KEY_STATE_RPT_START_DLY;
} else {
//KeySelRow(KEY_ALL_ROWS); /* Select all row */
KeyScanState = KEY_STATE_UP; /* Key was not pressed after all! */
}
break;
case KEY_STATE_RPT_START_DLY:
if (KeyIsKeyDown()) { /* See if key is still pressed */
if (KeyRptStartDlyCtr > 0) { /* See if we need to delay before auto rpt */
KeyRptStartDlyCtr--; /* Yes, decrement counter to start of rpt */
if (KeyRptStartDlyCtr == 0) { /* If delay to auto repeat is completed ... */
code = KeyDecode(); /* Determine the key scan code */
KeyBufIn(code); /* Input scan code in buffer */
KeyRptDlyCtr = KEY_RPT_DLY; /* Load delay before next repeat */
KeyScanState = KEY_STATE_RPT_DLY;
}
}
} else {
KeyScanState = KEY_STATE_DEBOUNCE; /* Key was not pressed after all */
}
break;
case KEY_STATE_RPT_DLY:
if (KeyIsKeyDown()) { /* See if key is still pressed */
if (KeyRptDlyCtr > 0) { /* See if we need to wait before repeat key */
KeyRptDlyCtr--; /* Yes, dec. wait time to next key repeat */
if (KeyRptDlyCtr == 0) { /* See if it's time to repeat key */
code = KeyDecode(); /* Determine the key scan code */
KeyBufIn(code); /* Input scan code in buffer */
KeyRptDlyCtr = KEY_RPT_DLY; /* Reload delay counter before auto repeat */
}
}
} else {
KeyScanState = KEY_STATE_DEBOUNCE; /* Key was not pressed after all */
}
break;
}
}
}
#else
void KeyScan(void)
{
INT8U code;
//OSTimeDlyHMSM(0, 0, 0, KEY_SCAN_TASK_DLY); /* Delay between keyboard scans */
switch (KeyScanState) {
case KEY_STATE_UP: /* See if need to look for a key pressed */
if (KeyIsKeyDown()) { /* See if key is pressed */
KeyScanState = KEY_STATE_DEBOUNCE; /* Next call we will have debounced the key */
KeyDownTmr = 0; /* Reset key down timer */
}
break;
case KEY_STATE_DEBOUNCE: /* Key pressed, get scan code and buffer */
if (KeyIsKeyDown()) { /* See if key is pressed */
code = KeyDecode(); /* Determine the key scan code */
KeyBufIn(code); /* Input scan code in buffer */
KeyRptStartDlyCtr = KEY_RPT_START_DLY;/* Start delay to auto-repeat function */
KeyScanState = KEY_STATE_RPT_START_DLY;
} else {
//KeySelRow(KEY_ALL_ROWS); /* Select all row */
KeyScanState = KEY_STATE_UP; /* Key was not pressed after all! */
}
break;
case KEY_STATE_RPT_START_DLY:
if (KeyIsKeyDown()) { /* See if key is still pressed */
if (KeyRptStartDlyCtr > 0) { /* See if we need to delay before auto rpt */
KeyRptStartDlyCtr--; /* Yes, decrement counter to start of rpt */
if (KeyRptStartDlyCtr == 0) { /* If delay to auto repeat is completed ... */
code = KeyDecode(); /* Determine the key scan code */
KeyBufIn(code); /* Input scan code in buffer */
KeyRptDlyCtr = KEY_RPT_DLY; /* Load delay before next repeat */
KeyScanState = KEY_STATE_RPT_DLY;
}
}
} else {
KeyScanState = KEY_STATE_DEBOUNCE; /* Key was not pressed after all */
}
break;
case KEY_STATE_RPT_DLY:
if (KeyIsKeyDown()) { /* See if key is still pressed */
if (KeyRptDlyCtr > 0) { /* See if we need to wait before repeat key */
KeyRptDlyCtr--; /* Yes, dec. wait time to next key repeat */
if (KeyRptDlyCtr == 0) { /* See if it's time to repeat key */
code = KeyDecode(); /* Determine the key scan code */
KeyBufIn(code); /* Input scan code in buffer */
KeyRptDlyCtr = KEY_RPT_DLY; /* Reload delay counter before auto repeat */
}
}
} else {
KeyScanState = KEY_STATE_DEBOUNCE; /* Key was not pressed after all */
}
break;
}
}
#endif
/*
*********************************************************************************************************
* READ PORT
*
* Description : This function is called to read the column port.
* Arguments : none
* Returns : the complement of the column port thus, ones are keys pressed
*********************************************************************************************************
*/
INT16U KeyGetPort(void)
{
return ((INT16U)(KEY_PORT_VAL&0xFF));
}
void KeyInitPort (void)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -