I. Ideas
The idea for button Processing Based on Listen 8 is as follows:
Scan the button once every 20 ms or so, use a key_now to record the current value, and use key_last to record the previous value. If both key_now and key_last are valid, CNT ++ is started.
I set two thresholds: long_press to 100 (100*20 Ms = 2 S) and short_press to 4 (4*20 Ms = 80 ms, deshake ).
CNT is greater than long_press, which indicates a long press. Otherwise, the system checks whether CNT is greater than short_press, indicating a short press. Otherwise, the CNT is cleared.
In another case, when we set the parameters, we need to add 1 or minus 1 continuously. I long press and want to increase or decrease the value continuously. How can this problem be achieved?
Next, set the CNT value and the connection threshold. The value is mid_press = 50 (50*20 Ms = 1 s ).
When CNT is greater than mid_press, I think a short press is triggered, but CNT is not cleared at this time, only the value of CNT minus short_press.
In this case, if the key is not released, the connection will trigger a short press.
II. Implementation
The hardware has three buttons: Set, up, and down.
// Control the key duration # define long_press 100 // 20 ms * 100 = 2 s, long press # define short_press 5 // 20 ms * 5 = 100 ms, short press # define mid_press 50 // 20 ms * 50 = 1 s, connect to press # define repeat_press 5 // 200 ms * 5 = 100 ms, connect to sensitive parameters # define key_port (gpioc) # define key_set (gpio_pin_5) // set key Pc5 # define key_up (gpio_pin_6) // up key PC6 # define key_down (gpio_pin_7) // down key pc7uchar Pinnow; bool pinset_now, pinup_now, pindown_now; bool pinset_last, pinu P_last, pindown_last; bool set_long = false; // press bool set_short = false for the set key length; // press bool up_short = false for the set key; // up key short press bool down_short = false; // down key short press // initialize void key_proc_init (void) {gpio_init (key_port, key_set | key_up | key_down, gpio_mode_in_fl_it ); exti_setextintsensitioc (exti_port_gpioc, exti_sensitivity_fall_only); Pinnow = 0; set_cnt = 0; up_cnt = 0; down_cnt = 0; set_long = false; Set _ Short = false; up_short = false; down_short = false;} // press the key to process void key_scan (void) {// The key port is pulled up. The default value is high, when a button is enabled, it becomes low. Here, the result is reversed. When a key is set, the value increases. Pinnow = 0xff-gpio_readinputdata (key_port); pinset_now = Pinnow & key_set; pinup_now = Pinnow & key_up; pindown_now = Pinnow & key_down; // The set key only supports Long and Short presses, if (pinset_now & pinset_last) {set_cnt ++; If (set_cnt> long_press) {set_long = true; set_cnt = 0 ;}} else {If (set_cnt> short_press) {set_short = true; set_cnt = 0;} else {set_cnt = 0 ;}// the up and down keys have a hyphen and a short press, and if (pinup_now & pinup_last) {up_cnt ++; If (up_cnt> mid_press) {up_short = true; up_cnt = up_cnt-repeat_press;} else {If (up_cnt> short_press) {up_short = true; up_cnt = 0 ;}}if (pindown_now & pindown_last) {down_cnt ++; If (down_cnt> mid_press) {down_short = true; down_cnt = down_cnt-repeat_press ;}} else {If (down_cnt> short_press) {down_short = true; down_cnt = 0 ;}// Add yourself to set_long, set_short, up_short, down_short processing code // do not forget to set the corresponding value to false pinset_last = pinset_now; pinup_last = pinup_now; pindown_last = pindown_now ;}
Then, use the timer to interrupt and execute a key_scan () function every 20 ms.