91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 6.08KB
    文件類型: .zip
    金幣: 1
    下載: 0 次
    發布日期: 2021-02-01
  • 標簽: 程序??按鍵??

資源簡介

MultiButton 是一個小巧簡單易用的事件驅動型按鍵驅動模塊,可無限量擴展按鍵,按鍵事件的回調異步處理方式可以簡化你的程序結構,去除冗余的按鍵處理硬編碼,讓你的按鍵業務邏輯更清晰。

【使用方法】

1.先申請一個按鍵結構

	
  1. struct Button button1;

2.初始化按鍵對象,綁定按鍵的GPIO電平讀取接口read_button_pin() ,后一個參數設置有效觸發電平

	
  1. button_init(&button1, read_button_pin, 0);

3.注冊按鍵事件

	
  1. button_attach(&button1, SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
  2. button_attach(&button1, DOUBLE_CLICK, Callback_DOUBLE_Click_Handler);
  3. ...

4.啟動按鍵

	
  1. button_start(&button1);

5.設置一個5ms間隔的定時器循環調用后臺處理函數

	
  1. while(1) {
  2. ...
  3. if(timer_ticks == 5) {
  4. timer_ticks = 0;
  5. button_ticks();
  6. }
  7. }

特性

MultiButton 使用C語言實現,基于面向對象方式設計思路,每個按鍵對象單獨用一份數據結構管理:

	
  1. struct Button {
  2. uint16_t ticks;
  3. uint8_t repeat: 4;
  4. uint8_t event : 4;
  5. uint8_t state : 3;
  6. uint8_t debounce_cnt : 3;
  7. uint8_t active_level : 1;
  8. uint8_t button_level : 1;
  9. uint8_t (*hal_button_Level)(void);
  10. BtnCallback cb[number_of_event];
  11. struct Button* next;
  12. };

這樣每個按鍵使用單向鏈表相連,依次進入 button_handler(struct Button* handle) 狀態機處理,所以每個按鍵的狀態彼此獨立。

按鍵事件

事件 說明
PRESS_DOWN 按鍵按下,每次按下都觸發
PRESS_UP 按鍵彈起,每次松開都觸發
PRESS_REPEAT 重復按下觸發,變量repeat計數連擊次數
SINGLE_CLICK 單擊按鍵事件
DOUBLE_CLICK 雙擊按鍵事件
LONG_RRESS_START 達到長按時間閾值時觸發一次
LONG_PRESS_HOLD 長按期間一直觸發

Examples

	
  1. #include "button.h"
  2. struct Button btn1;
  3. int read_button1_GPIO()
  4. {
  5. return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
  6. }
  7. int main()
  8. {
  9. button_init(&btn1, read_button1_GPIO, 0);
  10. button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler);
  11. button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);
  12. button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);
  13. button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler);
  14. button_attach(&btn1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler);
  15. button_attach(&btn1, LONG_RRESS_START, BTN1_LONG_RRESS_START_Handler);
  16. button_attach(&btn2, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
  17. button_start(&btn1);
  18. //make the timer invoking the button_ticks() interval 5ms.
  19. //This function is implemented by yourself.
  20. __timer_start(button_ticks, 0, 5);
  21. while(1)
  22. {}
  23. }
  24. void BTN1_PRESS_DOWN_Handler(void* btn)
  25. {
  26. //do something...
  27. }
  28. void BTN1_PRESS_UP_Handler(void* btn)
  29. {
  30. //do something...
  31. }
  32. ...

資源截圖

代碼片段和文件信息

/*
?*?Copyright?(c)?2016?Zibin?Zheng?
?*?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;
}

/**
??*?@br

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-12-16?01:46??MultiButton-master\
?????文件????????1068??2019-12-16?01:46??MultiButton-master\LICENSE
?????文件????????2904??2019-12-16?01:46??MultiButton-master\README.md
?????目錄???????????0??2019-12-16?01:46??MultiButton-master\examples\
?????文件????????1628??2019-12-16?01:46??MultiButton-master\examples\example_callback.c
?????文件?????????705??2019-12-16?01:46??MultiButton-master\examples\example_poll.c
?????文件????????4892??2019-12-16?01:46??MultiButton-master\multi_button.c
?????文件????????1298??2019-12-16?01:46??MultiButton-master\multi_button.h

評論

共有 條評論