按鍵事件識別開發利器:開源按鍵組件MultiButton ---- 使用、代碼分析

  • Growing up’s a funny thing. Sneaks up on you.
    長大是件很有趣的事,不經意間就發生了。

一、概括

  • 項目的倉庫地址:https://github.com/0x1abin/MultiButton
    0x1abin大佬的超精簡的軟件定時器multi_timer已經讓人眼前一亮,如今這個按鍵組件MultiButton更令我折服。

    把按鍵的各種事件(按下觸發、按下彈出觸發、單擊、雙擊、長按等)用狀態機非常perfect的實現了。該組件兼具按鍵的消抖處理,親測效果非常不錯。同時,該組件非常易於添加到項目的代碼裏,只需要簡單的幾步操作。

    說着說着,膝蓋又疼了。

二、使用和代碼分析

發現兩篇文章把這個組件的使用和代碼分析都講得蠻清楚了,沒興趣看github上的源碼,可以有興趣看一下下面的文章:

三、項目源代碼和個人的註釋

  • multi_button.h
/*
 * Copyright (c) 2016 Zibin Zheng <[email protected]>
 * All rights reserved
 */
 
#ifndef _MULTI_BUTTON_H_
#define _MULTI_BUTTON_H_

#include "stdint.h"
#include "string.h"

//According to your need to modify the constants.
#define TICKS_INTERVAL    5	//ms
#define DEBOUNCE_TICKS    3	//MAX 8
#define SHORT_TICKS       (300 /TICKS_INTERVAL)
#define LONG_TICKS        (1000 /TICKS_INTERVAL)

typedef void (*BtnCallback)(void*);

typedef enum {
	PRESS_DOWN = 0,		// 按鍵按下,每次按下都觸發
	PRESS_UP,			// 按鍵彈起,每次鬆開都觸發		
	PRESS_REPEAT,		// 重複按下觸發,變量repeat計數連擊次數
	SINGLE_CLICK,		// 單擊按鍵事件
	DOUBLE_CLICK,       // 雙擊按鍵事件
	LONG_RRESS_START,	// 達到長按時間閾值時觸發一次
	LONG_PRESS_HOLD,    // 長按期間一直觸發
	number_of_event,	// 事件種類數
	NONE_PRESS
}PressEvent;

typedef struct Button {
	uint16_t ticks;
	uint8_t  repeat : 4;  // 記錄連擊次數
	uint8_t  event : 4;	  // 記錄具體事件
	uint8_t  state : 3;	  // 下一個要跳轉的狀態,用於狀態的切換
	uint8_t  debounce_cnt : 3;  // 記錄持續次數(每次固定時間,用於去抖)
	uint8_t  active_level : 1;  // 有效電平(按鍵按下時的電平)
	uint8_t  button_level : 1;  // 記錄當前的電平
	uint8_t  (*hal_button_Level)(void); // 函數指針(指向獲取當前按鍵電平的函數)
	BtnCallback  cb[number_of_event];   // 函數指針數組:分別指向各個事件的回調函數
	struct Button* next;
}Button;

#ifdef __cplusplus  
extern "C" {  
#endif  

void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level);
void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
PressEvent get_button_event(struct Button* handle);
int  button_start(struct Button* handle);
void button_stop(struct Button* handle);
void button_ticks(void);

#ifdef __cplusplus
} 
#endif
#endif

  • multi_button.c
/*
 * Copyright (c) 2016 Zibin Zheng <[email protected]>
 * All rights reserved
 */

#include "multi_button.h"

#define EVENT_CB(ev)   if(handle->cb[ev]) handle->cb[ev]((Button*)handle) // 執行回調函數
	
//button handle list head.
static struct Button* head_handle = NULL;

/**
  * @brief  Initializes the button struct handle.
  * @param  handle: the button handle strcut.
  * @param  pin_level: read the HAL GPIO of the connet button level.
  * @param  active_level: pressed GPIO level.
  * @retval None
  */
void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level)
{
	memset(handle, 0, sizeof(struct Button));
	handle->event = (uint8_t)NONE_PRESS;
	handle->hal_button_Level = pin_level;
	handle->button_level = handle->hal_button_Level(); // 得到當前電平
	handle->active_level = active_level;
}

/**
  * @brief  Attach the button event callback function.
  * @param  handle: the button handle strcut.
  * @param  event: trigger event type.
  * @param  cb: callback function.
  * @retval None
  */
void button_attach(struct Button* handle, PressEvent event, BtnCallback cb)
{
	handle->cb[event] = cb; // 將事件類型與回調函數綁定
}

/**
  * @brief  Inquire the button event happen.
  * @param  handle: the button handle strcut.
  * @retval button event.
  */
// 返回按鍵事件類型
PressEvent get_button_event(struct Button* handle)
{
	return (PressEvent)(handle->event);
}

