fix: drop button id func implement

pull/32/head
IceBreak 3 years ago
parent 0610f1533a
commit 71c11b00de

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

@ -1,31 +1,22 @@
#include "multi_button.h"
enum Button_IDs {
btn1_id,
btn2_id,
};
struct Button btn1;
struct Button btn2;
uint8_t read_button_GPIO(uint8_t button_id)
uint8_t read_button1_GPIO()
{
// 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);
case btn2_id:
return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin);
default:
return 0;
}
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
}
uint8_t read_button2_GPIO()
{
return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin);
}
int main()
{
button_init(&btn1, read_button_GPIO, 0, btn1_id);
button_init(&btn2, read_button_GPIO, 0, btn2_id);
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);
@ -63,3 +54,5 @@ void BTN1_PRESS_UP_Handler(void* btn)
{
//do something...
}
...

@ -1,18 +1,10 @@
#include "multi_button.h"
unit8_t btn1_id = 0;
struct Button btn1;
uint8_t read_button_GPIO(uint8_t button_id)
uint8_t read_button1_GPIO()
{
// 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);
default:
return 0;
}
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
}
@ -20,7 +12,7 @@ int main()
{
static uint8_t btn1_event_val;
button_init(&btn1, read_button_GPIO, 0, btn1_id);
button_init(&btn1, read_button1_GPIO, 0);
button_start(&btn1);
//make the timer invoking the button_ticks() interval 5ms.
@ -42,3 +34,4 @@ int main()
}
}
}

@ -15,17 +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, uint8_t button_id)
void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level)
{
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(button_id);
handle->button_level = handle->hal_button_Level();
handle->active_level = active_level;
handle->button_id = button_id;
}
/**
@ -57,7 +55,7 @@ PressEvent get_button_event(struct Button* handle)
*/
void button_handler(struct Button* handle)
{
uint8_t read_gpio_level = handle->hal_button_Level(handle->button_id);
uint8_t read_gpio_level = handle->hal_button_Level();
//ticks counter working..
if((handle->state) > 0) handle->ticks++;
@ -148,9 +146,6 @@ void button_handler(struct Button* handle)
handle->state = 0; //reset
}
break;
default:
handle->state = 0; //reset
break;
}
}
@ -202,3 +197,4 @@ void button_ticks()
button_handler(target);
}
}

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