58 lines
1.2 KiB
C
58 lines
1.2 KiB
C
#include "key.h"
|
||
#include "btn.h"
|
||
|
||
#define INVALID_BUTTON_TICKS 200 // 无效按键时间 毫秒
|
||
static uint32_t key_start_ticks = 0;
|
||
/* 按钮 */
|
||
struct Button key_1;
|
||
struct Button key_2;
|
||
struct Button key_3;
|
||
struct Button key_4;
|
||
struct Button key_5;
|
||
struct Button key_6;
|
||
|
||
static BOOL allow_condition(void)
|
||
{
|
||
/**
|
||
* key的初始化在LCD板子上电之前,因为是低电平有效,所以会误判为按键按下
|
||
*/
|
||
if (sys_millis() - key_start_ticks < INVALID_BUTTON_TICKS) // 仿真的时候按键会有毛刺,在xx秒之后按下有效
|
||
{
|
||
return FALSE;
|
||
}
|
||
return TRUE;
|
||
}
|
||
|
||
static uint8_t read_button_gpio(button_id_e button_id)
|
||
{
|
||
if (allow_condition() == FALSE)
|
||
{
|
||
return ACTIVE_LEVEL_HIGH;
|
||
}
|
||
switch (button_id)
|
||
{
|
||
case KEY_1:
|
||
return GPIO_READ(KEY_1_GPIO_Port, KEY_1_Pin);
|
||
|
||
default:
|
||
return ACTIVE_LEVEL_LOW;
|
||
}
|
||
}
|
||
|
||
static void key_1_press_down_handler(void *btn)
|
||
{
|
||
key_1_callback(PRESS_DOWN);
|
||
}
|
||
|
||
void key_init(void)
|
||
{
|
||
key_start_ticks = sys_millis();
|
||
GPIO_SET_INPUT(KEY_1_GPIO_Port, KEY_1_Pin);
|
||
|
||
button_init(&key_1, read_button_gpio, ACTIVE_LEVEL_LOW, KEY_1, KEY_1);
|
||
|
||
button_attach(&key_1, PRESS_DOWN, key_1_press_down_handler);
|
||
|
||
button_start(&key_1);
|
||
}
|