独立按键扫描实现单击、双击、长按事件,使用MultiButton来实现
编译器平台及例程说明
编译器平台说明
1. Toolchain : MDK-ARM Community Version: 5.30.0.0(Kile V5)
2. Encoding : Encode in UTF-8 without signature
3. Device Specific Packs : Keil.STM32H7xx_DFP.2.7.0.pack
4. Preprocessor Symbols Define : USE_HAL_DRIVER, STM32H743xx
USE_HAL_DRIVER : 允许使用HAL库驱动
STM32H743xx : STM32H743xx芯片
5. Chip Type : STM32H743VIT6, Flash:2MByte, ROM:1MByte
6. 在stm32h7xx_hal_conf.h文件中HSE_VALUE为外部高速振荡器,默认25MHZ。此值必须与硬件保持一致。
例程说明
1. 独立按键扫描实现单击、双击、长按事件,使用MultiButton来实现
一、宏定义说明
#define XTO_STR(e) (#e)
#define USER_KEY1_PIN USER_KEY1_Pin
#define USER_KEY2_PIN USER_KEY2_Pin
#define Read_Key1_State() HAL_GPIO_ReadPin(USER_KEY1_GPIO_Port, USER_KEY1_PIN)
#define Read_Key2_State() HAL_GPIO_ReadPin(USER_KEY2_GPIO_Port, USER_KEY2_PIN)
二、变量的定义
static struct Button UserButton1 = {0};
static struct Button UserButton2 = {0};
static char * BTN_ID_STR[2] =
{
XTO_STR(BTN1),
XTO_STR(BTN2),
};
三、读取按键状态
static u8 ubRead_UserBut1_State(void)
{
return Read_Key1_State();
}
static u8 ubRead_UserBut2_State(void)
{
return Read_Key2_State();
}
四、按键回调处理
static void vShowButton_Event(u8 id, u8 evt)
{
switch (evt)
{
case SINGLE_CLICK:
dprintf("[%s] Click......\r\n", BTN_ID_STR[id]);
break;
case DOUBLE_CLICK:
dprintf("[%s] Double Click......\r\n", BTN_ID_STR[id]);
break;
case LONG_RRESS_START:
dprintf("[%s] Long Press......\r\n", BTN_ID_STR[id]);
break;
default: break;
}
}
static void vUserButton_CallBack(void * arg)
{
Button * btn = (Button *)arg;
if(btn == &UserButton1)
{
vShowButton_Event(0, btn->event);
}
else if(btn == &UserButton2)
{
vShowButton_Event(1, btn->event);
}
}
五、初始化 MultiButton
static void vInit_Key_Input(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = USER_KEY1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(USER_KEY1_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = USER_KEY2_Pin;
HAL_GPIO_Init(USER_KEY2_GPIO_Port, &GPIO_InitStruct);
dprintf("6-STM32H743xx_CubeMx_KeyInput_MultiButton......\r\n");
}
void vInit_Multi_Button(void)
{
vInit_Key_Input();
button_init(&UserButton1, ubRead_UserBut1_State, 0);
button_init(&UserButton2, ubRead_UserBut2_State, 0);
button_attach(&UserButton1, SINGLE_CLICK, vUserButton_CallBack);
button_attach(&UserButton1, DOUBLE_CLICK, vUserButton_CallBack);
button_attach(&UserButton1, LONG_RRESS_START, vUserButton_CallBack);
button_attach(&UserButton2, SINGLE_CLICK, vUserButton_CallBack);
button_attach(&UserButton2, DOUBLE_CLICK, vUserButton_CallBack);
button_attach(&UserButton2, LONG_RRESS_START, vUserButton_CallBack);
button_start(&UserButton1);
button_start(&UserButton2);
}
六、测试例程
int main(void)
{
static u8 i = 0;
vStartUp_System();
vInit_System_BSP();
vShow_PlatformDevice_Info();
vInit_Multi_Button();
while (1)
{
button_ticks();
HAL_Delay(5);
if((++i) > 100)
{
i = 0;
USER_LED_CPL();
}
}
}
七、运行效果
00> Segger Rtt Init Ok...
00> STM32H743VIT6 Start Running...
00> 6-STM32H743xx_CubeMx_KeyInput_MultiButton......
00> Debug Versions......
00> Compile Date : Mar 29 2022 22:04:20
00>
00> [BTN1] Click......
00> [BTN2] Click......
00> [BTN1] Double Click......
00> [BTN2] Double Click......
00> [BTN1] Long Press......
00> [BTN2] Long Press......
00> [BTN1] Click......
00> [BTN2] Click......
00> [BTN1] Double Click......
00> [BTN2] Double Click......
|