之前看网上的架构(感觉思路不错)自己仿照写的一个菜单,在LCD12864上成功运行,分享给大家(只贴出了部分主要代码,用来参考足够了),相互学习,
菜单的头文件
#ifndef MENU_H_
#define MENU_H_
#include "12864.h"
#include "word.h"
typedef struct menu//定义一个菜单
{
uchar range_from,range_to; //当前显示的项开始及结束序号
uchar itemCount;//项目总数
uchar selected;//当前选择项
char *menuItems[5];//菜单项目
char sel_menu;
//struct menu **subMenus;//子菜单
struct menu *subMenus[5];
struct menu *parent;//上级菜单 ,如果是顶级则为null
void (**func)();//选择相应项按确定键后执行的函数
}Menu, *Menu_ptr;
typedef struct param
{
float aver_mon;
float star_mon;
float mile;
float valu_mon;
char *s_rmb;
char *s_kmrmb;
char *s_km;
}Param;
extern Menu *currentMenu;//当前的菜单
extern void display(uchar line); //显示菜单项并设置选中的项反白
extern void func_add(char i);
extern void func_sub(char i);
extern void func_zero(char i);
extern void initMenu();
extern void dis_item(char i);
extern void dis_mon(void);
extern void add_mon(void);
#endif
菜单的.c文件
#include "menu.h"
#include <stdio.h>
#include <string.h>
static Menu MainMenu = { //定义主菜单
0,4,5,0,//默认显示0-3项,总共4项,当前选择第0项
{
"设置起步价",
"设置单价",
"里程显示",
"版本1.0",
"作者",
} ,
0,
};
static Menu SetAverMenu = {
0,0,1,0,//默认显示0-3项,总共4项,当前选择第0项
{
"1.60RMB",
},
1,
};
static Menu SetStarMenu = {
0,0,1,0,//默认显示0-3项,总共4项,当前选择第0项
{
"6.00RMB3km",
},
2,
};
static Menu ShowMenu = {
0,1,2,0,//默认显示0-3项,总共4项,当前选择第0项
{
"012.35km",
"xxx.xRMB"
},
3,
};
//static Menu AuthMenu = {
// 0,0,1,0,//默认显示0-3项,总共4项,当前选择第0项
// {
// "Miss-NING,Mr-ZHU",
// },
// 4,
//};
static Param set_param = {
1.60,
6.00,
1.35,
0.00,
"RMB",
"RMB3km",
"km",
};
Menu *currentMenu;//当前的菜单
static char s_value[14] = {""};
//extern char money[5];
void add_mon(void)
{ //memset(&s_value[10], 0, sizeof(&s_value[10]));
set_param.mile++;
sprintf(s_value, "%5.2f", set_param.mile);
m_strcat(s_value, set_param.s_km);
ShowMenu.menuItems[0] = s_value;
dis_mon();
}
void dis_mon(void)
{ //memset(&s_value[10], 0, sizeof(&s_value[10]));
if(set_param.mile <= 3.1 )
set_param.valu_mon = set_param.star_mon;
else
set_param.valu_mon = set_param.aver_mon * set_param.mile;
sprintf(&s_value[11], "%3.1f", set_param.valu_mon);
//m_strcat(&s_value[10], set_param.s_rmb);
ShowMenu.menuItems[1] = &s_value[11];
}
void dis_item(char i)
{
memset(s_value, 0, sizeof(s_value));
if(i == 1)
{
sprintf(s_value, "%3.2f", set_param.aver_mon);
m_strcat(s_value, set_param.s_rmb);
currentMenu->menuItems[0] = s_value;
}
if(i == 2)
{
sprintf(s_value, "%3.2f", set_param.star_mon);
m_strcat(s_value, set_param.s_kmrmb);
currentMenu->menuItems[0] = s_value;
}
if(i == 3)
{
//set_param.mile -= 0.5;
sprintf(s_value, "%5.2f", set_param.mile);
m_strcat(s_value, set_param.s_km);
currentMenu->menuItems[0] = s_value;
}
}
void func_add(char i)
{
memset(s_value, 0, sizeof(s_value));
if(i == 1)
{
set_param.aver_mon += 0.1;
sprintf(s_value, "%3.2f", set_param.aver_mon);
m_strcat(s_value, set_param.s_rmb);
currentMenu->menuItems[0] = s_value;
}
if(i == 2)
{
set_param.star_mon += 0.5;
sprintf(s_value, "%3.2f", set_param.star_mon);
m_strcat(s_value, set_param.s_kmrmb);
currentMenu->menuItems[0] = s_value;
}
}
void func_sub(char i)
{
memset(s_value, 0, sizeof(s_value));
if(i == 1)
{
set_param.aver_mon -= 0.1;
sprintf(s_value, "%3.2f", set_param.aver_mon);
m_strcat(s_value, set_param.s_rmb);
currentMenu->menuItems[0] = s_value;
}
if(i == 2)
{
set_param.star_mon -= 0.5;
sprintf(s_value, "%3.2f", set_param.star_mon);
m_strcat(s_value, set_param.s_kmrmb);
currentMenu->menuItems[0] = s_value;
}
}
void func_zero(char i)
{
memset(s_value, 0, sizeof(s_value));
if(i == 3)
{
set_param.mile = 0;
sprintf(s_value, "%5.2f", set_param.mile);
m_strcat(s_value, set_param.s_km);
currentMenu->menuItems[0] = s_value;
}
if(set_param.mile <= 3.1 )
set_param.valu_mon = set_param.star_mon;
else
set_param.valu_mon = set_param.aver_mon * set_param.mile;
sprintf(&s_value[11], "%3.1f", set_param.valu_mon);
//m_strcat(&s_value[10], set_param.s_rmb);
ShowMenu.menuItems[1] = &s_value[11];
}
void initMenu()
{
int i;
//MainMenu.subMenus = malloc(sizeof(Menu_ptr)*4);
MainMenu.subMenus[0] = &SetStarMenu;//第1到4项没有子菜单置null,选择后程序会调用func中相应项中的函数
MainMenu.subMenus[1] = &SetAverMenu;
MainMenu.subMenus[2] = &ShowMenu;
MainMenu.subMenus[3] = NULL;
MainMenu.subMenus[4] = NULL;
MainMenu.func = NULL;
MainMenu.parent = NULL;//表示是顶级菜单
//ShowMenu.subMenus = malloc(sizeof(Menu_ptr)*1);
for(i = 0; i < 5; i++)
ShowMenu.subMenus[i] = NULL;//第1到4项没有子菜单置null,选择后程序会调用func中相应项中的函数
ShowMenu.func = NULL;
ShowMenu.parent = &MainMenu;//表示是顶级菜单
//SetAverMenu.subMenus = malloc(sizeof(Menu_ptr)*4);
for(i = 0; i < 5; i++)
SetAverMenu.subMenus[i] = NULL;//第1到4项没有子菜单置null,选择后程序会调用func中相应项中的函数
SetAverMenu.func = NULL;
SetAverMenu.parent = &MainMenu;//表示是顶级菜单
//SetAverMenu.subMenus = malloc(sizeof(Menu_ptr)*4);
for(i = 0; i < 5; i++)
SetStarMenu.subMenus[i] = NULL;//第1到4项没有子菜单置null,选择后程序会调用func中相应项中的函数
SetStarMenu.func = NULL;
SetStarMenu.parent = &MainMenu;//表示是顶级菜单
// for(i = 0; i < 5; i++)
// AuthMenu.subMenus[i] = NULL;//第1到4项没有子菜单置null,选择后程序会调用func中相应项中的函数
// AuthMenu.func = NULL;
// AuthMenu.parent = &MainMenu;//表示是顶级菜单
currentMenu = &MainMenu;
}
void display(uchar line) //显示菜单项并设置选中的项反白
{
int i, offset , k = line - 3;
//line = 3-(currentMenu->range_to-line);
offset = ((k > 0) ? k : 0);
lcdGraphBegin();
for(i = 0;i < ((currentMenu->itemCount > 3) ? 4 : currentMenu->itemCount);i++)
{
Lcd_Disp(i,0,currentMenu->menuItems[i + currentMenu->range_from + offset]);//
}
if(line > 3)
line = 3;
highlight(0,line,8,255);//反白显示指定行
}