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

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

@ -1,37 +1,29 @@
#include "multi_button.h" #include "multi_button.h"
unit8_t btn1_id = 0;
struct Button btn1; 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 return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
switch(button_id)
{
case btn1_id:
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
default:
return 0;
}
} }
int main() int main()
{ {
static uint8_t btn1_event_val; 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); button_start(&btn1);
//make the timer invoking the button_ticks() interval 5ms. //make the timer invoking the button_ticks() interval 5ms.
//This function is implemented by yourself. //This function is implemented by yourself.
__timer_start(button_ticks, 0, 5); __timer_start(button_ticks, 0, 5);
while(1) while(1)
{ {
if(btn1_event_val != get_button_event(&btn1)) { if(btn1_event_val != get_button_event(&btn1)) {
btn1_event_val = get_button_event(&btn1); btn1_event_val = get_button_event(&btn1);
if(btn1_event_val == PRESS_DOWN) { if(btn1_event_val == PRESS_DOWN) {
//do something //do something
} else if(btn1_event_val == PRESS_UP) { } else if(btn1_event_val == PRESS_UP) {
@ -42,3 +34,4 @@ int main()
} }
} }
} }

@ -15,17 +15,15 @@ static struct Button* head_handle = NULL;
* @param handle: the button handle strcut. * @param handle: the button handle strcut.
* @param pin_level: read the HAL GPIO of the connet button level. * @param pin_level: read the HAL GPIO of the connet button level.
* @param active_level: pressed GPIO level. * @param active_level: pressed GPIO level.
* @param button_id: the button id.
* @retval None * @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)); memset(handle, 0, sizeof(struct Button));
handle->event = (uint8_t)NONE_PRESS; handle->event = (uint8_t)NONE_PRESS;
handle->hal_button_Level = pin_level; 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->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) 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.. //ticks counter working..
if((handle->state) > 0) handle->ticks++; if((handle->state) > 0) handle->ticks++;
@ -148,9 +146,6 @@ void button_handler(struct Button* handle)
handle->state = 0; //reset handle->state = 0; //reset
} }
break; break;
default:
handle->state = 0; //reset
break;
} }
} }
@ -202,3 +197,4 @@ void button_ticks()
button_handler(target); button_handler(target);
} }
} }

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

Loading…
Cancel
Save