This project is a DIY mbed version of your favorite childhood game
Dependencies: mbed 4DGL-uLCD-SE LCD_fonts SDFileSystem MMA8452
menu.cpp@0:bce994168592, 2019-04-14 (annotated)
- Committer:
- aklaussen
- Date:
- Sun Apr 14 01:39:35 2019 +0000
- Revision:
- 0:bce994168592
publish working code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aklaussen | 0:bce994168592 | 1 | #include "mbed.h" |
aklaussen | 0:bce994168592 | 2 | #include "uLCD_4DGL.h" |
aklaussen | 0:bce994168592 | 3 | |
aklaussen | 0:bce994168592 | 4 | #include <string> |
aklaussen | 0:bce994168592 | 5 | #include <vector> |
aklaussen | 0:bce994168592 | 6 | |
aklaussen | 0:bce994168592 | 7 | #include "menu.h" |
aklaussen | 0:bce994168592 | 8 | |
aklaussen | 0:bce994168592 | 9 | //devices declared in main |
aklaussen | 0:bce994168592 | 10 | extern AnalogIn left_pot; |
aklaussen | 0:bce994168592 | 11 | extern AnalogIn right_pot; |
aklaussen | 0:bce994168592 | 12 | extern uLCD_4DGL uLCD; |
aklaussen | 0:bce994168592 | 13 | extern bool pushed; |
aklaussen | 0:bce994168592 | 14 | |
aklaussen | 0:bce994168592 | 15 | |
aklaussen | 0:bce994168592 | 16 | //constructors |
aklaussen | 0:bce994168592 | 17 | Menu::Menu (std::string name, |
aklaussen | 0:bce994168592 | 18 | std::string label, |
aklaussen | 0:bce994168592 | 19 | void (*onClick)()) { |
aklaussen | 0:bce994168592 | 20 | this->name = name; |
aklaussen | 0:bce994168592 | 21 | this->label = label; |
aklaussen | 0:bce994168592 | 22 | this->parent = parent; |
aklaussen | 0:bce994168592 | 23 | this->onClick = onClick; |
aklaussen | 0:bce994168592 | 24 | } |
aklaussen | 0:bce994168592 | 25 | |
aklaussen | 0:bce994168592 | 26 | //static initialization |
aklaussen | 0:bce994168592 | 27 | int Menu::selected_entry = 0; |
aklaussen | 0:bce994168592 | 28 | Menu* Menu::active_menu = NULL; |
aklaussen | 0:bce994168592 | 29 | |
aklaussen | 0:bce994168592 | 30 | void Menu::go_back(){ |
aklaussen | 0:bce994168592 | 31 | active_menu = active_menu->parent; |
aklaussen | 0:bce994168592 | 32 | active_menu->draw(); |
aklaussen | 0:bce994168592 | 33 | active_menu->draw_cursor(); |
aklaussen | 0:bce994168592 | 34 | } |
aklaussen | 0:bce994168592 | 35 | void Menu::set_active(){ |
aklaussen | 0:bce994168592 | 36 | active_menu = this; |
aklaussen | 0:bce994168592 | 37 | } |
aklaussen | 0:bce994168592 | 38 | |
aklaussen | 0:bce994168592 | 39 | void Menu::draw(){ |
aklaussen | 0:bce994168592 | 40 | uLCD.cls(); |
aklaussen | 0:bce994168592 | 41 | uLCD.color(GREEN); |
aklaussen | 0:bce994168592 | 42 | uLCD.locate(0,0); |
aklaussen | 0:bce994168592 | 43 | //print each of the sub-entries |
aklaussen | 0:bce994168592 | 44 | for(int i = 0; i < this->entries.size(); i++) { |
aklaussen | 0:bce994168592 | 45 | uLCD.printf(" %s\n", this->entries[i]->label.c_str()); |
aklaussen | 0:bce994168592 | 46 | } |
aklaussen | 0:bce994168592 | 47 | } |
aklaussen | 0:bce994168592 | 48 | void Menu::clear_cursor(){ |
aklaussen | 0:bce994168592 | 49 | uLCD.locate(0, Menu::selected_entry); |
aklaussen | 0:bce994168592 | 50 | uLCD.color(BLACK); |
aklaussen | 0:bce994168592 | 51 | uLCD.printf("*"); |
aklaussen | 0:bce994168592 | 52 | } |
aklaussen | 0:bce994168592 | 53 | void Menu::draw_cursor() { |
aklaussen | 0:bce994168592 | 54 | uLCD.locate(0, Menu::selected_entry); |
aklaussen | 0:bce994168592 | 55 | uLCD.color(GREEN); |
aklaussen | 0:bce994168592 | 56 | uLCD.printf("*"); |
aklaussen | 0:bce994168592 | 57 | } |
aklaussen | 0:bce994168592 | 58 | void Menu::update_cursor() { |
aklaussen | 0:bce994168592 | 59 | int new_selection = (int)(left_pot * Menu::active_menu->entries.size()); |
aklaussen | 0:bce994168592 | 60 | if (new_selection == Menu::active_menu->entries.size()) new_selection--; |
aklaussen | 0:bce994168592 | 61 | if (new_selection != Menu::selected_entry) { |
aklaussen | 0:bce994168592 | 62 | clear_cursor(); |
aklaussen | 0:bce994168592 | 63 | selected_entry = new_selection; |
aklaussen | 0:bce994168592 | 64 | draw_cursor(); |
aklaussen | 0:bce994168592 | 65 | } |
aklaussen | 0:bce994168592 | 66 | } |
aklaussen | 0:bce994168592 | 67 | std::string Menu::run() { |
aklaussen | 0:bce994168592 | 68 | active_menu->draw(); |
aklaussen | 0:bce994168592 | 69 | draw_cursor(); |
aklaussen | 0:bce994168592 | 70 | while(1) { |
aklaussen | 0:bce994168592 | 71 | update_cursor(); |
aklaussen | 0:bce994168592 | 72 | if(pushed) { |
aklaussen | 0:bce994168592 | 73 | wait(.5); |
aklaussen | 0:bce994168592 | 74 | pushed = false; |
aklaussen | 0:bce994168592 | 75 | //if(active_menu->entries.size() == 0) |
aklaussen | 0:bce994168592 | 76 | // uLCD.printf("No Entries!"); |
aklaussen | 0:bce994168592 | 77 | if (active_menu->entries[selected_entry]->select() == 1) |
aklaussen | 0:bce994168592 | 78 | continue; |
aklaussen | 0:bce994168592 | 79 | else |
aklaussen | 0:bce994168592 | 80 | return active_menu->entries[selected_entry]->label; |
aklaussen | 0:bce994168592 | 81 | } |
aklaussen | 0:bce994168592 | 82 | } |
aklaussen | 0:bce994168592 | 83 | } |
aklaussen | 0:bce994168592 | 84 | int Menu::select() { |
aklaussen | 0:bce994168592 | 85 | if (name == "END") { |
aklaussen | 0:bce994168592 | 86 | return 0; |
aklaussen | 0:bce994168592 | 87 | } |
aklaussen | 0:bce994168592 | 88 | if (onClick != NULL) { |
aklaussen | 0:bce994168592 | 89 | onClick(); |
aklaussen | 0:bce994168592 | 90 | return 1; |
aklaussen | 0:bce994168592 | 91 | } else { |
aklaussen | 0:bce994168592 | 92 | set_active(); |
aklaussen | 0:bce994168592 | 93 | draw(); |
aklaussen | 0:bce994168592 | 94 | return 1; |
aklaussen | 0:bce994168592 | 95 | |
aklaussen | 0:bce994168592 | 96 | } |
aklaussen | 0:bce994168592 | 97 | |
aklaussen | 0:bce994168592 | 98 | } |
aklaussen | 0:bce994168592 | 99 | void Menu::add_child(Menu* child) { |
aklaussen | 0:bce994168592 | 100 | entries.push_back(child); |
aklaussen | 0:bce994168592 | 101 | child->parent = this; |
aklaussen | 0:bce994168592 | 102 | } |