freertos-f103/User/application/key.c

58 lines
1.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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);
}