📄 key.c
字号:
/*
*********************************************************************************************************
* Shanghai Maritime University
* Pudong Avenue 1550#
*
* (c)Copyright 2005,SMSC,Shanghai,China
* All Rights Reserved
*
*
* Filename : KEY.C
* Programmer : Jason Chen (Refer to Jean J. Labrosse's Matrix keyboard driver)
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#include "key_cfg.h"
/*
*********************************************************************************************************
* FUNCTION PROTOTYPES
*********************************************************************************************************
*/
void KeyFlush(void); /* Flush the keyboard buffer */
INT8U KeyGetKey(INT16U to); /* Get a key scan code from driver if one is present, -1 else */
INT32U KeyGetKeyDownTime(void); /* Get how long key has been pressed (in milliseconds) */
BOOLEAN KeyHit(void); /* See if a key has been pressed (TRUE if so, FALSE if not) */
void KeyInit(void); /* Initialize the keyboard handler */
void KeyInitPort(void); /* Initialize I/O ports */
INT16U KeyGetPort(void);
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static void KeyBufIn(INT8U code); /* Insert scan code into keyboard buffer */
static INT8U KeyDecode(void); /* Get scan code from current key pressed */
static BOOLEAN KeyIsKeyDown(void); /* See if key has been pressed */
static void KeyScanTask(void *data); /* Keyboard scanning task */
static void Delay(INT16U counts);
/*
**************************************************************************
* Dummy for a Period of Time
*
* Description : This function is used to delay a period and cpu can do nothing.
* Arguments : 'counts' is the dummy counts.
* Returns : None.
**************************************************************************
*/
static void Delay(INT16U counts)
{
int i;
for(i=0;i<counts;i++);
}
/*
*********************************************************************************************************
* INSERT KEY CHARACTER INTO KEYBOARD BUFFER
*
* Description : This function inserts a key character into the keyboard buffer
* Arguments : code is the keyboard scan code to insert into the buffer
* Returns : none
*********************************************************************************************************
*/
static void KeyBufIn (INT8U code)
{
OS_ENTER_CRITICAL(); /* Start of critical section of code, disable ints */
if (KeyNRead < KEY_BUF_SIZE) { /* Make sure that we don't overflow the buffer */
KeyNRead++; /* Increment the number of keys read */
KeyBuf[KeyBufInIx++] = code; /* Store the scan code into the buffer */
if (KeyBufInIx >= KEY_BUF_SIZE) { /* Adjust index to the next scan code to put in buffer*/
KeyBufInIx = 0;
}
OS_EXIT_CRITICAL(); /* End of critical section of code */
#if MULTITASK == 1
xOSSemGive(KeySemPtr); /* Signal sem if scan code inserted in the buffer */
#endif
} else { /* Buffer is full, key scan code is lost */
OS_EXIT_CRITICAL(); /* End of critical section of code */
}
}
/*
*********************************************************************************************************
* DECODE KEYBOARD
*
* Description : This function is called to determine the key scan code(index of the key character map table) of the key pressed.
* Arguments : none
* Returns : the key scan code(index of the key character map table)
*********************************************************************************************************
*/
static INT8U KeyDecode (void)
{
INT16U portvalue=KeyGetPort();
INT8U ixcode;
switch(portvalue>>4)
{
case 1: ixcode=0; break;
case 2: ixcode=4; break;
case 4: ixcode=8; break;
case 8: ixcode=12; break;
}
switch ( portvalue&0x0F )
{
case 1: ixcode+=3; break;
case 2: ixcode+=2; break;
case 4: ixcode+=1; break;
}
return(ixcode);
}
/*
*********************************************************************************************************
* FLUSH KEYBOARD BUFFER
*
* Description : This function clears the keyboard buffer
* Arguments : none
* Returns : none
*********************************************************************************************************
*/
void KeyFlush (void)
{
while (KeyHit()) { /* While there are keys in the buffer... */
KeyGetKey(0); /* ... extract the next key from the buffer */
}
}
/*
*********************************************************************************************************
* GET KEY
*
* Description : Get a keyboard scan code from the keyboard driver.
* Arguments : 'to' is the amount of time KeyGetKey() will wait (in number of ticks) for a key to be
* pressed. A timeout of '0' means that the caller is willing to wait forever for
* a key to be pressed.
* Returns : != 0xFF is the key scan code of the key pressed
* == 0xFF indicates that there is no key in the buffer within the specified timeout
*********************************************************************************************************
*/
INT8U KeyGetKey (INT16U to)
{
INT8U code;
INT8U err;
#if MULTITASK == 1
xOSSemTake(KeySemPtr, to, &err); /* Wait for a key to be pressed */
#endif
OS_ENTER_CRITICAL(); /* Start of critical section of code, disable ints */
if (KeyNRead > 0) { /* See if we have keys in the buffer */
KeyNRead--; /* Decrement the number of keys read */
code = KeyBuf[KeyBufOutIx]; /* Get scan code from the buffer */
KeyBufOutIx++;
if (KeyBufOutIx >= KEY_BUF_SIZE) { /* Adjust index into the keyboard buffer */
KeyBufOutIx = 0;
}
OS_EXIT_CRITICAL(); /* End of critical section of code */
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 */
}
}
/*
*********************************************************************************************************
* 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);
}
/*
*********************************************************************************************************
* 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -