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 Peihsun Yeh

Library to display menus on TextLCDs. Interaction with functions Up,Down and Select (Buttons or RPG) Based on menu-library from pyeh9

Files at this revision

API Documentation at this revision

Comitter:
pyeh9
Date:
Tue Mar 05 20:33:27 2013 +0000
Parent:
0:577f0ec71f4c
Child:
2:2654dc659298
Commit message:
second version - all necessary files in one folder;

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
Navigator.cpp Show annotated file Show diff for this revision Revisions of this file
Navigator.h Show annotated file Show diff for this revision Revisions of this file
Selection.cpp Show annotated file Show diff for this revision Revisions of this file
Selection.h Show annotated file Show diff for this revision Revisions of this file
--- a/Menu.cpp	Thu Feb 28 22:19:37 2013 +0000
+++ b/Menu.cpp	Tue Mar 05 20:33:27 2013 +0000
@@ -2,7 +2,7 @@
 #include "Menu.h"
 #include "Selection.h"
 
-Menu::Menu()
+Menu::Menu(char *id) : menuID(id)
 {
     selections.clear();
 }
--- a/Menu.h	Thu Feb 28 22:19:37 2013 +0000
+++ b/Menu.h	Tue Mar 05 20:33:27 2013 +0000
@@ -5,12 +5,16 @@
 #include "Selection.h"
 #include <vector>
 
+class Selection;
+
 class Menu {
     private:
                
     public:
         vector<Selection> selections;
-        Menu();
+        char *menuID;
+        
+        Menu(char *);
         
         void add(const Selection &toAdd);
         char *getText(int);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Navigator.cpp	Tue Mar 05 20:33:27 2013 +0000
@@ -0,0 +1,98 @@
+#include "Navigator.h"
+
+Navigator::Navigator(Menu *root, RPG &rpg, TextLCD *lcd) : activeMenu(root), rpg(rpg), lcd(lcd) 
+{
+    bottom = root->selections.size();
+    cursorPos = 0;
+    cursorLine = 1;
+    button = 0;
+    lastButton = 0;
+}
+
+void Navigator::printMenu()
+{ 
+    lcd->cls();
+    if(bottom == 1){ // the current Menu only has one selection
+        lcd->printf("%s\n", activeMenu->selections[0].selText);
+    } else {
+        if(cursorLine == 2){ // if we're at the bottom
+            lcd->printf("%s\n", activeMenu->selections[cursorPos-1].selText);
+            lcd->printf("%s\n", activeMenu->selections[cursorPos].selText);
+        } else {
+            lcd->printf("%s\n", activeMenu->selections[cursorPos].selText);
+            lcd->printf("%s\n", activeMenu->selections[cursorPos+1].selText);
+        }
+    }
+}
+
+void Navigator::printCursor()
+{   
+    if(activeMenu->selections[cursorPos].childMenu == NULL) printf("No child menu\n");
+    else printf("child menu: %s\n", activeMenu->selections[cursorPos].childMenu->menuID);
+     
+    lcd->locate(0,0);
+    if(cursorLine == 1){
+        lcd->putc('>');
+        lcd->locate(0,1);
+        lcd->putc(' ');
+    } else if(cursorLine == 2){
+        lcd->putc(' ');
+        lcd->locate(0,1);
+        lcd->putc('>');
+    }
+}
+
+void Navigator::poll()
+{
+    if((direction = rpg.dir())!=0){ //Get Dir
+        wait(0.2); 
+        if(direction == 1) moveDown();
+        else if(direction == -1) moveUp();
+    }
+       
+    if ((button = rpg.pb()) && !lastButton){ //prevents multiple selections when button is held down
+        wait(0.2);
+        if(activeMenu->selections[cursorPos].fun != NULL){
+            (activeMenu->selections[cursorPos].fun)();
+        }
+        if(activeMenu->selections[cursorPos].childMenu != NULL){
+            activeMenu = activeMenu->selections[cursorPos].childMenu;
+            bottom = activeMenu->selections.size();
+            cursorPos = 0;
+            cursorLine = 1;
+            printMenu();
+            printCursor();
+        }
+    }
+    lastButton = button;
+}
+
+void Navigator::moveUp()
+{
+    if(cursorLine == 1){
+        printMenu();
+    } else if(cursorLine == 2){
+        cursorLine = 1;
+    }
+    
+    if(cursorPos != 0){
+        cursorPos--;
+        printMenu();
+    }
+    printCursor();
+}
+
+void Navigator::moveDown()
+{
+    if(cursorLine == 1){
+        cursorLine = 2;
+    } else if(cursorLine == 2){
+        printMenu();
+    }
+    
+    if(cursorPos != (bottom-1)){
+        cursorPos++;
+        printMenu();
+    }
+    printCursor();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Navigator.h	Tue Mar 05 20:33:27 2013 +0000
@@ -0,0 +1,34 @@
+#ifndef NAVIGATOR_H
+#define NAVIGATOR_H
+
+#include "mbed.h"
+#include "Menu.h"
+#include "TextLCD.h"
+#include "RPG.h"
+
+class Navigator {   
+    private: 
+               
+    public:
+        //Navigator(Menu &, RPG &, TextLCD &);
+        Navigator(Menu *root, RPG &, TextLCD *lcd);
+        Menu *activeMenu;
+        RPG rpg;
+        TextLCD *lcd; 
+        
+        bool lastButton, button;
+        int direction;
+        
+        int bottom;
+        int cursorPos; // what selection the cursor points to
+        int cursorLine; // what line of the lcd the cursor is on. 1 = first line, 2 = second line
+        
+        void poll();
+        void moveUp();
+        void moveDown();
+        void printMenu();
+        void printCursor();
+        void update();
+};
+
+#endif 
\ No newline at end of file
--- a/Selection.cpp	Thu Feb 28 22:19:37 2013 +0000
+++ b/Selection.cpp	Tue Mar 05 20:33:27 2013 +0000
@@ -2,9 +2,5 @@
 #include "Selection.h"
 #include "TextLCD.h"
 
-Selection::Selection(void (*fun)(), int position, char* text)
-{
-    fun = fun;
-    selText = text;
-    pos = position;
-}
+Selection::Selection(void (*fun)(), int position, Menu *child, char* text) : 
+    fun(fun), selText(text), pos(position), childMenu(child) {}
--- a/Selection.h	Thu Feb 28 22:19:37 2013 +0000
+++ b/Selection.h	Tue Mar 05 20:33:27 2013 +0000
@@ -1,6 +1,10 @@
 #ifndef SELECTION_H
 #define SELECTION_H
 
+#include "Menu.h"
+
+class Menu; 
+
 class Selection {
     private:
         
@@ -8,8 +12,9 @@
         void (*fun)(); // pointer to a function to execute 
         char* selText; // selection text
         int pos; // selection position
+        Menu *childMenu; 
         
-        Selection(void (*)(), int, char *); 
+        Selection(void (*)(), int, Menu *, char *); 
         
         //debug functions 
 };