Key value conversion-complex key event recognition program (link layer-optimized version)

Source: Internet
Author: User

Complex key event recognition program (link layer-optimized version)


ComparedKey value conversion-complex key event recognition program (Link Layer)The key event conversion program described in this article is optimized (the original code is too large, too complex, and not easy to maintain ).

Advantages:The code is concise and easy to modify and read.

Disadvantages:It is impossible to differentiate the key time of a special key in different system states (for example, a key can be pressed for 3 s or 5 S in different cases ), A hyphen and a long one cannot exist at the same time (you can also add conditions to differentiate them ).


Complex buttons include multiple buttonsSingle-order event,Long press event,Pay-as-you-go events,Combination of Button events...

The single-press event is determined when the key is released. When other buttons reach the specified time, the corresponding button event is triggered.
This program is a link layer program that processes and recognizes the key information scanned by the key driver layer.G_u8keyvalue. Each digit of g_u8keyvalue indicates the press condition of a key to distinguish between different keys and combined keys. Definition:

// Macro defines the bit value when each key is pressed # define defaults 0x00 # define key_event_clock_click 0x01 # define key_event_down_click 0x02 # define defaults 0x04 # define key_event_lock_click 0x08 # define key_event_onoff_click 0x10 # define 1_0x20 # define key_event_up_click 0x40 # define key_event_timing_click 0x80 # define 1_0xff // long press the flag // key combination # define limit 0x60 # define key_event_setting_onoff_click 0x30 # define key_event_mode_up_down_click 0x46

The converted key information is stored in the g_keyevent struct. It is defined as follows:

Typedef Enum {keyvaluenull = 0, iterator = 1, keyvaluemode = 2, keyvalueclock = 3, keyvaluesetting = 4, keyvaluetiming = 5, keyvaluelock = 6, keyvalueup = 7, keyvaluedown = 8, keyvalueonoff1s = 9, keyvaluelock1s = 10, keyvaluemode3s = 11, keyvalueclock3s = 12, expires = 13, keyvalueup3s = 14, keyvalueup10s = 15, expires = 16, expires = 17, expires = 18, keyvalueuplong = 19, keyvaluedownlong = 20,} key_event_e; extern key_event_e g_keyevent; // enumeration of key events

The optimized code is as follows:

/* Private define interval */# define key_press_time 80 // minimum time per order # define key_single_time 1000 // Max time per order typedef struct {2010short_time; // whether there is a short press flag (key_event_e short_event if the key combination is not short press time is also defined); // short press event b2long_t; // long press time key_event_e long_event; // long press event continue_t; // The connection is triggered by time interval. The default force is short press event} key_button_t; // defines the key combination key_button_t key_long = {0, keyvaluenull, 0, Keyvaluenull, 0}, // long does not contain any event key_up = {1, keyvalueup, 0, keyvalueup3s, 500}, key_set = {1, keyvaluesetting, 3000, keyvaluesetting3s, 0}, key_clock = {1, keyvalueclock, 3000, keyvalueclock3s, 0}, key_down = {1, keyvaluedown, 0, keyvaluenull, 500}, key_mode = {1, keyvaluemode, 3000, keyvaluemode3s, 0}, key_lock = {1, keyvaluelock, 1000, keyvaluelock1s, 0}, key_onoff = {1, keyvalueonoff, 1000, K Eyvalueonoff1s, 0}, key_timing = {1, keyvaluetiming, 0, keyvaluenull, 0}, key_set_up = {0, keyvaluenull, 3000, delimiter, 0}, key_set_onoff = {0, keyvaluenull, 5000, keys, 0}, key_mode_up_down = {0, keyvaluenull, 3000, keyvaluemodeupdown3s, 0};/* private functions --------------------------------------------------------- */static key_button_t rfindkeyobject (u8 key_event) ;/* Public Functions -------------------------------------------------------- */void link_keyvalueget (void); void link_keyeventtrans (void ); /*************************************** **************************************** # function: link_keyvalueget # Description: Obtain the key value (key event) # parameter: NULL # Return: NULL # authoranddata: huangzhigang 20141027 ************************************** ******************* * ********************/Void link_keyvalueget (void) {vdrv_keyscanning (); // scan link_keyeventtrans () at the driver layer. // convert the link layer to a key event (processed at the application layer )} /*************************************** **************************************** # function: link_keyeventtrans # Description: key event conversion (short press, long press, even press, and combination key) can only have one valid long press event # parameter: NULL # Return: NULL # authoranddata: huangzhigang 20141027 ************************************** ********** * *****************************/Void link_keyeventtrans (void) {static u8 s_u8continuepressflag = 0; // The force-on-line flag is used to differentiate between the force-on-line and Short-based events. Static key_button_t key_tmp; // The temporary key object static timer_t keydelaytime; // press the key time static uint8_t keybackup = key_event_null_click; // press the key status to back up timems (& keydelaytime); If (g_u8keyvalue) // perform corresponding processing when a key is pressed {If (keybackup! = G_u8keyvalue) {keydelaytime. delaytime = 0; keybackup = g_u8keyvalue; key_tmp = rfindkeyobject (g_u8keyvalue);} If (key_tmp.continue_t & (keydelaytime. delaytime> = key_tmp.continue_t) {g_keyevent = key_tmp.short_event; // sets up keybackup = key_event_null_click by time; // clear that the next backup can continue here s_u8continuepressflag = 1 ;} else if (key_tmp.long_t & (keydelaytime. delaytime> = key_tmp.long_t) {g_keyevent = key_tmp.long_event; // set up key_tmp = key_long ;}} else // when no button is pressed, if the button is released, perform single-click detection {If (s_u8continuepressflag) // if the connection fails to detect short-click events on time {s_u8continuepressflag = 0; keybackup = key_event_nul ;} if (keybackup & key_tmp.short_time) // a value indicates the release time {If (key_tmp.short_time & (keydelaytime. delaytime> = key_press_time) & (keydelaytime. delaytime <= key_single_time) {g_keyevent = success; // set up short press event} keybackup = key_event_null_click;} else // No value indicates idle press {keybackup = key_event_null_click; g_keyevent = keyvaluenull; // clear key value} keydelaytime. delaytime = 0 ;}} /*************************************** **************************************** # function: rfindkeyobject # Description: locate the key object and return key_long. An empty object system is defined to not be processed. # parameter: NULL # Return: key object corresponding to the current key # authoranddata: huangzhigang 20141027 ************************************** **************************************** */static key_button_t rfindkeyobject (u8 key_event) // find the key object {Switch (key_event) {case when: Return key_long; Case key_event_clock_click: Return key_clock; Case key_event_down_click: Return key_down; case when: Return key_mode; Case key_event_lock_click: return key_lock; case when: Return key_onoff; case when: Return key_set; Case key_event_up_click: Return key_up; case when: Return key_timing; case when: Return key_set_up; case when: Return key_set_onoff; case key_event_mode_up_down_click: Return key_mode_up_down; Case key_event_long_key_click: // return key_long; default: Return key_long ;}}






Key value conversion-complex key event recognition program (link layer-optimized version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.