new Version

pull/8/head
Jaup 9 years ago
parent cb85075bfe
commit 305da86b64

@ -90,7 +90,7 @@ int main()
} }
} }
void BTN1_SINGLE_CLICK_Handler() void BTN1_PRESSED_Handler()
{ {
//do something.. //do something..
} }

@ -6,10 +6,11 @@
#include "button.h" #include "button.h"
#define TICKS_INTERVAL 5 //ms #define TICKS_INTERVAL 5 //ms
#define EVENT_CB(ev) if(handle->cb[ev]){handle->cb[ev](handle)}
//According to your need to modify the constants. //According to your need to modify the constants.
const uint8_t kDebounceTicks = 3; //MAX 8 const uint8_t kDebounceTicks = 3; //MAX 8
const uint16_t kClickTicks = (400/TICKS_INTERVAL); const uint16_t kShortTicks = (350/TICKS_INTERVAL);
const uint16_t kLongTicks = (1000/TICKS_INTERVAL); const uint16_t kLongTicks = (1000/TICKS_INTERVAL);
//button handle list head. //button handle list head.
@ -37,19 +38,19 @@ void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_le
* @param cb: callback function. * @param cb: callback function.
* @retval None * @retval None
*/ */
void button_attach(struct Button* handle, BtnEvent event, CallBackFunc cb) void button_attach(struct Button* handle, PressEvent event, CallBackFunc cb)
{ {
handle->cb[event] = cb; handle->cb[event] = cb;
} }
/** /**
* @brief Inquire the button is pressed. * @brief Inquire the button event happen.
* @param handle: the button handle strcut. * @param handle: the button handle strcut.
* @retval 0 not press, 1 pressed. * @retval button event.
*/ */
int button_is_pressed(struct Button* handle) PressEvent get_button_event(const struct Button* handle)
{ {
return ((handle->button_level == handle->active_level) ? 1:0); return (handle->event);
} }
/** /**
@ -79,55 +80,80 @@ void button_handler(struct Button* handle)
/*-----------------State machine-------------------*/ /*-----------------State machine-------------------*/
switch (handle->state) { switch (handle->state) {
case 0: case 0:
if(handle->button_level == handle->active_level) { //start press if(handle->button_level == handle->active_level) { //start press down
if(handle->cb[CLICK]) handle->cb[CLICK](); handle->event = (uint8_t)PRESS_DOWN;
EVENT_CB(PRESS_DOWN);
handle->ticks = 0; handle->ticks = 0;
handle->repeat = 1;
handle->state = 1; handle->state = 1;
} }
break; break;
case 1: case 1:
if(handle->button_level != handle->active_level) { //released if(handle->button_level != handle->active_level) { //released press up
handle->event = (uint8_t)PRESS_UP;
EVENT_CB(PRESS_UP);
handle->ticks = 0;
handle->state = 2; handle->state = 2;
} else if(handle->ticks > kLongTicks) { } else if(handle->ticks > kLongTicks) {
if(handle->cb[LONG_RRESS_START]) handle->cb[LONG_RRESS_START](); handle->event = (uint8_t)LONG_RRESS_START;
EVENT_CB(LONG_RRESS_START);
handle->state = 5; handle->state = 5;
} }
break; break;
case 2: case 2:
if(handle->ticks > kClickTicks) { //released if(handle->button_level == handle->active_level) { //press down again
//press event handle->event = (uint8_t)PRESS_DOWN;
if(handle->cb[PRESSED]) handle->cb[PRESSED](); //press event EVENT_CB(PRESS_DOWN);
handle->repeat++
handle->state = 0; //reset if(handle->repeat == 2) {
handle->event = (uint8_t)DOUBLE_CLICK;
} else if(handle->button_level == handle->active_level) { //press again EVENT_CB(DOUBLE_CLICK); // repeat hit
if(handle->cb[CLICK]) handle->cb[CLICK](); } else {
handle->event = (uint8_t)PRESS_REPEAT;
}
EVENT_CB(PRESS_REPEAT); // repeat hit
handle->ticks = 0;
handle->state = 3; handle->state = 3;
} else if(handle->ticks > kShortTicks) {
if(handle->repeat == 1) {
handle->event = (uint8_t)SINGLE_CLICK;
EVENT_CB(SINGLE_CLICK);
}
handle->state = 0;
handle->event = (uint8_t)NONE_PRESS;
} }
break; break;
case 3: //repeat press pressing case 3:
if(handle->button_level != handle->active_level) { //double releasd if(handle->button_level != handle->active_level) { //released press up
//double click event handle->event = (uint8_t)PRESS_UP;
if(handle->cb[DOUBLE_CLICK]) handle->cb[DOUBLE_CLICK](); EVENT_CB(PRESS_UP);
if(handle->ticks < kShortTicks) {
handle->state = 0; handle->ticks = 0;
handle->state = 2; //repeat press
} else {
handle->state = 0;
handle->event = (uint8_t)NONE_PRESS;
}
} }
break; break;
case 5: case 5:
if(handle->button_level == handle->active_level) { if(handle->button_level == handle->active_level) {
//continue hold trigger //continue hold trigger
if(handle->cb[LONG_PRESS_HOLD]) handle->cb[LONG_PRESS_HOLD](); handle->event = (uint8_t)LONG_PRESS_HOLD;
EVENT_CB(LONG_PRESS_HOLD);
} else { //releasd } else { //releasd
if(handle->cb[LONG_PRESS_STOP]) handle->cb[LONG_PRESS_STOP](); handle->event = (uint8_t)PRESS_UP;
EVENT_CB(PRESS_UP)
handle->state = 0; //reset handle->state = 0; //reset
handle->event = (uint8_t)NONE_PRESS;
} }
break; break;
} }

@ -9,26 +9,30 @@
#include "stdint.h" #include "stdint.h"
#include "string.h" #include "string.h"
typedef void (*CallBackFunc)(void); typedef void (*BtnCallback)(const struct Button* btn);
typedef enum { typedef enum {
CLICK = 0, PRESS_DOWN = 0,
PRESSED, PRESS_UP,
PRESS_REPEAT,
SINGLE_CLICK,
DOUBLE_CLICK, DOUBLE_CLICK,
LONG_RRESS_START, LONG_RRESS_START,
LONG_PRESS_HOLD, LONG_PRESS_HOLD,
LONG_PRESS_STOP, number_of_event
number_of_event NONE_PRESS
}BtnEvent; }PressEvent;
struct Button { struct Button {
uint16_t ticks; uint16_t ticks;
uint8_t repeat : 4;
uint8_t event : 4;
uint8_t state : 3; uint8_t state : 3;
uint8_t debounce_cnt : 3; uint8_t debounce_cnt : 3;
uint8_t active_level : 1; uint8_t active_level : 1;
uint8_t button_level : 1; uint8_t button_level : 1;
uint8_t (*hal_button_Level)(void); uint8_t (*hal_button_Level)(void);
CallBackFunc cb[number_of_event]; BtnCallback cb[number_of_event];
struct Button* next; struct Button* next;
}; };
@ -37,8 +41,8 @@ extern "C" {
#endif #endif
void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level); 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_attach(struct Button* handle, PressEvent event, CallBackFunc cb);
int button_is_pressed(struct Button* handle); PressEvent get_button_event(const struct Button* handle);
int button_start(struct Button* btn); int button_start(struct Button* btn);
void button_stop(struct Button* btn); void button_stop(struct Button* btn);
void button_ticks(void); void button_ticks(void);

Loading…
Cancel
Save