📄 key.c
字号:
return (code); /* Return the scan code of the key pressed */
} else {
OS_EXIT_CRITICAL(); /* End of critical section of code */
return (0xFF); /* No scan codes in the buffer, return -1 */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* GET HOW LONG KEY HAS BEEN PRESSED
*
* Description : This function returns the amount of time the key has been pressed.
* Arguments : none
* Returns : key down time in 'milliseconds'
*********************************************************************************************************
*/
INT32U KeyGetKeyDownTime (void)
{
INT16U tmr;
OS_ENTER_CRITICAL();
tmr = KeyDownTmr;
OS_EXIT_CRITICAL();
return (tmr * KEY_SCAN_TASK_DLY);
}
/*$PAGE*/
/*
*********************************************************************************************************
* SEE IF ANY KEY IN BUFFER
*
* Description : This function checks to see if a key was pressed
* Arguments : none
* Returns : TRUE if a key has been pressed
* FALSE if no key pressed
*********************************************************************************************************
*/
BOOLEAN KeyHit (void)
{
BOOLEAN hit;
OS_ENTER_CRITICAL();
hit = (BOOLEAN)(KeyNRead > 0) ? TRUE : FALSE;
OS_EXIT_CRITICAL();
return (hit);
}
/*
*********************************************************************************************************
* 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)
{
KeySelRow(KEY_ALL_ROWS); /* Select all row */
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 */
KeySemPtr = OSSemCreate(0); /* Initialize the keyboard semaphore */
KeyInitPort(); /* Initialize I/O ports used in keyboard driver */
OSTaskCreate(KeyScanTask, (void *)0, &KeyScanTaskStk[KEY_SCAN_TASK_STK_SIZE], KEY_SCAN_TASK_PRIO);
}
/*$PAGE*/
/*
*********************************************************************************************************
* 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 (KeyGetCol() & ((1 << KEY_MAX_COLS) - 1)) { /* Key not pressed if 0 */
OS_ENTER_CRITICAL();
KeyDownTmr++; /* Update key down counter */
OS_EXIT_CRITICAL();
return (TRUE);
} else {
return (FALSE);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* 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).
*********************************************************************************************************
*/
/*$PAGE*/
static void KeyScanTask (void *data)
{
INT8U code;
data = data; /* Avoid compiler warning (uC/OS-II req.) */
for (;;) {
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;
}
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* READ COLUMNS
*
* Description : This function is called to read the column port.
* Arguments : none
* Returns : the complement of the column port thus, ones are keys pressed
*********************************************************************************************************
*/
#ifndef CFG_C
INT8U KeyGetCol (void)
{
return (~inp(KEY_PORT_COL)); /* Complement columns (ones indicate key is pressed) */
}
#endif
/*
*********************************************************************************************************
* INITIALIZE I/O PORTS
*********************************************************************************************************
*/
#ifndef CFG_C
void KeyInitPort (void)
{
outp(KEY_PORT_CW, 0x82); /* Initialize 82C55: A=OUT, B=IN (COLS), C=OUT (ROWS) */
}
#endif
/*
*********************************************************************************************************
* SELECT A ROW
*
* Description : This function is called to select a row on the keyboard.
* Arguments : 'row' is the row number (0..7) or KEY_ALL_ROWS
* Returns : none
* Note : The row is selected by writing a LOW.
*********************************************************************************************************
*/
#ifndef CFG_C
void KeySelRow (INT8U row)
{
if (row == KEY_ALL_ROWS) {
outp(KEY_PORT_ROW, 0x00); /* Force all rows LOW */
} else {
outp(KEY_PORT_ROW, ~(1 << row)); /* Force desired row LOW */
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -