//首先贴出使用到的结构体 menu.h
#ifndef _MENU_H
#define _MENU_H
//菜单索引ID
typedef enum {
id_main,
id_set,
id_test,
id_set_date,
id_total,
id_invoid = 0xff,
}MENU_E;
//菜单子项结构体
typedef struct MenuItem
{
char *displayStr;
MENU_E childrenMenuId;
}MenuItem_T;
//一个菜单页结构体
typedef struct MenuFrame
{
MENU_E ownMenuId;
int maxIndex;
MENU_E parentMenuId;
MenuItem_T* pMenuItems;
}MenuFrame_T;
//菜单控制结构体
typedef struct MenuContorl
{
short currtIndex;
MenuFrame_T* pCurrtMenu;
void (*handle)(struct MenuContorl* currt);
}MenuContorl_T;
#endif
//下面是具体的测试使用部分 menu.c
#define ITERMS_LENGHT(a) {sizeof(a)/sizeof(a[0])}
MenuItem_T setDateMenusItem[] = {
{"set year",id_invoid},
{"set mon",id_invoid},
{"set day", id_invoid},
{"set hour", id_invoid},
{"set min", id_invoid},
{"set sec", id_invoid},
};
MenuItem_T setMenusItem[] = {
{"set1", id_invoid },
{"set2", id_set_date }
};
MenuItem_T mainMenuItem[] = {
{"SET", id_set },
{"TEST", id_test },
{"EXE", id_invoid }
};
MenuItem_T testMenusItem[] = {
{"test1", id_invoid},
{"test2", id_invoid}
};
void menuHandle(MenuContorl_T* currt)
{
printf("menuHandle: menu id:%d item:%d
",currt->pCurrtMenu->ownMenuId,currt->currtIndex);
}
MenuFrame_T sysMenu[id_total] = {
{id_main,ITERMS_LENGHT(mainMenuItem), id_invoid, mainMenuItem },
{id_set,ITERMS_LENGHT(setMenusItem), id_main, setMenusItem },
{id_test,ITERMS_LENGHT(testMenusItem), id_main, testMenusItem },
{id_set_date,ITERMS_LENGHT(setDateMenusItem),id_set, setDateMenusItem }
};
MenuContorl_T menuControl = {
0,
sysMenu,
menuHandle
};
void menu_Frame_init(MenuContorl_T* pMenu)
{
int i;
int len = pMenu->pCurrtMenu->maxIndex;
printf("***********menu**********
");
for(i=0;i<len;i++)
{
printf("%d.%s
",i+1,pMenu->pCurrtMenu->pMenuItems[i].displayStr);
}
pMenu->currtIndex = 0;
}
//测试部分,我这边是使用串口接收字符控制
if(ch == '+') //菜单下移
{
if(menuControl.currtIndex < menuControl.pCurrtMenu->maxIndex)
menuControl.currtIndex++;
if(menuControl.currtIndex == menuControl.pCurrtMenu->maxIndex)
menuControl.currtIndex = 0;
printf("***currt:%s***
",menuControl.pCurrtMenu->pMenuItems[menuControl.currtIndex].displayStr);
}
else if (ch == '>') //菜单进入子项
{
if(id_invoid != menuControl.pCurrtMenu->pMenuItems[menuControl.currtIndex].childrenMenuId)
{
menuControl.pCurrtMenu = &sysMenu[menuControl.pCurrtMenu->pMenuItems[menuControl.currtIndex].childrenMenuId];
menu_Frame_init(&menuControl);
}
}
else if (ch == '<') //菜单返回上一级
{
if(id_invoid != menuControl.pCurrtMenu->parentMenuId)
{
menuControl.pCurrtMenu = &sysMenu[menuControl.pCurrtMenu->parentMenuId];
menu_Frame_init(&menuControl);
}
}
else if (ch == '=') //菜单对应项处理函数
{
menuControl.handle(&menuControl);
}
一周热门 更多>