LCD菜单程序(多层菜单)

2020-01-02 19:38发布

本帖最后由 lindabell 于 2014-11-7 12:29 编辑

最近水帖太多了,发点技术贴
下面代码是实现一个多层菜单的程序,按这个程序框架可以把菜单做的很复杂,而且也很容易管理,具体请看代码

  1. #include "MenuAPP.h"
  2. #include "I2C_eeprom.h"

  3. void mainPageCallBack(u8 key);
  4. void mainPage_Item1_CallBack(u8 key);

  5. extern const struct PAGE Version_Page;
  6. void Version_CallBack(u8 key);

  7. extern const struct PAGE Setting_Page;
  8. void Setting_CallBack(u8 key);

  9. extern const struct PAGE Time_Page;
  10. void Time_CallBack(u8 key);

  11. extern const struct PAGE SMS_Page;
  12. void SMS_CallBack(u8 key);

  13. extern const struct PAGE SMS_Text_Page;
  14. void SMS_Text_CallBack(u8 key);

  15. /******************************************************************************************************/
  16. //主菜单
  17. //定义Item项             //显示方式&序号  项目的名字    项目指向的页(Page)
  18. const struct Item main_item[]={        0x00,        "信息",                        &SMS_Page,
  19.                                                                 0x01,        "设置",                        &Setting_Page,
  20.                                                                 0x02,        "版本",                        &Version_Page,
  21.                                                                 0x03,        "时间",                        &Time_Page,
  22.                                                                 0x04,        "状态",                        0,
  23.                                                                 0x05,        "报警",                        0,
  24.                                                                 0x06,        "飞信",                        0,
  25.                                                                 0x07,        "问答",                        0
  26. };
  27. //定义一个Page                      父页 该页的回调函数        该页的项          项的个数                               
  28. const struct PAGE mainPage={0,mainPageCallBack,main_item,sizeof(main_item)/sizeof(struct Item)};
  29. /*********************************************************************************************************/


  30. const struct PAGE Version_Page={&mainPage,Version_CallBack,0,0};
  31. /***************************************************************************************************************/

  32. //定义Item项              //显示方式&序号    项目的名字      项目指向的页(Page)
  33. const struct Item Setting_item[]={        0x10,        " 00.设0",                        0,
  34.                                                                         0x11,        " 01.设1",                        0,
  35.                                                                         0x12,        " 02.设2",                        0,
  36.                                                                         0x13,        " 03.设3",                        0,
  37.                                                                         0x14,        " 04.设4",                        0,
  38.                                                                         0x15,        " 05.设5",                        0,
  39.                                                                         0x16,        " 06.设6 你好",                0,
  40.                                                                         0x17,        " 07.设7",                        0,
  41.                                                                         0x18,        " 08.设8",                        0,
  42.                                                                         0x19,        " 09.设9",                        0,
  43.                                                                         0x1A,        " 10.设10",                        0
  44.                                                                         };
  45. const struct PAGE Setting_Page={&mainPage,Setting_CallBack,Setting_item,sizeof(Setting_item)/sizeof(struct Item)};
  46. /***************************************************************************************************************/

  47. const struct PAGE Time_Page={&mainPage,Time_CallBack,0,0};

  48. /***************************************************************************************************************/
  49. //定义Item项              //显示方式&序号    项目的名字      项目指向的页(Page)
  50. const struct Item SMS_item[]={       
  51.                                                                         0x10,        " 00.",                        &SMS_Text_Page,
  52.                                                                         0x11,        " 01.",                        &SMS_Text_Page,
  53.                                                                         0x12,        " 02.",                        &SMS_Text_Page,
  54.                                                                         0x13,        " 03.",                        &SMS_Text_Page,
  55.                                                                         0x14,        " 04.",                        &SMS_Text_Page,
  56.                                                                         0x15,        " 05.",                        &SMS_Text_Page,
  57.                                                                         0x16,        " 06.",                        &SMS_Text_Page,
  58.                                                                         0x17,        " 07.",                        &SMS_Text_Page,
  59.                                                                         0x18,        " 08.",                        &SMS_Text_Page,
  60.                                                                         0x19,        " 09.",                        &SMS_Text_Page,
  61.                                                                         0x1A,        " 10.",                        &SMS_Text_Page
  62.                                                                         };

  63. const struct PAGE SMS_Page={&mainPage,SMS_CallBack,SMS_item,sizeof(Setting_item)/sizeof(struct Item)};

  64. //请计算出有多少项
  65. #define THE_NUM_OF_SMS_ITEM  11


  66. #if (THE_NUM_OF_SMS_ITEM>SMS_MAX)
  67. #error "the number  of SMS item must not beyond than SMS EEPROM Item "
  68. #endif
  69. /***************************************************************************************************************/

  70. const struct PAGE SMS_Text_Page={&SMS_Page,SMS_Text_CallBack,0,0};

  71. /**
  72. 主菜单回调函数,对这个页得处理全部放在回调函数里
  73. @param key 按键代码
  74. */
  75. void mainPageCallBack(u8 key)
  76. {
  77.         switch (key)
  78.         {
  79.                 case KEY_UP:       
  80.                 case KEY_Down:       
  81.                 case KEY_Left:       
  82.                 case KEY_Right:
  83.                         KeySelItem(key);
  84.                         break;
  85.                        
  86.                 case KEY_Return:///<主菜单 对返回按键没有处理
  87.                         ShowPage(&mainPage);
  88.                         break;
  89.                 case KEY_Ok:
  90.                         ShowItemPage();
  91.                         break;
  92.         }
  93. }


  94. void Version_CallBack(u8 key)
  95. {
  96.         LCD_Write_Str(0,0,"版本信息");
  97.         LCD_Write_Str(1,0,"厂商:XXXX");
  98.         LCD_Write_Str(2,0,"地址:烟台");
  99.         LCD_Write_Str(3,0,"版本:V0.4");
  100.         if (key==KEY_Return)
  101.         {
  102.                 ShowParentPage();
  103.         }
  104. }


  105. void Setting_CallBack(u8 key)
  106. {
  107.         switch (key)
  108.         {
  109.                 case KEY_UP:       
  110.                 case KEY_Down:       
  111.                 case KEY_Left:       
  112.                 case KEY_Right:
  113.                         KeySelItem(key);
  114.                         break;
  115.                        
  116.                 case KEY_Return:///<主菜单 对返回按键没有处理
  117.                         ShowParentPage();
  118.                         break;
  119.                 case KEY_Ok:
  120.                         ShowItemPage();
  121.                         break;
  122.         }
  123. }


  124. void Time_CallBack(u8 key)
  125. {
  126.         LCD_Write_Str(0,0,"日期: 2012-7-5");
  127.         LCD_Write_Str(1,0,"时间: 16:59");
  128.         if (key==KEY_Return)
  129.         {
  130.                 ShowParentPage();
  131.         }
  132. }

  133. void SMS_CallBack(u8 key)
  134. {
  135.         u8 i,tempData[SMS_TITLE_MAX_LEN];
  136.         u8 max,maxTemp;
  137.         u8 min,minTemp;
  138.         u8 SelIndex,SelIndexTemp;
  139.        
  140.         SelIndexTemp=Menu_GetSelItem();        //获得当前选中的index
  141.         GetShowLst(&minTemp,&maxTemp);                 //获取当前显示的范围
  142.        
  143.         switch (key)
  144.         {
  145.                 case KEY_UP:       
  146.                 case KEY_Down:       
  147.                 case KEY_Left:       
  148.                 case KEY_Right:
  149.                         KeySelItem(key);
  150.                        
  151.                         SelIndex=Menu_GetSelItem();        //获得当前选中的index
  152.                         GetShowLst(&min,&max);                 //获取当前显示的范围
  153.                         if (max==maxTemp) break;//则表示当前显示的列表没有发生变化
  154.                        
  155.                         for (i=min;i<=max;i++)
  156.                         {
  157.                                 //读取SMS信息的title并显示
  158.                                 SMS_Read_Title(i,tempData,SMS_TITLE_MAX_LEN);
  159.                                 LCD_Write_Str(i-min,2,tempData);
  160.                         }
  161.                        
  162.                         break;
  163.                        
  164.                 case KEY_Return:///<主菜单 对返回按键没有处理
  165.                         ShowParentPage();
  166.                         break;
  167.                 case KEY_Ok:
  168.                         ShowItemPage();
  169.                         break;
  170.                 case KEY_Special://第一次进来时显示标题
  171.                         for (i=0;i<4;i++)
  172.                         {
  173.                                 SMS_Read_Title(i,tempData,SMS_TITLE_MAX_LEN);
  174.                                 LCD_Write_Str(i,2,tempData);
  175.                         }
  176.                        
  177.                         break;
  178.         }
  179. }

  180. void SMS_Text_CallBack(u8 key)
  181. {
  182.         u8 SelItemIdex;
  183.         u8 tempData[SMS_TEXT_MAX_LEN];
  184.         u8 len;
  185.         static u8 offset=0;
  186.        
  187.         switch (key)
  188.         {
  189.         case KEY_Special:
  190.                 offset=0;
  191.                 SelItemIdex=Menu_GetSelItem();               
  192.                 SMS_Read_Text(SelItemIdex,offset,tempData,SMS_TEXT_MAX_LEN);  ///<从EEPROM中读出信息文本
  193.                 offset+=LCD_Write_Screen(tempData);
  194.                 break;
  195.         case KEY_Left:
  196.                 if(offset<=64) break;  ///<offset 少于等于64即当前是第一屏,按左键是没有意义的

  197.                 offset=(offset/64-1)*64;
  198.                 SelItemIdex=Menu_GetSelItem();               
  199.                 SMS_Read_Text(SelItemIdex,offset,tempData,SMS_TEXT_MAX_LEN);  ///<从EEPROM中读出信息文本
  200.                 offset+=LCD_Write_Screen(tempData);
  201.                 break;
  202.         case KEY_Right:
  203.                 if (offset%64) break; ///<假如offset不是64的整数说明当前液晶没有显示完,即没有数据了
  204.                 SelItemIdex=Menu_GetSelItem();               
  205.                 SMS_Read_Text(SelItemIdex,offset,tempData,SMS_TEXT_MAX_LEN);  ///<从EEPROM中读出信息文本
  206.                 Lcd_Clr_Scr();
  207.                 offset+=LCD_Write_Screen(tempData);
  208.                 break;
  209.         case KEY_Return:
  210.                 ShowParentPage();
  211.                 return;
  212.                 break;
  213.                        
  214.         }
  215.        
  216.        

  217. }
复制代码

后面添加的:
简单易用菜单框架

发布时间:2014-11-7编辑:80eboy转载请保留出处:http://www.80eboy.com/blog/menu_frame

相信很多攻城狮都用过128x64的液晶,想写好一点的ui好像不太可能或且花费很多时间,直接写吧,感觉好像很零碎,coding都怕了。下面介绍一个简单易用的菜单框架,你会发现它能做多层菜单而且结果清晰。

基本原理:


page3.png (6.19 KB, 下载次数: 0) 下载附件 2014-11-7 12:20 上传

他们是什么关系呢?一个page中有item,那么用结构体就可以实现啦;item下面又有page,那么在item中加一个page的指针指向item对应的page页。前面都是从上到下的,那么怎么返回呢?观察发现返回就是子page返回父page,这样在page结构体中假如一项父page的指针不就ok了。具体实现请看源文件。

源文件下载(点击)
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。