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

Committer:
glansberry
Date:
Fri Jul 24 22:18:19 2015 +0000
Revision:
9:b1fdd7ea6f72
Parent:
8:170f8c0bb5ee
Fixed bug is choosing the first page.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
glansberry 0:907d2d5e77f7 1 #ifndef __MENUITEM_H
glansberry 0:907d2d5e77f7 2 #define __MENUITEM_H
glansberry 0:907d2d5e77f7 3
glansberry 0:907d2d5e77f7 4 #include "Terminal.h"
glansberry 0:907d2d5e77f7 5
glansberry 0:907d2d5e77f7 6 extern Terminal term;
glansberry 0:907d2d5e77f7 7
glansberry 0:907d2d5e77f7 8 typedef enum {menu, heading, display, control} MenuType;
glansberry 0:907d2d5e77f7 9 typedef char * (*callback_function)(bool); // type for conciseness
glansberry 0:907d2d5e77f7 10
glansberry 0:907d2d5e77f7 11 #define MAX_NAME_LEN (80-10-10)
glansberry 6:e992366d0684 12 #define MAKE_PORT(x, io) (new Menu##io(#x, io(x))) //use this macro to create a port (it saves name as well as creatign the IO
glansberry 6:e992366d0684 13 #define REUSE_PORT(x, io, port) (new Menu##io(#x, port)) //use this macro if you created the port externally
glansberry 0:907d2d5e77f7 14
iillyyaa 3:f308cd7a34ed 15 class MenuAction;
iillyyaa 3:f308cd7a34ed 16 class Page;
iillyyaa 3:f308cd7a34ed 17
iillyyaa 3:f308cd7a34ed 18 class MenuItem {
iillyyaa 3:f308cd7a34ed 19 public:
glansberry 0:907d2d5e77f7 20 MenuItem();
iillyyaa 3:f308cd7a34ed 21 MenuItem(const char * name_p, MenuAction *action_p, int level, MenuType type_p, int target_page = -1);
iillyyaa 3:f308cd7a34ed 22 MenuItem(Page &target_page_p); //construct a menu selection item this way
iillyyaa 3:f308cd7a34ed 23 const char *name; //reference to the name
glansberry 0:907d2d5e77f7 24 int level; //0 if primary 1 or greater if this is a sub-menu
glansberry 0:907d2d5e77f7 25 MenuType type; //are we displaying something or controlling something
iillyyaa 3:f308cd7a34ed 26
iillyyaa 3:f308cd7a34ed 27 MenuAction *action; //callback for getting/setting the data
iillyyaa 3:f308cd7a34ed 28
glansberry 0:907d2d5e77f7 29 int name_len;
iillyyaa 3:f308cd7a34ed 30
glansberry 0:907d2d5e77f7 31 int data_col; //column where the data is shown
glansberry 0:907d2d5e77f7 32 int target_page; //the page to go to if called
iillyyaa 3:f308cd7a34ed 33 };
iillyyaa 3:f308cd7a34ed 34
iillyyaa 3:f308cd7a34ed 35 class MenuAction {
iillyyaa 3:f308cd7a34ed 36 public:
iillyyaa 3:f308cd7a34ed 37 MenuAction(char const *name):
iillyyaa 3:f308cd7a34ed 38 m_name(name)
iillyyaa 3:f308cd7a34ed 39 {}
glansberry 0:907d2d5e77f7 40
iillyyaa 3:f308cd7a34ed 41 char const *getName() {
iillyyaa 3:f308cd7a34ed 42 return m_name;
iillyyaa 3:f308cd7a34ed 43 }
iillyyaa 3:f308cd7a34ed 44
iillyyaa 3:f308cd7a34ed 45 virtual void getString(char *buf, int bufLen) {
iillyyaa 3:f308cd7a34ed 46 if(buf && bufLen > 0) {
iillyyaa 3:f308cd7a34ed 47 buf[0] = '\0';
iillyyaa 3:f308cd7a34ed 48 }
iillyyaa 3:f308cd7a34ed 49 }
iillyyaa 3:f308cd7a34ed 50 virtual void doAction() {}
iillyyaa 3:f308cd7a34ed 51
iillyyaa 3:f308cd7a34ed 52 private:
iillyyaa 3:f308cd7a34ed 53 char const *m_name;
iillyyaa 3:f308cd7a34ed 54 };
iillyyaa 3:f308cd7a34ed 55
iillyyaa 3:f308cd7a34ed 56
iillyyaa 3:f308cd7a34ed 57 class MenuDigitalIn: public MenuAction {
iillyyaa 3:f308cd7a34ed 58 public:
iillyyaa 3:f308cd7a34ed 59 MenuDigitalIn(char const *name, DigitalIn const & myIO):
iillyyaa 3:f308cd7a34ed 60 MenuAction(name),
iillyyaa 3:f308cd7a34ed 61 m_io(myIO)
iillyyaa 3:f308cd7a34ed 62 {}
iillyyaa 3:f308cd7a34ed 63
iillyyaa 3:f308cd7a34ed 64 virtual void getString(char *buf, int bufLen) {
iillyyaa 3:f308cd7a34ed 65 snprintf(buf, bufLen, "%d", int(m_io));
iillyyaa 3:f308cd7a34ed 66 }
iillyyaa 3:f308cd7a34ed 67 private:
iillyyaa 3:f308cd7a34ed 68 DigitalIn m_io;
iillyyaa 3:f308cd7a34ed 69 };
iillyyaa 3:f308cd7a34ed 70
iillyyaa 3:f308cd7a34ed 71 class MenuDigitalOut: public MenuAction {
iillyyaa 3:f308cd7a34ed 72 public:
iillyyaa 3:f308cd7a34ed 73 MenuDigitalOut(char const *name, DigitalOut const & myIO):
iillyyaa 3:f308cd7a34ed 74 MenuAction(name),
iillyyaa 3:f308cd7a34ed 75 m_io(myIO)
iillyyaa 3:f308cd7a34ed 76 {}
iillyyaa 3:f308cd7a34ed 77
iillyyaa 3:f308cd7a34ed 78 virtual void getString(char *buf, int bufLen) {
iillyyaa 3:f308cd7a34ed 79 snprintf(buf, bufLen, "%d", int(m_io));
iillyyaa 3:f308cd7a34ed 80 }
iillyyaa 3:f308cd7a34ed 81 virtual void doAction() {
iillyyaa 3:f308cd7a34ed 82 m_io = !m_io;
iillyyaa 3:f308cd7a34ed 83 }
iillyyaa 3:f308cd7a34ed 84 private:
iillyyaa 3:f308cd7a34ed 85 DigitalOut m_io;
iillyyaa 3:f308cd7a34ed 86 };
iillyyaa 3:f308cd7a34ed 87
glansberry 4:800a75ffea3c 88 class MenuAnalogIn: public MenuAction {
glansberry 4:800a75ffea3c 89 public:
glansberry 4:800a75ffea3c 90 MenuAnalogIn(char const *name, AnalogIn const & myIO):
glansberry 4:800a75ffea3c 91 MenuAction(name),
glansberry 4:800a75ffea3c 92 m_io(myIO)
glansberry 4:800a75ffea3c 93 {}
glansberry 4:800a75ffea3c 94
glansberry 4:800a75ffea3c 95 virtual void getString(char *buf, int bufLen) {
tomo21 8:170f8c0bb5ee 96 snprintf(buf, bufLen, "%f", m_io.read());
glansberry 4:800a75ffea3c 97 }
glansberry 4:800a75ffea3c 98
glansberry 4:800a75ffea3c 99 private:
glansberry 4:800a75ffea3c 100 AnalogIn m_io;
glansberry 4:800a75ffea3c 101 };
glansberry 4:800a75ffea3c 102
glansberry 5:4a240f717b9d 103
glansberry 5:4a240f717b9d 104 class MenuNotImplementedAction:public MenuAction {
glansberry 5:4a240f717b9d 105 public:
glansberry 5:4a240f717b9d 106 MenuNotImplementedAction(char const *name): MenuAction(name)
glansberry 5:4a240f717b9d 107 {}
glansberry 5:4a240f717b9d 108
glansberry 5:4a240f717b9d 109 virtual void getString(char *buf, int bufLen) {
glansberry 5:4a240f717b9d 110 snprintf(buf, bufLen, "N/A");
glansberry 5:4a240f717b9d 111 }
glansberry 5:4a240f717b9d 112 };
glansberry 5:4a240f717b9d 113
iillyyaa 3:f308cd7a34ed 114 class MenuTestAction: public MenuAction {
iillyyaa 3:f308cd7a34ed 115 public:
iillyyaa 3:f308cd7a34ed 116 MenuTestAction(char const *name):
iillyyaa 3:f308cd7a34ed 117 MenuAction(name),
iillyyaa 3:f308cd7a34ed 118 m_value(false)
iillyyaa 3:f308cd7a34ed 119 {}
iillyyaa 3:f308cd7a34ed 120
iillyyaa 3:f308cd7a34ed 121 virtual void getString(char *buf, int bufLen) {
iillyyaa 3:f308cd7a34ed 122 snprintf(buf, bufLen, "%d", int(m_value));
iillyyaa 3:f308cd7a34ed 123 }
iillyyaa 3:f308cd7a34ed 124 virtual void doAction() {
iillyyaa 3:f308cd7a34ed 125 m_value = !m_value;
iillyyaa 3:f308cd7a34ed 126 }
iillyyaa 3:f308cd7a34ed 127
iillyyaa 3:f308cd7a34ed 128 private:
iillyyaa 3:f308cd7a34ed 129 bool m_value;
glansberry 0:907d2d5e77f7 130 };
glansberry 0:907d2d5e77f7 131
glansberry 0:907d2d5e77f7 132
glansberry 0:907d2d5e77f7 133 #endif