Library to display menus on TextLCDs. Interaction with functions Up,Down and Select (Buttons or RPG) Based on menu-library from pyeh9

Fork of Menu by Peihsun Yeh

Library to display menus on TextLCDs. Interaction with functions Up,Down and Select (Buttons or RPG) Based on menu-library from pyeh9

Committer:
charly
Date:
Thu Jan 01 22:27:30 2015 +0000
Revision:
3:cfc36b42ae75
Parent:
2:2654dc659298
Child:
4:67097127da6c
Removed RPG; added method select.; Interaction can be handled outside Menu with methods Navigator::moveUp, moveDown and select.; Changed from TextLCD to http://developer.mbed.org/cookbook/Text-LCD-Enhanced; Fixed bug to display 15 Chars per line

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pyeh9 1:84d263c8932d 1 #include "Navigator.h"
pyeh9 1:84d263c8932d 2
charly 3:cfc36b42ae75 3 Navigator::Navigator(Menu *root, /*RPG &rpg,*/ TextLCD_Base *lcd) : activeMenu(root), /*rpg(rpg), */ lcd(lcd)
pyeh9 1:84d263c8932d 4 {
pyeh9 1:84d263c8932d 5 bottom = root->selections.size();
pyeh9 1:84d263c8932d 6 cursorPos = 0;
pyeh9 1:84d263c8932d 7 cursorLine = 1;
pyeh9 1:84d263c8932d 8 button = 0;
pyeh9 1:84d263c8932d 9 lastButton = 0;
pyeh9 2:2654dc659298 10
pyeh9 2:2654dc659298 11 printMenu();
pyeh9 2:2654dc659298 12 printCursor();
pyeh9 1:84d263c8932d 13 }
pyeh9 1:84d263c8932d 14
pyeh9 1:84d263c8932d 15 void Navigator::printMenu()
pyeh9 1:84d263c8932d 16 {
pyeh9 1:84d263c8932d 17 lcd->cls();
pyeh9 1:84d263c8932d 18 if(bottom == 1){ // the current Menu only has one selection
charly 3:cfc36b42ae75 19 lcd->printf(" %s", activeMenu->selections[0].selText);
pyeh9 1:84d263c8932d 20 } else {
pyeh9 1:84d263c8932d 21 if(cursorLine == 2){ // if we're at the bottom
charly 3:cfc36b42ae75 22 lcd->printf(" %s", activeMenu->selections[cursorPos-1].selText);
charly 3:cfc36b42ae75 23 lcd->locate(0,1);
charly 3:cfc36b42ae75 24 lcd->printf(" %s", activeMenu->selections[cursorPos].selText);
pyeh9 1:84d263c8932d 25 } else {
charly 3:cfc36b42ae75 26 lcd->printf(" %s", activeMenu->selections[cursorPos].selText);
charly 3:cfc36b42ae75 27 lcd->locate(0,1);
charly 3:cfc36b42ae75 28 lcd->printf(" %s", activeMenu->selections[cursorPos+1].selText);
pyeh9 1:84d263c8932d 29 }
pyeh9 1:84d263c8932d 30 }
pyeh9 1:84d263c8932d 31 }
pyeh9 1:84d263c8932d 32
pyeh9 1:84d263c8932d 33 void Navigator::printCursor()
pyeh9 1:84d263c8932d 34 {
pyeh9 1:84d263c8932d 35 if(activeMenu->selections[cursorPos].childMenu == NULL) printf("No child menu\n");
pyeh9 1:84d263c8932d 36 else printf("child menu: %s\n", activeMenu->selections[cursorPos].childMenu->menuID);
pyeh9 1:84d263c8932d 37
pyeh9 1:84d263c8932d 38 lcd->locate(0,0);
pyeh9 1:84d263c8932d 39 if(cursorLine == 1){
pyeh9 1:84d263c8932d 40 lcd->putc('>');
pyeh9 1:84d263c8932d 41 lcd->locate(0,1);
pyeh9 1:84d263c8932d 42 lcd->putc(' ');
pyeh9 1:84d263c8932d 43 } else if(cursorLine == 2){
pyeh9 1:84d263c8932d 44 lcd->putc(' ');
pyeh9 1:84d263c8932d 45 lcd->locate(0,1);
pyeh9 1:84d263c8932d 46 lcd->putc('>');
pyeh9 1:84d263c8932d 47 }
pyeh9 1:84d263c8932d 48 }
pyeh9 1:84d263c8932d 49
pyeh9 1:84d263c8932d 50 void Navigator::poll()
pyeh9 1:84d263c8932d 51 {
charly 3:cfc36b42ae75 52 /*
pyeh9 1:84d263c8932d 53 if((direction = rpg.dir())!=0){ //Get Dir
pyeh9 1:84d263c8932d 54 wait(0.2);
pyeh9 1:84d263c8932d 55 if(direction == 1) moveDown();
pyeh9 1:84d263c8932d 56 else if(direction == -1) moveUp();
pyeh9 1:84d263c8932d 57 }
pyeh9 1:84d263c8932d 58
pyeh9 1:84d263c8932d 59 if ((button = rpg.pb()) && !lastButton){ //prevents multiple selections when button is held down
pyeh9 1:84d263c8932d 60 wait(0.2);
pyeh9 1:84d263c8932d 61 if(activeMenu->selections[cursorPos].fun != NULL){
pyeh9 1:84d263c8932d 62 (activeMenu->selections[cursorPos].fun)();
pyeh9 1:84d263c8932d 63 }
pyeh9 1:84d263c8932d 64 if(activeMenu->selections[cursorPos].childMenu != NULL){
pyeh9 1:84d263c8932d 65 activeMenu = activeMenu->selections[cursorPos].childMenu;
pyeh9 1:84d263c8932d 66 bottom = activeMenu->selections.size();
pyeh9 1:84d263c8932d 67 cursorPos = 0;
pyeh9 1:84d263c8932d 68 cursorLine = 1;
pyeh9 1:84d263c8932d 69 printMenu();
pyeh9 1:84d263c8932d 70 printCursor();
pyeh9 1:84d263c8932d 71 }
pyeh9 1:84d263c8932d 72 }
pyeh9 1:84d263c8932d 73 lastButton = button;
charly 3:cfc36b42ae75 74 */
pyeh9 1:84d263c8932d 75 }
pyeh9 1:84d263c8932d 76
charly 3:cfc36b42ae75 77 void Navigator::select()
charly 3:cfc36b42ae75 78 {
charly 3:cfc36b42ae75 79 if(activeMenu->selections[cursorPos].fun != NULL){
charly 3:cfc36b42ae75 80 (activeMenu->selections[cursorPos].fun)();
charly 3:cfc36b42ae75 81 }
charly 3:cfc36b42ae75 82 if(activeMenu->selections[cursorPos].childMenu != NULL){
charly 3:cfc36b42ae75 83 activeMenu = activeMenu->selections[cursorPos].childMenu;
charly 3:cfc36b42ae75 84 bottom = activeMenu->selections.size();
charly 3:cfc36b42ae75 85 cursorPos = 0;
charly 3:cfc36b42ae75 86 cursorLine = 1;
charly 3:cfc36b42ae75 87 printMenu();
charly 3:cfc36b42ae75 88 printCursor();
charly 3:cfc36b42ae75 89 }
charly 3:cfc36b42ae75 90 }
pyeh9 1:84d263c8932d 91 void Navigator::moveUp()
pyeh9 1:84d263c8932d 92 {
pyeh9 1:84d263c8932d 93 if(cursorLine == 1){
pyeh9 1:84d263c8932d 94 printMenu();
pyeh9 1:84d263c8932d 95 } else if(cursorLine == 2){
pyeh9 1:84d263c8932d 96 cursorLine = 1;
pyeh9 1:84d263c8932d 97 }
pyeh9 1:84d263c8932d 98
pyeh9 1:84d263c8932d 99 if(cursorPos != 0){
pyeh9 1:84d263c8932d 100 cursorPos--;
pyeh9 1:84d263c8932d 101 printMenu();
pyeh9 1:84d263c8932d 102 }
pyeh9 1:84d263c8932d 103 printCursor();
pyeh9 1:84d263c8932d 104 }
pyeh9 1:84d263c8932d 105
pyeh9 1:84d263c8932d 106 void Navigator::moveDown()
pyeh9 1:84d263c8932d 107 {
pyeh9 1:84d263c8932d 108 if(cursorLine == 1){
pyeh9 1:84d263c8932d 109 cursorLine = 2;
pyeh9 1:84d263c8932d 110 } else if(cursorLine == 2){
pyeh9 1:84d263c8932d 111 printMenu();
pyeh9 1:84d263c8932d 112 }
pyeh9 1:84d263c8932d 113
pyeh9 1:84d263c8932d 114 if(cursorPos != (bottom-1)){
pyeh9 1:84d263c8932d 115 cursorPos++;
pyeh9 1:84d263c8932d 116 printMenu();
pyeh9 1:84d263c8932d 117 }
pyeh9 1:84d263c8932d 118 printCursor();
pyeh9 1:84d263c8932d 119 }