Jelajahi Sumber

Merge pull request #10 from mysterywolf/syn

同步上游已合并PR
流光 2 tahun lalu
induk
melakukan
eeeade9b54
3 mengubah file dengan 20 tambahan dan 19 penghapusan
  1. 2 5
      README.md
  2. 15 11
      multi_button.c
  3. 3 3
      multi_button.h

+ 2 - 5
README.md

@@ -105,7 +105,7 @@ int main()
 	button_attach(&btn1, SINGLE_CLICK,     BTN1_SINGLE_Click_Handler);
 	button_attach(&btn1, DOUBLE_CLICK,     BTN1_DOUBLE_Click_Handler);
 	button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
-	button_attach(&btn2, LONG_PRESS_HOLD,  BTN1_LONG_PRESS_HOLD_Handler);
+	button_attach(&btn1, LONG_PRESS_HOLD,  BTN1_LONG_PRESS_HOLD_Handler);
 	button_start(&btn1);
 	
 	//make the timer invoking the button_ticks() interval 5ms.
@@ -116,11 +116,8 @@ int main()
 	{}
 }
 
-...
 ```
 
-
-
 ## 状态图
 
-![states.png](states.png)
+![states.png](states.png)

+ 15 - 11
multi_button.c

@@ -24,8 +24,8 @@ static struct button* head_handle = NULL;
 
 /**
   * @brief  Initializes the button struct handle.
-  * @param  handle: the button handle strcut.
-  * @param  pin_level: read the pin of the connet button level.
+  * @param  handle: the button handle struct.
+  * @param  pin_level: read the pin of the connected button level.
   * @param  active_level: pin pressed level.
   * @retval None
   */
@@ -40,7 +40,7 @@ void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t activ
 
 /**
   * @brief  Attach the button event callback function.
-  * @param  handle: the button handle strcut.
+  * @param  handle: the button handle struct.
   * @param  event: trigger event type.
   * @param  cb: callback function.
   * @retval None
@@ -52,7 +52,7 @@ void button_attach(struct button* handle, PressEvent event, BtnCallback cb)
 
 /**
   * @brief  Inquire the button event happen.
-  * @param  handle: the button handle strcut.
+  * @param  handle: the button handle struct.
   * @retval button event.
   */
 PressEvent get_button_event(struct button* handle)
@@ -62,10 +62,10 @@ PressEvent get_button_event(struct button* handle)
 
 /**
   * @brief  button driver core function, driver state machine.
-  * @param  handle: the button handle strcut.
+  * @param  handle: the button handle struct.
   * @retval None
   */
-void button_handler(struct button* handle)
+static void button_handler(struct button* handle)
 {
     uint8_t read_gpio_level = handle->hal_button_Level();
 
@@ -88,7 +88,7 @@ void button_handler(struct button* handle)
     }
     else
     {
-        // leved not change ,counter reset.
+        // level not change ,counter reset.
         handle->debounce_cnt = 0;
     }
 
@@ -169,9 +169,9 @@ void button_handler(struct button* handle)
                 handle->state = 0;
             }
         }
-        else if(handle->ticks > SHORT_TICKS)
+        else if(handle->ticks > SHORT_TICKS) // SHORT_TICKS < press down hold time < LONG_TICKS
         {
-            handle->state = 0;
+            handle->state = 1;
         }
         break;
 
@@ -192,12 +192,16 @@ void button_handler(struct button* handle)
             handle->state = 0;
         }
         break;
+
+    default:
+        handle->state = 0; /* reset */
+        break;
     }
 }
 
 /**
   * @brief  Start the button work, add the handle into work list.
-  * @param  handle: target handle strcut.
+  * @param  handle: target handle struct.
   * @retval 0: succeed. -1: already exist.
   */
 int button_start(struct button* handle)
@@ -222,7 +226,7 @@ int button_start(struct button* handle)
 
 /**
   * @brief  Stop the button work, remove the handle off work list.
-  * @param  handle: target handle strcut.
+  * @param  handle: target handle struct.
   * @retval None
   */
 void button_stop(struct button* handle)

+ 3 - 3
multi_button.h

@@ -1,12 +1,12 @@
 #ifndef __MULTI_BUTTON_H_
 #define __MULTI_BUTTON_H_
 
-#include "stdint.h"
-#include "string.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 DEBOUNCE_TICKS    3 //MAX 7 (0 ~ 7)
 #define SHORT_TICKS       (300  / TICKS_INTERVAL)
 #define LONG_TICKS        (1000 / TICKS_INTERVAL)
 #define LONG_HOLD_CYC     (500 / TICKS_INTERVAL)