Jelajahi Sumber

增加独立按键短按和长按时间控制

https://github.com/0x1abin/MultiButton/pull/41
KUAN 2 tahun lalu
induk
melakukan
fea3dc3c56
2 mengubah file dengan 32 tambahan dan 4 penghapusan
  1. 28 4
      multi_button.c
  2. 4 0
      multi_button.h

+ 28 - 4
multi_button.c

@@ -37,6 +37,8 @@ void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t activ
     handle->hal_button_Level = pin_level;
     handle->button_level = !active_level;
     handle->active_level = active_level;
+    handle->short_ticks = SHORT_TICKS;
+    handle->long_ticks = LONG_TICKS;
 }
 
 /**
@@ -51,6 +53,28 @@ void button_attach(struct button* handle, PressEvent event, BtnCallback cb)
     handle->cb[event] = cb;
 }
 
+/**
+  * @brief  Attach the button adjust ticks
+  * @param  handle: the button handle strcut.
+  * @param  ticks: judge short ticks(unit:ms)
+  * @retval None
+  */
+void button_set_short_ticks(struct Button* handle, uint16_t ticks)
+{
+    handle->short_ticks = ticks / TICKS_INTERVAL;
+}
+
+/**
+  * @brief  Attach the button adjust long ticks
+  * @param  handle: the button handle strcut.
+  * @param  ticks: judge long ticks(unit:ms)
+  * @retval None
+  */
+void button_set_long_ticks(struct Button* handle, uint16_t ticks)
+{
+    handle->long_ticks = ticks / TICKS_INTERVAL;
+}
+
 /**
   * @brief  Inquire the button event happen.
   * @param  handle: the button handle struct.
@@ -119,7 +143,7 @@ static void button_handler(struct button* handle)
             handle->ticks = 0;
             handle->state = 2;
         }
-        else if(handle->ticks > LONG_TICKS)
+        else if(handle->ticks > handle->long_ticks)
         {
             handle->event = (uint8_t)LONG_PRESS_START;
             EVENT_CB(LONG_PRESS_START);
@@ -140,7 +164,7 @@ static void button_handler(struct button* handle)
             handle->ticks = 0;
             handle->state = 3;
         }
-        else if(handle->ticks > SHORT_TICKS)
+        else if(handle->ticks > handle->short_ticks)
         {
             if(handle->repeat == 1)
             {
@@ -162,7 +186,7 @@ static void button_handler(struct button* handle)
             handle->event = (uint8_t)PRESS_UP;
             EVENT_CB(PRESS_UP);
 
-            if(handle->ticks < SHORT_TICKS)
+            if(handle->ticks < handle->short_ticks)
             {
                 handle->ticks = 0;
                 handle->state = 2;
@@ -172,7 +196,7 @@ static void button_handler(struct button* handle)
                 handle->state = 0;
             }
         }
-        else if(handle->ticks > SHORT_TICKS) // SHORT_TICKS < press down hold time < LONG_TICKS
+        else if(handle->ticks > handle->short_ticks) // SHORT_TICKS < press down hold time < LONG_TICKS
         {
             handle->state = 1;
         }

+ 4 - 0
multi_button.h

@@ -27,6 +27,8 @@ typedef enum {
 
 typedef struct button {
     uint16_t ticks;
+    uint16_t short_ticks;
+    uint16_t long_ticks;
     uint8_t  repeat       : 4;
     uint8_t  event        : 4;
     uint8_t  state        : 3;
@@ -48,6 +50,8 @@ PressEvent get_button_event(struct button* handle);
 int  button_start(struct button* handle);
 void button_stop(struct button* 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);
 
 #ifdef __cplusplus
 }