From e7fd5bc6cb321cfd89d30f1eafe412bc82ebfd34 Mon Sep 17 00:00:00 2001 From: Jaup <270995079@qq.com> Date: Tue, 30 Aug 2016 02:11:39 +0000 Subject: [PATCH] add the example --- README.md | 47 +++++++++++++++++++++++++++++++++++- button.c | 6 +++-- button.h | 3 +-- examples/SimpleMultiButton.c | 6 ++--- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f61d56e..52a425f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,47 @@ # MultiButton -embedded event-driven button driver. +Embedded event-driven button driver. + + +## Examples + +``` +#include "button.h" + +struct Button button1; + +int read_button_pin() +{ + return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); //HAL GPIO read. +} + +int main() +{ + button_init(&button1, read_K1_pin, 0); + button_attach(&button1, SINGLE_CLICK, BTN1_SINGLE_CLICK_Handler); + button_attach(&button1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler); + button_attach(&button1, LONG_RRESS_START, BTN1_LONG_RRESS_START_Handler); + button_attach(&button1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler); + button_attach(&button1, LONG_PRESS_STOP, BTN1_LONG_PRESS_STOP_Handler); + button_start(&button1); + + //make the timer repeat invoking the button_ticks() interval 5ms. + __timer_start(button_ticks, 0, 5); + + while(ture) + { + ... + } +} + +void BTN1_SINGLE_CLICK_Handler() +{ + //do something.. +} + +void BTN1_DOUBLE_Click_Handler() +{ + //do something.. +} +... +``` + diff --git a/button.c b/button.c index 90ac6ca..a6c44da 100644 --- a/button.c +++ b/button.c @@ -6,10 +6,12 @@ #define LOW 0 #define TICKS_INTERVAL 5 //ms -const uint8_t kDebounceTicks = 3; +//According to your need to modify the constants. +const uint8_t kDebounceTicks = 3; //MAX 3 const uint16_t kClickTicks = (400/TICKS_INTERVAL); const uint16_t kLongTicks = (1000/TICKS_INTERVAL); +//button handle list head. static struct Button* head_handle = NULL; /** @@ -86,7 +88,7 @@ void button_handler(struct Button* handle) case 2: if(handle->ticks > kClickTicks) { //released //press event - if(handle->cb[CLICK]) handle->cb[CLICK](); //signal click + if(handle->cb[SINGLE_CLICK]) handle->cb[SINGLE_CLICK](); //signal click handle->state = 0; //reset diff --git a/button.h b/button.h index 5e70843..332faa6 100644 --- a/button.h +++ b/button.h @@ -7,7 +7,7 @@ typedef void (*CallBackFunc)(void); typedef enum { - CLICK = 0, + SINGLE_CLICK = 0, DOUBLE_CLICK, LONG_RRESS_START, LONG_PRESS_HOLD, @@ -15,7 +15,6 @@ typedef enum { number_of_event }BtnEvent; - struct Button { uint16_t ticks; uint8_t state : 3; diff --git a/examples/SimpleMultiButton.c b/examples/SimpleMultiButton.c index bd7aac0..6aac949 100644 --- a/examples/SimpleMultiButton.c +++ b/examples/SimpleMultiButton.c @@ -7,7 +7,7 @@ struct Button btn3; void main() { button_init(&btn1, read_K1_pin, 0); - button_attach(&btn1, CLICK, BTN1_Click_Handler); + button_attach(&btn1, SINGLE_CLICK, BTN1_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_HOLD, BTN1_LONG_PRESS_HOLD_Handler); @@ -15,7 +15,7 @@ void main() button_start(&btn1); button_init(&btn2, read_K2_pin, 0); - button_attach(&btn2, CLICK, BTN2_Click_Handler); + button_attach(&btn2, SINGLE_CLICK, BTN2_Click_Handler); button_attach(&btn2, DOUBLE_CLICK, BTN2_DOUBLE_Click_Handler); button_attach(&btn2, LONG_RRESS_START, BTN2_LONG_RRESS_START_Handler); button_attach(&btn2, LONG_PRESS_HOLD, BTN2_LONG_PRESS_HOLD_Handler); @@ -23,7 +23,7 @@ void main() button_start(&btn2); button_init(&btn3, read_K3_pin, 0); - button_attach(&btn3, CLICK, BTN3_Click_Handler); + button_attach(&btn3, SINGLE_CLICK, BTN3_Click_Handler); button_attach(&btn3, DOUBLE_CLICK, BTN3_DOUBLE_Click_Handler); button_attach(&btn3, LONG_RRESS_START, BTN3_LONG_RRESS_START_Handler); button_attach(&btn3, LONG_PRESS_HOLD, BTN3_LONG_PRESS_HOLD_Handler);