STM32 long press, short press, double click, triple click algorithm (improved version)

STM32 long press, short press, double click, triple click algorithm

The key algorithm is here again!

**
**
**

This algorithm can effectively solve the following problems:

1. When the button is double-clicked or triple-clicked, it will also trigger the program corresponding to the button click

The key.h file is as follows

//定义一个按键结构体
	typedef struct
	{
	  uint8_t get_key_num; 
		uint8_t state;
		uint8_t long_count; 
		uint8_t key_resault;
		uint8_t count;    
		uint8_t double_count;
		uint8_t trible_count;
	} KEY_FLAG;
	
    KEY_FLAG key_s; //声明一个按键结构体变量
	
	#define KEY_NONE 0 
	#define KEY_1_DOWN 1
	#define	KEY_1_UP 2
	#define	KEY_1_LONG 3
	#define	KEY_1_DUBDOW 4
	#define	KEY_1_TIEBLE 5
	#define	KEY_1_DOUBLE 6
	
	#define KEY_LONG_TIME 40  //三击按键间隔最小时间40*50=2000ms
	#define KEY_DOUBLE_MAX 10 //双击按键间隔最小时间10*50=500ms
	#define KEY_DOUBLE_MIN 1  //双击按键消抖时间 1*50=50ms

The key.c file is as follows


if(key1==0)   //key1读取KEY按键,表示key1按下
	{                   
	switch(key_s.state)
		{
			case KEY_NONE:            
			 key_s.state=KEY_1_DOWN; //按键状态切换为单次按下
			 break;
			case KEY_1_DOWN:  //这里为按键长按检测   
			 key_s.long_count++;   
				if(key_s.long_count>=KEY_LONG_TIME)
			 {
				key_s.key_resault=KEY_1_LONG;
				key_s.state=KEY_1_LONG;
			 }
			 break;
			case KEY_1_UP: //如果本次按键按下距上次按下时间小于双击的最小时间间隔,则会执行以下程序             
			 key_s.state=KEY_1_DUBDOW; //按键状态切换为双击
				key_s.count=0;
			 break;
			case KEY_1_LONG: // 按键长按状态,不进行状态切换         
			 break;
			case KEY_1_DUBDOW:          
			 break;
			case KEY_1_TIEBLE:          
			 break;
			case KEY_1_DOUBLE: //双击按下后再次按下按键,为三击操作
				key_s.state=KEY_1_TIEBLE;
			 break;
			
			default:
			 break;
		}
	
	}
	else
	{
	 switch(key_s.state) //按键松开
	 {
		 case KEY_NONE:            
			key_s.count=0;
			key_s.double_count=0;
			key_s.long_count=0;
			key_s.trible_count=0;     
			break;
		 
		 case KEY_1_DOWN: //单击按键松开后,进行双击时间检测,为了和长按区分,变换按键状态
			key_s.state=KEY_1_UP;
		 break;
		 case KEY_1_UP://双击时间间隔检测
			 key_s.count++;         
			 key_s.double_count++;
			 if(key_s.count>=KEY_DOUBLE_MAX) //表示超过双击最小时间间隔
			 {
				key_s.key_resault=KEY_1_DOWN; //按键检测最终结果为单次按下
				key_s.state=KEY_NONE;//清除按键状态
			 }
			break;
			case KEY_1_LONG:  //长按按键后松开,需要对按键结果和状态即使清除
			 key_s.state=KEY_NONE;
			 key_s.key_resault=KEY_NONE;
			 break;
			case KEY_1_DUBDOW: //双击按键松手后,进行双击状态切换
			 key_s.state=KEY_1_DOUBLE;
			 break;
			case KEY_1_DOUBLE:
			 key_s.count++;        
				key_s.trible_count++;
				if(key_s.count>=KEY_DOUBLE_MAX)
			 {
				if((key_s.double_count>=KEY_DOUBLE_MIN)&&(key_s.double_count<=KEY_DOUBLE_MAX)) //双击按键间隔最小限制可以消抖
				 {
					key_s.key_resault=KEY_1_DOUBLE;
				 }
				key_s.state=KEY_NONE;
			 }      
				break;
			 case KEY_1_TIEBLE:
			 if((key_s.trible_count>=KEY_DOUBLE_MIN)&&(key_s.trible_count<=KEY_DOUBLE_MAX))
				{
				 key_s.key_resault=KEY_1_TIEBLE; //三击同双击
				}
			 key_s.state=KEY_NONE;  
				break;
		 default:
			break;
	 }
	}

Problems needing attention in this algorithm

1. In the algorithm, only when the button is pressed for a long time, the button status and result will be cleared when the button is released. In other cases, such as double-click and triple-click, the button result can be cleared in the program that calls the button.
2. In the key structure, the state and result of the key are distinguished. This is because after the state of the key is switched, it needs to wait for a time interval such as double-click and triple-click to determine it, that is, the result of the key.
3. In the button state, double-click is divided into KEY_1_DUBDOW and KEY_1_DOUBLE. The purpose is to perform the second key release detection.
4. The algorithm is only for the detection of one key signal, and the same key is detected for multiple keys.

Guess you like

Origin blog.csdn.net/qq_48691686/article/details/115222288