Browse Source

use button_t to replace struct button *

Meco Man 1 year ago
parent
commit
3a0524f535
5 changed files with 42 additions and 38 deletions
  1. 14 12
      README.md
  2. 2 2
      examples/event_async.c
  3. 1 1
      examples/event_inquire.c
  4. 13 13
      multi_button.c
  5. 12 10
      multi_button.h

+ 14 - 12
README.md

@@ -47,19 +47,21 @@ MultiButton 使用C语言实现,基于面向对象方式设计思路,每个
 
 ```c
 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;
+    uint16_t ticks;
+    uint16_t short_ticks;
+    uint16_t long_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];
+    button_t next;
 };
 ```
-这样每个按键使用单向链表相连,依次进入 button_handler(struct button* handle) 状态机处理,所以每个按键的状态彼此独立。
+这样每个按键使用单向链表相连,依次进入 button_handler(button_t handle) 状态机处理,所以每个按键的状态彼此独立。
 
 
 ## 按键事件
@@ -78,7 +80,7 @@ LONG_PRESS_HOLD | 长按期间一直触发
 ## Examples
 
 ```c
-#include "button.h"
+#include <multi_button.h>
 
 struct button btn1;
 

+ 2 - 2
examples/event_async.c

@@ -1,6 +1,6 @@
 #include <rtthread.h>
 #include <rtdevice.h>
-#include "multi_button.h"
+#include <multi_button.h>
 
 static struct button btn;
 
@@ -15,7 +15,7 @@ void button_callback(void *btn)
 {
     uint32_t btn_event_val;
 
-    btn_event_val = get_button_event((struct button *)btn);
+    btn_event_val = get_button_event((button_t)btn);
 
     switch(btn_event_val)
     {

+ 1 - 1
examples/event_inquire.c

@@ -1,6 +1,6 @@
 #include <rtthread.h>
 #include <rtdevice.h>
-#include "multi_button.h"
+#include <multi_button.h>
 
 static struct button btn;
 

+ 13 - 13
multi_button.c

@@ -21,7 +21,7 @@
 #define EVENT_CB(ev) if(handle->cb[ev]) handle->cb[ev]((void*)handle)
 #define PRESS_REPEAT_MAX_NUM  15 /*!< The maximum value of the repeat counter */
 
-static struct button* head_handle = NULL;
+static button_t head_handle = NULL;
 
 /**
   * @brief  Initializes the button struct handle.
@@ -30,7 +30,7 @@ static struct button* head_handle = NULL;
   * @param  active_level: pin pressed level.
   * @retval None
   */
-void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t active_level)
+void button_init(button_t handle, uint8_t(*pin_level)(void), uint8_t active_level)
 {
     memset(handle, 0, sizeof(struct button));
     handle->event = (uint8_t)NONE_PRESS;
@@ -48,7 +48,7 @@ void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t activ
   * @param  cb: callback function.
   * @retval None
   */
-void button_attach(struct button* handle, PressEvent event, BtnCallback cb)
+void button_attach(button_t handle, PressEvent event, BtnCallback cb)
 {
     handle->cb[event] = cb;
 }
@@ -59,7 +59,7 @@ void button_attach(struct button* handle, PressEvent event, BtnCallback cb)
   * @param  ticks: judge short ticks(unit:ms)
   * @retval None
   */
-void button_set_short_ticks(struct button* handle, uint16_t ticks)
+void button_set_short_ticks(button_t handle, uint16_t ticks)
 {
     handle->short_ticks = ticks / TICKS_INTERVAL;
 }
@@ -70,7 +70,7 @@ void button_set_short_ticks(struct button* handle, uint16_t ticks)
   * @param  ticks: judge long ticks(unit:ms)
   * @retval None
   */
-void button_set_long_ticks(struct button* handle, uint16_t ticks)
+void button_set_long_ticks(button_t handle, uint16_t ticks)
 {
     handle->long_ticks = ticks / TICKS_INTERVAL;
 }
@@ -80,7 +80,7 @@ void button_set_long_ticks(struct button* handle, uint16_t ticks)
   * @param  handle: the button handle struct.
   * @retval button event.
   */
-PressEvent get_button_event(struct button* handle)
+PressEvent get_button_event(button_t handle)
 {
     return (PressEvent)(handle->event);
 }
@@ -90,7 +90,7 @@ PressEvent get_button_event(struct button* handle)
   * @param  handle: the button handle struct.
   * @retval None
   */
-static void button_handler(struct button* handle)
+static void button_handler(button_t handle)
 {
     uint8_t read_gpio_level = handle->hal_button_Level();
 
@@ -231,9 +231,9 @@ static void button_handler(struct button* handle)
   * @param  handle: target handle struct.
   * @retval 0: succeed. -1: already exist.
   */
-int button_start(struct button* handle)
+int button_start(button_t handle)
 {
-    struct button* target = head_handle;
+    button_t target = head_handle;
 
     while(target)
     {
@@ -256,13 +256,13 @@ int button_start(struct button* handle)
   * @param  handle: target handle struct.
   * @retval None
   */
-void button_stop(struct button* handle)
+void button_stop(button_t handle)
 {
-    struct button** curr;
+    button_t* curr;
 
     for(curr = &head_handle; *curr;)
     {
-        struct button* entry = *curr;
+        button_t entry = *curr;
 
         if (entry == handle)
         {
@@ -283,7 +283,7 @@ void button_stop(struct button* handle)
   */
 void button_ticks(void)
 {
-    struct button* target;
+    button_t target;
 
     for(target = head_handle; target != NULL; target = target->next)
     {

+ 12 - 10
multi_button.h

@@ -25,7 +25,9 @@ typedef enum {
     NONE_PRESS
 }PressEvent;
 
-typedef struct button {
+
+typedef struct button *button_t;
+struct button {
     uint16_t ticks;
     uint16_t short_ticks;
     uint16_t long_ticks;
@@ -37,21 +39,21 @@ typedef struct button {
     uint8_t  button_level : 1;
     uint8_t  (*hal_button_Level)(void);
     BtnCallback  cb[number_of_event];
-    struct button* next;
-}button;
+    button_t next;
+};
 
 #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);
-PressEvent get_button_event(struct button* handle);
-int  button_start(struct button* handle);
-void button_stop(struct button* handle);
+void button_init(button_t handle, uint8_t(*pin_level)(void), uint8_t active_level);
+void button_attach(button_t handle, PressEvent event, BtnCallback cb);
+PressEvent get_button_event(button_t handle);
+int  button_start(button_t handle);
+void button_stop(button_t handle);
 void button_ticks(void);
-void button_set_short_ticks(struct button* handle, uint16_t ticks);
-void button_set_long_ticks(struct button* handle, uint16_t ticks);
+void button_set_short_ticks(button_t handle, uint16_t ticks);
+void button_set_long_ticks(button_t handle, uint16_t ticks);
 
 #ifdef __cplusplus
 }