智能车学习(八)——菜单的实现
一、代码分享
1、头文件
#ifndef __MENU_H #define __MENU_H /***********宏定义************/ //页面声明 typedef enum Menu_Page_Enum { MainMenu = 0, SubMenu1 = 1, SubMenu2 = 2, SubMenu3 = 3, SubMenu4 = 4, SubMenu5 = 5, SubMenu6 = 6 } Menu_Page_Enum; //页面位置声明 #define OLED_TOP 0 #define OLED_BUTTOM 7 #define OLED_LEFT 0 #define OLED_RIGHT 127 #define OLED_Line_MIDDLE 63 //行中间 /*****************************/ /***********函数声明***********/ //刷新 void Menu_Refresh(void); void MainMenu_Refresh(void); void SubMenu1_Refresh(void); void SubMenu2_Refresh(void); void SubMenu3_Refresh(void); void SubMenu4_Refresh(void); void SubMenu5_Refresh(void); void SubMenu6_Refresh(void); void MenuPointer_Refresh(int Menu_pointer); //按键功能 void Menu_Key_Detect(void); void Keynum2_function(void); void Keynum10_function(void); void Keynum5_function(void); void Keynum7_function(void); /*****************************/ #endif
2、源文件
#include "include.h" #include "common.h" #include "OLED.h" #include "gpio.h" #include "Menu.h" #include "lptmr.h" #include "KeySquare.h" /***********全局变量定义************/ //当前页面 int Menu_Page = MainMenu; //页面光标 int MainMenuPointer = 1; int SubMenuPointer1 = 1; int SubMenuPointer2 = 1; int SubMenuPointer3 = 1; int SubMenuPointer4 = 1; int SubMenuPointer5 = 1; int SubMenuPointer6 = 1; /**********************************/ //============================================================================ //函数名称:void Menu_Key_Detect(void) //函数返回:无 //参数说明: 五 //功能概要:按键检测 //============================================================================ void Menu_Key_Detect(void) { switch(keyBoard_get()) { case Keynum1: break; case Keynum2: Keynum2_function(); break; case Keynum3: break; case Keynum4: break; case Keynum5: Keynum5_function(); break; case Keynum6: break; case Keynum7: Keynum7_function(); break; case Keynum8: break; case Keynum9: break; case Keynum10: Keynum10_function(); break; case Keynum11: break; case Keynum12: break; default: break; } } //============================================================================ //函数名称:void Keynum5_function(void) //函数返回:无 //参数说明:无 //功能概要:按键5对应的功能 //============================================================================ void Keynum5_function(void) { Menu_Page = MainMenu; Menu_Refresh(); lptmr_delay_ms(200); } //============================================================================ //函数名称:void Keynum7_function(void) //函数返回:无 //参数说明:无 //功能概要:按键7对应的功能 //============================================================================ void Keynum7_function(void) { switch(MainMenuPointer) { case 1: Menu_Page = SubMenu1; break; case 2: Menu_Page = SubMenu2; break; case 3: Menu_Page = SubMenu3; break; case 4: Menu_Page = SubMenu4; break; case 5: Menu_Page = SubMenu5; break; case 6: Menu_Page = SubMenu6; break; default: break; } Menu_Refresh(); lptmr_delay_ms(200); } //============================================================================ //函数名称:void Keynum10_function(void) //函数返回:无 //参数说明:无 //功能概要:按键10对应的功能 //============================================================================ void Keynum10_function(void) { switch(Menu_Page) { case MainMenu: { MainMenuPointer++; if(MainMenuPointer > 6) MainMenuPointer = 1; break; } case SubMenu1: { SubMenuPointer1 ++; if(SubMenuPointer1 > 3) SubMenuPointer1 = 1; break; } case SubMenu2: { SubMenuPointer2 ++; if(SubMenuPointer2 > 3) SubMenuPointer2 = 1; break; } case SubMenu3: { SubMenuPointer3 ++; if(SubMenuPointer3 > 3) SubMenuPointer3 = 1; break; } case SubMenu4: { SubMenuPointer4 ++; if(SubMenuPointer4 > 3) SubMenuPointer4 = 1; break; } case SubMenu5: { SubMenuPointer5 ++; if(SubMenuPointer5 > 3) SubMenuPointer5 = 1; break; } case SubMenu6: { SubMenuPointer6 ++; if(SubMenuPointer6 > 3) SubMenuPointer6 = 1; break; } default: break; } Menu_Refresh(); lptmr_delay_ms(200); } //============================================================================ //函数名称:void Keynum2_function(void) //函数返回:无 //参数说明:无 //功能概要:按键2对应的功能 //============================================================================ void Keynum2_function(void) { switch(Menu_Page) { case MainMenu: { MainMenuPointer--; if(MainMenuPointer < 1) MainMenuPointer = 6; break; } case SubMenu1: { SubMenuPointer1--; if(SubMenuPointer1 < 1) SubMenuPointer1 = 3; break; } case SubMenu2: { SubMenuPointer2 --; if(SubMenuPointer2 < 1) SubMenuPointer2 = 3; break; } case SubMenu3: { SubMenuPointer3 --; if(SubMenuPointer3 < 1) SubMenuPointer3 = 3; break; } case SubMenu4: { SubMenuPointer4 --; if(SubMenuPointer4 < 1) SubMenuPointer4 = 3; break; } case SubMenu5: { SubMenuPointer5 --; if(SubMenuPointer5 < 1) SubMenuPointer5 = 3; break; } case SubMenu6: { SubMenuPointer6 --; if(SubMenuPointer6 < 1) SubMenuPointer6 = 3; break; } default: break; } Menu_Refresh(); lptmr_delay_ms(200); } //============================================================================ //函数名称:void MenuPointer_Refresh(int Menu_pointer) //函数返回:无 //参数说明:MPage 刷新对应页面 //功能概要:光标刷新函数 //============================================================================ void MenuPointer_Refresh(int Menu_pointer) { switch(Menu_pointer) { case 1: OLED_Write_String(2,OLED_LEFT,"@"); break; case 2: OLED_Write_String(4,OLED_LEFT,"@"); break; case 3: OLED_Write_String(6,OLED_LEFT,"@"); break; case 4: OLED_Write_String(2,OLED_Line_MIDDLE,"@"); break; case 5: OLED_Write_String(4,OLED_Line_MIDDLE,"@"); break; case 6: OLED_Write_String(6,OLED_Line_MIDDLE,"@"); break; default: break; } } //============================================================================ //函数名称:void Menu_Refresh(void) //函数返回:无 //参数说明:MPage 刷新对应页面 //功能概要:页面刷新函数 //============================================================================ void Menu_Refresh(void) { OLED_Clear(); switch(Menu_Page) { case MainMenu: MainMenu_Refresh(); break; case SubMenu1: SubMenu1_Refresh(); break; case SubMenu2: SubMenu2_Refresh(); break; case SubMenu3: SubMenu3_Refresh(); break; case SubMenu4: SubMenu4_Refresh(); break; case SubMenu5: SubMenu5_Refresh(); break; case SubMenu6: SubMenu6_Refresh(); break; default: break; } } //============================================================================ //函数名称:void MainMenu_Refresh(void) //函数返回:无 //参数说明:无 //功能概要:主页面刷新函数 //============================================================================ void MainMenu_Refresh(void) { OLED_Write_String(0,OLED_Line_MIDDLE-35,"MainMenu"); OLED_Write_String(2,10,"Menu1"); OLED_Write_String(4,10,"Menu2"); OLED_Write_String(6,10,"Menu3"); OLED_Write_String(2,OLED_Line_MIDDLE + 8,"Menu4"); OLED_Write_String(4,OLED_Line_MIDDLE + 8,"Menu5"); OLED_Write_String(6,OLED_Line_MIDDLE + 8,"Menu6"); MenuPointer_Refresh(MainMenuPointer); } //============================================================================ //函数名称:void SubMenu1_Refresh(void) //函数返回:无 //参数说明:无 //功能概要:子页面1刷新函数 //============================================================================ void SubMenu1_Refresh(void) { OLED_Write_String(2,10,"Menu1"); OLED_Write_String(4,10,"Menu1"); OLED_Write_String(6,10,"Menu1"); MenuPointer_Refresh(SubMenuPointer1); } //============================================================================ //函数名称:void SubMenu2_Refresh(void) //函数返回:无 //参数说明:无 //功能概要:子页面2刷新函数 //============================================================================ void SubMenu2_Refresh(void) { OLED_Write_String(2,10,"Menu2"); OLED_Write_String(4,10,"Menu2"); OLED_Write_String(6,10,"Menu2"); MenuPointer_Refresh(SubMenuPointer2); } //============================================================================ //函数名称:void SubMenu3_Refresh(void) //函数返回:无 //参数说明:无 //功能概要:子页面3刷新函数 //============================================================================ void SubMenu3_Refresh(void) { OLED_Write_String(2,10,"Menu3"); OLED_Write_String(4,10,"Menu3"); OLED_Write_String(6,10,"Menu3"); MenuPointer_Refresh(SubMenuPointer3); } //============================================================================ //函数名称:void SubMenu4_Refresh(void) //函数返回:无 //参数说明:无 //功能概要:子页面4刷新函数 //============================================================================ void SubMenu4_Refresh(void) { OLED_Write_String(2,10,"Menu4"); OLED_Write_String(4,10,"Menu4"); OLED_Write_String(6,10,"Menu4"); MenuPointer_Refresh(SubMenuPointer4); } //============================================================================ //函数名称:void SubMenu5_Refresh(void) //函数返回:无 //参数说明:无 //功能概要:子页面5刷新函数 //============================================================================ void SubMenu5_Refresh(void) { OLED_Write_String(2,10,"Menu5"); OLED_Write_String(4,10,"Menu5"); OLED_Write_String(6,10,"Menu5"); MenuPointer_Refresh(SubMenuPointer5); } //============================================================================ //函数名称:void SubMenu6_Refresh(void) //函数返回:无 //参数说明:无 //功能概要:子页面6刷新函数 //============================================================================ void SubMenu6_Refresh(void) { OLED_Write_String(2,10,"Menu6"); OLED_Write_String(4,10,"Menu6"); OLED_Write_String(6,10,"Menu6"); MenuPointer_Refresh(SubMenuPointer6); }
二、使用方法:
直接调用即可。
void main() { DISABLE_INTERRUPTS; //MPU上电,各模块功能初始化前需要关中断 system_init(); //各模块功能初始化 ENABLE_INTERRUPTS; //开中断使能 OLED_Init(); Menu_Refresh(); while(1) { Menu_Key_Detect(); } }