add the example

pull/8/head
Jaup 9 years ago
parent c7db5467c0
commit e7fd5bc6cb

@ -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..
}
...
```

@ -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

@ -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;

@ -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);

Loading…
Cancel
Save