代码拉取完成,页面将自动刷新
#include "menu.h"
#include "key.h"
#include "lcd12864ctrl.h"
#include "user_menu.h" //只能被menu.c一个c文件包含之外,不允许被其他任何c文件包含。客户的定制菜单在此添加
#define MENU_MAX 10 //本菜单支持最大层级,最多支持包括首目录的11级目录
//菜单目录
char hierarchy[MENU_MAX+1]={-1,-1};
menu_info menu_now;//当前的菜单信息
uchar window = 0;//显示窗口
//获取当前所在层
menu_str* getNowHierarchy()
{
menu_str* ret = first;
uchar len=0;
while(-1 != hierarchy[len])
{
ret = ret[hierarchy[len]].select.menu;
len++;
}
return ret;
}
//进入下层菜单
//返回值0,表示成功
//返回值1,表示最大目录限制,进入下层菜单失败。
uchar intoMenu(char a)
{
uchar len = 0;
while(-1 != hierarchy[len])
{
len++;
}
if(len < MENU_MAX)
{
hierarchy[len] = a;
hierarchy[len+1] = -1;
return 0;
}
else{
return 1;
}
}
//返回上层菜单
//返回值大于等于0,返回上次的光标位置
//返回值小于0,表示已经在首目录了,无法退回
char outtoMenu()
{
uchar pos;
uchar len = 0;
while(-1 != hierarchy[len])
{
len++;
}
if(0 != len)
{
pos = hierarchy[len-1];
hierarchy[len-1] = -1;//返回上一菜单,可以直接回到原来的地方
return pos;
}else
{
return -1;
}
}
uchar countMenu(menu_str* menulist)
{
uchar i = 0;
while(menulist[i].name)
{
i++;
}
return i;
}
void setNowMenu(char poss)
{
menu_now.menulist = getNowHierarchy();
menu_now.len = countMenu(menu_now.menulist) ; //菜单的长度
menu_now.position = poss; //当前光标位置
window = poss;
}
void setFirstMenu()//初始化执行
{
hierarchy[0] = -1;
setNowMenu(0);
}
/**
* 显示菜单
* menulist菜单结构体 指针
* list从哪开始显示。
*/
void menu_show()
{
uchar i=window;
uchar j=0;
pingword(4);
while(menu_now.menulist[i].name && j<4)
{
chinese_play(menu_now.menulist[i].name,j,1,0);
i++;
j++;
}
chinese_play(POS,menu_now.position-window,0,0);
}
void pos_option(uchar option)
{
switch(option)
{
case '-':
if(menu_now.position != 0)
{
menu_now.position --;
if(window > menu_now.position)
{
window--;
menu_show();
}
else
{
chinese_play(" ",menu_now.position+1-window,0,0);
chinese_play(POS,menu_now.position-window,0,0);
}
}
break;
case '+':
if((menu_now.position+1) < menu_now.len)
{
menu_now.position++;
if(window+3 < menu_now.position)
{
window++;
menu_show();
}
else
{
chinese_play(" ",menu_now.position-1-window,0,0);
chinese_play(POS,menu_now.position-window,0,0);
}
}
break;
}
}
void menuInit()
{
lcd12864Init(); //初始化液晶
setFirstMenu(); //当前目录设置为首目录
menu_show();
}
void menu_updown(uchar key)//上下翻菜单
{
if(K_EVENT_UP == key)
{
pos_option('-');
}
else if(K_EVENT_DOWN == key)
{
pos_option('+');
}
}
void showFunStat(ret_fun ret)
{
pingword(4);
if(ret == OK)
{
chinese_play("执行成功",1,2,0);
}
else
{
chinese_play("执行失败",1,2,0);
}
yanshi2(500);
}
void show_pos_yesorno(uchar sele)
{
if(sele == 0)
{
chinese_play(menu_now.menulist[menu_now.position].name,0,0,0);
chinese_play("是",1,1,0);
chinese_play("否",1,5,0);
}
else if(sele == 1)
{
chinese_play(" ",1,0,0);
chinese_play(" ",1,4,0);
chinese_play(POS,1,0,0);
}
else if(sele == 2)
{
chinese_play(" " ,1,0,0);
chinese_play(" ",1,4,0);
chinese_play(POS,1,4,0);
}
}
uchar funyesorno()
{
uchar sele = 1;//1:是 2:否 3:不执行
uchar buf;
show_pos_yesorno(0);
show_pos_yesorno(sele);
while(1)
{
buf= key_loop();
switch(buf)
{
case K_EVENT_LEFT :sele--;if(sele==0)sele=1;
show_pos_yesorno(sele);
break;
case K_EVENT_RIGHT : sele++;if(sele>2)sele=2;
show_pos_yesorno(sele);
break;
case K_EVENT_SELECT :
RI = 0;
return sele;
break;
case K_EVENT_BACK :
RI = 0;
return 3;
break;
}
}
}
void menu_select()//菜单确认
{
ret_fun ret;
uchar agc;
pingword(4);
switch(menu_now.menulist[menu_now.position].type&0x7f)
{
case SELECT_TWO:
agc = funyesorno();
if(agc!=3)
{
ret = menu_now.menulist[menu_now.position].select.select_fun(agc-1);
showFunStat(ret);
}
break;
case FUNCTION:
ret = menu_now.menulist[menu_now.position].select.fun();
showFunStat(ret);
break;
case MENU:
intoMenu(menu_now.position);
setNowMenu(0);
break;
default:break;
}
menu_show();
}
void menu_back()//菜单返回
{
char ret;
ret = outtoMenu();
if(ret>0)
{
setNowMenu(ret);
}
else
{
setNowMenu(0);
}
menu_show();
}
void menu_keyevent(uchar key)
{
switch(key)
{
case K_NO_EVENT :
return;
break;
case K_EVENT_UP :
case K_EVENT_DOWN :
menu_updown(key);
break;
case K_EVENT_SELECT :
menu_select();
break;
case K_EVENT_BACK :
menu_back();
break;
default:
break;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。