Martin Smith
/
TerminalFruit
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "Terminal.h" 00003 00004 #define MaxNo_Menu 5 00005 00006 Terminal term(USBTX, USBRX); // tx, rx 00007 00008 char *menu_list1[MaxNo_Menu] = { "Apple", "Pear", "Banana", "Kiwi", "Plum" }; 00009 char *menu_list2[MaxNo_Menu] = { "Orange", "Peach", "Strawberry", "Blueberry", "Grapes" }; 00010 int ypos[MaxNo_Menu] = { 3, 6, 9, 12, 15 }; 00011 00012 void InitScreen(void){ //Print all the fruits on the screen 00013 00014 term.background(0xFF0000); 00015 term.foreground(0xFFFFFF); 00016 for(int i=0; i< MaxNo_Menu; i++){ 00017 term.locate(5, ypos[i]); 00018 term.printf("%s",menu_list1[i]); 00019 term.locate(15, ypos[i]); 00020 term.printf("%s",menu_list2[i]); 00021 } 00022 } 00023 00024 void UpdateScreen(int LastIndex, int LastColumn, int Index, int Column){ //Just update the fuits that has changed 00025 00026 term.background(0xFFFFFF); //Highlight the new fruit 00027 term.foreground(0xFF0000); 00028 if(Column==0){ 00029 term.locate(5, ypos[Index]); 00030 term.printf("%s",menu_list1[Index]); 00031 } 00032 else{ 00033 term.locate(15, ypos[Index]); 00034 term.printf("%s",menu_list2[Index]); 00035 } 00036 00037 term.background(0xFF0000); //Black out the old fruit 00038 term.foreground(0xFFFFFF); 00039 if(LastColumn==0){ 00040 term.locate(5, ypos[LastIndex]); 00041 term.printf("%s",menu_list1[LastIndex]); 00042 } 00043 else{ 00044 term.locate(15, ypos[LastIndex]); 00045 term.printf("%s",menu_list2[LastIndex]); 00046 } 00047 } 00048 00049 int main() { 00050 00051 int Key; 00052 int Index=0, Column=0, LastIndex=0, LastColumn=1; 00053 00054 term.background(0xFF0000); 00055 term.foreground(0xFFFFFF); 00056 term.cls(); 00057 term.locate(15,1); 00058 term.printf("What is your favourite fruit?"); 00059 InitScreen(); 00060 UpdateScreen(LastIndex, LastColumn,Index, Column); 00061 term.box(2,2,30,15); 00062 00063 while(1){ 00064 Key=term.getc(); //Get the keypad pressed 00065 if((Key>=0x30)&&(Key<=0x39)){ //See if it's a number (0-9) 00066 } 00067 else if(Key==0x1B){ //If the keypress was an arrow key 00068 Key = term.getc(); //Check again! 00069 if (Key == 0x5B){ 00070 Key = term.getc(); 00071 LastIndex=Index; 00072 LastColumn=Column; 00073 switch(Key){ //Check to see which arrow key... 00074 case 0x41: //It was the UP arrow key... 00075 Index--; 00076 if(Index<0) 00077 Index = MaxNo_Menu - 1; 00078 break; 00079 case 0x42: //It was the DOWN arrow key... 00080 Index++; 00081 if(Index >= MaxNo_Menu) 00082 Index = 0; 00083 break; 00084 case 0x43: //It was the RIGHT arrow key... 00085 Column++; 00086 if(Column>1) 00087 Column=0; 00088 break; 00089 case 0x44: //It was the LEFT arrow key... 00090 Column--; 00091 if(Column<0) 00092 Column=1; 00093 break; 00094 } 00095 UpdateScreen(LastIndex, LastColumn,Index, Column); 00096 } 00097 } 00098 else if(Key>=0x0D){ //See if it was ENTER 00099 term.locate(15,18); 00100 if(Column==0) 00101 term.printf("Your favourite fruit is %s ",menu_list1[Index]); 00102 else 00103 term.printf("Your favourite fruit is %s ",menu_list2[Index]); 00104 } 00105 } 00106 }
Generated on Sun Aug 7 2022 17:02:06 by 1.7.2