-
Notifications
You must be signed in to change notification settings - Fork 1
FWT-58 Oh no, Its Terminal! #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3040afe
32a082e
c36a901
c4380b5
8e58a65
bba9d1f
b9eee71
3f2b8e6
a494476
ce2c918
5af230c
5e0b090
c3db65d
4615f35
6629b97
55303df
5e2c4e1
d7c16a9
bb89320
d3bebcb
1c21acd
e9c339b
9afb8ad
4299ff3
21f3d7e
d76b916
100bac1
9199f73
a6bcd56
b923207
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| #ifndef EVT_TERM_MENU | ||
| #define EVT_TERM_MENU | ||
|
|
||
| // macro for max initial item count of a main menu | ||
| #include <core/utils/terminal/menuItem.hpp> | ||
| #include <core/utils/terminal/terminal.hpp> | ||
|
|
||
| namespace core::utils { | ||
|
|
||
|
|
||
| class Menu : public MenuItem{ | ||
| public: | ||
| /** | ||
| * constructor for sub-menu sub-class | ||
| * @param parent same as menuitem | ||
| * @param term same as menuitem | ||
| * @param option same as menuitem | ||
| * @param text same as menuitem | ||
| * @param cb exit behavior callback, leave null for nothing | ||
| * @param ctx enterence behavior callback void*(leave null for nothing) | ||
| * @param items list of items in submenu | ||
| */ | ||
| SubMenu(Menu* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** items); | ||
|
|
||
| /** | ||
| * unique overridden printStr() method for sub-menus | ||
| */ | ||
| void printStr(io::UART& uart); | ||
|
|
||
| /** | ||
| * print method that displays the submenu like a menu, instead of an item | ||
| */ | ||
| void printMStr(io::UART& uart); | ||
|
|
||
| /** | ||
| * unique overridden equals() method for sub-menus | ||
| * true if every attribute is equivalent, checks all but items with == | ||
| * items is checked the same way as menu equivalence | ||
| * @param sub2 the other submenu to compare to | ||
| */ | ||
| bool equals(SubMenu* sub2); | ||
|
|
||
| /** | ||
| * returns itemCount | ||
| */ | ||
| int getCount() { | ||
| return itemCount; | ||
| } | ||
|
|
||
| /** | ||
| * returns parent | ||
| */ | ||
| void* getParent() { | ||
| return parent; | ||
| } | ||
|
|
||
| /** | ||
| * replaces current item list with provided one | ||
| * @param itms items to replace current list with | ||
| */ | ||
| void setItems(MenuItem** itms); | ||
|
|
||
| /** | ||
| * returns a list of all items contained in the submenu | ||
| */ | ||
| MenuItem** getItems() { | ||
| return sitems; | ||
| } | ||
|
|
||
| // # | ||
| // # USE THESE TO DO ANY NEEDED SETUP/CLEANUP WHEN ENTERING/EXITING A SUB MENU: | ||
| // # | ||
|
|
||
| /** | ||
| * automatic callback executor for entering | ||
| * custom behavor is stored in ctx void* | ||
| * if cb is empty will do nothing | ||
| * @param uart uart instance to use for cb | ||
| * @param args arguments for cb | ||
| */ | ||
| void enter(io::UART& uart, char** args); | ||
|
|
||
| /** | ||
| * automatic callback executor for exiting | ||
| * custom behavor is stored in cb. | ||
| * if ctx is nullptr will do nothing | ||
| * @param uart uart instance to use for cb | ||
| * @param args arguments for cb | ||
| */ | ||
| void exit(io::UART& uart, char** args); | ||
|
|
||
| private: | ||
| /** | ||
| * key value for item, this is used to select it in your commands | ||
| */ | ||
| char* option; | ||
|
|
||
| /** | ||
| * description/name of item | ||
| */ | ||
| char* text; | ||
|
|
||
| /** | ||
| * pointer to callback method for this item | ||
| */ | ||
| callback_t cb; | ||
|
|
||
| /** | ||
| * context for this item, void* because it is of an abstract type | ||
| */ | ||
| void* ctx; | ||
| /** | ||
| * the total number of items that can be contained in any sub-menu | ||
| */ | ||
| int itemCount = 10; | ||
|
|
||
| /** | ||
| * list of all items inside of the sub-menu | ||
| */ | ||
| MenuItem** sitems; | ||
|
|
||
| /** | ||
| * submenu or menu this item is in | ||
| * if nullptr the current node is the head | ||
| */ | ||
| Menu* parent; | ||
|
|
||
| /** | ||
| * terminal this is in | ||
| */ | ||
| void* term; | ||
| }; | ||
| } // namespace core::utils | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| #ifndef EVT_TERM_MENUITEM | ||
| #define EVT_TERM_MENUITEM | ||
| #include <core/io/UART.hpp> | ||
| #include <core/utils/terminal/terminal.hpp> | ||
|
|
||
| // macro for max initial item count of submenus | ||
|
|
||
| // struct used for callback functions, a uart instance to communicate over, | ||
| // a list of input strings(your input from the terminal), and a void* | ||
| // mostly a placeholder to ease handling void* to a function, when you fill with a function make sure the parameters are | ||
| // (UART,char**,void*) | ||
|
|
||
| namespace core::utils { | ||
| class MenuItem { | ||
| public: | ||
| /** | ||
| * constructor for menu item object | ||
| * takes a string for the option key, a string for the description text, | ||
| * a void pointer to the items callback method, and a void pointer to the items context | ||
| * @param parent pointer to parent node | ||
| * @param term pointer to terminal instance this item resides within | ||
| * @param option a string representing the items "key", the char/string used to select it | ||
| * @param text a short text description/name of an item | ||
| * @param cb a void pointer to this items callback method | ||
| * @param ctx a void pointer to any context information for this menu(if none provided, is NULL) | ||
| */ | ||
| MenuItem(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If many of these are going to be passed a null value commonly, they could be given default values. Default values only work for objects at the end of the initialization list, so perhaps they should be re-ordered so that things that are almost never null are at the front of the list. |
||
|
|
||
| /** | ||
| * option acessor | ||
| */ | ||
| char* getOption() { | ||
| return option; | ||
| } | ||
|
|
||
| /** | ||
| * returns parent | ||
| */ | ||
| void* getParent() { | ||
| return parent; | ||
| } | ||
|
|
||
| /** | ||
| * text acessor | ||
| */ | ||
| char* getText() { | ||
| return text; | ||
| } | ||
|
|
||
| /** | ||
| * callback acessor | ||
| */ | ||
| callback_t getcb() { | ||
| return cb; | ||
| } | ||
|
|
||
| /** | ||
| * replace this item with another one | ||
| * @param newItem the new item | ||
| */ | ||
| void replace(MenuItem* newItem); | ||
|
|
||
| /** | ||
| * context acessor | ||
| */ | ||
| void* getctx() { | ||
| return ctx; | ||
| } | ||
|
|
||
| /** | ||
| * print string representation | ||
| * @param uart the uart instance to print over | ||
| */ | ||
| void printStr(core::io::UART& uart); | ||
|
|
||
| /** | ||
| * checks if 2 items are equivalent | ||
| * true if every attribute is equivalent using == | ||
| * @param it2 a different menu item to compare to | ||
| */ | ||
| bool equals(MenuItem* it2); | ||
|
|
||
| private: | ||
| /** | ||
| * key value for item, this is used to select it in your commands | ||
| */ | ||
| char* option; | ||
|
|
||
| /** | ||
| * description/name of item | ||
| */ | ||
| char* text; | ||
|
|
||
| /** | ||
| * pointer to callback method for this item | ||
| */ | ||
| callback_t cb; | ||
|
|
||
| /** | ||
| * context for this item, void* because it is of an abstract type | ||
| */ | ||
| void* ctx; | ||
|
|
||
| /** | ||
| * submenu or menu this item is in | ||
| */ | ||
| void* parent; | ||
|
|
||
| /** | ||
| * terminal this is in | ||
| */ | ||
| void* term; | ||
| }; | ||
| } // namespace core::utils | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #ifndef EVT_TERM_STRUCTS | ||
| #define EVT_TERM_STRUCTS | ||
| #include <core/utils/terminal/terminalManager.hpp> | ||
|
|
||
| struct TerminalInterface | ||
| { | ||
| //fill with list full of pointers to terminalManager functions to circumvent | ||
| void** funclist; | ||
|
|
||
| FunctionRunner(void** funclist): funclist(funclist) {} | ||
|
|
||
| void runAll() | ||
| { | ||
|
|
||
| } | ||
| }; | ||
|
|
||
| using callback_t = void (*)(core::io::UART&, char** inputList, void*); | ||
|
|
||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #ifndef EVT_TERMINAL | ||
| #define EVT_TERMINAL | ||
|
|
||
| #include <core/io/UART.hpp> | ||
| #include <core/io/pin.hpp> | ||
| #include <core/utils/terminal/menu.hpp> | ||
| #include <core/utils/terminal/menuItem.hpp> | ||
| #include <core/utils/terminal/terminal.hpp> | ||
| #include <cstring> | ||
|
|
||
| namespace core::utils { | ||
|
|
||
| class TerminalManager { | ||
| public: | ||
| /** | ||
| * constructor for terminal object | ||
| * takes a uart object and sets up the command line terminal | ||
| * @param uart an instance of a UART object | ||
| * @param menu head node of terminal structure | ||
| */ | ||
| Terminal(io::UART& uart, Menu* menu); | ||
|
|
||
| /** | ||
| * returns uart instance used | ||
| */ | ||
| io::UART& getUART() { | ||
| return uart; | ||
| } | ||
|
|
||
| /** | ||
| * returns main menu | ||
| */ | ||
| Menu* getMenu() { | ||
| return menu; | ||
| } | ||
|
|
||
| /** | ||
| * enters the given submenu | ||
| * @param uart the uart instance to use | ||
| * @param args the arguments provided, used for enter callback, first index is still option string | ||
| * @param term void pointer to the terminal this is being done inside of(optional to leave as nullptr, just here to | ||
| * be used with callbacks) | ||
| */ | ||
| void enterSub(io::UART& uart, char** args, void* term); | ||
| /** | ||
| * sets current menu/submenu to provided value | ||
| * @param sub the submenu to replace the current one with | ||
| */ | ||
| void setMenu(SubMenu* sub); | ||
|
|
||
| /** | ||
| * checks if the terminal is still on the main menu | ||
| * OK FOR NOW< MAYBE MAKE IT DETECT WHEN HEAD INSTED OF USING A FLAG | ||
| */ | ||
| bool isMain() { | ||
| return m; | ||
| } | ||
|
|
||
| /** | ||
| * Sends a provided message over UART | ||
| * also replaces menu with provided menu | ||
| * used to reset menu to desired state and notify user | ||
| * @param message a string message to send via UART | ||
| * @param menu the menu to replace the current menu with | ||
| */ | ||
| void update(char* message, utils::Menu menu); | ||
|
|
||
| /** | ||
| * proccesses incoming UART messages | ||
| * @param holder list to store tokenized strings in, should be full of "/0" to start | ||
| */ | ||
| bool recieve(char** holder); | ||
|
|
||
| /** | ||
| * processes chosen menu item and runs its callback(if exit, exits into higher menu) | ||
| * @param tag the user input key for the item | ||
| */ | ||
| void process(char* tag, char** args); | ||
|
|
||
| // TERMINAL specific print function | ||
| void printTerm(); | ||
|
|
||
| private: | ||
| // menu instance | ||
| Menu* menu; | ||
|
|
||
| // current submenu, if there is one, nullptr otherwise | ||
| Menu* current; | ||
|
|
||
| // true if currently in main menu | ||
| bool m; | ||
|
|
||
| // uart instance this terminal is using | ||
| core::io::UART& uart; | ||
|
|
||
| // char buffer for UART entry | ||
| char buffer[99]; | ||
| }; | ||
| } // namespace core::utils | ||
|
|
||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason these are void* instead of MenuItem pointers? Isn't it always the case that the parent to a MenuItem will be another MenuItem?