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@10:2b6ddf53b05e, 2015-03-16 (annotated)
- Committer:
- charly
- Date:
- Mon Mar 16 21:05:37 2015 +0000
- Revision:
- 10:2b6ddf53b05e
- Parent:
- 9:c9df0b33d176
- Child:
- 11:6814cbc83ae0
First version of a Yes/No Question with UserInterface and result.
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 | 10:2b6ddf53b05e | 9 | _display_cols = lcd->columns(); |
charly | 10:2b6ddf53b05e | 10 | |
charly | 8:fbaeab73fe1a | 11 | activeMenu->CurrentSelection = _cursorPos; |
charly | 5:91b1bc68290b | 12 | |
charly | 10:2b6ddf53b05e | 13 | _wait_for_select = false; |
charly | 10:2b6ddf53b05e | 14 | _wait_for_yesno = false; |
charly | 10:2b6ddf53b05e | 15 | |
pyeh9 | 2:2654dc659298 | 16 | printMenu(); |
pyeh9 | 2:2654dc659298 | 17 | printCursor(); |
pyeh9 | 1:84d263c8932d | 18 | } |
pyeh9 | 1:84d263c8932d | 19 | |
pyeh9 | 1:84d263c8932d | 20 | void Navigator::printMenu() |
charly | 5:91b1bc68290b | 21 | { |
charly | 5:91b1bc68290b | 22 | int index =0; |
pyeh9 | 1:84d263c8932d | 23 | lcd->cls(); |
charly | 5:91b1bc68290b | 24 | |
charly | 5:91b1bc68290b | 25 | for(int row=0; row < _display_rows; row++) { |
charly | 5:91b1bc68290b | 26 | |
charly | 5:91b1bc68290b | 27 | lcd->locate(0,row); |
charly | 5:91b1bc68290b | 28 | index = row + _cursorPos - (_cursorLine-1); // index into selection for this line |
charly | 5:91b1bc68290b | 29 | //should we display a menu on this line? |
charly | 5:91b1bc68290b | 30 | if (index <= activeMenu->selections.size()-1 ) { |
charly | 5:91b1bc68290b | 31 | lcd->printf(" %s", activeMenu->selections[index].selText); |
pyeh9 | 1:84d263c8932d | 32 | } |
pyeh9 | 1:84d263c8932d | 33 | } |
pyeh9 | 1:84d263c8932d | 34 | } |
pyeh9 | 1:84d263c8932d | 35 | |
pyeh9 | 1:84d263c8932d | 36 | void Navigator::printCursor() |
charly | 5:91b1bc68290b | 37 | { |
charly | 5:91b1bc68290b | 38 | if(activeMenu->selections[_cursorPos].childMenu == NULL) printf("No child menu\n"); |
charly | 5:91b1bc68290b | 39 | else printf("child menu: %s\n", activeMenu->selections[_cursorPos].childMenu->menuID); |
charly | 5:91b1bc68290b | 40 | |
pyeh9 | 1:84d263c8932d | 41 | lcd->locate(0,0); |
charly | 5:91b1bc68290b | 42 | for (int row=0; row<_display_rows; row++) { |
charly | 5:91b1bc68290b | 43 | lcd->locate(0,row); |
charly | 5:91b1bc68290b | 44 | if (row == _cursorLine-1) { |
charly | 5:91b1bc68290b | 45 | //we are on Cursor-Line |
charly | 5:91b1bc68290b | 46 | //print cursor |
charly | 5:91b1bc68290b | 47 | lcd->putc('>'); |
charly | 5:91b1bc68290b | 48 | } else { |
charly | 5:91b1bc68290b | 49 | //on other lines print a space |
charly | 5:91b1bc68290b | 50 | lcd->putc(' '); |
charly | 5:91b1bc68290b | 51 | } |
pyeh9 | 1:84d263c8932d | 52 | } |
pyeh9 | 1:84d263c8932d | 53 | } |
pyeh9 | 1:84d263c8932d | 54 | |
pyeh9 | 1:84d263c8932d | 55 | void Navigator::poll() |
pyeh9 | 1:84d263c8932d | 56 | { |
charly | 5:91b1bc68290b | 57 | // no longer needed |
pyeh9 | 1:84d263c8932d | 58 | } |
pyeh9 | 1:84d263c8932d | 59 | |
charly | 10:2b6ddf53b05e | 60 | void Navigator::show_yes_no(bool yesorno) |
charly | 10:2b6ddf53b05e | 61 | { |
charly | 10:2b6ddf53b05e | 62 | // MenuItem is a Yes/no question? |
charly | 10:2b6ddf53b05e | 63 | // show the text in yesnodata and wait for a yes or no |
charly | 10:2b6ddf53b05e | 64 | lcd->cls(); |
charly | 10:2b6ddf53b05e | 65 | //printf("YesNo: \n"); |
charly | 10:2b6ddf53b05e | 66 | //printf("%s <Yes><No>",activeMenu->selections[_cursorPos].menu_parameter->text); |
charly | 10:2b6ddf53b05e | 67 | if (activeMenu->selections[_cursorPos].menu_parameter->text != NULL) { |
charly | 10:2b6ddf53b05e | 68 | if (yesorno) { |
charly | 10:2b6ddf53b05e | 69 | // Yes is default |
charly | 10:2b6ddf53b05e | 70 | lcd->printf("%s <Yes> No ",activeMenu->selections[_cursorPos].menu_parameter->text); |
charly | 10:2b6ddf53b05e | 71 | } else { |
charly | 10:2b6ddf53b05e | 72 | //No is default |
charly | 10:2b6ddf53b05e | 73 | lcd->printf("%s Yes <No>",activeMenu->selections[_cursorPos].menu_parameter->text); |
charly | 10:2b6ddf53b05e | 74 | } |
charly | 10:2b6ddf53b05e | 75 | activeMenu->selections[_cursorPos].menu_parameter->yes_no = yesorno; |
charly | 10:2b6ddf53b05e | 76 | } |
charly | 10:2b6ddf53b05e | 77 | } |
charly | 10:2b6ddf53b05e | 78 | |
charly | 3:cfc36b42ae75 | 79 | void Navigator::select() |
charly | 3:cfc36b42ae75 | 80 | { |
charly | 8:fbaeab73fe1a | 81 | Menu *lastMenu; |
charly | 10:2b6ddf53b05e | 82 | |
charly | 10:2b6ddf53b05e | 83 | // are we waiting for a Select()? |
charly | 10:2b6ddf53b05e | 84 | if (_wait_for_select) { |
charly | 10:2b6ddf53b05e | 85 | _wait_for_select = false; |
charly | 10:2b6ddf53b05e | 86 | // show the menu again |
charly | 10:2b6ddf53b05e | 87 | printMenu(); |
charly | 10:2b6ddf53b05e | 88 | printCursor(); |
charly | 10:2b6ddf53b05e | 89 | |
charly | 10:2b6ddf53b05e | 90 | } else if (_wait_for_yesno) { |
charly | 10:2b6ddf53b05e | 91 | // user selected a value |
charly | 10:2b6ddf53b05e | 92 | _wait_for_yesno = false; |
charly | 10:2b6ddf53b05e | 93 | // show the menu again |
charly | 10:2b6ddf53b05e | 94 | printMenu(); |
charly | 10:2b6ddf53b05e | 95 | printCursor(); |
charly | 10:2b6ddf53b05e | 96 | } else if(activeMenu->selections[_cursorPos].itemMode == MenuItem::mode_yes_no) { |
charly | 10:2b6ddf53b05e | 97 | show_yes_no(activeMenu->selections[_cursorPos].menu_parameter->yes_no); |
charly | 10:2b6ddf53b05e | 98 | _wait_for_yesno = true; |
charly | 10:2b6ddf53b05e | 99 | } else { |
charly | 10:2b6ddf53b05e | 100 | // normal mneuItem |
charly | 10:2b6ddf53b05e | 101 | if(activeMenu->selections[_cursorPos].userAction != NULL) { |
charly | 10:2b6ddf53b05e | 102 | //execute function |
charly | 10:2b6ddf53b05e | 103 | (activeMenu->selections[_cursorPos].userAction)(); |
charly | 10:2b6ddf53b05e | 104 | // refresh the Menu |
charly | 10:2b6ddf53b05e | 105 | //printMenu(); |
charly | 10:2b6ddf53b05e | 106 | //printCursor(); |
charly | 10:2b6ddf53b05e | 107 | } |
charly | 10:2b6ddf53b05e | 108 | //change the menu? |
charly | 10:2b6ddf53b05e | 109 | if(activeMenu->selections[_cursorPos].childMenu != NULL) { |
charly | 10:2b6ddf53b05e | 110 | lastMenu = activeMenu; |
charly | 10:2b6ddf53b05e | 111 | |
charly | 10:2b6ddf53b05e | 112 | //change to childMenu |
charly | 10:2b6ddf53b05e | 113 | activeMenu = activeMenu->selections[_cursorPos].childMenu; |
charly | 10:2b6ddf53b05e | 114 | |
charly | 10:2b6ddf53b05e | 115 | // if we went up one level, set CurrectSelection of SubMenu to zero, if we come back again |
charly | 10:2b6ddf53b05e | 116 | if (activeMenu->selections[activeMenu->CurrentSelection].childMenu == lastMenu) { |
charly | 10:2b6ddf53b05e | 117 | //reset menuposition of submenu to zero |
charly | 10:2b6ddf53b05e | 118 | lastMenu->CurrentSelection = 0; |
charly | 10:2b6ddf53b05e | 119 | } |
charly | 10:2b6ddf53b05e | 120 | |
charly | 10:2b6ddf53b05e | 121 | // return to last position from that menu, if we went up on level |
charly | 10:2b6ddf53b05e | 122 | _cursorPos = activeMenu->CurrentSelection; |
charly | 10:2b6ddf53b05e | 123 | |
charly | 10:2b6ddf53b05e | 124 | _cursorLine = 1; |
charly | 10:2b6ddf53b05e | 125 | printMenu(); |
charly | 10:2b6ddf53b05e | 126 | printCursor(); |
charly | 10:2b6ddf53b05e | 127 | } |
charly | 10:2b6ddf53b05e | 128 | // only accept select after showing this menu/user_action ? |
charly | 10:2b6ddf53b05e | 129 | if(activeMenu->selections[_cursorPos].itemMode == MenuItem::mode_wait_select) { |
charly | 10:2b6ddf53b05e | 130 | _wait_for_select = true; |
charly | 10:2b6ddf53b05e | 131 | } |
charly | 5:91b1bc68290b | 132 | } |
charly | 10:2b6ddf53b05e | 133 | } |
charly | 10:2b6ddf53b05e | 134 | |
charly | 10:2b6ddf53b05e | 135 | void Navigator::moveUp() |
charly | 10:2b6ddf53b05e | 136 | { |
charly | 10:2b6ddf53b05e | 137 | if (_wait_for_yesno) { |
charly | 10:2b6ddf53b05e | 138 | // change Yes/no Selection |
charly | 10:2b6ddf53b05e | 139 | show_yes_no( ! activeMenu->selections[_cursorPos].menu_parameter->yes_no); |
charly | 10:2b6ddf53b05e | 140 | }else |
charly | 10:2b6ddf53b05e | 141 | // only if we don't wait for a select() |
charly | 10:2b6ddf53b05e | 142 | if (! _wait_for_select) { |
charly | 10:2b6ddf53b05e | 143 | // Show the MenuItems |
charly | 10:2b6ddf53b05e | 144 | // allready on TOP of Display? |
charly | 10:2b6ddf53b05e | 145 | if(_cursorLine > 1) { |
charly | 10:2b6ddf53b05e | 146 | // scroll up cursor one line |
charly | 10:2b6ddf53b05e | 147 | _cursorLine--; |
charly | 10:2b6ddf53b05e | 148 | } |
charly | 10:2b6ddf53b05e | 149 | |
charly | 10:2b6ddf53b05e | 150 | if(_cursorPos > 0) { |
charly | 10:2b6ddf53b05e | 151 | //scroll up one item |
charly | 10:2b6ddf53b05e | 152 | _cursorPos--; |
charly | 10:2b6ddf53b05e | 153 | activeMenu->CurrentSelection = _cursorPos; |
charly | 10:2b6ddf53b05e | 154 | |
charly | 10:2b6ddf53b05e | 155 | } |
charly | 5:91b1bc68290b | 156 | printMenu(); |
charly | 5:91b1bc68290b | 157 | printCursor(); |
charly | 5:91b1bc68290b | 158 | } |
charly | 3:cfc36b42ae75 | 159 | } |
pyeh9 | 1:84d263c8932d | 160 | |
pyeh9 | 1:84d263c8932d | 161 | void Navigator::moveDown() |
pyeh9 | 1:84d263c8932d | 162 | { |
charly | 10:2b6ddf53b05e | 163 | if (_wait_for_yesno) { |
charly | 10:2b6ddf53b05e | 164 | // change Yes/no Selection |
charly | 10:2b6ddf53b05e | 165 | show_yes_no( ! activeMenu->selections[_cursorPos].menu_parameter->yes_no); |
charly | 10:2b6ddf53b05e | 166 | }else |
charly | 10:2b6ddf53b05e | 167 | // only if we don't wait for a select() |
charly | 10:2b6ddf53b05e | 168 | if (! _wait_for_select) { |
charly | 10:2b6ddf53b05e | 169 | //Show the menuItem |
charly | 10:2b6ddf53b05e | 170 | // allready on last line of display? |
charly | 10:2b6ddf53b05e | 171 | if (_cursorPos == activeMenu->selections.size()-1) { |
charly | 10:2b6ddf53b05e | 172 | //stay on this line |
charly | 5:91b1bc68290b | 173 | } else { |
charly | 10:2b6ddf53b05e | 174 | // move down |
charly | 10:2b6ddf53b05e | 175 | if(_cursorLine < _display_rows) { |
charly | 10:2b6ddf53b05e | 176 | // Only move down cursor |
charly | 10:2b6ddf53b05e | 177 | _cursorLine++; |
charly | 10:2b6ddf53b05e | 178 | _cursorPos++; |
charly | 10:2b6ddf53b05e | 179 | } else { |
charly | 10:2b6ddf53b05e | 180 | // on last Display-Line scroll down Menu |
charly | 10:2b6ddf53b05e | 181 | _cursorPos++; |
charly | 10:2b6ddf53b05e | 182 | } |
charly | 10:2b6ddf53b05e | 183 | // save currentPosition in Menu |
charly | 10:2b6ddf53b05e | 184 | activeMenu->CurrentSelection = _cursorPos; |
charly | 5:91b1bc68290b | 185 | |
charly | 10:2b6ddf53b05e | 186 | } |
charly | 5:91b1bc68290b | 187 | |
charly | 10:2b6ddf53b05e | 188 | printMenu(); |
charly | 10:2b6ddf53b05e | 189 | printCursor(); |
charly | 10:2b6ddf53b05e | 190 | } |
charly | 10:2b6ddf53b05e | 191 | } |