diff --git a/README.md b/README.md index 58b0263..db9519e 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ while(1) { ... if(timer_ticks == 5) { timer_ticks = 0; - + button_ticks(); } } @@ -49,7 +49,7 @@ struct Button { uint8_t repeat: 4; uint8_t event : 4; uint8_t state : 3; - uint8_t debounce_cnt : 3; + uint8_t debounce_cnt : 3; uint8_t active_level : 1; uint8_t button_level : 1; uint8_t (*hal_button_Level)(void); @@ -69,7 +69,7 @@ PRESS_UP | 按键弹起,每次松开都触发 PRESS_REPEAT | 重复按下触发,变量repeat计数连击次数 SINGLE_CLICK | 单击按键事件 DOUBLE_CLICK | 双击按键事件 -LONG_RRESS_START | 达到长按时间阈值时触发一次 +LONG_PRESS_START | 达到长按时间阈值时触发一次 LONG_PRESS_HOLD | 长按期间一直触发 @@ -80,7 +80,7 @@ LONG_PRESS_HOLD | 长按期间一直触发 struct Button btn1; -int read_button1_GPIO() +int read_button1_GPIO() { return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); } @@ -93,15 +93,15 @@ int main() button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler); button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_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_START, BTN1_LONG_PRESS_START_Handler); button_attach(&btn2, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler); button_start(&btn1); - + //make the timer invoking the button_ticks() interval 5ms. //This function is implemented by yourself. - __timer_start(button_ticks, 0, 5); - - while(1) + __timer_start(button_ticks, 0, 5); + + while(1) {} } diff --git a/examples/example_callback.c b/examples/example_callback.c index a7fbc6e..d774069 100644 --- a/examples/example_callback.c +++ b/examples/example_callback.c @@ -3,12 +3,12 @@ struct Button btn1; struct Button btn2; -uint8_t read_button1_GPIO() +uint8_t read_button1_GPIO() { return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); } -uint8_t read_button2_GPIO() +uint8_t read_button2_GPIO() { return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin); } @@ -17,31 +17,31 @@ int main() { button_init(&btn1, read_button1_GPIO, 0); button_init(&btn2, read_button2_GPIO, 0); - + button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler); button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler); button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler); button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_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_START, BTN1_LONG_PRESS_START_Handler); button_attach(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler); - + button_attach(&btn2, PRESS_DOWN, BTN2_PRESS_DOWN_Handler); button_attach(&btn2, PRESS_UP, BTN2_PRESS_UP_Handler); button_attach(&btn2, PRESS_REPEAT, BTN2_PRESS_REPEAT_Handler); button_attach(&btn2, SINGLE_CLICK, BTN2_SINGLE_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_START, BTN2_LONG_PRESS_START_Handler); button_attach(&btn2, LONG_PRESS_HOLD, BTN2_LONG_PRESS_HOLD_Handler); - + button_start(&btn1); button_start(&btn2); - + //make the timer invoking the button_ticks() interval 5ms. //This function is implemented by yourself. - __timer_start(button_ticks, 0, 5); - - while(1) + __timer_start(button_ticks, 0, 5); + + while(1) {} } diff --git a/multi_button.c b/multi_button.c index 5c1385b..3a96fa7 100644 --- a/multi_button.c +++ b/multi_button.c @@ -6,7 +6,7 @@ #include "multi_button.h" #define EVENT_CB(ev) if(handle->cb[ev])handle->cb[ev]((Button*)handle) - + //button handle list head. static struct Button* head_handle = NULL; @@ -93,8 +93,8 @@ void button_handler(struct Button* handle) handle->state = 2; } else if(handle->ticks > LONG_TICKS) { - handle->event = (uint8_t)LONG_RRESS_START; - EVENT_CB(LONG_RRESS_START); + handle->event = (uint8_t)LONG_PRESS_START; + EVENT_CB(LONG_PRESS_START); handle->state = 5; } break; @@ -103,7 +103,7 @@ void button_handler(struct Button* handle) if(handle->button_level == handle->active_level) { //press down again handle->event = (uint8_t)PRESS_DOWN; EVENT_CB(PRESS_DOWN); - handle->repeat++; + handle->repeat++; EVENT_CB(PRESS_REPEAT); // repeat hit handle->ticks = 0; handle->state = 3; diff --git a/multi_button.h b/multi_button.h index 7bbb26f..6cf54d7 100644 --- a/multi_button.h +++ b/multi_button.h @@ -2,7 +2,7 @@ * Copyright (c) 2016 Zibin Zheng * All rights reserved */ - + #ifndef _MULTI_BUTTON_H_ #define _MULTI_BUTTON_H_ @@ -24,7 +24,7 @@ typedef enum { PRESS_REPEAT, SINGLE_CLICK, DOUBLE_CLICK, - LONG_RRESS_START, + LONG_PRESS_START, LONG_PRESS_HOLD, number_of_event, NONE_PRESS @@ -35,7 +35,7 @@ typedef struct Button { uint8_t repeat : 4; uint8_t event : 4; uint8_t state : 3; - uint8_t debounce_cnt : 3; + uint8_t debounce_cnt : 3; uint8_t active_level : 1; uint8_t button_level : 1; uint8_t (*hal_button_Level)(void); @@ -43,9 +43,9 @@ typedef struct Button { struct Button* next; }Button; -#ifdef __cplusplus -extern "C" { -#endif +#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, PressEvent event, BtnCallback cb); @@ -55,7 +55,7 @@ void button_stop(struct Button* handle); void button_ticks(void); #ifdef __cplusplus -} +} #endif #endif