ucgui界面设计&实体按键驱动
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
环境:
主机:WIN8
开发环境:MDK4.72
ucgui版本:3.90
mcu: stm32f103VE
说明:
本程序基于ucgui对话框机制设计了一个界面,并用6个实体按键对界面进行控制
效果图:
源码:
gui_main.h
/********************************************************************* * 主界面模块头文件 * (c)copyright 2014,jdh * All Right Reserved *新建时间:2014/10/23 by jdh **********************************************************************/ #ifndef _GUI_MAIN_H_ #define _GUI_MAIN_H_ /********************************************************************* * 头文件 **********************************************************************/ #include "world.h" /********************************************************************* * 宏定义 **********************************************************************/ /********************************************************************* * 工作间隔 *单位:ms **********************************************************************/ #define INTERVAL_GUI_MAIN 100 /********************************************************************* * 函数 **********************************************************************/ /********************************************************************* * 模块载入 **********************************************************************/ void gui_main_load(void); /********************************************************************* * 模块运行 **********************************************************************/ void gui_main_run(void); /********************************************************************* * 设置显示状态 *参数:enable:0:关闭显示,1:打开显示 **********************************************************************/ void gui_main_show(uint8_t enable); /********************************************************************* * 得到当前的显示状态 *返回:0:关闭显示,1:打开显示 **********************************************************************/ uint8_t gui_main_get_show(void); #endif
gui_main.c
/********************************************************************* * 主界面模块主文件 * (c)copyright 2014,jdh * All Right Reserved *新建时间:2014/10/23 by jdh **********************************************************************/ /********************************************************************* * 头文件 **********************************************************************/ #include "gui_main.h" /********************************************************************* * 宏定义 **********************************************************************/ #define ID_FRAMEWIN (GUI_ID_USER + 0x10) #define ID_BUTTON_OK (GUI_ID_USER + 0x11) #define ID_BUTTON_CANCEL (GUI_ID_USER + 0x12) #define ID_BUTTON_UP (GUI_ID_USER + 0x13) #define ID_BUTTON_DOWN (GUI_ID_USER + 0x14) #define ID_BUTTON_LEFT (GUI_ID_USER + 0x15) #define ID_BUTTON_RIGHT (GUI_ID_USER + 0x16) #define ID_TEXT_MATCH (GUI_ID_USER + 0x17) #define ID_TEXT_RSSI (GUI_ID_USER + 0x18) #define ID_LISTBOX_FUNC (GUI_ID_USER + 0x19) /********************************************************************* * 数据结构 **********************************************************************/ /********************************************************************* * 按键状态 **********************************************************************/ struct _Key_State { uint8_t ok; uint8_t cancel; uint8_t up; uint8_t down; uint8_t left; uint8_t right; }; /********************************************************************* * 静态变量 **********************************************************************/ /********************************************************************* * 界面句柄 **********************************************************************/ static WM_HWIN Handle_Gui; /********************************************************************* * 资源表 **********************************************************************/ static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = { { FRAMEWIN_CreateIndirect, "Frame3", ID_FRAMEWIN, 0, 0, 240, 320, 0, 0x0}, { BUTTON_CreateIndirect, "OK", ID_BUTTON_OK, 5, 255, 40, 20, 0, 0x0}, { BUTTON_CreateIndirect, "Button", ID_BUTTON_DOWN, 95, 280, 40, 20, 0, 0x0}, { BUTTON_CreateIndirect, "UP", ID_BUTTON_UP, 95, 255, 40, 20, 0, 0x0}, { BUTTON_CreateIndirect, "CANCEL", ID_BUTTON_CANCEL, 185, 255, 40, 20, 0, 0x0}, { BUTTON_CreateIndirect, "RIGHT", ID_BUTTON_RIGHT, 140, 270, 40, 20, 0, 0x0}, { BUTTON_CreateIndirect, "LEFT", ID_BUTTON_LEFT, 50, 270, 40, 20, 0, 0x0}, { TEXT_CreateIndirect, "Match", ID_TEXT_MATCH, 140, 15, 80, 20, 0, 0x0}, { TEXT_CreateIndirect, "RSSI", ID_TEXT_RSSI, 140, 40, 80, 20, 0, 0x0}, { LISTBOX_CreateIndirect, "Listbox", ID_LISTBOX_FUNC, 5, 10, 120, 220, 0, 0x0}, // USER START (Optionally insert additional widgets) // USER END }; /********************************************************************* * 上次工作时间 **********************************************************************/ static struct _Time Time_Last = { .s = 0, .ms = 0, .us = 0 }; /********************************************************************* * 按键状态 **********************************************************************/ static struct _Key_State Key_State = { .ok = 0, .cancel = 0, .up = 0, .down = 0, .left = 0, .right = 0 }; /********************************************************************* * 当前显示状态 *0:未显示,1显示 **********************************************************************/ static uint8_t State_Show = 1; /********************************************************************* * 静态函数 **********************************************************************/ /********************************************************************* * 回调函数 **********************************************************************/ static void _cbDialog(WM_MESSAGE * pMsg); /********************************************************************* * 定时工作 **********************************************************************/ static void slot_tick(void); /********************************************************************* * 按键处理 **********************************************************************/ static void deal_key(void); /********************************************************************* * 函数 **********************************************************************/ /********************************************************************* * 模块载入 **********************************************************************/ void gui_main_load(void) { //新建界面 Handle_Gui = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0); } /********************************************************************* * 模块运行 **********************************************************************/ void gui_main_run(void) { struct _Time time; //判断当前是否显示 if (State_Show == 0) { return; } //得到当前时间 get_time(&time); //判断是否到工作时间 if (sub_time(time,Time_Last) >= INTERVAL_GUI_MAIN * 1000) { //更新时间 Time_Last = time; //定时工作 slot_tick(); } } /********************************************************************* * 设置显示状态 *参数:enable:0:关闭显示,1:打开显示 **********************************************************************/ void gui_main_show(uint8_t enable) { State_Show = enable; if (enable) { WM_ShowWindow(Handle_Gui); } else { WM_HideWindow(Handle_Gui); } } /********************************************************************* * 得到当前的显示状态 *返回:0:关闭显示,1:打开显示 **********************************************************************/ uint8_t gui_main_get_show(void) { return State_Show; } /********************************************************************* * 定时工作 **********************************************************************/ static void slot_tick(void) { //按键处理 deal_key(); } /********************************************************************* * 按键处理 **********************************************************************/ static void deal_key(void) { WM_HWIN hItem; //按键扫描 //inf_key_scan(); //判断关机键是否按下 if (inf_key_detect_hold(KEY_CANCEL)) { if (Key_State.cancel == 0) { Key_State.cancel = 1; //按下状态 hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_CANCEL); BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS | BUTTON_STATE_PRESSED); //关机 inf_power_set(OFF); } } else { if (Key_State.cancel == 1) { Key_State.cancel = 0; //初始化按键 hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_CANCEL); BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS); //开机 inf_power_set(ON); } } //判断下键是否按下 if (inf_key_detect_hold(KEY_DOWN)) { if (Key_State.down == 0) { Key_State.down = 1; //初始化按键 hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_DOWN); BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS | BUTTON_STATE_PRESSED); //发送按键信号 WM_NotifyParent(hItem,WM_NOTIFICATION_CLICKED); } } else { if (Key_State.down == 1) { Key_State.down = 0; //初始化按键 hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_DOWN); BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS); } } //判断上键是否按下 if (inf_key_detect_hold(KEY_UP)) { if (Key_State.up == 0) { Key_State.up = 1; //初始化按键 hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_UP); BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS | BUTTON_STATE_PRESSED); //发送按键信号 WM_NotifyParent(hItem,WM_NOTIFICATION_CLICKED); } } else { if (Key_State.up == 1) { Key_State.up = 0; //初始化按键 hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_UP); BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS); } } //判断测试键是否按下 if (inf_key_detect_hold(KEY_OK)) { if (Key_State.ok == 0) { Key_State.ok = 1; //初始化按键 hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_OK); BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS | BUTTON_STATE_PRESSED); //发送按键信号 WM_NotifyParent(hItem,WM_NOTIFICATION_CLICKED); } } else { if (Key_State.ok == 1) { Key_State.ok = 0; //初始化按键 hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_OK); BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS); } } } /********************************************************************* * 回调函数 **********************************************************************/ static void _cbDialog(WM_MESSAGE * pMsg) { WM_HWIN hItem; int NCode; int Id; // USER START (Optionally insert additional variables) // USER END char str[20] = {0}; switch (pMsg->MsgId) { case WM_INIT_DIALOG: { //初始化窗体 //增加小按键 FRAMEWIN_AddCloseButton(pMsg->hWin, FRAMEWIN_BUTTON_RIGHT, 0); FRAMEWIN_AddMaxButton(pMsg->hWin, FRAMEWIN_BUTTON_RIGHT, 1); FRAMEWIN_AddMinButton(pMsg->hWin, FRAMEWIN_BUTTON_RIGHT, 2); FRAMEWIN_SetFont(pMsg->hWin, &GUI_FontHZ12); sprintf(str,"脚镣测试系统Ver%d by jdh",VERSION_SOFT); FRAMEWIN_SetText(pMsg->hWin,str); //初始化列表框 hItem = WM_GetDialogItem(pMsg->hWin, ID_LISTBOX_FUNC); //设置为焦点 WM_SetFocus(hItem); //设置内容 LISTBOX_SetFont(hItem, &GUI_FontHZ12); LISTBOX_AddString(hItem, "对码绑定"); LISTBOX_AddString(hItem, "频点选择"); LISTBOX_AddString(hItem, "通话测试"); LISTBOX_AddString(hItem, "GPRS测试"); LISTBOX_AddString(hItem, "GPS测试"); LISTBOX_AddString(hItem, "125K测试"); SCROLLBAR_CreateAttached(hItem, SCROLLBAR_CF_VERTICAL); //初始化按键 hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_OK); BUTTON_SetFont(hItem, &GUI_FontHZ12); BUTTON_SetText(hItem,"测试"); hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_CANCEL); BUTTON_SetFont(hItem, &GUI_FontHZ12); BUTTON_SetText(hItem,"关机"); hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_UP); BUTTON_SetFont(hItem, &GUI_FontHZ12); BUTTON_SetText(hItem,"上"); hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_DOWN); BUTTON_SetFont(hItem, &GUI_FontHZ12); BUTTON_SetText(hItem,"下"); hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_LEFT); BUTTON_SetFont(hItem, &GUI_FontHZ12); BUTTON_SetText(hItem,""); hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_RIGHT); BUTTON_SetFont(hItem, &GUI_FontHZ12); BUTTON_SetText(hItem,""); //初始化文本 hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_MATCH); TEXT_SetFont(hItem, &GUI_FontHZ12); TEXT_SetText(hItem,"未绑定脚镣"); hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_RSSI); TEXT_SetFont(hItem, &GUI_FontHZ12); TEXT_SetText(hItem,""); break; } case WM_NOTIFY_PARENT: { Id = WM_GetId(pMsg->hWinSrc); NCode = pMsg->Data.v; switch(Id) { case ID_BUTTON_OK: { switch(NCode) { case WM_NOTIFICATION_CLICKED: { //获得当前列表数目 hItem = WM_GetDialogItem(pMsg->hWin, ID_LISTBOX_FUNC); int a = LISTBOX_GetSel(hItem); sprintf(str,"%d",a); hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_RSSI); TEXT_SetText(hItem,str); hItem = WM_GetDialogItem(pMsg->hWin, ID_LISTBOX_FUNC); LISTBOX_GetItemText(hItem,a,str,20); hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_MATCH); TEXT_SetText(hItem,str); break; } case WM_NOTIFICATION_RELEASED: { break; } } break; } case ID_BUTTON_DOWN: { switch(NCode) { case WM_NOTIFICATION_CLICKED: { //发送按键信号 GUI_SendKeyMsg(GUI_KEY_DOWN, 1); break; } case WM_NOTIFICATION_RELEASED: { break; } } break; } case ID_BUTTON_UP: { switch(NCode) { case WM_NOTIFICATION_CLICKED: { //发送按键信号 GUI_SendKeyMsg(GUI_KEY_UP, 1); break; } case WM_NOTIFICATION_RELEASED: { break; } } break; } case ID_BUTTON_CANCEL: { switch(NCode) { case WM_NOTIFICATION_CLICKED: { break; } case WM_NOTIFICATION_RELEASED: { break; } } break; } case ID_BUTTON_RIGHT: { switch(NCode) { case WM_NOTIFICATION_CLICKED: { break; } case WM_NOTIFICATION_RELEASED: { break; } } break; } case ID_BUTTON_LEFT: { switch(NCode) { case WM_NOTIFICATION_CLICKED: { break; } case WM_NOTIFICATION_RELEASED: { break; } } break; } case ID_LISTBOX_FUNC: { switch(NCode) { case WM_NOTIFICATION_CLICKED: { break; } case WM_NOTIFICATION_RELEASED: { break; } } break; } } break; } default: { WM_DefaultProc(pMsg); break; } } }