最近费了一些时间,终于会用结构体写一些多级菜单了。
void (*current_operation_index)();
uchar func_index=0;
这是我定义的结构体:
typedef struct
{
uchar current; //当前状态
uchar up; //这是向上翻页
uchar down; //向下翻页
uchar enter; //选中确定
uchar cancel; //后退
void (*current_operation)(); //要执行的函数
}key_table;
接下的各层数字要自己根据所写的函数自己排序,分清逻辑就很好弄的。
key_table code table[10]=
{
{0,1,0,1,0,(*display0)}, //current up down enter cancel
{1,3,2,1,0,(*display1)},
{2,1,3,2,0,(*display2)},
{3,2,4,3,0,(*display3)},
{4,3,5,4,0,(*display4)},
{5,4,1,6,0,(*display5)},
{6,6,6,6,5,(*display6)},
..........
};
主函数里:
keynum=newkeyscan();
switch(keynum)
{
case 0: func_index=table[func_index].enter;lcd_fill(); break; //回车
case 1: func_index=table[func_index].cancel;lcd_fill(); break;//后退
case 2: func_index=table[func_index].down;lcd_fill(); break; //向下翻
case 3: func_index=table[func_index].up;lcd_fill(); break; //向上翻
case 4:
case 5:
case 6:
case 7:
default: break;
}
current_operation_index=table[func_index].current_operation;
(*current_operation_index)(); //执行当前操作函数
好了,有了这些基本的其他的就很好写了。