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
Library to display menus on TextLCDs. Interaction with functions Up,Down and Select (Buttons or RPG) Based on menu-library from pyeh9
Navigator.cpp@6:819049708d51, 2015-01-02 (annotated)
- Committer:
- charly
- Date:
- Fri Jan 02 20:49:59 2015 +0000
- Revision:
- 6:819049708d51
- Parent:
- 5:91b1bc68290b
- Child:
- 7:12f2b3b7975f
addes Method to Chnage Text of an MenuItem
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pyeh9 | 1:84d263c8932d | 1 | #include "Navigator.h" |
pyeh9 | 1:84d263c8932d | 2 | |
charly | 5:91b1bc68290b | 3 | Navigator::Navigator(Menu *root, TextLCD_Base *lcd) : activeMenu(root), lcd(lcd) |
pyeh9 | 1:84d263c8932d | 4 | { |
charly | 5:91b1bc68290b | 5 | |
charly | 5:91b1bc68290b | 6 | _cursorPos = 0; |
charly | 5:91b1bc68290b | 7 | _cursorLine = 1; |
charly | 5:91b1bc68290b | 8 | _display_rows = lcd->rows(); |
charly | 5:91b1bc68290b | 9 | |
pyeh9 | 2:2654dc659298 | 10 | printMenu(); |
pyeh9 | 2:2654dc659298 | 11 | printCursor(); |
pyeh9 | 1:84d263c8932d | 12 | } |
pyeh9 | 1:84d263c8932d | 13 | |
pyeh9 | 1:84d263c8932d | 14 | void Navigator::printMenu() |
charly | 5:91b1bc68290b | 15 | { |
charly | 5:91b1bc68290b | 16 | int index =0; |
pyeh9 | 1:84d263c8932d | 17 | lcd->cls(); |
charly | 5:91b1bc68290b | 18 | |
charly | 5:91b1bc68290b | 19 | for(int row=0; row < _display_rows; row++) { |
charly | 5:91b1bc68290b | 20 | |
charly | 5:91b1bc68290b | 21 | lcd->locate(0,row); |
charly | 5:91b1bc68290b | 22 | index = row + _cursorPos - (_cursorLine-1); // index into selection for this line |
charly | 5:91b1bc68290b | 23 | //should we display a menu on this line? |
charly | 5:91b1bc68290b | 24 | if (index <= activeMenu->selections.size()-1 ) { |
charly | 5:91b1bc68290b | 25 | lcd->printf(" %s", activeMenu->selections[index].selText); |
pyeh9 | 1:84d263c8932d | 26 | } |
pyeh9 | 1:84d263c8932d | 27 | } |
pyeh9 | 1:84d263c8932d | 28 | } |
pyeh9 | 1:84d263c8932d | 29 | |
pyeh9 | 1:84d263c8932d | 30 | void Navigator::printCursor() |
charly | 5:91b1bc68290b | 31 | { |
charly | 5:91b1bc68290b | 32 | if(activeMenu->selections[_cursorPos].childMenu == NULL) printf("No child menu\n"); |
charly | 5:91b1bc68290b | 33 | else printf("child menu: %s\n", activeMenu->selections[_cursorPos].childMenu->menuID); |
charly | 5:91b1bc68290b | 34 | |
pyeh9 | 1:84d263c8932d | 35 | lcd->locate(0,0); |
charly | 5:91b1bc68290b | 36 | for (int row=0; row<_display_rows; row++) { |
charly | 5:91b1bc68290b | 37 | lcd->locate(0,row); |
charly | 5:91b1bc68290b | 38 | if (row == _cursorLine-1) { |
charly | 5:91b1bc68290b | 39 | //we are on Cursor-Line |
charly | 5:91b1bc68290b | 40 | //print cursor |
charly | 5:91b1bc68290b | 41 | lcd->putc('>'); |
charly | 5:91b1bc68290b | 42 | } else { |
charly | 5:91b1bc68290b | 43 | //on other lines print a space |
charly | 5:91b1bc68290b | 44 | lcd->putc(' '); |
charly | 5:91b1bc68290b | 45 | } |
pyeh9 | 1:84d263c8932d | 46 | } |
pyeh9 | 1:84d263c8932d | 47 | } |
pyeh9 | 1:84d263c8932d | 48 | |
pyeh9 | 1:84d263c8932d | 49 | void Navigator::poll() |
pyeh9 | 1:84d263c8932d | 50 | { |
charly | 5:91b1bc68290b | 51 | // no longer needed |
pyeh9 | 1:84d263c8932d | 52 | } |
pyeh9 | 1:84d263c8932d | 53 | |
charly | 3:cfc36b42ae75 | 54 | void Navigator::select() |
charly | 3:cfc36b42ae75 | 55 | { |
charly | 5:91b1bc68290b | 56 | if(activeMenu->selections[_cursorPos].fun != NULL) { |
charly | 5:91b1bc68290b | 57 | //execute function |
charly | 5:91b1bc68290b | 58 | (activeMenu->selections[_cursorPos].fun)(); |
charly | 6:819049708d51 | 59 | // refresh the Menu |
charly | 6:819049708d51 | 60 | printMenu(); |
charly | 6:819049708d51 | 61 | printCursor(); |
charly | 5:91b1bc68290b | 62 | } |
charly | 5:91b1bc68290b | 63 | if(activeMenu->selections[_cursorPos].childMenu != NULL) { |
charly | 5:91b1bc68290b | 64 | activeMenu = activeMenu->selections[_cursorPos].childMenu; |
charly | 5:91b1bc68290b | 65 | _cursorPos = 0; |
charly | 5:91b1bc68290b | 66 | _cursorLine = 1; |
charly | 5:91b1bc68290b | 67 | printMenu(); |
charly | 5:91b1bc68290b | 68 | printCursor(); |
charly | 5:91b1bc68290b | 69 | } |
charly | 3:cfc36b42ae75 | 70 | } |
pyeh9 | 1:84d263c8932d | 71 | void Navigator::moveUp() |
pyeh9 | 1:84d263c8932d | 72 | { |
charly | 5:91b1bc68290b | 73 | // allready on TOP of Display? |
charly | 5:91b1bc68290b | 74 | if(_cursorLine > 1) { |
charly | 5:91b1bc68290b | 75 | // scroll up cursor one line |
charly | 5:91b1bc68290b | 76 | _cursorLine--; |
pyeh9 | 1:84d263c8932d | 77 | } |
charly | 5:91b1bc68290b | 78 | |
charly | 5:91b1bc68290b | 79 | if(_cursorPos > 0) { |
charly | 5:91b1bc68290b | 80 | //scroll up one item |
charly | 5:91b1bc68290b | 81 | _cursorPos--; |
charly | 5:91b1bc68290b | 82 | |
pyeh9 | 1:84d263c8932d | 83 | } |
charly | 5:91b1bc68290b | 84 | printMenu(); |
pyeh9 | 1:84d263c8932d | 85 | printCursor(); |
pyeh9 | 1:84d263c8932d | 86 | } |
pyeh9 | 1:84d263c8932d | 87 | |
pyeh9 | 1:84d263c8932d | 88 | void Navigator::moveDown() |
pyeh9 | 1:84d263c8932d | 89 | { |
charly | 5:91b1bc68290b | 90 | |
charly | 5:91b1bc68290b | 91 | // allready on last line of display? |
charly | 5:91b1bc68290b | 92 | if (_cursorPos == activeMenu->selections.size()-1) { |
charly | 5:91b1bc68290b | 93 | //stay on this line |
charly | 5:91b1bc68290b | 94 | } else { |
charly | 5:91b1bc68290b | 95 | // move down |
charly | 5:91b1bc68290b | 96 | if(_cursorLine < _display_rows) { |
charly | 5:91b1bc68290b | 97 | // Only move down cursor |
charly | 5:91b1bc68290b | 98 | _cursorLine++; |
charly | 5:91b1bc68290b | 99 | _cursorPos++; |
charly | 5:91b1bc68290b | 100 | } else { |
charly | 5:91b1bc68290b | 101 | // on last Display-Line scroll down Menu |
charly | 5:91b1bc68290b | 102 | _cursorPos++; |
charly | 5:91b1bc68290b | 103 | } |
charly | 5:91b1bc68290b | 104 | |
pyeh9 | 1:84d263c8932d | 105 | } |
charly | 5:91b1bc68290b | 106 | |
charly | 5:91b1bc68290b | 107 | printMenu(); |
pyeh9 | 1:84d263c8932d | 108 | printCursor(); |
pyeh9 | 1:84d263c8932d | 109 | } |