Martin Smith
/
TerminalFruit
main.cpp@0:408a2c6feb1f, 2010-01-20 (annotated)
- Committer:
- ms523
- Date:
- Wed Jan 20 15:09:13 2010 +0000
- Revision:
- 0:408a2c6feb1f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ms523 | 0:408a2c6feb1f | 1 | #include "mbed.h" |
ms523 | 0:408a2c6feb1f | 2 | #include "Terminal.h" |
ms523 | 0:408a2c6feb1f | 3 | |
ms523 | 0:408a2c6feb1f | 4 | #define MaxNo_Menu 5 |
ms523 | 0:408a2c6feb1f | 5 | |
ms523 | 0:408a2c6feb1f | 6 | Terminal term(USBTX, USBRX); // tx, rx |
ms523 | 0:408a2c6feb1f | 7 | |
ms523 | 0:408a2c6feb1f | 8 | char *menu_list1[MaxNo_Menu] = { "Apple", "Pear", "Banana", "Kiwi", "Plum" }; |
ms523 | 0:408a2c6feb1f | 9 | char *menu_list2[MaxNo_Menu] = { "Orange", "Peach", "Strawberry", "Blueberry", "Grapes" }; |
ms523 | 0:408a2c6feb1f | 10 | int ypos[MaxNo_Menu] = { 3, 6, 9, 12, 15 }; |
ms523 | 0:408a2c6feb1f | 11 | |
ms523 | 0:408a2c6feb1f | 12 | void InitScreen(void){ //Print all the fruits on the screen |
ms523 | 0:408a2c6feb1f | 13 | |
ms523 | 0:408a2c6feb1f | 14 | term.background(0xFF0000); |
ms523 | 0:408a2c6feb1f | 15 | term.foreground(0xFFFFFF); |
ms523 | 0:408a2c6feb1f | 16 | for(int i=0; i< MaxNo_Menu; i++){ |
ms523 | 0:408a2c6feb1f | 17 | term.locate(5, ypos[i]); |
ms523 | 0:408a2c6feb1f | 18 | term.printf("%s",menu_list1[i]); |
ms523 | 0:408a2c6feb1f | 19 | term.locate(15, ypos[i]); |
ms523 | 0:408a2c6feb1f | 20 | term.printf("%s",menu_list2[i]); |
ms523 | 0:408a2c6feb1f | 21 | } |
ms523 | 0:408a2c6feb1f | 22 | } |
ms523 | 0:408a2c6feb1f | 23 | |
ms523 | 0:408a2c6feb1f | 24 | void UpdateScreen(int LastIndex, int LastColumn, int Index, int Column){ //Just update the fuits that has changed |
ms523 | 0:408a2c6feb1f | 25 | |
ms523 | 0:408a2c6feb1f | 26 | term.background(0xFFFFFF); //Highlight the new fruit |
ms523 | 0:408a2c6feb1f | 27 | term.foreground(0xFF0000); |
ms523 | 0:408a2c6feb1f | 28 | if(Column==0){ |
ms523 | 0:408a2c6feb1f | 29 | term.locate(5, ypos[Index]); |
ms523 | 0:408a2c6feb1f | 30 | term.printf("%s",menu_list1[Index]); |
ms523 | 0:408a2c6feb1f | 31 | } |
ms523 | 0:408a2c6feb1f | 32 | else{ |
ms523 | 0:408a2c6feb1f | 33 | term.locate(15, ypos[Index]); |
ms523 | 0:408a2c6feb1f | 34 | term.printf("%s",menu_list2[Index]); |
ms523 | 0:408a2c6feb1f | 35 | } |
ms523 | 0:408a2c6feb1f | 36 | |
ms523 | 0:408a2c6feb1f | 37 | term.background(0xFF0000); //Black out the old fruit |
ms523 | 0:408a2c6feb1f | 38 | term.foreground(0xFFFFFF); |
ms523 | 0:408a2c6feb1f | 39 | if(LastColumn==0){ |
ms523 | 0:408a2c6feb1f | 40 | term.locate(5, ypos[LastIndex]); |
ms523 | 0:408a2c6feb1f | 41 | term.printf("%s",menu_list1[LastIndex]); |
ms523 | 0:408a2c6feb1f | 42 | } |
ms523 | 0:408a2c6feb1f | 43 | else{ |
ms523 | 0:408a2c6feb1f | 44 | term.locate(15, ypos[LastIndex]); |
ms523 | 0:408a2c6feb1f | 45 | term.printf("%s",menu_list2[LastIndex]); |
ms523 | 0:408a2c6feb1f | 46 | } |
ms523 | 0:408a2c6feb1f | 47 | } |
ms523 | 0:408a2c6feb1f | 48 | |
ms523 | 0:408a2c6feb1f | 49 | int main() { |
ms523 | 0:408a2c6feb1f | 50 | |
ms523 | 0:408a2c6feb1f | 51 | int Key; |
ms523 | 0:408a2c6feb1f | 52 | int Index=0, Column=0, LastIndex=0, LastColumn=1; |
ms523 | 0:408a2c6feb1f | 53 | |
ms523 | 0:408a2c6feb1f | 54 | term.background(0xFF0000); |
ms523 | 0:408a2c6feb1f | 55 | term.foreground(0xFFFFFF); |
ms523 | 0:408a2c6feb1f | 56 | term.cls(); |
ms523 | 0:408a2c6feb1f | 57 | term.locate(15,1); |
ms523 | 0:408a2c6feb1f | 58 | term.printf("What is your favourite fruit?"); |
ms523 | 0:408a2c6feb1f | 59 | InitScreen(); |
ms523 | 0:408a2c6feb1f | 60 | UpdateScreen(LastIndex, LastColumn,Index, Column); |
ms523 | 0:408a2c6feb1f | 61 | term.box(2,2,30,15); |
ms523 | 0:408a2c6feb1f | 62 | |
ms523 | 0:408a2c6feb1f | 63 | while(1){ |
ms523 | 0:408a2c6feb1f | 64 | Key=term.getc(); //Get the keypad pressed |
ms523 | 0:408a2c6feb1f | 65 | if((Key>=0x30)&&(Key<=0x39)){ //See if it's a number (0-9) |
ms523 | 0:408a2c6feb1f | 66 | } |
ms523 | 0:408a2c6feb1f | 67 | else if(Key==0x1B){ //If the keypress was an arrow key |
ms523 | 0:408a2c6feb1f | 68 | Key = term.getc(); //Check again! |
ms523 | 0:408a2c6feb1f | 69 | if (Key == 0x5B){ |
ms523 | 0:408a2c6feb1f | 70 | Key = term.getc(); |
ms523 | 0:408a2c6feb1f | 71 | LastIndex=Index; |
ms523 | 0:408a2c6feb1f | 72 | LastColumn=Column; |
ms523 | 0:408a2c6feb1f | 73 | switch(Key){ //Check to see which arrow key... |
ms523 | 0:408a2c6feb1f | 74 | case 0x41: //It was the UP arrow key... |
ms523 | 0:408a2c6feb1f | 75 | Index--; |
ms523 | 0:408a2c6feb1f | 76 | if(Index<0) |
ms523 | 0:408a2c6feb1f | 77 | Index = MaxNo_Menu - 1; |
ms523 | 0:408a2c6feb1f | 78 | break; |
ms523 | 0:408a2c6feb1f | 79 | case 0x42: //It was the DOWN arrow key... |
ms523 | 0:408a2c6feb1f | 80 | Index++; |
ms523 | 0:408a2c6feb1f | 81 | if(Index >= MaxNo_Menu) |
ms523 | 0:408a2c6feb1f | 82 | Index = 0; |
ms523 | 0:408a2c6feb1f | 83 | break; |
ms523 | 0:408a2c6feb1f | 84 | case 0x43: //It was the RIGHT arrow key... |
ms523 | 0:408a2c6feb1f | 85 | Column++; |
ms523 | 0:408a2c6feb1f | 86 | if(Column>1) |
ms523 | 0:408a2c6feb1f | 87 | Column=0; |
ms523 | 0:408a2c6feb1f | 88 | break; |
ms523 | 0:408a2c6feb1f | 89 | case 0x44: //It was the LEFT arrow key... |
ms523 | 0:408a2c6feb1f | 90 | Column--; |
ms523 | 0:408a2c6feb1f | 91 | if(Column<0) |
ms523 | 0:408a2c6feb1f | 92 | Column=1; |
ms523 | 0:408a2c6feb1f | 93 | break; |
ms523 | 0:408a2c6feb1f | 94 | } |
ms523 | 0:408a2c6feb1f | 95 | UpdateScreen(LastIndex, LastColumn,Index, Column); |
ms523 | 0:408a2c6feb1f | 96 | } |
ms523 | 0:408a2c6feb1f | 97 | } |
ms523 | 0:408a2c6feb1f | 98 | else if(Key>=0x0D){ //See if it was ENTER |
ms523 | 0:408a2c6feb1f | 99 | term.locate(15,18); |
ms523 | 0:408a2c6feb1f | 100 | if(Column==0) |
ms523 | 0:408a2c6feb1f | 101 | term.printf("Your favourite fruit is %s ",menu_list1[Index]); |
ms523 | 0:408a2c6feb1f | 102 | else |
ms523 | 0:408a2c6feb1f | 103 | term.printf("Your favourite fruit is %s ",menu_list2[Index]); |
ms523 | 0:408a2c6feb1f | 104 | } |
ms523 | 0:408a2c6feb1f | 105 | } |
ms523 | 0:408a2c6feb1f | 106 | } |