Răsfoiți Sursa

升级到官方最新

Meco Man 4 ani în urmă
părinte
comite
998c5a27e3
5 a modificat fișierele cu 145 adăugiri și 149 ștergeri
  1. 19 20
      README.md
  2. 29 29
      examples/event_async.c
  3. 28 28
      examples/event_inquire.c
  4. 63 66
      multi_button.c
  5. 6 6
      multi_button.h

+ 19 - 20
README.md

@@ -8,29 +8,29 @@ MultiButton的作者是0x1abin, github地址: https://github.com/0x1abin/MultiBu
 ## 使用方法
 1.先申请一个按键结构
 
-```
+```c
 struct Button button1;
 ```
 2.初始化按键对象,绑定按键的GPIO电平读取接口**read_button_pin()** ,后一个参数设置有效触发电平
 
-```
+```c
 button_init(&button1, read_button_pin, 0);
 ```
 3.注册按键事件
 
-```
+```c
 button_attach(&button1, SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
 button_attach(&button1, DOUBLE_CLICK, Callback_DOUBLE_Click_Handler);
 ...
 ```
 4.启动按键
 
-```
+```c
 button_start(&button1);
 ```
 5.设置一个5ms间隔的定时器循环调用后台处理函数
 
-```
+```c
 while(1) {
     ...
     if(timer_ticks == 5) {
@@ -45,7 +45,7 @@ while(1) {
 
 MultiButton 使用C语言实现,基于面向对象方式设计思路,每个按键对象单独用一份数据结构管理:
 
-```
+```c
 struct Button {
 	uint16_t ticks;
 	uint8_t  repeat: 4;
@@ -71,22 +71,31 @@ PRESS_UP | 按键弹起,每次松开都触发
 PRESS_REPEAT | 重复按下触发,变量repeat计数连击次数
 SINGLE_CLICK | 单击按键事件
 DOUBLE_CLICK | 双击按键事件
-LONG_RRESS_START | 达到长按时间阈值时触发一次
+LONG_PRESS_START | 达到长按时间阈值时触发一次
 LONG_PRESS_HOLD | 长按期间一直触发
 
 
 ## Examples
 
-```
+```c
 #include "button.h"
 
 struct Button btn1;
 
-int read_button1_GPIO() 
+uint8_t read_button1_GPIO() 
 {
 	return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
 }
 
+void BTN1_PRESS_DOWN_Handler(void* btn)
+{
+	//do something...
+}
+void BTN1_PRESS_UP_Handler(void* btn)
+{
+	//do something...
+}
+
 int main()
 {
 	button_init(&btn1, read_button1_GPIO, 0);
@@ -95,7 +104,7 @@ int main()
 	button_attach(&btn1, PRESS_REPEAT,     BTN1_PRESS_REPEAT_Handler);
 	button_attach(&btn1, SINGLE_CLICK,     BTN1_SINGLE_Click_Handler);
 	button_attach(&btn1, DOUBLE_CLICK,     BTN1_DOUBLE_Click_Handler);
-	button_attach(&btn1, LONG_RRESS_START, BTN1_LONG_RRESS_START_Handler);
+	button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
 	button_attach(&btn2, LONG_PRESS_HOLD,  BTN1_LONG_PRESS_HOLD_Handler);
 	button_start(&btn1);
 	
@@ -107,16 +116,6 @@ int main()
 	{}
 }
 
-void BTN1_PRESS_DOWN_Handler(void* btn)
-{
-	//do something...
-}
-
-void BTN1_PRESS_UP_Handler(void* btn)
-{
-	//do something...
-}
-
 ...
 ```
 

+ 29 - 29
examples/event_async.c

@@ -1,51 +1,51 @@
-#include <rtthread.h> 
-#include <rtdevice.h> 
+#include <rtthread.h>
+#include <rtdevice.h>
 #include "multi_button.h"
 
 static struct button btn;
 
 #define BUTTON_PIN (10)
 
-static uint8_t button_read_pin(void) 
+static uint8_t button_read_pin(void)
 {
-    return rt_pin_read(BUTTON_PIN); 
+    return rt_pin_read(BUTTON_PIN);
 }
 
 void button_callback(void *btn)
 {
-    uint32_t btn_event_val; 
-    
-    btn_event_val = get_button_event((struct button *)btn); 
-    
+    uint32_t btn_event_val;
+
+    btn_event_val = get_button_event((struct button *)btn);
+
     switch(btn_event_val)
     {
     case PRESS_DOWN:
-        rt_kprintf("button press down\n"); 
-    break; 
+        rt_kprintf("button press down\n");
+    break;
 
-    case PRESS_UP: 
+    case PRESS_UP:
         rt_kprintf("button press up\n");
-    break; 
+    break;
 
-    case PRESS_REPEAT: 
+    case PRESS_REPEAT:
         rt_kprintf("button press repeat\n");
-    break; 
+    break;
 
-    case SINGLE_CLICK: 
+    case SINGLE_CLICK:
         rt_kprintf("button single click\n");
-    break; 
+    break;
 
-    case DOUBLE_CLICK: 
+    case DOUBLE_CLICK:
         rt_kprintf("button double click\n");
-    break; 
+    break;
 
-    case LONG_RRESS_START: 
+    case LONG_RRESS_START:
         rt_kprintf("button long press start\n");
-    break; 
+    break;
 
-    case LONG_PRESS_HOLD: 
+    case LONG_PRESS_HOLD:
         rt_kprintf("button long press hold\n");
-    break; 
+    break;
     }
 }
 
@@ -54,25 +54,25 @@ void btn_thread_entry(void* p)
     while(1)
     {
         /* 5ms */
-        rt_thread_delay(RT_TICK_PER_SECOND/200); 
-        button_ticks(); 
+        rt_thread_delay(RT_TICK_PER_SECOND/200);
+        button_ticks();
     }
 }
 
 int multi_button_test(void)
 {
     rt_thread_t thread = RT_NULL;
-    
+
     /* Create background ticks thread */
     thread = rt_thread_create("btn", btn_thread_entry, RT_NULL, 1024, 10, 10);
     if(thread == RT_NULL)
     {
-        return RT_ERROR; 
+        return RT_ERROR;
     }
     rt_thread_startup(thread);
 
     /* low level drive */
-    rt_pin_mode  (BUTTON_PIN, PIN_MODE_INPUT); 
+    rt_pin_mode  (BUTTON_PIN, PIN_MODE_INPUT);
     button_init  (&btn, button_read_pin, PIN_LOW);
     button_attach(&btn, PRESS_DOWN,       button_callback);
     button_attach(&btn, PRESS_UP,         button_callback);
@@ -83,6 +83,6 @@ int multi_button_test(void)
     button_attach(&btn, LONG_PRESS_HOLD,  button_callback);
     button_start (&btn);
 
-    return RT_EOK; 
+    return RT_EOK;
 }
-INIT_APP_EXPORT(multi_button_test); 
+INIT_APP_EXPORT(multi_button_test);

+ 28 - 28
examples/event_inquire.c

@@ -1,60 +1,60 @@
-#include <rtthread.h> 
-#include <rtdevice.h> 
+#include <rtthread.h>
+#include <rtdevice.h>
 #include "multi_button.h"
 
 static struct button btn;
 
 #define BUTTON_PIN (10)
 
-static uint8_t button_read_pin(void) 
+static uint8_t button_read_pin(void)
 {
-    return rt_pin_read(BUTTON_PIN); 
+    return rt_pin_read(BUTTON_PIN);
 }
 
 void btn_test_thread_entry(void *p)
 {
-    uint32_t btn_event_val; 
-    
+    uint32_t btn_event_val;
+
     while(1)
     {
-        if(btn_event_val != get_button_event(&btn)) 
+        if(btn_event_val != get_button_event(&btn))
         {
             btn_event_val = get_button_event(&btn);
 
             switch(btn_event_val)
             {
             case PRESS_DOWN:
-                rt_kprintf("button press down\n"); 
-            break; 
+                rt_kprintf("button press down\n");
+            break;
 
-            case PRESS_UP: 
+            case PRESS_UP:
                 rt_kprintf("button press up\n");
-            break; 
+            break;
 
-            case PRESS_REPEAT: 
+            case PRESS_REPEAT:
                 rt_kprintf("button press repeat\n");
-            break; 
+            break;
 
-            case SINGLE_CLICK: 
+            case SINGLE_CLICK:
                 rt_kprintf("button single click\n");
-            break; 
+            break;
 
-            case DOUBLE_CLICK: 
+            case DOUBLE_CLICK:
                 rt_kprintf("button double click\n");
-            break; 
+            break;
 
-            case LONG_RRESS_START: 
+            case LONG_RRESS_START:
                 rt_kprintf("button long press start\n");
-            break; 
+            break;
 
-            case LONG_PRESS_HOLD: 
+            case LONG_PRESS_HOLD:
                 rt_kprintf("button long press hold\n");
-            break; 
+            break;
             }
         }
-        
-        button_ticks(); 
-        rt_thread_delay(RT_TICK_PER_SECOND/200); 
+
+        button_ticks();
+        rt_thread_delay(RT_TICK_PER_SECOND/200);
     }
 }
 
@@ -65,15 +65,15 @@ int multi_button_test(void)
     thread = rt_thread_create("btn_test", btn_test_thread_entry, RT_NULL, 1024, 15, 10);
     if(thread == RT_NULL)
     {
-        return RT_ERROR; 
+        return RT_ERROR;
     }
     rt_thread_startup(thread);
 
     /* low level drive */
-    rt_pin_mode (BUTTON_PIN, PIN_MODE_INPUT); 
+    rt_pin_mode (BUTTON_PIN, PIN_MODE_INPUT);
     button_init (&btn, button_read_pin, PIN_LOW);
     button_start(&btn);
 
-    return RT_EOK; 
+    return RT_EOK;
 }
-INIT_APP_EXPORT(multi_button_test); 
+INIT_APP_EXPORT(multi_button_test);

+ 63 - 66
multi_button.c

@@ -1,25 +1,25 @@
 /*
  * @File:    multi_button.c
  * @Author:  Zibin Zheng
- * @Date:    2018-01-23 20:36:01 
- * 
+ * @Date:    2018-01-23 20:36:01
+ *
  * @LICENSE: Copyright (c) 2016 Zibin Zheng <znbin@qq.com>
- *           All rights reserved. 
- * 
+ *           All rights reserved.
+ *
  * @NOTE:    The original author of MultiButton is Zibin Zheng.
- *           Please contact the original author for authorization 
- *           before use.I(liu2guang) only adapt the library to 
- *           rt-thread and fix some bugs. I(liu2guang) am not 
+ *           Please contact the original author for authorization
+ *           before use.I(liu2guang) only adapt the library to
+ *           rt-thread and fix some bugs. I(liu2guang) am not
  *           responsible for the authorization of the library.
  *
- * Change Logs: 
- * Date           Author       Notes 
- * 2018-01-23     liu2guang    Adapter rtthread and Fix the bug. 
- */ 
-#include "multi_button.h" 
+ * Change Logs:
+ * Date           Author       Notes
+ * 2018-01-23     liu2guang    Adapter rtthread and Fix the bug.
+ */
+#include "multi_button.h"
 
 #define EVENT_CB(ev) if(handle->cb[ev]) handle->cb[ev]((button*)handle)
-    
+
 static struct button* head_handle = NULL;
 
 /**
@@ -70,41 +70,41 @@ void button_handler(struct button* handle)
     uint8_t read_gpio_level = handle->hal_button_Level();
 
     //ticks counter working..
-    if((handle->state) > 0) 
+    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) 
+        if(++(handle->debounce_cnt) >= DEBOUNCE_TICKS)
         {
             handle->button_level = read_gpio_level;
             handle->debounce_cnt = 0;
         }
-    } 
-    else 
-    { 
+    }
+    else
+    {
         // leved not change ,counter reset.
         handle->debounce_cnt = 0;
     }
 
     /*-----------------State machine-------------------*/
-    switch (handle->state) 
+    switch (handle->state)
     {
     case 0:
-        if(handle->button_level == handle->active_level) 
-        { 
+        if(handle->button_level == handle->active_level)
+        {
             handle->event = (uint8_t)PRESS_DOWN;
             EVENT_CB(PRESS_DOWN);
             handle->ticks  = 0;
             handle->repeat = 1;
-            handle->state  = 1; 
-        } 
-        else 
+            handle->state  = 1;
+        }
+        else
         {
             handle->event = (uint8_t)NONE_PRESS;
         }
@@ -112,62 +112,59 @@ void button_handler(struct button* handle)
 
     case 1:
         if(handle->button_level != handle->active_level)
-        { 
+        {
             handle->event = (uint8_t)PRESS_UP;
             EVENT_CB(PRESS_UP);
             handle->ticks = 0;
-            handle->state = 2; 
+            handle->state = 2;
 
-        } 
+        }
         else if(handle->ticks > LONG_TICKS)
         {
-            handle->event = (uint8_t)LONG_RRESS_START;
-            EVENT_CB(LONG_RRESS_START);
-            handle->state = 5; 
+            handle->event = (uint8_t)LONG_PRESS_START;
+            EVENT_CB(LONG_PRESS_START);
+            handle->state = 5;
         }
         break;
 
     case 2:
-        if(handle->button_level == handle->active_level) 
-        { 
+        if(handle->button_level == handle->active_level)
+        {
             handle->event = (uint8_t)PRESS_DOWN;
             EVENT_CB(PRESS_DOWN);
             handle->repeat++;
-            
-            handle->event = (uint8_t)PRESS_REPEAT;
             EVENT_CB(PRESS_REPEAT);
             handle->ticks = 0;
             handle->state = 3;
-            
-        } 
-        else if(handle->ticks > SHORT_TICKS) 
-        { 
-            if(handle->repeat == 1) 
+        }
+        else if(handle->ticks > SHORT_TICKS)
+        {
+            if(handle->repeat == 1)
             {
                 handle->event = (uint8_t)SINGLE_CLICK;
                 EVENT_CB(SINGLE_CLICK);
-            } 
-            else if(handle->repeat == 2) 
+            }
+            else if(handle->repeat == 2)
             {
                 handle->event = (uint8_t)DOUBLE_CLICK;
-                EVENT_CB(DOUBLE_CLICK); 
+                EVENT_CB(DOUBLE_CLICK);
             }
             handle->state = 0;
         }
         break;
 
     case 3:
-        if(handle->button_level != handle->active_level) 
+        if(handle->button_level != handle->active_level)
         {
             handle->event = (uint8_t)PRESS_UP;
             EVENT_CB(PRESS_UP);
-            
-            if(handle->ticks < SHORT_TICKS) 
+
+            if(handle->ticks < SHORT_TICKS)
             {
                 handle->ticks = 0;
-                handle->state = 2; 
-            } 
-            else 
+                handle->state = 2;
+            }
+            else
             {
                 handle->state = 0;
             }
@@ -175,20 +172,20 @@ void button_handler(struct button* handle)
         break;
 
     case 5:
-        if(handle->button_level == handle->active_level) 
+        if(handle->button_level == handle->active_level)
         {
             handle->event = (uint8_t)LONG_PRESS_HOLD;
             if (handle->ticks % LONG_HOLD_CYC == 0)
             {
                 EVENT_CB(LONG_PRESS_HOLD);
             }
-        } 
-        else 
+        }
+        else
         {
             handle->event = (uint8_t)PRESS_UP;
             EVENT_CB(PRESS_UP);
-            
-            handle->state = 0; 
+
+            handle->state = 0;
         }
         break;
     }
@@ -202,20 +199,20 @@ void button_handler(struct button* handle)
 int button_start(struct button* handle)
 {
     struct button* target = head_handle;
-    
-    while(target) 
+
+    while(target)
     {
-        if(target == handle) 
+        if(target == handle)
         {
             return -1;  //already exist.
         }
-        
+
         target = target->next;
     }
-    
+
     handle->next = head_handle;
     head_handle = handle;
-    
+
     return 0;
 }
 
@@ -227,15 +224,15 @@ int button_start(struct button* handle)
 void button_stop(struct button* handle)
 {
     struct button** curr;
-    
-    for(curr = &head_handle; *curr;) 
+
+    for(curr = &head_handle; *curr;)
     {
         struct button* entry = *curr;
-        
-        if (entry == handle) 
+
+        if (entry == handle)
         {
             *curr = entry->next;
-        } 
+        }
         else
         {
             curr = &entry->next;
@@ -251,7 +248,7 @@ void button_stop(struct button* handle)
 void button_ticks(void)
 {
     struct button* target;
-    
+
     for(target = head_handle; target != NULL; target = target->next)
     {
         button_handler(target);

+ 6 - 6
multi_button.h

@@ -19,7 +19,7 @@ typedef enum {
     PRESS_REPEAT,
     SINGLE_CLICK,
     DOUBLE_CLICK,
-    LONG_RRESS_START,
+    LONG_PRESS_START,
     LONG_PRESS_HOLD,
     number_of_event,
     NONE_PRESS
@@ -30,7 +30,7 @@ typedef struct button {
     uint8_t  repeat       : 4;
     uint8_t  event        : 4;
     uint8_t  state        : 3;
-    uint8_t  debounce_cnt : 3; 
+    uint8_t  debounce_cnt : 3;
     uint8_t  active_level : 1;
     uint8_t  button_level : 1;
     uint8_t  (*hal_button_Level)(void);
@@ -38,9 +38,9 @@ typedef struct button {
     struct button* next;
 }button;
 
-#ifdef __cplusplus  
-extern "C" {  
-#endif  
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t active_level);
 void button_attach(struct button* handle, PressEvent event, BtnCallback cb);
@@ -50,7 +50,7 @@ void button_stop(struct button* handle);
 void button_ticks(void);
 
 #ifdef __cplusplus
-} 
+}
 #endif
 
 #endif