Library which creates a serial test console, it supports pages and menu items. The items are added and the pages are added as necessary when the user sets it up. This is a great too for creating an easy to maintain menu system, whether for a test sytem, or anything else.

Dependencies:   Terminal

Revision:
6:e992366d0684
Parent:
3:f308cd7a34ed
Child:
7:903c8e16ed25
--- a/testconsole.cpp	Sat May 02 00:41:23 2015 +0000
+++ b/testconsole.cpp	Mon May 04 14:43:35 2015 +0000
@@ -1,41 +1,53 @@
 #include "testconsole.h"
 
-TestConsole::TestConsole(const char * Name_p):
-    Name(Name_p),
+TestConsole::TestConsole(const char * name_p, PinName tx, PinName rx, int baud_rate): 
+    term(tx, rx),
+    name(name_p),
     num_pages(0),
     current_page(0)
 {       
-    
-       // term.printf("TestConsole::TestConsole('%')\n",Name);
+     term.baud(baud_rate);
+       active_page = page[current_page];
+ //       term.printf("TestConsole::TestConsole('%')\r\n",name);
         term.HideCursor();
-        page_change(current_page);
+        for(int i = 0; i < MAX_PAGES; i++) page[i] = NULL;   //init all pages to NULL
 
     }
 
-Page& TestConsole::add_page(Page const &page_p){
-term.printf("TestConsole::add_page\n");
-    if(num_pages < MAX_PAGES) {
-        page[num_pages] = page_p;
-        page[num_pages].page_num = num_pages;   //let the page know what number it is to help with lookups
+//Page& TestConsole::add_page(Page const &page_p){
+Page* TestConsole::add_page(const char * name_p){
+    if(num_pages >=  MAX_PAGES) 
+    {
+        term.printf("MAX_PAGES Exceeded adding'%s'\r\n", name_p);
+        return NULL;  //return valid reference to home page on
+
+    }
+    
+        page[num_pages] = new Page(name_p, &term);
+        Page* this_page = page[num_pages];
+        this_page->page_num = num_pages;   //let the page know what number it is to help with lookups
             
                 if(num_pages == 0) {   //if this is the first page, set it active
-                    page[num_pages].set_active();
+                    this_page->set_active();
                 }
-        term.printf("Added page '%s'\n", page[num_pages].Name);
-        return page[num_pages++];
-        }
-
-    term.printf("Failed to add page'%s'\n", page_p.Name);
-    return page[MAX_PAGES-1];  //return 0 if no error
+        //term.printf("Added page '%s'\r\n", this_page->name);
+        num_pages++;
+        return this_page;
     
     }
     
 int TestConsole::page_change(int new_page){
+            if(NULL == page[current_page]) {
+                term.printf("invalid page passed to page_chage\r\n");
+                return current_page;
+                }
             previous_page = current_page;  //save a copy of the page so we can go back
             current_page = new_page;
-            page[current_page].display();
+            active_page = page[current_page];
+            
+            active_page->display();
 
-            page[current_page].ack_active();
+            active_page->ack_active();
             return current_page;
     }
     
@@ -45,7 +57,7 @@
         page_change(previous_page);
         return 0;
     }   
-    Page* active_page = &page[current_page];
+
     for(int index=0; index < active_page->num_menuitems; index++) {
         MenuItem *item = &active_page->item[index];
         if(active_page->command_letter[index] != cmd) continue;
@@ -72,11 +84,15 @@
         }
         
         //go through the list of pages, and see if any want to become active
-        for(int index=0; index < page[current_page].num_menuitems; index++){
-            if(page[index].check_active()) page_change(index);
+        for(int index=0; index < num_pages; index++){
+            if(active_page->check_active()){
+                 page_change(index);
+                term.locate((TERMINAL_WIDTH - strlen(name))/2, 0);
+                term.printf("%s", name);
+                }
         }
         
          
-    page[current_page].update();
+    active_page->update();
     return 0;
     }