-
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 26 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 |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
|
|
||
|
|
||
|
|
||
| [submodule "libs/canopen"] | ||
| path = libs/canopen | ||
| url = ../canopen-stack.git | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #ifndef EVT_TERM_MENU | ||
| #define EVT_TERM_MENU | ||
|
|
||
| // macro for max initial item count of a main menu | ||
| #include <core/utils/terminal/menuItem.hpp> | ||
|
|
||
| namespace core::utils { | ||
| class Menu { | ||
| public: | ||
| /** | ||
| * Basic constructor takes a list a menu items | ||
| * @param items a list of menu items | ||
| */ | ||
| Menu(MenuItem** items); | ||
|
|
||
| /** | ||
| * creates a string representation of a menu, with each menu item on its own line | ||
| */ | ||
| void printStr(io::UART& uart); | ||
|
|
||
| /** | ||
| * returns number of items in menu | ||
| */ | ||
| int getCount() { | ||
| return itemCount; | ||
| } | ||
|
|
||
| /** | ||
| * replaces this instance of a menu with another menu | ||
| * @param m the menu you want to replace it with | ||
| */ | ||
| void replace(Menu m); | ||
|
|
||
| /** | ||
| * returns list of items in menu | ||
| */ | ||
| MenuItem** getItems() { | ||
| return items; | ||
| } | ||
|
|
||
| /** | ||
| * adds an item to the menu | ||
| * @param item the item to add | ||
| */ | ||
| void addItem(utils::MenuItem* item); | ||
|
|
||
| /** | ||
| * replaces the current list of items with a new one | ||
| * @param itms the list to replace current with | ||
| */ | ||
| void newItems(utils::MenuItem** itms); | ||
|
|
||
| /** | ||
| * delete an item from the list | ||
| * @param index the index of the item in the list(top of list when printing is 0) | ||
| */ | ||
| void delItem(int index); | ||
|
|
||
| /** | ||
| * checks if this menu is equivalent to another menu | ||
| * true if every menu item is equal | ||
| * @param mnu2 the menu to compare to | ||
| */ | ||
| bool equals(Menu* mnu2); | ||
|
|
||
| private: | ||
| /** | ||
| * list of all items contained in the menu | ||
| */ | ||
| MenuItem** items; | ||
|
|
||
| /** | ||
| * maximum number of items allowed in any menu | ||
| */ | ||
| int itemCount = 10; | ||
| }; | ||
| } // namespace core::utils | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| #ifndef EVT_TERM_MENUITEM | ||
| #define EVT_TERM_MENUITEM | ||
| #include <core/io/UART.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*) | ||
| using callback_t = void (*)(core::io::UART&, char** inputList, void*); | ||
|
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. Big fan of this, we should possibly consider requiring this instead of typedefs in our code, because this is far more readable. However, it should probably be in the namespace. |
||
|
|
||
| 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. 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?
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; | ||
| }; | ||
|
|
||
| class SubMenu : public MenuItem { | ||
|
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. I don't particularly like that these are in the same hpp but have different cpps. Is there any reason for them to be in the same hpp? if there isn't, please move them to separate hpps. |
||
| 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(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** items); | ||
|
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. I'm struggling to understand what make a subMenu different from a MenuItem. If they aren't significantly different we should consider if we need to care about the distinction between them.
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. additionally, if a submenu is also a menu, maybe it should inherit from menu as well? |
||
|
|
||
| /** | ||
| * 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; | ||
|
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. rename this to callback |
||
|
|
||
| /** | ||
| * context for this item, void* because it is of an abstract type | ||
| */ | ||
| 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. rename this to context |
||
| /** | ||
| * 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 | ||
| */ | ||
| void* parent; | ||
|
|
||
| /** | ||
| * terminal this is in | ||
| */ | ||
| void* term; | ||
| }; | ||
| } // namespace core::utils | ||
|
|
||
| #endif | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm confused about what we're doing here tbh. Since we are capping the size of the list to such a small number, we could just create the array statically, inside this class, instead of having the user pass the array into the object. This will both protect access and also clean up the main code.
We may want to look into using templates to set the size of the menu, but I'm not married to that.
Also see my notes on having this be a linked list instead. I think for our use case it makes far more sense.