add button_id

pull/22/head
CloudSir 4 years ago
parent 0d6f663972
commit 138e97fe82

@ -12,7 +12,7 @@ struct Button button1;
2.初始化按键对象绑定按键的GPIO电平读取接口**read_button_pin()** ,后一个参数设置有效触发电平
```c
button_init(&button1, read_button_pin, 0);
button_init(&button1, read_button_pin, 0, 0);
```
3.注册按键事件
@ -52,7 +52,8 @@ struct Button {
uint8_t debounce_cnt : 3;
uint8_t active_level : 1;
uint8_t button_level : 1;
uint8_t (*hal_button_Level)(void);
uint8_t button_id;
uint8_t (*hal_button_Level)(uint8_t button_id_);
BtnCallback cb[number_of_event];
struct Button* next;
};
@ -78,11 +79,23 @@ LONG_PRESS_HOLD | 长按期间一直触发
```c
#include "button.h"
unit8_t btn1_id = 0;
struct Button btn1;
uint8_t read_button1_GPIO()
uint8_t read_button_GPIO(uint8_t button_id)
{
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
// you can share the GPIO read function with multiple Buttons
switch(button_id)
{
case btn1_id:
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
break;
default:
return 0;
break;
}
}
void BTN1_PRESS_DOWN_Handler(void* btn)
{
@ -98,7 +111,7 @@ void BTN1_PRESS_UP_Handler(void* btn)
int main()
{
button_init(&btn1, read_button1_GPIO, 0);
button_init(&btn1, read_button_GPIO, 0, btn1_id);
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);

@ -1,22 +1,36 @@
#include "multi_button.h"
enum Button_IDs {
btn1_id,
btn2_id,
};
struct Button btn1;
struct Button btn2;
uint8_t read_button1_GPIO()
uint8_t read_button_GPIO(uint8_t button_id)
{
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
}
// you can share the GPIO read function with multiple Buttons
switch(button_id)
{
case btn1_id:
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
break;
uint8_t read_button2_GPIO()
{
return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin);
case btn2_id:
return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin);
break;
default:
return 0;
break;
}
}
int main()
{
button_init(&btn1, read_button1_GPIO, 0);
button_init(&btn2, read_button2_GPIO, 0);
button_init(&btn1, read_button_GPIO, 0, btn1_id);
button_init(&btn2, read_button_GPIO, 0, btn2_id);
button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler);
button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);
@ -54,5 +68,3 @@ void BTN1_PRESS_UP_Handler(void* btn)
{
//do something...
}
...

@ -1,10 +1,21 @@
#include "multi_button.h"
unit8_t btn1_id = 0;
struct Button btn1;
uint8_t read_button1_GPIO()
uint8_t read_button_GPIO(uint8_t button_id)
{
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
// you can share the GPIO read function with multiple Buttons
switch(button_id)
{
case btn1_id:
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
break;
default:
return 0;
break;
}
}
@ -12,7 +23,7 @@ int main()
{
static uint8_t btn1_event_val;
button_init(&btn1, read_button1_GPIO, 0);
button_init(&btn1, read_button_GPIO, 0, btn1_id);
button_start(&btn1);
//make the timer invoking the button_ticks() interval 5ms.

@ -15,14 +15,15 @@ static struct Button* head_handle = NULL;
* @param handle: the button handle strcut.
* @param pin_level: read the HAL GPIO of the connet button level.
* @param active_level: pressed GPIO level.
* @param button_id: the button id.
* @retval None
*/
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, uint8_t button_id)
{
memset(handle, 0, sizeof(struct Button));
handle->event = (uint8_t)NONE_PRESS;
handle->hal_button_Level = pin_level;
handle->button_level = handle->hal_button_Level();
handle->button_level = handle->hal_button_Level(button_id);
handle->active_level = active_level;
}
@ -55,7 +56,7 @@ PressEvent get_button_event(struct Button* handle)
*/
void button_handler(struct Button* handle)
{
uint8_t read_gpio_level = handle->hal_button_Level();
uint8_t read_gpio_level = handle->hal_button_Level(handle->button_id);
//ticks counter working..
if((handle->state) > 0) handle->ticks++;

@ -38,7 +38,8 @@ typedef struct Button {
uint8_t debounce_cnt : 3;
uint8_t active_level : 1;
uint8_t button_level : 1;
uint8_t (*hal_button_Level)(void);
uint8_t button_id;
uint8_t (*hal_button_Level)(uint8_t button_id_);
BtnCallback cb[number_of_event];
struct Button* next;
}Button;
@ -47,7 +48,7 @@ typedef struct Button {
extern "C" {
#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, uint8_t button_id);
void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
PressEvent get_button_event(struct Button* handle);
int button_start(struct Button* handle);

Loading…
Cancel
Save