Home > other >  The code of key FIFO - enjoy wealth
The code of key FIFO - enjoy wealth

Time:09-23

Why want to design of FIFO, enjoy wealth lai bosses of answer:
1. Record each key-press events in a reliable way, to avoid missing key event, especially the need to implement the button pressed, long press, automatic running, pop-up event, such as
2. Read the key function can be designed to be a non-blocking, no need to wait for key jitter filtering processed,
3 buttons FIFO procedure perform tests on a regular basis in the tick timer, no need to inspect in the main program has been doing, so that we can effectively reduce the system resource consumption,

 typedef struct 
{
The following is a function pointer to determine key hand no press function
IsKeyDownFunc uint8_t (*) (void); Button press the judging function, 1 press

Uint8_t Count; Filter counter
Uint16_t LongCount; Long press counter
Uint16_t LongTime; Button press the duration, 0 means no detection long press
Uint8_t State; Key current state (press or pop-up)
Uint8_t RepeatSpeed; Continuous button cycle
Uint8_t RepeatCount; Continuous key counter
} KEY_T;


Bsp_DetectKey this function key. Detection of c core


 typedef struct 
{
The following is a function pointer to determine key hand no press function
IsKeyDownFunc uint8_t (*) (void); Button press the judging function, 1 press

Uint8_t Count; Filter counter
Uint16_t LongCount; Long press counter
Uint16_t LongTime; Button press the duration, 0 means no detection long press
Uint8_t State; Key current state (press or pop-up)
Uint8_t RepeatSpeed; Continuous button cycle
Uint8_t RepeatCount; Continuous key counter
} KEY_T;



Is mainly on the structure, bsp_KeyScan () function inside constantly scanning, bsp_KeyScan () mainly on bsp_RunPer10ms (), and then into the SysTick_Handler scanning a 10 ms.

In bsp_DetectKey () function inside directly call external s_tBtn results, then numerical changes,

Initialization has two, one is to structure the value of the default initialization, one is the hardware initialization, and our common hardware port initialization is same,
/* to read/write pointer reset button FIFO */
S_tKey. Read=0;
S_tKey. Write=0;
S_tKey. Read2=0;

Software initialization need to take out separate way, initialize it first set the FIFO buffer initial value.
Static file defines a structure array KEY_T s_tBtn [KEY_COUNT], KEY_COUNT is the number of buttons, and this value is used in this file, can't call outside, really read the key value is s_tBtn [0]. IsKeyDownFunc=IsKeyDown1;
KEY_COUNT is the serial number of keystrokes
 typedef struct 
{
Uint8_t Buf [KEY_FIFO_SIZE];/* * key buffer/
Uint8_t Read;/* buffer read pointer 1 */
Uint8_t Write;/* buffer write pointer */
Uint8_t Read2;/* buffer read pointer 2 */
} KEY_FIFO_T;


 
The static KEY_FIFO_T s_tKey;/* button FIFO variables, structure */

//bsp_PutKey (); Press a key value into key FIFO. Used to simulate a button
Void bsp_PutKey (uint8_t _KeyCode)
{
S_tKey. Buf [s_tKey. Write]=_KeyCode;

If (+ + s_tKey. Write & gt;=KEY_FIFO_SIZE)
{
S_tKey. Write=0;
}
}
//bsp_GetKey (); A key value read from the FIFO buffer.
Uint8_t bsp_GetKey (void)
{
Uint8_t ret.

If (s_tKey. Read==s_tKey. Write)
{
Return KEY_NONE;
}
The else
{
Ret=s_tKey. Buf [s_tKey. Read];

If (+ + s_tKey. Read & gt;=KEY_FIFO_SIZE)
{
S_tKey. Read=0;
}
return ret;
}
}


  • Related