161 lines
4.3 KiB
C
161 lines
4.3 KiB
C
#include <bsp_io_dev.h>
|
|
#include <rtthread.h>
|
|
#include "flexible_button.h"
|
|
#include "drv_gpio.h"
|
|
#include "main.h"
|
|
|
|
#define LOG_TAG "bsp_button"
|
|
#define LOG_LVL LOG_LVL_DBG
|
|
#include <ulog.h>
|
|
|
|
#define BUTTON_LOGIC_LEVEL 0
|
|
#define BUTTON_THREAD_PRIORITY 5 // 优先级拉高,这时按键响应快点
|
|
#define BUTTON_THREAD_TICKS 10
|
|
#define BUTTON_THREAD_STACK_SIZE 512 /**< button_thread 线程堆栈大小 */
|
|
static rt_uint8_t button_thread_stack[BUTTON_THREAD_STACK_SIZE]; /**< button_thread 线程堆栈 */
|
|
static struct rt_thread button_0;
|
|
|
|
|
|
|
|
#define ENUM_TO_STR(e) (#e)
|
|
|
|
typedef enum
|
|
{
|
|
USER_BUTTON_0 = 0,
|
|
USER_BUTTON_MAX
|
|
} user_button_t;
|
|
|
|
static char *enum_event_string[] = {
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_DOWN),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_CLICK),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_DOUBLE_CLICK),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_REPEAT_CLICK),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_SHORT_START),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_SHORT_UP),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_LONG_START),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_LONG_UP),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_LONG_HOLD),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_LONG_HOLD_UP),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_MAX),
|
|
ENUM_TO_STR(FLEX_BTN_PRESS_NONE),
|
|
};
|
|
|
|
static char *enum_btn_id_string[] = {
|
|
ENUM_TO_STR(USER_BUTTON_0),
|
|
ENUM_TO_STR(USER_BUTTON_MAX),
|
|
};
|
|
|
|
static flex_button_t user_button[USER_BUTTON_MAX];
|
|
|
|
static uint8_t common_btn_read(void *arg)
|
|
{
|
|
uint8_t value = 0;
|
|
|
|
flex_button_t *btn = (flex_button_t *)arg;
|
|
|
|
switch (btn->id)
|
|
{
|
|
case USER_BUTTON_0:
|
|
{
|
|
return rt_pin_read(BUTTON_0);
|
|
}
|
|
default:
|
|
RT_ASSERT(0);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
/*事件回调*/
|
|
static void common_btn_evt_cb(void *arg)
|
|
{
|
|
flex_button_t *btn = (flex_button_t *)arg;
|
|
|
|
LOG_D("id: [%d - %s] event: [%d - %30s] repeat: %d\n",
|
|
btn->id, enum_btn_id_string[btn->id], btn->event,
|
|
enum_event_string[btn->event], btn->click_cnt);
|
|
|
|
// 也可以使用组合键
|
|
if (flex_button_event_read(&user_button[USER_BUTTON_0]) == FLEX_BTN_PRESS_CLICK) // 单击
|
|
{
|
|
LOG_I("FLEX_BTN_PRESS_CLICK\n");
|
|
Send_Laser_Alarm_Event(kSelfInspectionEvents);
|
|
}
|
|
if (flex_button_event_read(&user_button[USER_BUTTON_0]) == FLEX_BTN_PRESS_DOUBLE_CLICK) // 双击
|
|
{
|
|
LOG_I("FLEX_BTN_PRESS_DOUBLE_CLICK\n");
|
|
|
|
// Send_Laser_Alarm_Event(kFlexBtnPressDoubleClick);
|
|
}
|
|
if (flex_button_event_read(&user_button[USER_BUTTON_0]) == FLEX_BTN_PRESS_SHORT_START) // 短按开始
|
|
{
|
|
|
|
LOG_I("FLEX_BTN_PRESS_SHORT_START\n");
|
|
// Send_Laser_Alarm_Event(kFlexBtnPressShoartStart);
|
|
}
|
|
}
|
|
|
|
static void BSP_Button_Thread_Entry(void *parameter)
|
|
{
|
|
log_i("button_scan_thread\r\n");
|
|
while (1)
|
|
{
|
|
flex_button_scan();
|
|
rt_thread_mdelay(20); // 20 ms
|
|
}
|
|
}
|
|
|
|
static void _Button_Init(void)
|
|
{
|
|
int i;
|
|
|
|
rt_memset(&user_button[0], 0x0, sizeof(user_button));
|
|
|
|
rt_pin_mode(BUTTON_0, PIN_MODE_INPUT_PULLUP); /* set KEY pin mode to input */
|
|
|
|
for (i = 0; i < USER_BUTTON_MAX; i++)
|
|
{
|
|
user_button[i].id = i;
|
|
user_button[i].usr_button_read = common_btn_read;
|
|
user_button[i].cb = common_btn_evt_cb;
|
|
user_button[i].pressed_logic_level = BUTTON_LOGIC_LEVEL;
|
|
user_button[i].short_press_start_tick = FLEX_MS_TO_SCAN_CNT(1500);
|
|
user_button[i].long_press_start_tick = FLEX_MS_TO_SCAN_CNT(3000);
|
|
user_button[i].long_hold_start_tick = FLEX_MS_TO_SCAN_CNT(4500);
|
|
|
|
flex_button_register(&user_button[i]);
|
|
}
|
|
} /**< button_thread 线程控制块 */
|
|
|
|
int Flex_Button_Thread(void)
|
|
{
|
|
|
|
LOG_I("button_thread start");
|
|
rt_err_t ret = RT_ERROR;
|
|
_Button_Init();
|
|
|
|
ret = rt_thread_init(&button_0,
|
|
"button_0",
|
|
BSP_Button_Thread_Entry,
|
|
RT_NULL,
|
|
&button_thread_stack[0],
|
|
sizeof(button_thread_stack),
|
|
BUTTON_THREAD_PRIORITY,
|
|
BUTTON_THREAD_TICKS);
|
|
|
|
if (ret == RT_EOK)
|
|
{
|
|
LOG_D("button thread init success");
|
|
rt_thread_startup(&button_0);
|
|
return RT_EOK;
|
|
}
|
|
else
|
|
{
|
|
LOG_E("button_thread create failed...\r\n");
|
|
return RT_ERROR;
|
|
}
|
|
}
|
|
INIT_DEVICE_EXPORT(Flex_Button_Thread);
|
|
|
|
|