From c7db5467c01c36927fc5cda92ccc76a439483a3b Mon Sep 17 00:00:00 2001 From: Jaup <270995079@qq.com> Date: Mon, 29 Aug 2016 10:06:36 +0000 Subject: [PATCH] init --- button.c | 80 ++++++-------------------------------------------------- button.h | 6 ++--- 2 files changed, 11 insertions(+), 75 deletions(-) diff --git a/button.c b/button.c index 233699c..90ac6ca 100644 --- a/button.c +++ b/button.c @@ -120,12 +120,18 @@ void button_handler(struct Button* handle) /** * @brief Start the button work, add the handle into work list. * @param btn: target handle strcut. - * @retval None + * @retval 0: succeed. -1: already exist. */ -void button_start(struct Button* btn) +int button_start(struct Button* btn) { + struct Button* target = head_handle; + while(target) { + if(target == btn) return -1; //already exist. + target = target->next; + } btn->next = head_handle; head_handle = btn; + return 0; } /** @@ -158,74 +164,4 @@ void button_ticks() button_handler(target); } } - - - -/* - -struct Button btn1; -struct Button btn2; -struct Button btn3; - -void main() -{ - button_init(&btn1, read_K1_pin, 0); - button_attach(&btn1, 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); - button_attach(&btn1, LONG_PRESS_STOP, BTN1_LONG_PRESS_STOP_Handler); - button_start(&btn1); - - button_init(&btn2, read_K2_pin, 0); - button_attach(&btn2, 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); - button_attach(&btn2, LONG_PRESS_STOP, BTN2_LONG_PRESS_STOP_Handler); - button_start(&btn2); - - button_init(&btn3, read_K3_pin, 0); - button_attach(&btn3, 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); - button_attach(&btn3, LONG_PRESS_STOP, BTN3_LONG_PRESS_STOP_Handler); - button_start(&btn3); - - //make the timer invoking the button_ticks() interval 5ms. - // timer_start(button_ticks, 0, 5); - - while(1) - { - - } - - button_stop(&btn1); - button_stop(&btn2); - button_stop(&btn3); -} - -*/ - - - - - - - - - - - - - - - - - - - - - diff --git a/button.h b/button.h index 0427096..5e70843 100644 --- a/button.h +++ b/button.h @@ -15,6 +15,7 @@ typedef enum { number_of_event }BtnEvent; + struct Button { uint16_t ticks; uint8_t state : 3; @@ -26,15 +27,14 @@ struct Button { struct Button* next; }; + #ifdef __cplusplus extern "C" { #endif void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level); void button_attach(struct Button* handle, BtnEvent event, CallBackFunc cb); -void button_handler(struct Button* handle); - -void button_start(struct Button* btn); +int button_start(struct Button* btn); void button_stop(struct Button* btn); void button_ticks(void);