Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Menu by
Navigator.cpp@5:91b1bc68290b, 2015-01-02 (annotated)
- Committer:
- charly
- Date:
- Fri Jan 02 15:41:09 2015 +0000
- Revision:
- 5:91b1bc68290b
- Parent:
- 4:67097127da6c
- Child:
- 6:819049708d51
Bug fixes. ; Should work for displays with more than 2 rows.
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 | 5:91b1bc68290b | 59 | } |
| charly | 5:91b1bc68290b | 60 | if(activeMenu->selections[_cursorPos].childMenu != NULL) { |
| charly | 5:91b1bc68290b | 61 | activeMenu = activeMenu->selections[_cursorPos].childMenu; |
| charly | 5:91b1bc68290b | 62 | _cursorPos = 0; |
| charly | 5:91b1bc68290b | 63 | _cursorLine = 1; |
| charly | 5:91b1bc68290b | 64 | printMenu(); |
| charly | 5:91b1bc68290b | 65 | printCursor(); |
| charly | 5:91b1bc68290b | 66 | } |
| charly | 3:cfc36b42ae75 | 67 | } |
| pyeh9 | 1:84d263c8932d | 68 | void Navigator::moveUp() |
| pyeh9 | 1:84d263c8932d | 69 | { |
| charly | 5:91b1bc68290b | 70 | // allready on TOP of Display? |
| charly | 5:91b1bc68290b | 71 | if(_cursorLine > 1) { |
| charly | 5:91b1bc68290b | 72 | // scroll up cursor one line |
| charly | 5:91b1bc68290b | 73 | _cursorLine--; |
| pyeh9 | 1:84d263c8932d | 74 | } |
| charly | 5:91b1bc68290b | 75 | |
| charly | 5:91b1bc68290b | 76 | if(_cursorPos > 0) { |
| charly | 5:91b1bc68290b | 77 | //scroll up one item |
| charly | 5:91b1bc68290b | 78 | _cursorPos--; |
| charly | 5:91b1bc68290b | 79 | |
| pyeh9 | 1:84d263c8932d | 80 | } |
| charly | 5:91b1bc68290b | 81 | printMenu(); |
| pyeh9 | 1:84d263c8932d | 82 | printCursor(); |
| pyeh9 | 1:84d263c8932d | 83 | } |
| pyeh9 | 1:84d263c8932d | 84 | |
| pyeh9 | 1:84d263c8932d | 85 | void Navigator::moveDown() |
| pyeh9 | 1:84d263c8932d | 86 | { |
| charly | 5:91b1bc68290b | 87 | |
| charly | 5:91b1bc68290b | 88 | // allready on last line of display? |
| charly | 5:91b1bc68290b | 89 | if (_cursorPos == activeMenu->selections.size()-1) { |
| charly | 5:91b1bc68290b | 90 | //stay on this line |
| charly | 5:91b1bc68290b | 91 | } else { |
| charly | 5:91b1bc68290b | 92 | // move down |
| charly | 5:91b1bc68290b | 93 | if(_cursorLine < _display_rows) { |
| charly | 5:91b1bc68290b | 94 | // Only move down cursor |
| charly | 5:91b1bc68290b | 95 | _cursorLine++; |
| charly | 5:91b1bc68290b | 96 | _cursorPos++; |
| charly | 5:91b1bc68290b | 97 | } else { |
| charly | 5:91b1bc68290b | 98 | // on last Display-Line scroll down Menu |
| charly | 5:91b1bc68290b | 99 | _cursorPos++; |
| charly | 5:91b1bc68290b | 100 | } |
| charly | 5:91b1bc68290b | 101 | |
| pyeh9 | 1:84d263c8932d | 102 | } |
| charly | 5:91b1bc68290b | 103 | |
| charly | 5:91b1bc68290b | 104 | printMenu(); |
| pyeh9 | 1:84d263c8932d | 105 | printCursor(); |
| pyeh9 | 1:84d263c8932d | 106 | } |
