emWin的Button控件的单击、双击、长按等识别
emWin的button按钮控件只有按下和弹起两种消息,想识别emWin的双击、长按等还不得不另寻他法。突然想到emWin的button和物理机械按钮有什么不同呢?MulitiButton库应该可以用,try to do it.MulitiButton仓库地址:GIthub地址:https://github.com/0x1abin/MultiButton使用起来和物理机械按键一样1、定义butto
·
emWin的button按钮控件只有按下和弹起两种消息,想识别emWin的双击、长按等还不得不另寻他法。
突然想到emWin的button和物理机械按钮有什么不同呢?MulitiButton库应该可以用,try to do it.
MulitiButton仓库地址:GIthub地址:https://github.com/0x1abin/MultiButton
使用起来和物理机械按键一样
1、定义button对象
static struct Button g_PressureUp; //
static struct Button g_PressureDown; //
static struct Button g_FlowUpBtn; //
2、定义电平获取函数
static uint8_t pressureUp = 1;
static uint8_t ReadPressureUpBtn(void)
{
return pressureUp;
}
static uint8_t pressureDown = 1;
static uint8_t ReadPressureDownBtn(void)
{
return pressureDown;
}
static uint8_t flowUp = 1;
static uint8_t ReadFlowUpBtn(void)
{
return flowUp;
}
和物理机械按键不同就在于获取按钮电平不同:
在Button的消息处理,将标志电平变量在WM_NOTIFICATION_CLICKED
消息中清零
在WM_NOTIFICATION_RELEASED
消息中置1
switch (id)
{
case ID_BUTTON_0: // Notifications sent by 'pressure up button'
{
switch (NCode)
{
case WM_NOTIFICATION_CLICKED:
pressureUp = 0;
break;
case WM_NOTIFICATION_RELEASED:
pressureUp = 1;
break;
}
break;
}
case ID_BUTTON_1: // Notifications sent by 'Pressure down button'
{
switch (NCode)
{
case WM_NOTIFICATION_CLICKED:
pressureDown = 0;
break;
case WM_NOTIFICATION_RELEASED:
pressureDown = 1;
break;
}
break;
}
...
...
}
3、初始化、设置回调处理函数以及启动Button
button_init(&g_PressureUp, ReadPressureUpBtn, 0);
button_init(&g_PressureDown, ReadPressureDownBtn, 0);
button_init(&g_FlowUpBtn, ReadFlowUpBtn, 0);
// button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);
button_attach(&g_PressureUp, SINGLE_CLICK, PressureUpBtnSingleClickHandler);
button_attach(&g_PressureUp, DOUBLE_CLICK, PressureUpBtnDoubleClickHandler);
button_attach(&g_PressureDown, SINGLE_CLICK, PressureUpBtnSingleClickHandler);
button_attach(&g_FlowUpBtn, SINGLE_CLICK, PressureUpBtnSingleClickHandler);
button_start(&g_PressureUp);
button_start(&g_PressureDown);
button_start(&g_FlowUpBtn);
4、给MulitButton提供时基
WM_CreateTimer(hWinParent, 0, 5, 0); //创建5ms定时器
switch (pMsg->MsgId)
{
case WM_INIT_DIALOG:
{
InitMainWindow(pMsg->hWin);
break;
}
case WM_PAINT:
{
WindowPaint();
break;
}
case WM_TIMER: //time out msg
{
button_ticks();
WM_RestartTimer(pMsg->Data.v, 5); //重启定时器
break;
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献3条内容
所有评论(0)