ucgui界面设计示例2
2020-10-15 来源:eefocus
主机:WIN8
开发环境:MDK4.72
ucgui版本:3.90
mcu: stm32f103VE
说明:
本程序基于ucgui对话框机制设计了一个界面,并用6个实体按键对界面进行控制,并增加了进度条显示
效果图:
源码:gui_match.h
/*********************************************************************
* 对码绑定界面模块头文件
* (c)copyright 2014,jdh
* All Right Reserved
*新建时间:2014/10/24 by jdh
**********************************************************************/
#ifndef _GUI_MATCH_H_
#define _GUI_MATCH_H_
/*********************************************************************
* 头文件
**********************************************************************/
#include 'world.h'
/*********************************************************************
* 宏定义
**********************************************************************/
/*********************************************************************
* 工作间隔
*单位:ms
**********************************************************************/
#define INTERVAL_GUI_MATCH 100
/*********************************************************************
* 进度条显示时间
*单位:ms
**********************************************************************/
#define TIME_PROGBAR_GUI_MATCH 3000
/*********************************************************************
* 函数
**********************************************************************/
/*********************************************************************
* 模块载入
**********************************************************************/
void gui_match_load(void);
/*********************************************************************
* 模块运行
**********************************************************************/
void gui_match_run(void);
/*********************************************************************
* 设置显示状态
*参数:enable:0:关闭显示,1:打开显示
**********************************************************************/
void gui_match_show(uint8_t enable);
/*********************************************************************
* 得到当前的显示状态
*返回:0:关闭显示,1:打开显示
**********************************************************************/
uint8_t gui_match_get_show(void);
/*********************************************************************
* 得到绑定的脚镣id
*返回:0:未绑定,其他:脚镣id
**********************************************************************/
uint16_t gui_match_get_fetter_id_bind(void);
#endif
gui_match.c
/*********************************************************************
* 对码绑定界面模块主文件
* (c)copyright 2014,jdh
* All Right Reserved
*新建时间:2014/10/24 by jdh
*修改时间:2014/10/29 by jdh
**********************************************************************/
/*********************************************************************
* 头文件
**********************************************************************/
#include 'gui_match.h'
#include 'cc1100.h'
#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_ID (GUI_ID_USER + 0x18)
#define ID_TEXT_PROGBAR (GUI_ID_USER + 0x19)
#define ID_LISTBOX (GUI_ID_USER + 0x1A)
#define ID_PROGBAR (GUI_ID_USER + 0x1B)
/*********************************************************************
* 列表框存储的数据数量
**********************************************************************/
#define LIST_LEN 20
/*********************************************************************
* 数据结构
**********************************************************************/
/*********************************************************************
* 按键状态
**********************************************************************/
struct _Key_State
{
uint8_t ok;
uint8_t cancel;
uint8_t up;
uint8_t down;
uint8_t left;
uint8_t right;
};
/*********************************************************************
* 列表框缓存
**********************************************************************/
struct _List
{
uint8_t len;
uint16_t buf[LIST_LEN];
};
/*********************************************************************
* 静态变量
**********************************************************************/
/*********************************************************************
* 界面句柄
**********************************************************************/
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, 'ID', ID_TEXT_ID, 140, 40, 80, 20, 0, 0x0},
{ TEXT_CreateIndirect, '', ID_TEXT_PROGBAR, 140, 65, 80, 20, 0, 0x0},
{ LISTBOX_CreateIndirect, 'Listbox', ID_LISTBOX, 5, 10, 120, 220, 0, 0x0},
{ PROGBAR_CreateIndirect, 'Progbar', ID_PROGBAR, 140, 85, 80, 20, 0, 0x0},
// USER START (Optionally insert additional widgets)
// USER END
};
/*********************************************************************
* 上次工作时间
**********************************************************************/
static struct _Time Time_Last =
{
.s = 0,
.ms = 0,
.us = 0
};
/*********************************************************************
* 进度条开始的时间
**********************************************************************/
static struct _Time Time_Progbar =
{
.s = 0,
.ms = 0,
.us = 0
};
/*********************************************************************
* 接收CC1100的时间
**********************************************************************/
static struct _Time Time_Recv_CC1100 =
{
.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 = 0;
/*********************************************************************
* 进度条动画开始标志
*0:终止,1:扫描,2:绑定
**********************************************************************/
static uint8_t Flag_Progbar = 0;
/*********************************************************************
* 列表框存储器
**********************************************************************/
static struct _List List =
{
.len = 0
};
/*********************************************************************
* 已绑定脚镣ID
*0:未绑定,其他:ID
**********************************************************************/
static uint16_t Fetter_Id_Bind = 0;
/*********************************************************************
* 静态函数
**********************************************************************/
/*********************************************************************
* 回调函数
**********************************************************************/
static void _cbDialog(WM_MESSAGE * pMsg);
/*********************************************************************
* 定时工作
**********************************************************************/
static void slot_tick(void);
/*********************************************************************
* 按键处理
**********************************************************************/
static void deal_key(void);
/*********************************************************************
* 进度条动画
*参数:percent:百分比
**********************************************************************/
static void progbar_show(uint8_t percent);
/*********************************************************************
* 发送125k绑定信号
**********************************************************************/
static void emit_125k_bind(void);
/*********************************************************************
* 处理cc1100接收数据
*参数:task:0:扫描,1:绑定
**********************************************************************/
static void deal_cc1100(uint8_t task);
/*********************************************************************
* 产生激发数据
*参数:cmd:功能码
* data:产生的2字节激发数据
**********************************************************************/
static void generate_exciter_data(uint8_t cmd,uint8_t *data);
/*********************************************************************
* 函数
**********************************************************************/
/*********************************************************************
* 模块载入
**********************************************************************/
void gui_match_load(void)
{
//新建界面
Handle_Gui = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate),
_cbDialog, WM_HBKWIN, 0, 0);
gui_match_show(OFF);
}
/*********************************************************************
* 模块运行
**********************************************************************/
void gui_match_run(void)
{
struct _Time time;
//判断当前是否显示
if (State_Show == 0)
{
return;
}
//判断当前是否处于扫描状态
if (Flag_Progbar == 1)
{
deal_cc1100(0);
}
else
{
//判断当前是否处于绑定状态
if (Flag_Progbar == 2)
{
deal_cc1100(1);
}
}
//得到当前时间
get_time(&time);
//判断是否到工作时间
if (sub_time(time,Time_Last) >= INTERVAL_GUI_MATCH * 1000)
{
//更新时间
Time_Last = time;
//定时工作
slot_tick();
}
}
/*********************************************************************
* 设置显示状态
*参数:enable:0:关闭显示,1:打开显示
**********************************************************************/
void gui_match_show(uint8_t enable)
{
WM_HWIN hItem;
State_Show = enable;
if (enable)
{
//设置列表框为焦点
hItem = WM_GetDialogItem(Handle_Gui, ID_LISTBOX);
WM_SetFocus(hItem);
WM_ShowWindow(Handle_Gui);
}
else
{
WM_HideWindow(Handle_Gui);
}
}
/*********************************************************************
* 得到当前的显示状态
*返回:0:关闭显示,1:打开显示
**********************************************************************/
uint8_t gui_match_get_show(void)
{
return State_Show;
}
/*********************************************************************
* 得到绑定的脚镣id
*返回:0:未绑定,其他:脚镣id
**********************************************************************/
uint16_t gui_match_get_fetter_id_bind(void)
{
//return Fetter_Id_Bind;
return 0x100;
}
/*********************************************************************
* 定时工作
**********************************************************************/
static void slot_tick(void)
{
uint32_t ms = 0;
uint8_t percent = 0;
WM_HWIN hItem;
//按键处理
deal_key();
//判断当前进度条是否需要显示
if (Flag_Progbar)
{
//判断是否是扫描
if (Flag_Progbar == 1)
{
//判断激发信号是否结束
if (inf_exciter_get_busy() == 0)
{
//关闭激发模块电源
inf_exciter_power(OFF);
}
}
//判断是否到工作时间
ms = sub_time(Time_Last,Time_Progbar) / 1000;
if (ms < TIME_PROGBAR_GUI_MATCH)
{
percent = ms * 100 / TIME_PROGBAR_GUI_MATCH;
progbar_show(percent);
}
else
{
//结束进度条动画
Flag_Progbar = 0;
progbar_show(100);
//文本:结束
hItem = WM_GetDialogItem(Handle_Gui, ID_TEXT_PROGBAR);
TEXT_SetFont(hItem, &GUI_FontHZ12);
TEXT_SetText(hItem,'结束');
}
}
}
/*********************************************************************
* 按键处理
**********************************************************************/
static void deal_key(void)
{
WM_HWIN hItem;
uint8_t index = 0;
uint16_t id = 0;
uint8_t data[3] = {0};
//判断下键是否按下
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);
//发送按键信号
GUI_SendKeyMsg(GUI_KEY_DOWN, 1);
}
}
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);
//发送按键信号
GUI_SendKeyMsg(GUI_KEY_UP, 1);
}
}
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) && (Flag_Progbar == 0))
{
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);
//文本:开始扫描
hItem = WM_GetDialogItem(Handle_Gui, ID_TEXT_PROGBAR);
TEXT_SetFont(hItem, &GUI_FontHZ12);
TEXT_SetText(hItem,'开始扫描');
//开始扫描
Flag_Progbar = 1;
//保存当前时间
get_time(&Time_Progbar);
//打开125k电源
inf_exciter_power(ON);
//发送125k绑定信号
emit_125k_bind();
//清空列表框
List.len = 0;
hItem = WM_GetDialogItem(Handle_Gui, ID_LISTBOX);
listbox_clear(hItem);
}
}
else
{
if (Key_State.ok == 1)
{
Key_State.ok = 0;
//按键状态改变
hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_OK);
BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS);
}
}
//判断清除键是否按下
if (inf_key_detect_hold(KEY_LEFT))
{
if (Key_State.left == 0)
{
Key_State.left = 1;
//按键状态改变
hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_LEFT);
BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS | BUTTON_STATE_PRESSED);
//清空列表框
List.len = 0;
hItem = WM_GetDialogItem(Handle_Gui, ID_LISTBOX);
listbox_clear(hItem);
}
}
else
{
if (Key_State.left == 1)
{
Key_State.left = 0;
//按键状态改变
hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_LEFT);
BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS);
}
}
//判断绑定键是否按下
if (inf_key_detect_hold(KEY_RIGHT) && (Flag_Progbar == 0))
{
if (Key_State.right == 0)
{
Key_State.right = 1;
//按键状态改变
hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_RIGHT);
BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS | BUTTON_STATE_PRESSED);
//判断当前是否有项
if (List.len == 0)
{
return;
}
//得到脚镣id
hItem = WM_GetDialogItem(Handle_Gui, ID_LISTBOX);
index = LISTBOX_GetSel(hItem);
if (index >= List.len)
{
//清空列表框
List.len = 0;
hItem = WM_GetDialogItem(Handle_Gui, ID_LISTBOX);
listbox_clear(hItem);
return;
}
id = List.buf[index];
//数据
data[0] = 0;
data[1] = 0;
data[2] = 0;
//等待可以发送
while (cc1100_judge_tx() == 0);
//发送对码绑定帧
cc1100_tx(CMD_RF_BIND,id,data);
//开始绑定
Flag_Progbar = 2;
//保存当前时间
get_time(&Time_Progbar);
//更新绑定的脚镣ID
Fetter_Id_Bind = 0;
//更新文本
hItem = WM_GetDialogItem(Handle_Gui, ID_TEXT_MATCH);
TEXT_SetFont(hItem, &GUI_FontHZ12);
TEXT_SetText(hItem,'未绑定脚镣');
hItem = WM_GetDialogItem(Handle_Gui, ID_TEXT_ID);
TEXT_SetFont(hItem, &GUI_FontHZ12);
TEXT_SetText(hItem,'ID:0');
//更新文本
hItem = WM_GetDialogItem(Handle_Gui, ID_TEXT_PROGBAR);
TEXT_SetFont(hItem, &GUI_FontHZ12);
TEXT_SetText(hItem,'正在绑定');
}
}
else
{
if (Key_State.right == 1)
{
Key_State.right = 0;
//按键状态改变
hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_RIGHT);
BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS);
}
}
//判断返回键是否按下
if (inf_key_detect_hold(KEY_CANCEL) && (Flag_Progbar == 0))
{
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);
}
}
else
{
if (Key_State.cancel == 1)
{
Key_State.cancel = 0;
//按键状态改变
hItem = WM_GetDialogItem(Handle_Gui, ID_BUTTON_CANCEL);
BUTTON_SetState(hItem,BUTTON_STATE_HASFOCUS);
//返回主界面
gui_match_show(OFF);
gui_main_show(ON);
}
}
}
/*********************************************************************
* 进度条动画
*参数:percent:百分比
**********************************************************************/
static void progbar_show(uint8_t percent)
{
WM_HWIN hItem;
//进度条显示
hItem = WM_GetDialogItem(Handle_Gui, ID_PROGBAR);
PROGBAR_SetValue(hItem,percent);
}
/*********************************************************************
* 发送125k绑定信号
**********************************************************************/
static void emit_125k_bind(void)
{
uint8_t data[2] = {0};
//生成激活数据
generate_exciter_data(CMD_125K_BIND,data);
//生成新的激发序列
inf_exciter_generate_frame(data,2);
//等待空闲
while (inf_exciter_get_busy());
//发送
inf_exciter_start();
}
/*********************************************************************
* 处理cc1100接收数据
*参数:task:0:扫描,1:绑定
**********************************************************************/
static void deal_cc1100(uint8_t task)
{
struct _Rx_Buf_CC1100 buf;
WM_HWIN hItem;
char str[20] = {0};
buf = cc1100_get_rx_buf();
//判断是否是最新帧
if (compare_time(buf.time,Time_Recv_CC1100) != 2)
{
return;
}
//保存时间
Time_Recv_CC1100 = buf.time;
if (task == 0)
{
//判断功能码是否是上传脚镣ID帧
if (buf.cmd != CMD_RF_UP_FETTER_ID)
{
return;
}
//判断是否还有存储空间
if (List.len >= LIST_LEN)
{
return;
}
//更新数据
List.buf[List.len++] = buf.src_id;
sprintf(str,'%d',List.buf[List.len - 1]);
//更新列表框
hItem = WM_GetDialogItem(Handle_Gui, ID_LISTBOX);
LISTBOX_AddString(hItem, str);
return;
}
if (task == 1)
{
//判断功能码是否是确认对码绑定
if (buf.cmd != CMD_RF_ACK_BIND)
{
return;
}
//更新绑定的脚镣ID
Fetter_Id_Bind = buf.src_id;
//更新文本
hItem = WM_GetDialogItem(Handle_Gui, ID_TEXT_MATCH);
TEXT_SetFont(hItem, &GUI_FontHZ12);
TEXT_SetText(hItem,'已绑定脚镣');
hItem = WM_GetDialogItem(Handle_Gui, ID_TEXT_ID);
TEXT_SetFont(hItem, &GUI_FontHZ12);
sprintf(str,'ID:0x%x',Fetter_Id_Bind);
TEXT_SetText(hItem,str);
return;
}
}
/*********************************************************************
* 产生激发数据
*参数:cmd:功能码
* data:产生的2字节激发数据
**********************************************************************/
static void generate_exciter_data(uint8_t cmd,uint8_t *data)
{
uint8_t temp = 0;
uint8_t check = 0;
temp = cmd << 6;
temp |= LOCAL_ID >> 4;
data[0] = temp;
temp = LOCAL_ID << 4;
check = checksum4(data,2);
temp |= check;
data[1] = temp;
}
/*********************************************************************
* 回调函数
**********************************************************************/
static void _cbDialog(WM_MESSAGE * pMsg)
{
WM_HWIN hItem;
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);
FRAMEWIN_SetText(pMsg->hWin,'对码绑定');
//初始化列表框
hItem = WM_GetDialogItem(pMsg->hWin, ID_LISTBOX);
// //设置内容
// LISTBOX_SetFont(hItem, &GUI_FontHZ12);
// LISTBOX_AddString(hItem, '0x10');
// LISTBOX_AddString(hItem, '0x11');
// LISTBOX_AddString(hItem, '0x12');
// LISTBOX_AddString(hItem, '0x13');
// LISTBOX_AddString(hItem, '0x14');
// List.buf[0] = 0x10;
// List.buf[1] = 0x11;
// List.buf[2] = 0x12;
// List.buf[3] = 0x13;
// List.buf[4] = 0x14;
// List.len = 5;
//设置为焦点
WM_SetFocus(hItem);
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_ID);
TEXT_SetFont(hItem, &GUI_FontHZ12);
TEXT_SetText(hItem,'ID:0');
hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_PROGBAR);
TEXT_SetFont(hItem, &GUI_FontHZ12);
TEXT_SetText(hItem,'结束');
//初始化进度条
hItem = WM_GetDialogItem(pMsg->hWin, ID_PROGBAR);
PROGBAR_SetBarColor(hItem,0,GUI_RED);
PROGBAR_SetBarColor(hItem,1,GUI_GREEN);
break;
}
default:
{
WM_DefaultProc(pMsg);
break;
}
}
}
下一篇:ucos-ii示例2:信号量测试