A simple Menu Library

Dependents:   MenuExample

A Menu is light and easy to use. It's composed of a:

  • an output interface to show which program is selected,
  • a way to change the selection,
  • a way to validate the selection and launch the linked program.

If you want a example on how to use it, look at the MenuExample program (http://mbed.org/users/rominos2/code/MenuExample/)

Committer:
rominos2
Date:
Thu Sep 04 12:25:13 2014 +0000
Revision:
0:0f92518679f3
Initial Release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rominos2 0:0f92518679f3 1 /*
rominos2 0:0f92518679f3 2 Copyright (c) 2014 Romain Berrada
rominos2 0:0f92518679f3 3
rominos2 0:0f92518679f3 4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
rominos2 0:0f92518679f3 5 and associated documentation files (the "Software"), to deal in the Software without restriction,
rominos2 0:0f92518679f3 6 including without limitation the rights to use, copy, modify, merge, publish, distribute,
rominos2 0:0f92518679f3 7 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
rominos2 0:0f92518679f3 8 furnished to do so, subject to the following conditions:
rominos2 0:0f92518679f3 9
rominos2 0:0f92518679f3 10 The above copyright notice and this permission notice shall be included in all copies or
rominos2 0:0f92518679f3 11 substantial portions of the Software.
rominos2 0:0f92518679f3 12
rominos2 0:0f92518679f3 13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
rominos2 0:0f92518679f3 14 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
rominos2 0:0f92518679f3 15 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
rominos2 0:0f92518679f3 16 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rominos2 0:0f92518679f3 17 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
rominos2 0:0f92518679f3 18 */
rominos2 0:0f92518679f3 19
rominos2 0:0f92518679f3 20 #include "mbed.h"
rominos2 0:0f92518679f3 21 #include "Menu.h"
rominos2 0:0f92518679f3 22
rominos2 0:0f92518679f3 23 Menu::Program::Program(void* output_argument, int(*program)()) : _output_interface_argument(output_argument), _program(program), _next(NULL) {
rominos2 0:0f92518679f3 24 }
rominos2 0:0f92518679f3 25
rominos2 0:0f92518679f3 26 Menu::Menu() : _first(NULL) {
rominos2 0:0f92518679f3 27 }
rominos2 0:0f92518679f3 28
rominos2 0:0f92518679f3 29 void Menu::addProgram(void* output_argument, int(*program)()) {
rominos2 0:0f92518679f3 30 Program* prog = new Program(output_argument, program);
rominos2 0:0f92518679f3 31 if (_first==NULL) { // if first program added
rominos2 0:0f92518679f3 32 _first = prog;
rominos2 0:0f92518679f3 33 return;
rominos2 0:0f92518679f3 34 } else {
rominos2 0:0f92518679f3 35 Program* temp = _first;
rominos2 0:0f92518679f3 36 while (temp->_next!=NULL) temp=temp->_next;
rominos2 0:0f92518679f3 37 temp->_next = prog;
rominos2 0:0f92518679f3 38 }
rominos2 0:0f92518679f3 39 }
rominos2 0:0f92518679f3 40
rominos2 0:0f92518679f3 41 void Menu::clean() {
rominos2 0:0f92518679f3 42 Program* prg = _first;
rominos2 0:0f92518679f3 43 Program* next;
rominos2 0:0f92518679f3 44 while (prg!=NULL) {
rominos2 0:0f92518679f3 45 next = prg->_next;
rominos2 0:0f92518679f3 46 delete prg;
rominos2 0:0f92518679f3 47 prg = next;
rominos2 0:0f92518679f3 48 }
rominos2 0:0f92518679f3 49 }
rominos2 0:0f92518679f3 50
rominos2 0:0f92518679f3 51 int Menu::launch() {
rominos2 0:0f92518679f3 52 Program* choice = _first;
rominos2 0:0f92518679f3 53 bool done = false;
rominos2 0:0f92518679f3 54
rominos2 0:0f92518679f3 55 if (choice==NULL) return -1; // if no program
rominos2 0:0f92518679f3 56
rominos2 0:0f92518679f3 57 startMenu();
rominos2 0:0f92518679f3 58 displaySelectedProgram(choice->_output_interface_argument);
rominos2 0:0f92518679f3 59
rominos2 0:0f92518679f3 60 while(!done) {
rominos2 0:0f92518679f3 61 if (isSelectionChanging()) {
rominos2 0:0f92518679f3 62 // change the current choice
rominos2 0:0f92518679f3 63 choice = choice->_next;
rominos2 0:0f92518679f3 64 if (choice==NULL) choice = _first;
rominos2 0:0f92518679f3 65 displaySelectedProgram(choice->_output_interface_argument);
rominos2 0:0f92518679f3 66 }
rominos2 0:0f92518679f3 67
rominos2 0:0f92518679f3 68 if (isValidating()) {
rominos2 0:0f92518679f3 69 // validate the choice
rominos2 0:0f92518679f3 70 done = true;
rominos2 0:0f92518679f3 71 }
rominos2 0:0f92518679f3 72
rominos2 0:0f92518679f3 73 wait(0.1);
rominos2 0:0f92518679f3 74 }
rominos2 0:0f92518679f3 75
rominos2 0:0f92518679f3 76 displaySelectedProgram(NULL);
rominos2 0:0f92518679f3 77 return choice->_program(); // call the program function and return the result
rominos2 0:0f92518679f3 78 }