Library implementing menu system, with RPG interface, navigator system
Dependencies: Menu RPG TextLCD mbed
main.cpp@3:30dc8952d0ae, 2013-03-05 (annotated)
- Committer:
- pyeh9
- Date:
- Tue Mar 05 22:16:35 2013 +0000
- Revision:
- 3:30dc8952d0ae
- Parent:
- 2:202735df93cd
2nd version - commented
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pyeh9 | 0:10b365b7873b | 1 | #include "mbed.h" |
pyeh9 | 0:10b365b7873b | 2 | #include "TextLCD.h" |
pyeh9 | 0:10b365b7873b | 3 | #include "RPG.h" |
pyeh9 | 0:10b365b7873b | 4 | #include "Selection.h" |
pyeh9 | 0:10b365b7873b | 5 | #include "Menu.h" |
pyeh9 | 0:10b365b7873b | 6 | #include "Navigator.h" |
pyeh9 | 2:202735df93cd | 7 | #include <vector> |
pyeh9 | 2:202735df93cd | 8 | #include <string> |
pyeh9 | 0:10b365b7873b | 9 | |
pyeh9 | 0:10b365b7873b | 10 | DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); |
pyeh9 | 0:10b365b7873b | 11 | TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 |
pyeh9 | 0:10b365b7873b | 12 | RPG rpg(p21,p22,p23); |
pyeh9 | 0:10b365b7873b | 13 | |
pyeh9 | 0:10b365b7873b | 14 | using namespace std; |
pyeh9 | 0:10b365b7873b | 15 | |
pyeh9 | 2:202735df93cd | 16 | // some functions to tie to selections |
pyeh9 | 2:202735df93cd | 17 | void toggleLED1() { led1 = !led1; } |
pyeh9 | 2:202735df93cd | 18 | void toggleLED2() { led2 = !led2; } |
pyeh9 | 2:202735df93cd | 19 | void toggleLED3() { led3 = !led3; } |
pyeh9 | 2:202735df93cd | 20 | void toggleLED4() { led4 = !led4; } |
pyeh9 | 2:202735df93cd | 21 | void resetLED() |
pyeh9 | 2:202735df93cd | 22 | { |
pyeh9 | 2:202735df93cd | 23 | led1 = 0; |
pyeh9 | 2:202735df93cd | 24 | led2 = 0; |
pyeh9 | 2:202735df93cd | 25 | led3 = 0; |
pyeh9 | 2:202735df93cd | 26 | led4 = 0; |
pyeh9 | 2:202735df93cd | 27 | } |
pyeh9 | 0:10b365b7873b | 28 | |
pyeh9 | 0:10b365b7873b | 29 | int main() { |
pyeh9 | 2:202735df93cd | 30 | // In using this library, the user declares "Menus", which are essentially arrays of "Selections". |
pyeh9 | 2:202735df93cd | 31 | // Selections describe each individual selectable item. |
pyeh9 | 2:202735df93cd | 32 | // Selections can be tied to functions. Functions must output void and take in no arguments. |
pyeh9 | 2:202735df93cd | 33 | // Selections can also have references to child menus. |
pyeh9 | 2:202735df93cd | 34 | |
pyeh9 | 2:202735df93cd | 35 | // It makes sense to declare a root menu first, but you don't have to. |
pyeh9 | 2:202735df93cd | 36 | // Menus should have an identifier (the argument in constructor). |
pyeh9 | 2:202735df93cd | 37 | Menu rootMenu("root"); |
pyeh9 | 0:10b365b7873b | 38 | |
pyeh9 | 2:202735df93cd | 39 | // Selections are added to menus through the Menu's "add" method. |
pyeh9 | 2:202735df93cd | 40 | // If a function is to be executed when the RPG is depressed, make a REFERENCE to it. Otherwise null. |
pyeh9 | 2:202735df93cd | 41 | // The second argument is its position - the will be deprecated soon. |
pyeh9 | 2:202735df93cd | 42 | // The last is the text to display. *INCLUDE A SPACE IN THE BEGINNING, this is where the cursor goes. |
pyeh9 | 2:202735df93cd | 43 | // *This means the text is limited to 14 characters (not counting the space) with this display. |
pyeh9 | 2:202735df93cd | 44 | // **It is 14 not 15 for reasons pertaining to the implementation of the TextLCD library. |
pyeh9 | 2:202735df93cd | 45 | Menu ledMenu("LED menu"); |
pyeh9 | 2:202735df93cd | 46 | ledMenu.add(Selection(&toggleLED1, 0, NULL, " Toggle LED1")); // The function argument of selection can be added directly |
pyeh9 | 2:202735df93cd | 47 | ledMenu.add(Selection(NULL, 1, NULL, " Toggle LED2")); // It can also be set to NULL temporarily and changed later. |
pyeh9 | 2:202735df93cd | 48 | ledMenu.selections[1].fun = &toggleLED2; // Useful for when functions are methods in other classes. |
pyeh9 | 2:202735df93cd | 49 | ledMenu.add(Selection(&toggleLED3, 2, NULL, " Toggle LED3")); |
pyeh9 | 2:202735df93cd | 50 | ledMenu.add(Selection(&toggleLED4, 3, NULL, " Toggle LED4")); |
pyeh9 | 2:202735df93cd | 51 | ledMenu.add(Selection(&resetLED, 4, &rootMenu, " Go back")); // always add a Selection at the end to point to the parent |
pyeh9 | 2:202735df93cd | 52 | |
pyeh9 | 2:202735df93cd | 53 | Menu aboutMenu("About Menu"); // about menu crediting us :) |
pyeh9 | 2:202735df93cd | 54 | aboutMenu.add(Selection(NULL, 0, NULL, " Authors:")); |
pyeh9 | 2:202735df93cd | 55 | aboutMenu.add(Selection(NULL, 1, NULL, " Ben Y & Tony T")); |
pyeh9 | 2:202735df93cd | 56 | aboutMenu.add(Selection(NULL, 2, NULL, " ECE4180")); |
pyeh9 | 2:202735df93cd | 57 | aboutMenu.add(Selection(NULL, 3, &rootMenu, " Go back")); |
pyeh9 | 2:202735df93cd | 58 | |
pyeh9 | 2:202735df93cd | 59 | // Selections to the root menu should be added last |
pyeh9 | 2:202735df93cd | 60 | rootMenu.add(Selection(NULL, 0, &ledMenu, " LED MENU")); |
pyeh9 | 2:202735df93cd | 61 | rootMenu.add(Selection(NULL, 1, NULL, " Dummy menu 1")); // a dummy menu, doesn't do anything |
pyeh9 | 2:202735df93cd | 62 | rootMenu.add(Selection(NULL, 2, &aboutMenu, " About menu")); |
pyeh9 | 2:202735df93cd | 63 | |
pyeh9 | 2:202735df93cd | 64 | // Here is the heart of the system: the navigator. |
pyeh9 | 2:202735df93cd | 65 | // The navigator takes in a reference to the root, an interface, and a reference to an lcd |
pyeh9 | 2:202735df93cd | 66 | Navigator navigator(&rootMenu, rpg, &lcd); |
pyeh9 | 2:202735df93cd | 67 | |
pyeh9 | 0:10b365b7873b | 68 | while(1){ |
pyeh9 | 2:202735df93cd | 69 | navigator.poll(); // In a loop, call navigator's poll method to determine if the user is interacting with the rpg. |
pyeh9 | 0:10b365b7873b | 70 | } |
pyeh9 | 0:10b365b7873b | 71 | } |