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/)

Files at this revision

API Documentation at this revision

Comitter:
rominos2
Date:
Thu Sep 04 12:25:13 2014 +0000
Commit message:
Initial Release.

Changed in this revision

Menu.cpp Show annotated file Show diff for this revision Revisions of this file
Menu.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 0f92518679f3 Menu.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Menu.cpp	Thu Sep 04 12:25:13 2014 +0000
@@ -0,0 +1,78 @@
+/* 
+    Copyright (c) 2014 Romain Berrada
+    
+    Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+    and associated documentation files (the "Software"), to deal in the Software without restriction, 
+    including without limitation the rights to use, copy, modify, merge, publish, distribute, 
+    sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
+    furnished to do so, subject to the following conditions:
+ 
+    The above copyright notice and this permission notice shall be included in all copies or 
+    substantial portions of the Software.
+ 
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
+    BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
+    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "mbed.h"
+#include "Menu.h"
+
+Menu::Program::Program(void* output_argument, int(*program)()) : _output_interface_argument(output_argument), _program(program), _next(NULL) {
+}
+
+Menu::Menu() : _first(NULL) {
+}
+
+void Menu::addProgram(void* output_argument, int(*program)()) {
+    Program* prog = new Program(output_argument, program);
+    if (_first==NULL) { // if first program added
+        _first = prog;
+        return;   
+    } else {
+        Program* temp = _first;
+        while (temp->_next!=NULL) temp=temp->_next;
+        temp->_next = prog;   
+    }    
+}
+
+void Menu::clean() {
+    Program* prg = _first;
+    Program* next;
+    while (prg!=NULL) {
+        next = prg->_next;
+        delete prg;
+        prg = next;
+    }
+}
+
+int Menu::launch() {   
+    Program* choice = _first;
+    bool done = false;
+        
+    if (choice==NULL) return -1; // if no program
+    
+    startMenu();
+    displaySelectedProgram(choice->_output_interface_argument);
+           
+    while(!done) {
+        if (isSelectionChanging()) {
+            // change the current choice
+            choice = choice->_next;
+            if (choice==NULL) choice = _first;
+            displaySelectedProgram(choice->_output_interface_argument);
+        }
+        
+        if (isValidating()) {
+            // validate the choice
+            done = true;
+        }
+
+        wait(0.1);
+    }
+        
+    displaySelectedProgram(NULL);
+    return choice->_program(); // call the program function and return the result
+}
\ No newline at end of file
diff -r 000000000000 -r 0f92518679f3 Menu.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Menu.h	Thu Sep 04 12:25:13 2014 +0000
@@ -0,0 +1,93 @@
+/* 
+    Copyright (c) 2014 Romain Berrada
+    
+    Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+    and associated documentation files (the "Software"), to deal in the Software without restriction, 
+    including without limitation the rights to use, copy, modify, merge, publish, distribute, 
+    sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
+    furnished to do so, subject to the following conditions:
+ 
+    The above copyright notice and this permission notice shall be included in all copies or 
+    substantial portions of the Software.
+ 
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
+    BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
+    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#ifndef INCLUDE_MENU_H
+#define INCLUDE_MENU_H
+
+/** A simple Menu Library \n
+    A Menu is light and easy to use. It's composed of a : \n
+        - an output interface to show which program is selected, \n
+        - a way to change the selection, \n
+        - a way to validate the selection and launch the linked program. \n
+        
+    If you want a example on how to use it, look at the MenuExample program (http://mbed.org/users/rominos2/code/MenuExample/)
+*/
+class Menu {
+private:
+    /** A Program is reprensented with this nested class.
+    */
+    class Program {
+        private:
+        void* _output_interface_argument; /**< the output displayed when this program is seletected. For example, a LED Color or a message. */
+        int(*_program)(); /**< the function to call when the program is validated. */
+        Program* _next; /**< the next program in the chained list */
+        
+        Program(void* output_argument, int(*program)()); /**< Constructor of a Program */
+        
+        friend class Menu;
+    };
+    
+    Program* _first; /**< First program of the chained list. */
+    
+protected:
+    /** Start the Menu.
+        Implementation can make LED blinks or print a message on a screen for example
+    */
+    virtual void startMenu() = 0;
+    /** Implementation must links an input to know if the user change the selection.
+        For example, a switch can be checked.
+        @return 1 if the selection changes, else false.
+    */
+    virtual bool isSelectionChanging() = 0;
+    /** Implementation must links an input to know if the user validate the selection.
+        For example, a switch can be checked.
+        @return 1 if the user validates, else false.
+    */
+    virtual bool isValidating() = 0;
+    
+    /** Implementation must links an output to show the selected program has changed.
+        For example, the color of a LED can be changed or a message printed.
+        @param output_argument the argument of the selected program. If NULL, the interface must clear.
+    */
+    virtual void displaySelectedProgram(void* output_argument) = 0;
+
+public:
+    /** Construcor of the Menu.
+    */
+    Menu();
+    
+    /** Add a program to the Menu.
+        @param output_argument the output displayed when this program is seletected. For example, a LED Color or a message.
+        @param the function to call when the program is validated.
+    */
+    void addProgram(void* output_argument, int(*program)());
+    /** Free the alocated memory of the Bootstrap.
+        All program will be removed from the list.
+    */
+    void clean();
+    /** Start the Menu. \n
+        First startMenu() is called. \n
+        Then the output interface show the chosen program (with the output argument) and the Bootstrap check isSelectionChanging() and isValidating(). \n
+        Once validate, the function of the program is called. \n
+        @return the result of the program's function.
+    */
+    int launch();
+};
+
+#endif
\ No newline at end of file