/**
  * @brief  Button driver core function, driver state machine.
  * @param  handle: the button handle strcut.
  * @retval None
  */
void button_handler(struct Button* handle)
{
	uint8_t read_gpio_level = handle->hal_button_Level();

	//ticks counter working..
	if((handle->state) > 0)  handle->ticks++;

	/*------------button debounce handle---------------*/
	if(read_gpio_level != handle->button_level) { //not equal to prev one
		//continue read 3 times same new level change
		if(++(handle->debounce_cnt) >= DEBOUNCE_TICKS) {
			handle->button_level = read_gpio_level;
			handle->debounce_cnt = 0;
		}
	} else { //leved not change ,counter reset.
		handle->debounce_cnt = 0;
	}

	/*-----------------State machine-------------------*/
	switch (handle->state) {
	case 0:
		if(handle->button_level == handle->active_level) {	//start press down
			handle->event = (uint8_t)PRESS_DOWN;
			EVENT_CB(PRESS_DOWN); // 跳轉到回調函數
			handle->ticks = 0;
			handle->repeat = 1;
			handle->state = 1;
		} else {
			handle->event = (uint8_t)NONE_PRESS;
		}
		break;

	case 1:
		if(handle->button_level != handle->active_level) { //released press up
			handle->event = (uint8_t)PRESS_UP;
			EVENT_CB(PRESS_UP);  // 跳轉到回調函數
			handle->ticks = 0;
			handle->state = 2;

		} else if(handle->ticks > LONG_TICKS) {
			handle->event = (uint8_t)LONG_RRESS_START;
			EVENT_CB(LONG_RRESS_START); // 長按
			handle->state = 5;
		}
		break;

	case 2:
		if(handle->button_level == handle->active_level) { //press down again
			handle->event = (uint8_t)PRESS_DOWN;
			EVENT_CB(PRESS_DOWN);
			handle->repeat++;	
            
            // 若將所有的按鍵事件回調函數寫在同一個函數裏,需要添加這一句(該回調函數內是靠handle->event來判斷怎麼處理)
            // 除非你不需要處理PRESS_REPEAT
			handle->event = (uint8_t)PRESS_REPEAT;
            
			EVENT_CB(PRESS_REPEAT); // repeat hit
			handle->ticks = 0;
			handle->state = 3;
		} else if(handle->ticks > SHORT_TICKS) { //released timeout
			if(handle->repeat == 1) {
				handle->event = (uint8_t)SINGLE_CLICK;
				EVENT_CB(SINGLE_CLICK);
			} else if(handle->repeat == 2) {
				handle->event = (uint8_t)DOUBLE_CLICK;
				EVENT_CB(DOUBLE_CLICK); // repeat hit
			}
			handle->state = 0;
		}
		break;

	case 3:
		if(handle->button_level != handle->active_level) { //released press up
			handle->event = (uint8_t)PRESS_UP;
			EVENT_CB(PRESS_UP);
			if(handle->ticks < SHORT_TICKS) {
				handle->ticks = 0;
				handle->state = 2; //repeat press
			} else {
				handle->state = 0;
			}
		}
		break;

	case 5:
		if(handle->button_level == handle->active_level) {
			//continue hold trigger
			handle->event = (uint8_t)LONG_PRESS_HOLD;
			EVENT_CB(LONG_PRESS_HOLD);

		} else { //releasd
			handle->event = (uint8_t)PRESS_UP;
			EVENT_CB(PRESS_UP);
			handle->state = 0; //reset
		}
		break;
	}
}

/**
  * @brief  Start the button work, add the handle into work list.
  * @param  handle: target handle strcut.
  * @retval 0: succeed. -1: already exist.
  */
// 將按鍵結構體插入掃描隊列
int button_start(struct Button* handle)
{
	struct Button* target = head_handle;
	while(target) {
		if(target == handle) return -1;	//already exist.
		target = target->next;
	}
	handle->next = head_handle;
	head_handle = handle;
	return 0;
}

/**
  * @brief  Stop the button work, remove the handle off work list.
  * @param  handle: target handle strcut.
  * @retval None
  */
// 將按鍵結構體脫離掃描隊列
void button_stop(struct Button* handle)
{
	struct Button** curr;
	for(curr = &head_handle; *curr; ) {
		struct Button* entry = *curr;
		if (entry == handle) {
			*curr = entry->next;
//			free(entry);
		} else
			curr = &entry->next;
	}
}

/**
  * @brief  background ticks, timer repeat invoking interval 5ms.
  * @param  None.
  * @retval None
  */

// 將其放在週期定時執行的函數裏,每一個週期掃描一次各個按鍵是否有事件發生
void button_ticks()
{
	struct Button* target;
	for(target=head_handle; target; target=target->next) {
		button_handler(target);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章