From 3040afe6714d35f1bad3f38ed25758800d34856a Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Thu, 7 Nov 2024 20:58:50 -0500 Subject: [PATCH 01/30] Creation of branch for FWT-58 UART Terminal --- terminal.cpp | 0 terminal.hpp | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 terminal.cpp create mode 100644 terminal.hpp diff --git a/terminal.cpp b/terminal.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/terminal.hpp b/terminal.hpp new file mode 100644 index 000000000..e69de29bb From 32a082ed33807320b6e00905a4478e0f15f10455 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Mon, 11 Nov 2024 20:32:36 -0500 Subject: [PATCH 02/30] everything so far --- include/core/utils/terminal/menu.hpp | 21 +++++++ include/core/utils/terminal/menuItem.hpp | 33 +++++++++++ include/core/utils/terminal/terminal.hpp | 48 ++++++++++++++++ src/core/utils/terminal/menu.cpp | 10 ++++ src/core/utils/terminal/menuItem.cpp | 8 +++ src/core/utils/terminal/subMenu.cpp | 12 ++++ src/core/utils/terminal/terminal.cpp | 73 ++++++++++++++++++++++++ terminal.cpp | 0 terminal.hpp | 0 9 files changed, 205 insertions(+) create mode 100644 include/core/utils/terminal/menu.hpp create mode 100644 include/core/utils/terminal/menuItem.hpp create mode 100644 include/core/utils/terminal/terminal.hpp create mode 100644 src/core/utils/terminal/menu.cpp create mode 100644 src/core/utils/terminal/menuItem.cpp create mode 100644 src/core/utils/terminal/subMenu.cpp create mode 100644 src/core/utils/terminal/terminal.cpp delete mode 100644 terminal.cpp delete mode 100644 terminal.hpp diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp new file mode 100644 index 000000000..f520275cf --- /dev/null +++ b/include/core/utils/terminal/menu.hpp @@ -0,0 +1,21 @@ +#include +#include + + + +namespace core:utils +{ + class Menu + { + public: + + Menu(menuItem[itemCount] items); + + private: + + menuItem[itemCount] items; + + int itemCount = 10; + + } +} \ No newline at end of file diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp new file mode 100644 index 000000000..5c070235d --- /dev/null +++ b/include/core/utils/terminal/menuItem.hpp @@ -0,0 +1,33 @@ + +namespace core:utils +{ + class MenuItem + { + public: + + MenuItem(char* option, char* text, void* cb, void* ctx); + + private: + + char* option; + char* text; + void* cb; + void* ctx; + } + + class subMenu : public menuItem + { + public: + + SubMenu(char* option, char* text, void* cb, void* ctx, menuItem[itemCount] items) + + private: + + int itemCount = 10; + char* option; + char* text; + void* cb; + void* ctx; + void* items; + } +} \ No newline at end of file diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp new file mode 100644 index 000000000..851e75e85 --- /dev/null +++ b/include/core/utils/terminal/terminal.hpp @@ -0,0 +1,48 @@ +/** + * header for terminal, finish this comment later + */ + +#include +#include +#include +#include + +namespace io = core::io; + +namespace utils = core::utils; + +namespace core:utils +{ + class Terminal + { + 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 baud 9600 or 115200 + */ + Terminal(io::UART& uart, int baud); + + /** + * Sends a provided message over UART + * @param message a string message to send via UART + */ + void update(char* message); + + /** + * proccesses incoming UART messages + */ + char*[10] recieve(); + + private: + utils::Menu menu; + // UART instance + io::UART uart; + + int baud; + + } +} + + diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp new file mode 100644 index 000000000..ea8c5bc56 --- /dev/null +++ b/src/core/utils/terminal/menu.cpp @@ -0,0 +1,10 @@ +#include +#include + +namespace core:utils +{ + class Menu + { + Menu::Menu(menuItem[itemCount] items) : items(items){} + } +} \ No newline at end of file diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp new file mode 100644 index 000000000..8cf5e83f2 --- /dev/null +++ b/src/core/utils/terminal/menuItem.cpp @@ -0,0 +1,8 @@ + +namespace core:utils +{ + class MenuItem + { + MenuItem::MenuItem(char* option, char* text, void* cb, void* ctx) : ctx(ctx), cb(cb), text(text), option(option){} + } +} \ No newline at end of file diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp new file mode 100644 index 000000000..5e808ef37 --- /dev/null +++ b/src/core/utils/terminal/subMenu.cpp @@ -0,0 +1,12 @@ + +#include + + + +namespace core:utils +{ + class subMenu extends menuItem + { + SubMenu::SubMenu(char* option, char* text, void* cb, void* ctx, menuItem[itemCount] items) + } +} \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp new file mode 100644 index 000000000..ab8202a09 --- /dev/null +++ b/src/core/utils/terminal/terminal.cpp @@ -0,0 +1,73 @@ +/** + * + */ +#include +#include +#include +#include +#include + +namespace utils +{ + class Terminal + { + Terminal::Terminal(io::Uart& uart, utils::Menu menu, int baud) : baud(baud), menu(menu), uart(uart) + { + core::platform::init(); + + io::UART& uart = io::getUART(baud); + + uart.printf("Starting Terminal...") + } + + void update(char* message) + { + for(int i = 0; i < 5; i ++) + { + uart.printf("\n") + } + uart.printf(message); + } + + char*[10] recieve() + { + char buffer[100]; + char* argN[10]; + uart.gets(buffer, 100); + + int c = 0; + char hold[20]; + h = 0; + for(int i = 0; i < 100; i ++) + { + if(buffer[i] = '\0') + { + break; + } + + if(c >= 10) + { + uart.printf("ERROR TOO MANY ARGUMENTS"); + break; + } + + if(buffer[i] != ' ') + { + hold[h] = buffer[i]; + h ++; + } + else + { + argN[c] = hold; + hold[20] = {'','','','','','','','','','','','','','','','','','','',''}; + h = 0; + c ++; + } + + } + + return argN; + + } + } +} \ No newline at end of file diff --git a/terminal.cpp b/terminal.cpp deleted file mode 100644 index e69de29bb..000000000 diff --git a/terminal.hpp b/terminal.hpp deleted file mode 100644 index e69de29bb..000000000 From c36a9016885f6442bb38b18674a53e490fa1c88b Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Thu, 14 Nov 2024 14:15:15 -0500 Subject: [PATCH 03/30] nothing really changed just saving --- include/core/utils/terminal/menu.hpp | 3 --- include/core/utils/terminal/menuItem.hpp | 19 +++++++++---------- include/core/utils/terminal/terminal.hpp | 4 +--- src/core/utils/terminal/menu.cpp | 5 ++++- src/core/utils/terminal/menuItem.cpp | 7 ++++++- src/core/utils/terminal/subMenu.cpp | 5 ++++- src/core/utils/terminal/terminal.cpp | 6 +----- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index f520275cf..8685e651e 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -1,7 +1,4 @@ #include -#include - - namespace core:utils { diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 5c070235d..5f1a02126 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -1,6 +1,9 @@ namespace core:utils { + + + class MenuItem { public: @@ -13,21 +16,17 @@ namespace core:utils char* text; void* cb; void* ctx; - } + }; - class subMenu : public menuItem + class SubMenu : MenuItem { public: - SubMenu(char* option, char* text, void* cb, void* ctx, menuItem[itemCount] items) + SubMenu(char* option, char* text, void* cb, void* ctx, MenuItem[itemCount] items); private: - + int itemCount = 10; - char* option; - char* text; - void* cb; - void* ctx; - void* items; - } + MenuItem[10] items; + }; } \ No newline at end of file diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 851e75e85..3fde46c65 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -9,8 +9,6 @@ namespace io = core::io; -namespace utils = core::utils; - namespace core:utils { class Terminal @@ -22,7 +20,7 @@ namespace core:utils * @param uart an instance of a UART object * @param baud 9600 or 115200 */ - Terminal(io::UART& uart, int baud); + Terminal(io::UART& uart, utils::Menu menu); /** * Sends a provided message over UART diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index ea8c5bc56..5dde66593 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -5,6 +5,9 @@ namespace core:utils { class Menu { - Menu::Menu(menuItem[itemCount] items) : items(items){} + Menu::Menu(menuItem[itemCount] items) : items(items) + { + + } } } \ No newline at end of file diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index 8cf5e83f2..41f7c6cd6 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -1,8 +1,13 @@ +#include + namespace core:utils { class MenuItem { - MenuItem::MenuItem(char* option, char* text, void* cb, void* ctx) : ctx(ctx), cb(cb), text(text), option(option){} + MenuItem::MenuItem(char* option, char* text, void* cb, void* ctx) : ctx(ctx), cb(cb), text(text), option(option) + { + + } } } \ No newline at end of file diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index 5e808ef37..c19461bdd 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -5,8 +5,11 @@ namespace core:utils { - class subMenu extends menuItem + class SubMenu : public MenuItem { SubMenu::SubMenu(char* option, char* text, void* cb, void* ctx, menuItem[itemCount] items) + { + + } } } \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index ab8202a09..c166db061 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -11,12 +11,8 @@ namespace utils { class Terminal { - Terminal::Terminal(io::Uart& uart, utils::Menu menu, int baud) : baud(baud), menu(menu), uart(uart) + Terminal::Terminal(io::Uart& uart, utils::Menu menu) : menu(menu), uart(uart) { - core::platform::init(); - - io::UART& uart = io::getUART(baud); - uart.printf("Starting Terminal...") } From c4380b57242c396bfca063a7212186e9e58b4223 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Thu, 21 Nov 2024 20:13:27 -0500 Subject: [PATCH 04/30] created accessors, string representations, and equivalence rules for all classes neccecary made comments in hpp files --- include/core/utils/terminal/menu.hpp | 31 ++++++++- include/core/utils/terminal/menuItem.hpp | 88 ++++++++++++++++++++++-- include/core/utils/terminal/terminal.hpp | 19 +++-- src/core/utils/terminal/menu.cpp | 36 +++++++++- src/core/utils/terminal/menuItem.cpp | 43 +++++++++++- src/core/utils/terminal/subMenu.cpp | 46 ++++++++++++- src/core/utils/terminal/terminal.cpp | 20 ++++-- 7 files changed, 260 insertions(+), 23 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 8685e651e..0d4eb9499 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -1,4 +1,5 @@ #include +#include namespace core:utils { @@ -6,12 +7,38 @@ namespace core:utils { public: - Menu(menuItem[itemCount] items); + /** + * Basic constructor takes a list a menu items + * @param items a list of menu items + */ + Menu(MenuItem[itemCount] items); + + /** + * creates a string representation of a menu, with each menu item on its own line + */ + std::string toStr(); + + /** + * returns the list of menu items + */ + MenuItem[itemCount] getItems(); + + /** + * checks if this menu is equivalent to another menu + * true if every menu item is equal + */ + bool equals(Menu mnu) private: - menuItem[itemCount] items; + /** + * list of all items contained in the menu + */ + MenuItem[itemCount] items; + /** + * maximum number of items allowed in any menu + */ int itemCount = 10; } diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 5f1a02126..225d61dcc 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -1,3 +1,4 @@ +#include namespace core:utils { @@ -7,26 +8,103 @@ 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 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(std::string option, std::string text, void* cb, void* ctx = nullptr); - MenuItem(char* option, char* text, void* cb, void* ctx); + /** + * returns a strings representation of the menu item + * this is the key, followed by the text description/name + */ + std::string toStr(); + + /** + * returns the option attribute that is the items key as a string + */ + std::string getOption(); + + /** + * returns description text/name as a string + */ + std::string getText(); + + /** + * returns callback method pointer + */ + void* getcb(); + + /** + * returns context for this item + */ + void* getctx(); + + /** + * checks if 2 items are equivalent + * true if every attribute is equivalent using == + * @param it a different menu item to compare to + */ + bool equals(MenuItem it); private: + /** + * key value for item, this is used to select it in your commands + */ + std::string option; - char* option; - char* text; + /** + * description/name of item + */ + std::string text; + + /** + * pointer to callback method for this item + */ void* cb; + + /** + * context for this item, void* because it is of an abstract type + */ void* ctx; }; class SubMenu : MenuItem { public: + /** + * constructor for sub-menu sub-class + */ + SubMenu(std::string option, std::string text, void* cb, void* ctx, MenuItem[itemCount] items); + + /** + * unique overridden toStr() method for sub-menus + */ + std::string toStr(); - SubMenu(char* option, char* text, void* cb, void* ctx, MenuItem[itemCount] items); + /** + * 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 sub the other submenu to compare to + */ + bool equals(SubMenu sub); + MenuItem[itemCOunt] getItems(); private: - + /** + * 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[10] items; }; } \ No newline at end of file diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 3fde46c65..8890e0d4a 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace io = core::io; @@ -22,24 +23,34 @@ namespace core:utils */ Terminal(io::UART& uart, utils::Menu menu); + /** + * returns the uart instance of this terminal + */ + io::UART& getUART(); + + /** + * returns the menu instance that serves as the root node of the tree containing the terminal + */ + utils::Menu getMenu(); + /** * Sends a provided message over UART * @param message a string message to send via UART */ - void update(char* message); + void update(std::string message); /** * proccesses incoming UART messages */ - char*[10] recieve(); + std::string recieve(); private: + // menu instance utils::Menu menu; + // UART instance io::UART uart; - int baud; - } } diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 5dde66593..98435188e 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -1,13 +1,45 @@ #include #include +#include namespace core:utils { class Menu { - Menu::Menu(menuItem[itemCount] items) : items(items) + Menu::Menu(MenuItem[itemCount] items) : items(items) { - + + } + + std::string toStr() + { + std::string out = ""; + + for(int i = 0; i < itemCount; i ++) + { + out += MenuItem[0].toStr() + out += "\n"; + } + + return out; + } + + MenuItem[itemCount] getItems() + { + return items; + } + + bool equals(Menu mnu) : mnu(mnu) + { + MenuItem items2[itemCount] = mnu.getItems(); + for (i = 0; i < itemCount; i ++) + { + if(!(items[i].equals(items2[i]))) + { + return false; + } + } + return true; } } } \ No newline at end of file diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index 41f7c6cd6..8c1216536 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -1,13 +1,52 @@ #include - +#include namespace core:utils { class MenuItem { - MenuItem::MenuItem(char* option, char* text, void* cb, void* ctx) : ctx(ctx), cb(cb), text(text), option(option) + MenuItem::MenuItem(std::string option, std::string text, void* cb, void* ctx) : ctx(ctx), cb(cb), text(text), option(option) + { + + } + + std::string toStr() + { + std::string string = option + string += '|'; + string += text; + out += "\n" + return string + } + + std::string getOption() { + return option; + } + std::string getText() + { + return text; + } + void* getcb() + { + return cb; + } + void* getctx() + { + return ctx; + } + bool equals(MenuItem it) + { + std::string option2 = it.getOption(); + std::string text2 = it.getText(); + void* cb2 = it.getcb(); + void* ctx2 = it.getctx(); + if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) + { + return false; + } + return true; } } } \ No newline at end of file diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index c19461bdd..0ffdbcaa8 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -1,15 +1,55 @@ #include - +#include namespace core:utils { class SubMenu : public MenuItem { - SubMenu::SubMenu(char* option, char* text, void* cb, void* ctx, menuItem[itemCount] items) + SubMenu::SubMenu(std::string option, std::string text, void* cb, void* ctx, menuItem[itemCount] items) + { + + } + + std::string toStr() + { + //create string for line showing this sub-menu + std::string out = option + out += "|" + out += text; + out += "\n" + return out; + } + + MenuItem[itemCount] getItems() + { + return items; + } + + bool equals(MenuItem sub) : sub(sub) { - + //check equivalence of attributes besides items + //this section is identical to menuItem + std::string option2 = it.getOption(); + std::string text2 = it.getText(); + void* cb2 = it.getcb(); + void* ctx2 = it.getctx(); + MenuItem items2[itemCount] = it.getItems(); + if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) + { + return false; + } + + //Check items equivalence + for (i = 0; i < itemCount; i ++) + { + if(!(items[i].equals(items2[i]))) + { + return false; + } + } + return true; } } } \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index c166db061..ce4191170 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include namespace utils { @@ -16,7 +16,17 @@ namespace utils uart.printf("Starting Terminal...") } - void update(char* message) + io::UART& getUART() + { + return uart; + } + + utils::Menu getMenu() + { + return menu; + } + + void update(std::string message) { for(int i = 0; i < 5; i ++) { @@ -25,14 +35,14 @@ namespace utils uart.printf(message); } - char*[10] recieve() + std::string[10] recieve() { char buffer[100]; - char* argN[10]; + std::string argN[10]; uart.gets(buffer, 100); int c = 0; - char hold[20]; + char hold[20] = {'','','','','','','','','','','','','','','','','','','',''}; h = 0; for(int i = 0; i < 100; i ++) { From 8e58a653cceadca73f618ac0abb9f0c1ecaa0cb4 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Thu, 28 Nov 2024 15:27:28 -0500 Subject: [PATCH 05/30] added to cmake path --- CMakeLists.txt | 6 +++++- samples/CMakeLists.txt | 1 + samples/terminal/CMakeLists.txt | 3 +++ samples/terminal/main.cpp | 27 +++++++++++++++++++++++++++ src/core/utils/terminal/menu.cpp | 14 +++++++------- src/core/utils/terminal/menuItem.cpp | 10 +++++----- src/core/utils/terminal/subMenu.cpp | 17 ++++++++--------- src/core/utils/terminal/terminal.cpp | 8 ++++---- 8 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 samples/terminal/CMakeLists.txt create mode 100644 samples/terminal/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b689707c..9b0be4979 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,11 @@ target_sources(${PROJECT_NAME} PRIVATE src/core/dev/RTCTimer.cpp src/core/dev/storage/M24C32.cpp src/core/dev/Thermistor.cpp - src/core/utils/log.cpp) + src/core/utils/log.cpp + src/core/utils/terminal/menu.cpp + src/core/utils/terminal/menuItem.cpp + src/core/utils/terminal/subMenu.cpp + src/core/utils/terminal/terminal.cpp) get_directory_property(COMPDEFS COMPILE_DEFINITIONS) if(COMPDEFS MATCHES "(.*)STM32F3xx(.*)") diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 2949ab704..10f9f4863 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -23,3 +23,4 @@ add_subdirectory(rtos) add_subdirectory(thermistor) add_subdirectory(timer) add_subdirectory(spi) +add_subdirectory(terminal) diff --git a/samples/terminal/CMakeLists.txt b/samples/terminal/CMakeLists.txt new file mode 100644 index 000000000..8429e9846 --- /dev/null +++ b/samples/terminal/CMakeLists.txt @@ -0,0 +1,3 @@ +include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/evt-core_build.cmake) + +make_exe(terminal main.cpp) diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp new file mode 100644 index 000000000..4c7030f0b --- /dev/null +++ b/samples/terminal/main.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace io = core::io; +namespace utils = core::dev::utils; + +int main() +{ + + + utils::Terminal::MenuItem print = MenuItem::MenuItem(); + utils::Terminal::MenuItem send = MenuItem::MenuItem(); + utils::Terminal::SubMenu sub = MenuItem::SubMenu(); + utils::Terminal::Menu menu = Menu::Menu(); + utils::Terminal::Terminal term = Terminal::Terminal(); +} + + +std:string print(utils::Terminal::Terminal term) +{ + io::UART uart = term.getUART(); +} \ No newline at end of file diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 98435188e..4f6be75d5 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -1,12 +1,12 @@ -#include -#include +#include +#include #include -namespace core:utils +namespace utils { class Menu { - Menu::Menu(MenuItem[itemCount] items) : items(items) + Menu::Menu(utils::Terminal::MenuItem[itemCount] items) : items(items) { } @@ -17,21 +17,21 @@ namespace core:utils for(int i = 0; i < itemCount; i ++) { - out += MenuItem[0].toStr() + out += items[0].toStr() out += "\n"; } return out; } - MenuItem[itemCount] getItems() + utils::Terminal::MenuItem[itemCount] getItems() { return items; } bool equals(Menu mnu) : mnu(mnu) { - MenuItem items2[itemCount] = mnu.getItems(); + utils::Terminal::MenuItem items2[itemCount] = mnu.getItems(); for (i = 0; i < itemCount; i ++) { if(!(items[i].equals(items2[i]))) diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index 8c1216536..99e7787d0 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -1,7 +1,7 @@ -#include +#include #include -namespace core:utils +namespace utils { class MenuItem { @@ -12,11 +12,11 @@ namespace core:utils std::string toStr() { - std::string string = option + std::string string = option ; string += '|'; string += text; - out += "\n" - return string + out += "\n"; + return string; } std::string getOption() diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index 0ffdbcaa8..13e77c6cf 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -1,13 +1,12 @@ - -#include +#include #include -namespace core:utils +namespace utils { class SubMenu : public MenuItem { - SubMenu::SubMenu(std::string option, std::string text, void* cb, void* ctx, menuItem[itemCount] items) + SubMenu::SubMenu(std::string option, std::string text, void* cb, void* ctx, utils::Terminal::MenuItem[itemCount] items) { } @@ -15,14 +14,14 @@ namespace core:utils std::string toStr() { //create string for line showing this sub-menu - std::string out = option - out += "|" + std::string out = option ; + out += "|"; out += text; - out += "\n" + out += "\n"; return out; } - MenuItem[itemCount] getItems() + utils::Terminal::MenuItem[itemCount] getItems() { return items; } @@ -35,7 +34,7 @@ namespace core:utils std::string text2 = it.getText(); void* cb2 = it.getcb(); void* ctx2 = it.getctx(); - MenuItem items2[itemCount] = it.getItems(); + utils::Terminal::MenuItem items2[itemCount] = it.getItems(); if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { return false; diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index ce4191170..1b3df4b58 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -4,16 +4,16 @@ #include #include #include -#include +#include #include namespace utils { class Terminal { - Terminal::Terminal(io::Uart& uart, utils::Menu menu) : menu(menu), uart(uart) + Terminal::Terminal(io::Uart& uart, utils::Terminal::Menu menu) : menu(menu), uart(uart) { - uart.printf("Starting Terminal...") + uart.printf("Starting Terminal..."); } io::UART& getUART() @@ -30,7 +30,7 @@ namespace utils { for(int i = 0; i < 5; i ++) { - uart.printf("\n") + uart.printf("\n"); } uart.printf(message); } From bba9d1f9934be423b603771291b6bf9434fa71aa Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Mon, 13 Jan 2025 18:26:37 -0500 Subject: [PATCH 06/30] working on testing implementation for terminal --- include/core/utils/terminal/terminal.hpp | 21 +++++++++++++++++++++ samples/terminal/main.cpp | 23 ++++++++++++++++++++--- src/core/utils/terminal/terminal.cpp | 20 ++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 8890e0d4a..e8b95f2c4 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -33,6 +33,23 @@ namespace core:utils */ utils::Menu getMenu(); + /** + * returns the current page of the menu, only if the menu is not currently on the main page + * */ + utils::MenuItem getCurrent(); + + /** + * checks if the terminal is still on the main menu + */ + bool isMain(); + + /** + * takes a command that has been processed into a list of strings, performs the command + * if the item specified has a callback function, this is what runs it + * if the item specified is a submenu, this is what opens it + */ + void runCmd(std::string[10] cmd); + /** * Sends a provided message over UART * @param message a string message to send via UART @@ -48,6 +65,10 @@ namespace core:utils // menu instance utils::Menu menu; + utils::SubMenu current; + + bool m; + // UART instance io::UART uart; diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index 4c7030f0b..93e2120ce 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -11,17 +11,34 @@ namespace utils = core::dev::utils; int main() { - + //core setup + core::platform::init(); + + //UART setup + io::UART& uart = io::getUART(9600); + //make some items utils::Terminal::MenuItem print = MenuItem::MenuItem(); utils::Terminal::MenuItem send = MenuItem::MenuItem(); utils::Terminal::SubMenu sub = MenuItem::SubMenu(); utils::Terminal::Menu menu = Menu::Menu(); utils::Terminal::Terminal term = Terminal::Terminal(); + + printTerm(uart, term); + + + return 0 +} + +//This print is specifically for MENUITEMS, SUBMENUS, and MENUS +void print(io:UART uart, void* item) +{ + uart.printf(item.toStr()); } -std:string print(utils::Terminal::Terminal term) +//TERMINAL specific print function +void printTerm(io:UART uart, utils:Terminal::Terminal term) { - io::UART uart = term.getUART(); + uart.printf(term.getMenu().toStr()); } \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index 1b3df4b58..c23ab8d37 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include namespace utils @@ -26,6 +28,24 @@ namespace utils return menu; } + utils::SubMenu getCurrent() + { + return current; + } + + bool isMain() + { + return m; + } + + void runCmd(std::string[10] cmd) + { + char tag = cmd[0]; + char* args = cmd[1:]; + + + } + void update(std::string message) { for(int i = 0; i < 5; i ++) From b9eee718d298b67c21238cad718066358ff9a1ec Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Mon, 13 Jan 2025 19:54:12 -0500 Subject: [PATCH 07/30] more work on making example implementation --- include/core/utils/terminal/menu.hpp | 5 ++++ include/core/utils/terminal/menuItem.hpp | 13 +++++++++- samples/terminal/main.cpp | 30 ++++++++++++++++++++++++ src/core/utils/terminal/menu.cpp | 5 ++++ src/core/utils/terminal/subMenu.cpp | 5 ++++ 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 0d4eb9499..912c1c557 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -23,6 +23,11 @@ namespace core:utils */ MenuItem[itemCount] getItems(); + /** + * returns itemCount + */ + int getCount(); + /** * checks if this menu is equivalent to another menu * true if every menu item is equal diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 225d61dcc..13399c40b 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -35,6 +35,8 @@ namespace core:utils */ std::string getText(); + + /** * returns callback method pointer */ @@ -94,6 +96,15 @@ namespace core:utils * @param sub the other submenu to compare to */ bool equals(SubMenu sub); + + /** + * returns itemCount + */ + int getCount(); + + /** + * returns a list of all items contained in the submenu + */ MenuItem[itemCOunt] getItems(); private: @@ -105,6 +116,6 @@ namespace core:utils /** * list of all items inside of the sub-menu */ - MenuItem[10] items; + MenuItem[itemCount] items; }; } \ No newline at end of file diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index 93e2120ce..ca2ed5fb8 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -24,9 +24,39 @@ int main() utils::Terminal::Menu menu = Menu::Menu(); utils::Terminal::Terminal term = Terminal::Terminal(); + term. + printTerm(uart, term); + + while(true) + { + std::string[10] args = term.recieve(); + + utils::Terminal::MenuItem[10] items; + + int c = 0; + + if(term.isMain()) + { + items = term.getMenu().getItems(); + c = term.getMenu().getCount(); + } + else + { + items = term.getCurrent().getItems(); + c = term.getCurrent().getCount() + } + + for(int i = 0; i < c; i ++) + { + + } + + + } + return 0 } diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 4f6be75d5..18d457ea5 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -24,6 +24,11 @@ namespace utils return out; } + int getCount() + { + return itemCount; + } + utils::Terminal::MenuItem[itemCount] getItems() { return items; diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index 13e77c6cf..99e866183 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -26,6 +26,11 @@ namespace utils return items; } + int getCount() + { + return itemCount; + } + bool equals(MenuItem sub) : sub(sub) { //check equivalence of attributes besides items From 3f2b8e6387fee82eabab33397ff9daf4c684926b Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Mon, 13 Jan 2025 20:18:25 -0500 Subject: [PATCH 08/30] saving work --- samples/terminal/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index ca2ed5fb8..a65e69360 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -51,7 +51,7 @@ int main() for(int i = 0; i < c; i ++) { - + } From a494476d1112376a7c1fd2774afffefcfccb4255 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Thu, 16 Jan 2025 17:04:04 -0500 Subject: [PATCH 09/30] fixing commmand running --- include/core/utils/terminal/terminal.hpp | 10 ++--- samples/terminal/main.cpp | 55 ++++++++++++++++++------ src/core/utils/terminal/terminal.cpp | 12 +++--- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index e8b95f2c4..a816f98d9 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -39,16 +39,14 @@ namespace core:utils utils::MenuItem getCurrent(); /** - * checks if the terminal is still on the main menu + * sets current submenu to provided value */ - bool isMain(); + void setCurrent(); /** - * takes a command that has been processed into a list of strings, performs the command - * if the item specified has a callback function, this is what runs it - * if the item specified is a submenu, this is what opens it + * checks if the terminal is still on the main menu */ - void runCmd(std::string[10] cmd); + bool isMain(); /** * Sends a provided message over UART diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index a65e69360..3995a134f 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -18,22 +18,18 @@ int main() io::UART& uart = io::getUART(9600); //make some items - utils::Terminal::MenuItem print = MenuItem::MenuItem(); - utils::Terminal::MenuItem send = MenuItem::MenuItem(); - utils::Terminal::SubMenu sub = MenuItem::SubMenu(); - utils::Terminal::Menu menu = Menu::Menu(); - utils::Terminal::Terminal term = Terminal::Terminal(); - - term. + utils::Terminal::MenuItem print = MenuItem::MenuItem("p","Print, takes only key", printCB, nullptr); + utils::Terminal::MenuItem send = MenuItem::MenuItem("s","Send, takes key and message", sendCB, nullptr); + utils::Terminal::SubMenu sub = MenuItem::SubMenu("b", "SubMenu", enterCB, nullptr, {print,send}); + utils::Terminal::MenuItem[2] items = {print, send, sub}; + utils::Terminal::Menu menu = Menu::Menu(items); + utils::Terminal::Terminal term = Terminal::Terminal(uart, menu); printTerm(uart, term); - while(true) { - std::string[10] args = term.recieve(); - utils::Terminal::MenuItem[10] items; int c = 0; @@ -42,31 +38,64 @@ int main() { items = term.getMenu().getItems(); c = term.getMenu().getCount(); + term.update(term.getMenu().toStr()); } else { items = term.getCurrent().getItems(); - c = term.getCurrent().getCount() + c = term.getCurrent().getCount(); + term.update(term.getCurrent().toStr()); } + + std::string[10] inputList = term.recieve(); + std::string tag = inputList[0]; + std::string[10] args = inputList[1:]; + + void (*cb)(io::UART, void*, utils::Terminal::Terminal); for(int i = 0; i < c; i ++) { - + if(tag = items[i].getOption()) + { + cb = items[i].getcb(); + } } + if(tag = "b") + { + cb(uart, sub, term); + } + else + { + cb(uart, args, term); + } } return 0 } +void printCB(io::UART uart, void* args, utils::Terminal::Terminal term) +{ + uart.printf("Test Message"); +} + +void sendCB(io::UART uart, void* args, utils::Terminal::Terminal term) +{ + uart.printf(args[0]); +} + +void enterCB(io::UART uart, void* sub, utils::Terminal::Terminal term) +{ + term.setCurrent(sub); +} + //This print is specifically for MENUITEMS, SUBMENUS, and MENUS void print(io:UART uart, void* item) { uart.printf(item.toStr()); } - //TERMINAL specific print function void printTerm(io:UART uart, utils:Terminal::Terminal term) { diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index c23ab8d37..5f653b940 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -33,19 +33,17 @@ namespace utils return current; } - bool isMain() + void setCurrent(utils::Terminal::SubMenu sub) { - return m; + current = sub; } - void runCmd(std::string[10] cmd) + bool isMain() { - char tag = cmd[0]; - char* args = cmd[1:]; - - + return m; } + void update(std::string message) { for(int i = 0; i < 5; i ++) From ce2c91800f325fd4d4a84f4d4fd6283487907b6e Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Fri, 17 Jan 2025 11:15:13 -0500 Subject: [PATCH 10/30] fixing compiling errors --- include/core/utils/terminal/menu.hpp | 25 +++++++++++++++--------- include/core/utils/terminal/menuItem.hpp | 19 ++++++++++++------ include/core/utils/terminal/terminal.hpp | 18 ++++++++--------- src/core/utils/terminal/menu.cpp | 8 ++++---- src/core/utils/terminal/menuItem.cpp | 2 +- src/core/utils/terminal/subMenu.cpp | 10 +++++----- src/core/utils/terminal/terminal.cpp | 6 ++++-- 7 files changed, 52 insertions(+), 36 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 912c1c557..7245b7d3e 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -1,7 +1,12 @@ -#include +#ifndef EVT_TERM_MENU +#define EVT_TERM_MENU + +//macro for max initial item count of a main menu +#define MITEMCOUNT 10 +#include #include -namespace core:utils +namespace core::utils { class Menu { @@ -11,7 +16,7 @@ namespace core:utils * Basic constructor takes a list a menu items * @param items a list of menu items */ - Menu(MenuItem[itemCount] items); + Menu(MenuItem items[MITEMCOUNT]); /** * creates a string representation of a menu, with each menu item on its own line @@ -21,7 +26,7 @@ namespace core:utils /** * returns the list of menu items */ - MenuItem[itemCount] getItems(); + MenuItem* getItems(); /** * returns itemCount @@ -32,19 +37,21 @@ namespace core:utils * checks if this menu is equivalent to another menu * true if every menu item is equal */ - bool equals(Menu mnu) + bool equals(Menu mnu); private: /** * list of all items contained in the menu */ - MenuItem[itemCount] items; + MenuItem items[MITEMCOUNT]; /** * maximum number of items allowed in any menu */ - int itemCount = 10; + int itemCount = MITEMCOUNT; + + }; +} - } -} \ No newline at end of file +#endif \ No newline at end of file diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 13399c40b..ebe87aefc 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -1,6 +1,11 @@ +#ifndef EVT_TERM_MENUITEM +#define EVT_TERM_MENUITEM + +//macro for max initial item count of submenus +#define ITEMCOUNT 10 #include -namespace core:utils +namespace core::utils { @@ -82,7 +87,7 @@ namespace core:utils /** * constructor for sub-menu sub-class */ - SubMenu(std::string option, std::string text, void* cb, void* ctx, MenuItem[itemCount] items); + SubMenu(std::string option, std::string text, void* cb, void* ctx, MenuItem items[ITEMCOUNT]); /** * unique overridden toStr() method for sub-menus @@ -105,17 +110,19 @@ namespace core:utils /** * returns a list of all items contained in the submenu */ - MenuItem[itemCOunt] getItems(); + MenuItem* getItems(); private: /** * the total number of items that can be contained in any sub-menu */ - int itemCount = 10; + int itemCount = ITEMCOUNT; /** * list of all items inside of the sub-menu */ - MenuItem[itemCount] items; + MenuItem items[ITEMCOUNT]; }; -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index a816f98d9..194635f6a 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -1,17 +1,17 @@ -/** - * header for terminal, finish this comment later - */ +#ifndef EVT_TERMINAL +#define EVT_TERMINAL #include #include #include -#include +#include #include -namespace io = core::io; - -namespace core:utils +namespace core::utils { + + namespace io = core::io; + class Terminal { public: @@ -70,7 +70,7 @@ namespace core:utils // UART instance io::UART uart; - } + }; } - +#endif \ No newline at end of file diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 18d457ea5..77826d1be 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -6,7 +6,7 @@ namespace utils { class Menu { - Menu::Menu(utils::Terminal::MenuItem[itemCount] items) : items(items) + utils::Menu::Menu(core::utils::MenuItem item[ITEMCOUNT]) : items(items) { } @@ -15,7 +15,7 @@ namespace utils { std::string out = ""; - for(int i = 0; i < itemCount; i ++) + for(int i = 0; i < getCount(); i ++) { out += items[0].toStr() out += "\n"; @@ -29,14 +29,14 @@ namespace utils return itemCount; } - utils::Terminal::MenuItem[itemCount] getItems() + core::utils::MenuItem[itemCount] getItems() { return items; } bool equals(Menu mnu) : mnu(mnu) { - utils::Terminal::MenuItem items2[itemCount] = mnu.getItems(); + core::utils::MenuItem items2[itemCount] = mnu.getItems(); for (i = 0; i < itemCount; i ++) { if(!(items[i].equals(items2[i]))) diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index 99e7787d0..6bb748806 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -5,7 +5,7 @@ namespace utils { class MenuItem { - MenuItem::MenuItem(std::string option, std::string text, void* cb, void* ctx) : ctx(ctx), cb(cb), text(text), option(option) + utils::MenuItem::MenuItem(std::string option, std::string text, void* cb, void* ctx) : ctx(ctx), cb(cb), text(text), option(option) { } diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index 99e866183..3a24e9bed 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -4,9 +4,9 @@ namespace utils { - class SubMenu : public MenuItem + class SubMenu : public core::utils::MenuItem { - SubMenu::SubMenu(std::string option, std::string text, void* cb, void* ctx, utils::Terminal::MenuItem[itemCount] items) + utils::SubMenu::SubMenu(std::string option, std::string text, void* cb, void* ctx, core::utils::MenuItem[itemCount] items) { } @@ -21,7 +21,7 @@ namespace utils return out; } - utils::Terminal::MenuItem[itemCount] getItems() + core::utils::MenuItem[itemCount] getItems() { return items; } @@ -31,7 +31,7 @@ namespace utils return itemCount; } - bool equals(MenuItem sub) : sub(sub) + bool equals(core::utils::MenuItem sub) : sub(sub) { //check equivalence of attributes besides items //this section is identical to menuItem @@ -39,7 +39,7 @@ namespace utils std::string text2 = it.getText(); void* cb2 = it.getcb(); void* ctx2 = it.getctx(); - utils::Terminal::MenuItem items2[itemCount] = it.getItems(); + core::utils::MenuItem items2[itemCount] = it.getItems(); if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { return false; diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index 5f653b940..89fcac9d2 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -9,11 +9,13 @@ #include #include +namespace io = core::io; + namespace utils { class Terminal { - Terminal::Terminal(io::Uart& uart, utils::Terminal::Menu menu) : menu(menu), uart(uart) + utils::Terminal::Terminal(io::Uart& uart, core::utils::Menu menu) : menu(menu), uart(uart) { uart.printf("Starting Terminal..."); } @@ -33,7 +35,7 @@ namespace utils return current; } - void setCurrent(utils::Terminal::SubMenu sub) + void setCurrent(utils::terminal::SubMenu sub) { current = sub; } From 5af230ceab364a9b1d5c26888b9063da49a86296 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Mon, 20 Jan 2025 15:51:47 -0500 Subject: [PATCH 11/30] error fixing still --- include/core/utils/terminal/menu.hpp | 8 +- include/core/utils/terminal/menuItem.hpp | 58 +++++----- include/core/utils/terminal/terminal.hpp | 16 +-- samples/terminal/main.cpp | 10 +- src/core/utils/terminal/menu.cpp | 69 ++++++------ src/core/utils/terminal/menuItem.cpp | 67 +++++------- src/core/utils/terminal/subMenu.cpp | 87 ++++++++------- src/core/utils/terminal/terminal.cpp | 128 +++++++++++------------ 8 files changed, 209 insertions(+), 234 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 7245b7d3e..2c74fbde5 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -2,7 +2,7 @@ #define EVT_TERM_MENU //macro for max initial item count of a main menu -#define MITEMCOUNT 10 +const int MITEMCOUNT = 10; #include #include @@ -16,7 +16,7 @@ namespace core::utils * Basic constructor takes a list a menu items * @param items a list of menu items */ - Menu(MenuItem items[MITEMCOUNT]); + Menu(MenuItem* items); /** * creates a string representation of a menu, with each menu item on its own line @@ -39,12 +39,12 @@ namespace core::utils */ bool equals(Menu mnu); - private: + protected: /** * list of all items contained in the menu */ - MenuItem items[MITEMCOUNT]; + MenuItem* items; /** * maximum number of items allowed in any menu diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index ebe87aefc..d835b65d3 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -2,14 +2,11 @@ #define EVT_TERM_MENUITEM //macro for max initial item count of submenus -#define ITEMCOUNT 10 +const int ITEMCOUNT = 10; #include namespace core::utils { - - - class MenuItem { public: @@ -24,34 +21,16 @@ namespace core::utils */ MenuItem(std::string option, std::string text, void* cb, void* ctx = nullptr); - /** - * returns a strings representation of the menu item - * this is the key, followed by the text description/name - */ - std::string toStr(); - - /** - * returns the option attribute that is the items key as a string - */ std::string getOption(); - - /** - * returns description text/name as a string - */ - std::string getText(); - + std::string getText(); - /** - * returns callback method pointer - */ void* getcb(); - /** - * returns context for this item - */ void* getctx(); + std::string toStr(); + /** * checks if 2 items are equivalent * true if every attribute is equivalent using == @@ -59,7 +38,7 @@ namespace core::utils */ bool equals(MenuItem it); - private: + protected: /** * key value for item, this is used to select it in your commands */ @@ -81,13 +60,13 @@ namespace core::utils void* ctx; }; - class SubMenu : MenuItem + class SubMenu : public MenuItem { public: /** * constructor for sub-menu sub-class */ - SubMenu(std::string option, std::string text, void* cb, void* ctx, MenuItem items[ITEMCOUNT]); + SubMenu(std::string option, std::string text, void* cb, void* ctx, MenuItem* items); /** * unique overridden toStr() method for sub-menus @@ -112,7 +91,26 @@ namespace core::utils */ MenuItem* getItems(); - private: + protected: + /** + * key value for item, this is used to select it in your commands + */ + std::string option; + + /** + * description/name of item + */ + std::string text; + + /** + * pointer to callback method for this item + */ + void* 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 */ @@ -121,7 +119,7 @@ namespace core::utils /** * list of all items inside of the sub-menu */ - MenuItem items[ITEMCOUNT]; + MenuItem* items; }; } diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 194635f6a..9471837d2 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -6,12 +6,12 @@ #include #include #include +#include + namespace core::utils { - namespace io = core::io; - class Terminal { public: @@ -36,12 +36,13 @@ namespace core::utils /** * returns the current page of the menu, only if the menu is not currently on the main page * */ - utils::MenuItem getCurrent(); + utils::SubMenu getCurrent(); /** * sets current submenu to provided value + * @param sub the submenu to replace the current one with */ - void setCurrent(); + void setCurrent(SubMenu sub); /** * checks if the terminal is still on the main menu @@ -57,9 +58,9 @@ namespace core::utils /** * proccesses incoming UART messages */ - std::string recieve(); + std::array recieve(); - private: + protected: // menu instance utils::Menu menu; @@ -67,8 +68,9 @@ namespace core::utils bool m; + core::io::UART& uart; + // UART instance - io::UART uart; }; } diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index 3995a134f..2e48b7e8d 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -7,7 +7,7 @@ #include namespace io = core::io; -namespace utils = core::dev::utils; +namespace utils = core::utils; int main() { @@ -75,17 +75,17 @@ int main() return 0 } -void printCB(io::UART uart, void* args, utils::Terminal::Terminal term) +void printCB(io::UART uart, void* args, utils::terminal::Terminal term) { uart.printf("Test Message"); } -void sendCB(io::UART uart, void* args, utils::Terminal::Terminal term) +void sendCB(io::UART uart, void* args, utils::terminal::Terminal term) { uart.printf(args[0]); } -void enterCB(io::UART uart, void* sub, utils::Terminal::Terminal term) +void enterCB(io::UART uart, void* sub, utils::terminal::Terminal term) { term.setCurrent(sub); } @@ -97,7 +97,7 @@ void print(io:UART uart, void* item) } //TERMINAL specific print function -void printTerm(io:UART uart, utils:Terminal::Terminal term) +void printTerm(io:UART uart, core::utils:terminal::Terminal term) { uart.printf(term.getMenu().toStr()); } \ No newline at end of file diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 77826d1be..f3a2f2e22 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -2,49 +2,48 @@ #include #include -namespace utils + +namespace core::utils { - class Menu + Menu::Menu(MenuItem* items) : items(items) { - utils::Menu::Menu(core::utils::MenuItem item[ITEMCOUNT]) : items(items) - { - - } - - std::string toStr() - { - std::string out = ""; - for(int i = 0; i < getCount(); i ++) - { - out += items[0].toStr() - out += "\n"; - } + } - return out; - } + std::string Menu::toStr() + { + std::string out = ""; + MenuItem* items = this->items; + for(int i = 0; i < MITEMCOUNT; i ++) + { + out += items[0].toStr(); + out += "\n"; + } + + return out; + } - int getCount() - { - return itemCount; - } + int Menu::getCount() + { + return MITEMCOUNT; + } - core::utils::MenuItem[itemCount] getItems() - { - return items; - } + MenuItem* Menu::getItems() + { + return this->items; + } - bool equals(Menu mnu) : mnu(mnu) + bool Menu::equals(Menu mnu) + { + MenuItem* items2; + std::copy(items2[0],items2[MITEMCOUNT-1],(mnu.getItems())[0]); + for (int i = 0; i < MITEMCOUNT; i ++) + { + if(!(items[i].equals(items2[i]))) { - core::utils::MenuItem items2[itemCount] = mnu.getItems(); - for (i = 0; i < itemCount; i ++) - { - if(!(items[i].equals(items2[i]))) - { - return false; - } - } - return true; + return false; } + } + return true; } } \ No newline at end of file diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index 6bb748806..2423740f2 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -1,52 +1,33 @@ #include #include -namespace utils +namespace core::utils { - class MenuItem - { - utils::MenuItem::MenuItem(std::string option, std::string text, void* cb, void* ctx) : ctx(ctx), cb(cb), text(text), option(option) - { - - } - std::string toStr() - { - std::string string = option ; - string += '|'; - string += text; - out += "\n"; - return string; - } + MenuItem::MenuItem(std::string option, std::string text, void* cb, void* ctx) : option(option), text(text), cb(cb), ctx(ctx) + { + + } - std::string getOption() - { - return option; - } - std::string getText() - { - return text; - } - void* getcb() - { - return cb; - } - void* getctx() - { - return ctx; - } + std::string MenuItem::toStr() + { + std::string string = this->option; + string += ('|'); + string += (this->text); + string += ("\n"); + return string; + } - bool equals(MenuItem it) - { - std::string option2 = it.getOption(); - std::string text2 = it.getText(); - void* cb2 = it.getcb(); - void* ctx2 = it.getctx(); - if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) - { - return false; - } - return true; - } + bool MenuItem::equals(MenuItem it) + { + std::string option2 = it.getOption(); + std::string text2 = it.getText(); + void* cb2 = it.getcb(); + void* ctx2 = it.getctx(); + if(this->option != option2 || this->text != text2 || this->cb != cb2 || this->ctx != ctx2) + { + return false; + } + return true; } } \ No newline at end of file diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index 3a24e9bed..44356c310 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -2,58 +2,55 @@ #include -namespace utils +namespace core::utils { - class SubMenu : public core::utils::MenuItem + SubMenu::SubMenu(std::string option, std::string text, void* cb, void* ctx, MenuItem* items) : MenuItem(option, text, cb, ctx) { - utils::SubMenu::SubMenu(std::string option, std::string text, void* cb, void* ctx, core::utils::MenuItem[itemCount] items) - { - - } + this->items = items; + } - std::string toStr() - { - //create string for line showing this sub-menu - std::string out = option ; - out += "|"; - out += text; - out += "\n"; - return out; - } + std::string SubMenu::toStr() + { + //create string for line showing this sub-menu + std::string out = this->option; + out += ("|"); + out += (this->text); + out += ("\n"); + return out; + } - core::utils::MenuItem[itemCount] getItems() - { - return items; - } + MenuItem* SubMenu::getItems() + { + return items; + } - int getCount() - { - return itemCount; - } + int SubMenu::getCount() + { + return itemCount; + } - bool equals(core::utils::MenuItem sub) : sub(sub) + bool SubMenu::equals(SubMenu sub) + { + //check equivalence of attributes besides items + //this section is identical to menuItem + std::string option2 = sub.getOption(); + std::string text2 = sub.getText(); + void* cb2 = sub.getcb(); + void* ctx2 = sub.getctx(); + MenuItem* items2 = sub.getItems(); + if(this->option != option2 || this->text != text2 || this->cb != cb2 || this->ctx != ctx2) + { + return false; + } + + //Check items equivalence + for (int i = 0; i < itemCount; i ++) + { + if(!(items[i].equals(items2[i]))) { - //check equivalence of attributes besides items - //this section is identical to menuItem - std::string option2 = it.getOption(); - std::string text2 = it.getText(); - void* cb2 = it.getcb(); - void* ctx2 = it.getctx(); - core::utils::MenuItem items2[itemCount] = it.getItems(); - if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) - { - return false; - } - - //Check items equivalence - for (i = 0; i < itemCount; i ++) - { - if(!(items[i].equals(items2[i]))) - { - return false; - } - } - return true; + return false; } + } + return true; } } \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index 89fcac9d2..cfa6b40a9 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -8,92 +8,90 @@ #include #include #include +#include namespace io = core::io; -namespace utils +namespace core::utils { - class Terminal + Terminal::Terminal(io::UART& uart, utils::Menu menu) : menu(menu), uart(uart), current(nullptr) { - utils::Terminal::Terminal(io::Uart& uart, core::utils::Menu menu) : menu(menu), uart(uart) - { - uart.printf("Starting Terminal..."); - } + uart.printf("Starting Terminal..."); + } - io::UART& getUART() - { - return uart; - } + io::UART& Terminal::getUART() + { + return uart; + } - utils::Menu getMenu() - { - return menu; - } + utils::Menu Terminal::getMenu() + { + return menu; + } - utils::SubMenu getCurrent() - { - return current; - } + utils::SubMenu Terminal::getCurrent() + { + return current; + } - void setCurrent(utils::terminal::SubMenu sub) - { - current = sub; - } + void Terminal::setCurrent(SubMenu sub) + { + current = sub; + } - bool isMain() - { - return m; - } + bool Terminal::isMain() + { + return m; + } - void update(std::string message) + void Terminal::update(std::string message) + { + for(int i = 0; i < 5; i ++) { - for(int i = 0; i < 5; i ++) - { - uart.printf("\n"); - } - uart.printf(message); + uart.printf("\n"); } + uart.printf(message.c_str()); + } - std::string[10] recieve() + std::array Terminal::recieve() + { + char buffer[100]; + std::string argN[10]; + uart.gets(buffer, 100); + + int c = 0; + char hold[20] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; + h = 0; + for(int i = 0; i < 100; i ++) { - char buffer[100]; - std::string argN[10]; - uart.gets(buffer, 100); - - int c = 0; - char hold[20] = {'','','','','','','','','','','','','','','','','','','',''}; - h = 0; - for(int i = 0; i < 100; i ++) + if(buffer[i] = '\0') { - if(buffer[i] = '\0') - { - break; - } + break; + } - if(c >= 10) - { - uart.printf("ERROR TOO MANY ARGUMENTS"); - break; - } + if(c >= 10) + { + uart.printf("ERROR TOO MANY ARGUMENTS"); + break; + } - if(buffer[i] != ' ') - { - hold[h] = buffer[i]; - h ++; - } - else - { - argN[c] = hold; - hold[20] = {'','','','','','','','','','','','','','','','','','','',''}; - h = 0; - c ++; - } - + if(buffer[i] != ' ') + { + hold[h] = buffer[i]; + h ++; } + else + { + argN[c] = hold; + hold[20] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; + h = 0; + c ++; + } + + } - return argN; + return argN; - } } } \ No newline at end of file From 5e0b090c1f06cdff4391ade04588d33b48bdd121 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Wed, 22 Jan 2025 19:45:57 -0500 Subject: [PATCH 12/30] no more errors, still failing to compile --- include/core/utils/terminal/menu.hpp | 21 ++-- include/core/utils/terminal/menuItem.hpp | 34 +++--- include/core/utils/terminal/terminal.hpp | 48 ++++----- samples/terminal/main.cpp | 132 ++++++++++++++--------- src/core/utils/terminal/menu.cpp | 29 ++--- src/core/utils/terminal/menuItem.cpp | 20 ++-- src/core/utils/terminal/subMenu.cpp | 35 +++--- src/core/utils/terminal/terminal.cpp | 45 ++------ 8 files changed, 180 insertions(+), 184 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 2c74fbde5..b4cfbccc9 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -2,10 +2,10 @@ #define EVT_TERM_MENU //macro for max initial item count of a main menu -const int MITEMCOUNT = 10; #include #include + namespace core::utils { class Menu @@ -21,35 +21,36 @@ namespace core::utils /** * creates a string representation of a menu, with each menu item on its own line */ - std::string toStr(); + std::string toStr(Menu mnu); /** - * returns the list of menu items + * returns number of items in menu */ - MenuItem* getItems(); + int getCount(){return itemCount;} /** - * returns itemCount + * returns list of items in menu */ - int getCount(); + MenuItem* getItems() {return items;} + /** * checks if this menu is equivalent to another menu * true if every menu item is equal */ - bool equals(Menu mnu); + bool equals(Menu mnu); - protected: + private: /** * list of all items contained in the menu */ - MenuItem* items; + MenuItem* items; /** * maximum number of items allowed in any menu */ - int itemCount = MITEMCOUNT; + int itemCount = 10; }; } diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index d835b65d3..1a3929359 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -1,9 +1,11 @@ #ifndef EVT_TERM_MENUITEM #define EVT_TERM_MENUITEM +#include +#include //macro for max initial item count of submenus -const int ITEMCOUNT = 10; -#include + +using callback_t = void (*)(core::io::UART&, void*); namespace core::utils { @@ -19,17 +21,17 @@ namespace core::utils * @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(std::string option, std::string text, void* cb, void* ctx = nullptr); + MenuItem(std::string option, std::string text, callback_t cb, void* ctx = nullptr); - std::string getOption(); + std::string getOption(){return option;} - std::string getText(); + std::string getText(){return text;} - void* getcb(); + callback_t getcb(){return cb;} - void* getctx(); + void* getctx(){return ctx;} - std::string toStr(); + std::string toStr(MenuItem it); /** * checks if 2 items are equivalent @@ -52,7 +54,7 @@ namespace core::utils /** * pointer to callback method for this item */ - void* cb; + callback_t cb; /** * context for this item, void* because it is of an abstract type @@ -66,12 +68,12 @@ namespace core::utils /** * constructor for sub-menu sub-class */ - SubMenu(std::string option, std::string text, void* cb, void* ctx, MenuItem* items); + SubMenu(std::string option, std::string text, callback_t cb, void* ctx, MenuItem* items); /** * unique overridden toStr() method for sub-menus */ - std::string toStr(); + std::string toStr(SubMenu sub); /** * unique overridden equals() method for sub-menus @@ -84,14 +86,14 @@ namespace core::utils /** * returns itemCount */ - int getCount(); + int getCount(){return itemCount;} /** * returns a list of all items contained in the submenu */ - MenuItem* getItems(); + MenuItem* getItems(){return items;} - protected: + private: /** * key value for item, this is used to select it in your commands */ @@ -105,7 +107,7 @@ namespace core::utils /** * pointer to callback method for this item */ - void* cb; + callback_t cb; /** * context for this item, void* because it is of an abstract type @@ -114,7 +116,7 @@ namespace core::utils /** * the total number of items that can be contained in any sub-menu */ - int itemCount = ITEMCOUNT; + int itemCount = 10; /** * list of all items inside of the sub-menu diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 9471837d2..16b8c7f0a 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -21,50 +22,49 @@ namespace core::utils * @param uart an instance of a UART object * @param baud 9600 or 115200 */ - Terminal(io::UART& uart, utils::Menu menu); - - /** - * returns the uart instance of this terminal - */ - io::UART& getUART(); - - /** - * returns the menu instance that serves as the root node of the tree containing the terminal - */ - utils::Menu getMenu(); - - /** - * returns the current page of the menu, only if the menu is not currently on the main page - * */ - utils::SubMenu getCurrent(); - + Terminal(io::UART& uart, Menu* menu); + + io::UART& getUART() + { + return uart; + } + + Menu* getMenu() + { + return menu; + } + + SubMenu* getCurrent() + { + return current; + } /** * sets current submenu to provided value * @param sub the submenu to replace the current one with */ - void setCurrent(SubMenu sub); + void setCurrent(SubMenu* sub){current = sub;} /** * checks if the terminal is still on the main menu */ - bool isMain(); + bool isMain(){return m;} /** * Sends a provided message over UART * @param message a string message to send via UART */ - void update(std::string message); + void update(std::string message, io::UART& uart); /** * proccesses incoming UART messages */ - std::array recieve(); + std::array recieve(io::UART& uart); - protected: + private: // menu instance - utils::Menu menu; + Menu* menu; - utils::SubMenu current; + SubMenu* current; bool m; diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index 2e48b7e8d..032754948 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -5,10 +5,61 @@ #include #include #include +#include namespace io = core::io; namespace utils = core::utils; +void printCB(io::UART& uart, void* args) +{ + uart.printf("Test Message"); +} + +void sendCB(io::UART& uart, void* args) +{ + void* argsArray = static_cast(args); + char* message = static_cast(argsArray); + uart.printf(message); +} + +void enterCB(io::UART& uart, void* args) +{ + // Cast the void pointer to an array of void pointers + void** argsArray = static_cast(args); + + // Cast the first element of the array to SubMenu* + utils::SubMenu* sub = static_cast(argsArray[0]); + + // Cast the second element of the array to Terminal* + utils::Terminal* term = static_cast(argsArray[1]); + + // Set the current submenu + term->setCurrent(sub); +} + +//This print is specifically for MENUITEMS, SUBMENUS, and MENUS +void print(io::UART& uart, utils::Menu item) +{ + uart.printf(item.toStr(item).c_str()); +} + +void print(io::UART& uart, utils::MenuItem item) +{ + uart.printf(item.toStr(item).c_str()); +} + +void print(io::UART& uart, utils::SubMenu item) +{ + uart.printf(item.toStr(item).c_str()); +} + +//TERMINAL specific print function +void printTerm(io::UART& uart, core::utils::Terminal term) +{ + utils::Menu* mnu = term.getMenu(); + uart.printf((mnu->toStr(*mnu)).c_str()); +} + int main() { //core setup @@ -18,86 +69,69 @@ int main() io::UART& uart = io::getUART(9600); //make some items - utils::Terminal::MenuItem print = MenuItem::MenuItem("p","Print, takes only key", printCB, nullptr); - utils::Terminal::MenuItem send = MenuItem::MenuItem("s","Send, takes key and message", sendCB, nullptr); - utils::Terminal::SubMenu sub = MenuItem::SubMenu("b", "SubMenu", enterCB, nullptr, {print,send}); - utils::Terminal::MenuItem[2] items = {print, send, sub}; - utils::Terminal::Menu menu = Menu::Menu(items); - utils::Terminal::Terminal term = Terminal::Terminal(uart, menu); + utils::MenuItem print = utils::MenuItem("p","Print, takes only key", printCB, nullptr); + utils::MenuItem send = utils::MenuItem("s","Send, takes key and message", sendCB, nullptr); + utils::MenuItem items[2] = {print, send}; + utils::SubMenu sub = utils::SubMenu("b", "SubMenu", enterCB, nullptr, items); + utils::MenuItem items2[3] = {print, send, sub}; + utils::Menu menu = utils::Menu(items2); + utils::Terminal term = utils::Terminal(uart, &menu); printTerm(uart, term); + callback_t cb; while(true) { - utils::Terminal::MenuItem[10] items; + utils::MenuItem* mitems; int c = 0; if(term.isMain()) { - items = term.getMenu().getItems(); - c = term.getMenu().getCount(); - term.update(term.getMenu().toStr()); + utils::Menu* men = term.getMenu(); + mitems = men->getItems(); + c = men->getCount(); + term.update(men->toStr(*men), uart); } else { - items = term.getCurrent().getItems(); - c = term.getCurrent().getCount(); - term.update(term.getCurrent().toStr()); + utils::SubMenu* sub = term.getCurrent(); + mitems = sub->getItems(); + c = sub->getCount(); + term.update(sub->toStr(*sub), uart); } - std::string[10] inputList = term.recieve(); + std::array inputList = term.recieve(uart); std::string tag = inputList[0]; - std::string[10] args = inputList[1:]; + std::string args[10]; + for (int i = 1; i < 10; i++) + { + args[i - 1] = inputList[i]; + } - void (*cb)(io::UART, void*, utils::Terminal::Terminal); + //void (*cb)(io::UART, void*, utils::Terminal); + for(int i = 0; i < c; i ++) { - if(tag = items[i].getOption()) + if(tag == mitems[i].getOption()) { - cb = items[i].getcb(); + cb = mitems[i].getcb(); } } - if(tag = "b") + if(tag == "b") { - cb(uart, sub, term); + void* argsArray[2] = { static_cast(&sub), static_cast(&term) }; + cb(uart, argsArray); } else { - cb(uart, args, term); + cb(uart, args); } } - return 0 -} - -void printCB(io::UART uart, void* args, utils::terminal::Terminal term) -{ - uart.printf("Test Message"); -} - -void sendCB(io::UART uart, void* args, utils::terminal::Terminal term) -{ - uart.printf(args[0]); -} - -void enterCB(io::UART uart, void* sub, utils::terminal::Terminal term) -{ - term.setCurrent(sub); -} - -//This print is specifically for MENUITEMS, SUBMENUS, and MENUS -void print(io:UART uart, void* item) -{ - uart.printf(item.toStr()); -} - -//TERMINAL specific print function -void printTerm(io:UART uart, core::utils:terminal::Terminal term) -{ - uart.printf(term.getMenu().toStr()); + return 0; } \ No newline at end of file diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index f3a2f2e22..331491633 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -7,39 +7,30 @@ namespace core::utils { Menu::Menu(MenuItem* items) : items(items) { - + } - std::string Menu::toStr() + std::string toStr(Menu mnu) { std::string out = ""; - MenuItem* items = this->items; - for(int i = 0; i < MITEMCOUNT; i ++) + MenuItem* items = mnu.getItems(); + for(int i = 0; i < mnu.getCount(); i ++) { - out += items[0].toStr(); + out += items[0].toStr(items[0]); out += "\n"; } return out; } - int Menu::getCount() - { - return MITEMCOUNT; - } - - MenuItem* Menu::getItems() - { - return this->items; - } - - bool Menu::equals(Menu mnu) + bool equals(Menu mnu) { MenuItem* items2; - std::copy(items2[0],items2[MITEMCOUNT-1],(mnu.getItems())[0]); - for (int i = 0; i < MITEMCOUNT; i ++) + std::copy(items2, items2 + mnu.getCount(), mnu.getItems()); + + for (int i = 0; i < mnu.getCount(); i ++) { - if(!(items[i].equals(items2[i]))) + if(!(mnu.getItems()[i].equals(items2[i]))) { return false; } diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index 2423740f2..ee5bd4022 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -4,27 +4,27 @@ namespace core::utils { - MenuItem::MenuItem(std::string option, std::string text, void* cb, void* ctx) : option(option), text(text), cb(cb), ctx(ctx) + MenuItem::MenuItem(std::string option, std::string text, callback_t cb, void* ctx) : option(option), text(text), cb(cb), ctx(ctx) { } - std::string MenuItem::toStr() + std::string toStr(MenuItem it) { - std::string string = this->option; + std::string string = it.getOption(); string += ('|'); - string += (this->text); + string += (it.getText()); string += ("\n"); return string; } - bool MenuItem::equals(MenuItem it) + bool equals(MenuItem it, MenuItem it2) { - std::string option2 = it.getOption(); - std::string text2 = it.getText(); - void* cb2 = it.getcb(); - void* ctx2 = it.getctx(); - if(this->option != option2 || this->text != text2 || this->cb != cb2 || this->ctx != ctx2) + std::string option2 = it2.getOption(); + std::string text2 = it2.getText(); + callback_t cb2 = it2.getcb(); + void* ctx2 = it2.getctx(); + if(it.getOption() != option2 || it.getText() != text2 || it.getcb() != cb2 || it.getctx() != ctx2) { return false; } diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index 44356c310..6bd1229a3 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -4,47 +4,38 @@ namespace core::utils { - SubMenu::SubMenu(std::string option, std::string text, void* cb, void* ctx, MenuItem* items) : MenuItem(option, text, cb, ctx) + SubMenu::SubMenu(std::string option, std::string text, callback_t cb, void* ctx, MenuItem* items) : MenuItem(option, text, cb, ctx) { this->items = items; } - std::string SubMenu::toStr() + std::string toStr(SubMenu sub) { //create string for line showing this sub-menu - std::string out = this->option; + std::string out = sub.getOption(); out += ("|"); - out += (this->text); + out += (sub.getText()); out += ("\n"); return out; } - MenuItem* SubMenu::getItems() - { - return items; - } - - int SubMenu::getCount() - { - return itemCount; - } - - bool SubMenu::equals(SubMenu sub) + bool equals(SubMenu sub, SubMenu sub2) { //check equivalence of attributes besides items //this section is identical to menuItem - std::string option2 = sub.getOption(); - std::string text2 = sub.getText(); - void* cb2 = sub.getcb(); - void* ctx2 = sub.getctx(); - MenuItem* items2 = sub.getItems(); - if(this->option != option2 || this->text != text2 || this->cb != cb2 || this->ctx != ctx2) + std::string option2 = sub2.getOption(); + std::string text2 = sub2.getText(); + callback_t cb2 = sub2.getcb(); + void* ctx2 = sub2.getctx(); + MenuItem* items2 = sub2.getItems(); + if(sub.getOption() != option2 || sub.getText() != text2 || sub.getcb() != cb2 || sub.getctx() != ctx2) { return false; } //Check items equivalence - for (int i = 0; i < itemCount; i ++) + MenuItem* items = sub.getItems(); + for (int i = 0; i < sub.getCount(); i ++) { if(!(items[i].equals(items2[i]))) { diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index cfa6b40a9..7592a1cde 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -14,38 +14,12 @@ namespace io = core::io; namespace core::utils { - Terminal::Terminal(io::UART& uart, utils::Menu menu) : menu(menu), uart(uart), current(nullptr) + Terminal::Terminal(io::UART& uart, utils::Menu* menu) : menu(menu), uart(uart) { uart.printf("Starting Terminal..."); } - io::UART& Terminal::getUART() - { - return uart; - } - - utils::Menu Terminal::getMenu() - { - return menu; - } - - utils::SubMenu Terminal::getCurrent() - { - return current; - } - - void Terminal::setCurrent(SubMenu sub) - { - current = sub; - } - - bool Terminal::isMain() - { - return m; - } - - - void Terminal::update(std::string message) + void update(std::string message, io::UART& uart) { for(int i = 0; i < 5; i ++) { @@ -54,18 +28,18 @@ namespace core::utils uart.printf(message.c_str()); } - std::array Terminal::recieve() + std::array recieve(io::UART& uart) { char buffer[100]; - std::string argN[10]; + std::array argN; uart.gets(buffer, 100); int c = 0; - char hold[20] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; - h = 0; + char hold[20]; + int h = 0; for(int i = 0; i < 100; i ++) { - if(buffer[i] = '\0') + if(buffer[i] == '\0') { break; } @@ -84,7 +58,10 @@ namespace core::utils else { argN[c] = hold; - hold[20] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; + for (int i = 0; i < 20; i++) + { + hold[i] = '\0'; + } h = 0; c ++; } From c3db65d963f551b0d3aa0f65577e7b30e743e9be Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Sat, 8 Feb 2025 11:54:22 -0500 Subject: [PATCH 13/30] fixing sample --- include/core/utils/terminal/menu.hpp | 6 +- include/core/utils/terminal/menuItem.hpp | 36 ++++---- include/core/utils/terminal/terminal.hpp | 9 +- samples/terminal/main.cpp | 100 +++++++++++++---------- src/core/utils/terminal/menu.cpp | 27 +++--- src/core/utils/terminal/menuItem.cpp | 23 +++--- src/core/utils/terminal/subMenu.cpp | 43 ++++++---- src/core/utils/terminal/terminal.cpp | 91 +++++++++++---------- 8 files changed, 179 insertions(+), 156 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index b4cfbccc9..095f9da15 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -3,7 +3,6 @@ //macro for max initial item count of a main menu #include -#include namespace core::utils @@ -21,7 +20,7 @@ namespace core::utils /** * creates a string representation of a menu, with each menu item on its own line */ - std::string toStr(Menu mnu); + void printStr(io::UART& uart); /** * returns number of items in menu @@ -37,8 +36,9 @@ namespace core::utils /** * 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 mnu); + bool equals(Menu mnu2); private: diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 1a3929359..7c3cd8203 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -1,7 +1,6 @@ #ifndef EVT_TERM_MENUITEM #define EVT_TERM_MENUITEM #include -#include //macro for max initial item count of submenus @@ -21,35 +20,35 @@ namespace core::utils * @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(std::string option, std::string text, callback_t cb, void* ctx = nullptr); + MenuItem(char* option, char* text, callback_t cb, void* ctx = nullptr); - std::string getOption(){return option;} + char* getOption(){return option;} - std::string getText(){return text;} + char* getText(){return text;} callback_t getcb(){return cb;} void* getctx(){return ctx;} - std::string toStr(MenuItem it); + void printStr(core::io::UART& uart); /** * checks if 2 items are equivalent * true if every attribute is equivalent using == - * @param it a different menu item to compare to + * @param it2 a different menu item to compare to */ - bool equals(MenuItem it); + bool equals(MenuItem it2); protected: /** * key value for item, this is used to select it in your commands */ - std::string option; + char* option; /** * description/name of item */ - std::string text; + char* text; /** * pointer to callback method for this item @@ -68,20 +67,25 @@ namespace core::utils /** * constructor for sub-menu sub-class */ - SubMenu(std::string option, std::string text, callback_t cb, void* ctx, MenuItem* items); + SubMenu(char* option, char* text, callback_t cb, void* ctx, MenuItem* items); /** - * unique overridden toStr() method for sub-menus + * unique overridden printStr() method for sub-menus */ - std::string toStr(SubMenu sub); + 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 sub the other submenu to compare to + * @param sub2 the other submenu to compare to */ - bool equals(SubMenu sub); + bool equals(SubMenu sub2); /** * returns itemCount @@ -97,12 +101,12 @@ namespace core::utils /** * key value for item, this is used to select it in your commands */ - std::string option; + char* option; /** * description/name of item */ - std::string text; + char* text; /** * pointer to callback method for this item diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 16b8c7f0a..1761c9c90 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -3,12 +3,9 @@ #include #include -#include #include #include -#include -#include - +#include namespace core::utils { @@ -53,12 +50,12 @@ namespace core::utils * Sends a provided message over UART * @param message a string message to send via UART */ - void update(std::string message, io::UART& uart); + void update(char* message); /** * proccesses incoming UART messages */ - std::array recieve(io::UART& uart); + char** recieve(char** holder); private: // menu instance diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index 032754948..bf8735e69 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -12,7 +12,7 @@ namespace utils = core::utils; void printCB(io::UART& uart, void* args) { - uart.printf("Test Message"); + uart.printf("Test Message\n\r"); } void sendCB(io::UART& uart, void* args) @@ -40,24 +40,31 @@ void enterCB(io::UART& uart, void* args) //This print is specifically for MENUITEMS, SUBMENUS, and MENUS void print(io::UART& uart, utils::Menu item) { - uart.printf(item.toStr(item).c_str()); + item.printStr(uart); } void print(io::UART& uart, utils::MenuItem item) { - uart.printf(item.toStr(item).c_str()); + item.printStr(uart); } void print(io::UART& uart, utils::SubMenu item) { - uart.printf(item.toStr(item).c_str()); + item.printStr(uart); } //TERMINAL specific print function void printTerm(io::UART& uart, core::utils::Terminal term) { - utils::Menu* mnu = term.getMenu(); - uart.printf((mnu->toStr(*mnu)).c_str()); + if(term.isMain()) + { + utils::Menu* mnu = term.getMenu(); + mnu->printStr(uart); + } + else + { + term.getCurrent()->printMStr(uart); + } } int main() @@ -68,69 +75,72 @@ int main() //UART setup io::UART& uart = io::getUART(9600); + //item list end item + utils::MenuItem nul = utils::MenuItem("null","LIST END", nullptr, nullptr); + //make some items - utils::MenuItem print = utils::MenuItem("p","Print, takes only key", printCB, nullptr); - utils::MenuItem send = utils::MenuItem("s","Send, takes key and message", sendCB, nullptr); - utils::MenuItem items[2] = {print, send}; - utils::SubMenu sub = utils::SubMenu("b", "SubMenu", enterCB, nullptr, items); - utils::MenuItem items2[3] = {print, send, sub}; + utils::MenuItem print = utils::MenuItem("p","Print, takes only key\n\r", printCB, nullptr); + utils::MenuItem send = utils::MenuItem("s","Send, takes key and message\n\r", sendCB, nullptr); + utils::MenuItem items[3] = {print, send, nul}; + utils::SubMenu sub = utils::SubMenu("b", "SubMenu\n\r", enterCB, nullptr, items); + utils::MenuItem items2[4] = {print, send, sub, nul}; utils::Menu menu = utils::Menu(items2); utils::Terminal term = utils::Terminal(uart, &menu); printTerm(uart, term); callback_t cb; - + while(true) { utils::MenuItem* mitems; int c = 0; - if(term.isMain()) - { - utils::Menu* men = term.getMenu(); - mitems = men->getItems(); - c = men->getCount(); - term.update(men->toStr(*men), uart); - } - else - { - utils::SubMenu* sub = term.getCurrent(); - mitems = sub->getItems(); - c = sub->getCount(); - term.update(sub->toStr(*sub), uart); - } - - std::array inputList = term.recieve(uart); - std::string tag = inputList[0]; - std::string args[10]; - for (int i = 1; i < 10; i++) - { - args[i - 1] = inputList[i]; - } + char** inputList; + term.recieve(inputList); + char* item = inputList[0]; - //void (*cb)(io::UART, void*, utils::Terminal); + bool m = term.isMain(); + utils::Menu* men = term.getMenu(); + utils::MenuItem* subitemsM = men->getItems(); + + utils::SubMenu* cur = term.getCurrent(); + + utils::MenuItem* subitemsC; - for(int i = 0; i < c; i ++) - { - if(tag == mitems[i].getOption()) - { - cb = mitems[i].getcb(); - } + if(!m) + { + subitemsC = cur->getItems(); } - if(tag == "b") + utils::MenuItem* chosen; + + if(m) { - void* argsArray[2] = { static_cast(&sub), static_cast(&term) }; - cb(uart, argsArray); + for(int i = 0; i < 10; i ++) + { + if(item == subitemsM[i].getOption()) + { + *chosen = subitemsM[i]; + break; + } + } } else { - cb(uart, args); + for(int i = 0; i < 10; i ++) + { + if(item == subitemsC[i].getOption()) + { + *chosen = subitemsC[i]; + break; + } + } } + uart.printf("\n\rend\n\r"); } return 0; diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 331491633..6615e03f9 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -1,7 +1,6 @@ #include #include -#include - +#include namespace core::utils { @@ -10,27 +9,25 @@ namespace core::utils } - std::string toStr(Menu mnu) + void Menu::printStr(core::io::UART& uart) { - std::string out = ""; - MenuItem* items = mnu.getItems(); - for(int i = 0; i < mnu.getCount(); i ++) + for(int c = 0; c < itemCount; c ++) { - out += items[0].toStr(items[0]); - out += "\n"; + if(items[c].getOption() == "null") + { + return; + } + items[c].printStr(uart); } - - return out; } - bool equals(Menu mnu) + bool Menu::equals(Menu mnu2) { - MenuItem* items2; - std::copy(items2, items2 + mnu.getCount(), mnu.getItems()); + MenuItem* items2 = mnu2.getItems(); - for (int i = 0; i < mnu.getCount(); i ++) + for (int i = 0; i < itemCount; i ++) { - if(!(mnu.getItems()[i].equals(items2[i]))) + if(!(items[i].equals(items2[i]))) { return false; } diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index ee5bd4022..0e2211acc 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -1,30 +1,27 @@ #include -#include - +#include namespace core::utils { - MenuItem::MenuItem(std::string option, std::string text, callback_t cb, void* ctx) : option(option), text(text), cb(cb), ctx(ctx) + MenuItem::MenuItem(char* option, char* text, callback_t cb, void* ctx) : option(option), text(text), cb(cb), ctx(ctx) { } - std::string toStr(MenuItem it) + void MenuItem::printStr(core::io::UART& uart) { - std::string string = it.getOption(); - string += ('|'); - string += (it.getText()); - string += ("\n"); - return string; + uart.printf(option); + uart.printf("|"); + uart.printf(text); } - bool equals(MenuItem it, MenuItem it2) + bool MenuItem::equals(MenuItem it2) { - std::string option2 = it2.getOption(); - std::string text2 = it2.getText(); + char* option2 = it2.getOption(); + char* text2 = it2.getText(); callback_t cb2 = it2.getcb(); void* ctx2 = it2.getctx(); - if(it.getOption() != option2 || it.getText() != text2 || it.getcb() != cb2 || it.getctx() != ctx2) + if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { return false; } diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index 6bd1229a3..f7ef2447a 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -1,41 +1,50 @@ #include -#include +#include namespace core::utils { - SubMenu::SubMenu(std::string option, std::string text, callback_t cb, void* ctx, MenuItem* items) : MenuItem(option, text, cb, ctx) + SubMenu::SubMenu(char* option, char* text, callback_t cb, void* ctx, MenuItem* items) : option(option), text(text), cb(cb), ctx(ctx), items(items), MenuItem(option, text, cb, ctx)//SubMenu(option, text, cb, ctx, items) { - this->items = items; + + } + + void SubMenu::printStr(core::io::UART& uart) + { + //this is exclusively for displaying the submenu inside of a menu list + uart.printf(option); + uart.printf("|"); + uart.printf(text); } - std::string toStr(SubMenu sub) + void SubMenu::printMStr(core::io::UART& uart) { - //create string for line showing this sub-menu - std::string out = sub.getOption(); - out += ("|"); - out += (sub.getText()); - out += ("\n"); - return out; + for(int c = 0; c < itemCount; c ++) + { + if(items[c].getOption() == "null") + { + return; + } + items[c].printStr(uart); + } } - bool equals(SubMenu sub, SubMenu sub2) + bool SubMenu::equals(SubMenu sub2) { //check equivalence of attributes besides items //this section is identical to menuItem - std::string option2 = sub2.getOption(); - std::string text2 = sub2.getText(); + char* option2 = sub2.getOption(); + char* text2 = sub2.getText(); callback_t cb2 = sub2.getcb(); void* ctx2 = sub2.getctx(); MenuItem* items2 = sub2.getItems(); - if(sub.getOption() != option2 || sub.getText() != text2 || sub.getcb() != cb2 || sub.getctx() != ctx2) + if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { return false; } - //Check items equivalence - MenuItem* items = sub.getItems(); - for (int i = 0; i < sub.getCount(); i ++) + //Check items equivalence; + for (int i = 0; i < itemCount; i ++) { if(!(items[i].equals(items2[i]))) { diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index 7592a1cde..09225c520 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -3,12 +3,10 @@ */ #include #include -#include #include #include #include -#include -#include +#include namespace io = core::io; @@ -16,59 +14,70 @@ namespace core::utils { Terminal::Terminal(io::UART& uart, utils::Menu* menu) : menu(menu), uart(uart) { - uart.printf("Starting Terminal..."); + uart.printf("\n\rStarting Terminal...\n\r"); } - void update(std::string message, io::UART& uart) + void Terminal::update(char* message) { for(int i = 0; i < 5; i ++) { - uart.printf("\n"); + uart.printf("\n\r"); } - uart.printf(message.c_str()); + uart.printf(message); } - std::array recieve(io::UART& uart) + char** Terminal::recieve(char** holder) { char buffer[100]; - std::array argN; + char* argN[10]; + uart.gets(buffer, 100); - - int c = 0; - char hold[20]; - int h = 0; - for(int i = 0; i < 100; i ++) - { - if(buffer[i] == '\0') - { - break; - } - if(c >= 10) - { - uart.printf("ERROR TOO MANY ARGUMENTS"); - break; - } - if(buffer[i] != ' ') - { - hold[h] = buffer[i]; - h ++; - } - else - { - argN[c] = hold; - for (int i = 0; i < 20; i++) - { - hold[i] = '\0'; - } - h = 0; - c ++; - } - + argN[0] = strtok(buffer, " "); + for(int i = 1; i < 10; i ++) + { + argN[i] = strtok(NULL," "); } - return argN; + + // uart.gets(buffer, 100); + + // int c = 0; + // char hold[20]; + // int h = 0; + // for(int i = 0; i < 100; i ++) + // { + // if(buffer[i] == '\0') + // { + // break; + // } + + // if(c >= 10) + // { + // uart.printf("ERROR TOO MANY ARGUMENTS"); + // break; + // } + + // if(buffer[i] != ' ') + // { + // hold[h] = buffer[i]; + // h ++; + // } + // else + // { + // argN[c] = hold; + // for (int i = 0; i < 20; i++) + // { + // hold[i] = '\0'; + // } + // h = 0; + // c ++; + // } + + // } + holder = argN; + return holder; } } \ No newline at end of file From 4615f3546e260842e3435afd279cb561599ea820 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Mon, 10 Feb 2025 20:09:15 -0500 Subject: [PATCH 14/30] fixing sample --- include/core/utils/terminal/menu.hpp | 8 ++--- include/core/utils/terminal/menuItem.hpp | 12 ++++--- include/core/utils/terminal/terminal.hpp | 2 +- samples/terminal/main.cpp | 43 ++++++++++++++-------- src/core/utils/terminal/menu.cpp | 12 +++---- src/core/utils/terminal/menuItem.cpp | 18 +++++++--- src/core/utils/terminal/subMenu.cpp | 20 +++++------ src/core/utils/terminal/terminal.cpp | 46 +++--------------------- 8 files changed, 74 insertions(+), 87 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 095f9da15..17908680f 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -15,7 +15,7 @@ namespace core::utils * Basic constructor takes a list a menu items * @param items a list of menu items */ - Menu(MenuItem* items); + Menu(MenuItem** items); /** * creates a string representation of a menu, with each menu item on its own line @@ -30,7 +30,7 @@ namespace core::utils /** * returns list of items in menu */ - MenuItem* getItems() {return items;} + MenuItem** getItems() {return items;} /** @@ -38,14 +38,14 @@ namespace core::utils * true if every menu item is equal * @param mnu2 the menu to compare to */ - bool equals(Menu mnu2); + bool equals(Menu* mnu2); private: /** * list of all items contained in the menu */ - MenuItem* items; + MenuItem** items; /** * maximum number of items allowed in any menu diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 7c3cd8203..85ef230e8 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -28,6 +28,8 @@ namespace core::utils callback_t getcb(){return cb;} + void replace(MenuItem* newItem); + void* getctx(){return ctx;} void printStr(core::io::UART& uart); @@ -37,7 +39,7 @@ namespace core::utils * true if every attribute is equivalent using == * @param it2 a different menu item to compare to */ - bool equals(MenuItem it2); + bool equals(MenuItem* it2); protected: /** @@ -67,7 +69,7 @@ namespace core::utils /** * constructor for sub-menu sub-class */ - SubMenu(char* option, char* text, callback_t cb, void* ctx, MenuItem* items); + SubMenu(char* option, char* text, callback_t cb, void* ctx, MenuItem** items); /** * unique overridden printStr() method for sub-menus @@ -85,7 +87,7 @@ namespace core::utils * items is checked the same way as menu equivalence * @param sub2 the other submenu to compare to */ - bool equals(SubMenu sub2); + bool equals(SubMenu* sub2); /** * returns itemCount @@ -95,7 +97,7 @@ namespace core::utils /** * returns a list of all items contained in the submenu */ - MenuItem* getItems(){return items;} + MenuItem** getItems(){return items;} private: /** @@ -125,7 +127,7 @@ namespace core::utils /** * list of all items inside of the sub-menu */ - MenuItem* items; + MenuItem** items; }; } diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 1761c9c90..835984c36 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -55,7 +55,7 @@ namespace core::utils /** * proccesses incoming UART messages */ - char** recieve(char** holder); + void recieve(char** holder); private: // menu instance diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index bf8735e69..7ce270da2 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include namespace io = core::io; @@ -81,9 +81,9 @@ int main() //make some items utils::MenuItem print = utils::MenuItem("p","Print, takes only key\n\r", printCB, nullptr); utils::MenuItem send = utils::MenuItem("s","Send, takes key and message\n\r", sendCB, nullptr); - utils::MenuItem items[3] = {print, send, nul}; + utils::MenuItem* items[3] = {&print, &send, &nul}; utils::SubMenu sub = utils::SubMenu("b", "SubMenu\n\r", enterCB, nullptr, items); - utils::MenuItem items2[4] = {print, send, sub, nul}; + utils::MenuItem* items2[4] = {&print, &send, &sub, &nul}; utils::Menu menu = utils::Menu(items2); utils::Terminal term = utils::Terminal(uart, &menu); @@ -97,33 +97,38 @@ int main() int c = 0; - char** inputList; + char* inputList[10]; term.recieve(inputList); - char* item = inputList[0]; - + char* tag = inputList[0]; bool m = term.isMain(); + utils::Menu* men = term.getMenu(); - utils::MenuItem* subitemsM = men->getItems(); + utils::MenuItem** subitemsM = men->getItems(); utils::SubMenu* cur = term.getCurrent(); - utils::MenuItem* subitemsC; + utils::MenuItem** subitemsC; + if(!m) { subitemsC = cur->getItems(); } - utils::MenuItem* chosen; - + utils::MenuItem* chosen = &nul; if(m) { for(int i = 0; i < 10; i ++) { - if(item == subitemsM[i].getOption()) + char* op = subitemsM[i]->getOption(); + if(strcmp(op, "null") == 0) + { + break; + } + if(strcmp(tag,op) == 0) { - *chosen = subitemsM[i]; + chosen = (subitemsM[i]); break; } } @@ -132,15 +137,23 @@ int main() { for(int i = 0; i < 10; i ++) { - if(item == subitemsC[i].getOption()) + char* op = subitemsC[i]->getOption(); + if(strcmp(op, "null") == 0) { - *chosen = subitemsC[i]; + break; + } + if(strcmp(tag,op) == 0) + { + chosen = (subitemsC[i]); break; } } } + cb = chosen->getcb(); + + cb(uart, nullptr); - uart.printf("\n\rend\n\r"); + printTerm(uart, term); } return 0; diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 6615e03f9..443dfe526 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -4,7 +4,7 @@ namespace core::utils { - Menu::Menu(MenuItem* items) : items(items) + Menu::Menu(MenuItem** items) : items(items) { } @@ -13,21 +13,21 @@ namespace core::utils { for(int c = 0; c < itemCount; c ++) { - if(items[c].getOption() == "null") + if(items[c]->getOption() == "null") { return; } - items[c].printStr(uart); + items[c]->printStr(uart); } } - bool Menu::equals(Menu mnu2) + bool Menu::equals(Menu* mnu2) { - MenuItem* items2 = mnu2.getItems(); + MenuItem** items2 = mnu2->getItems(); for (int i = 0; i < itemCount; i ++) { - if(!(items[i].equals(items2[i]))) + if(!(items[i]->equals(items2[i]))) { return false; } diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index 0e2211acc..e377b2235 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -15,12 +15,20 @@ namespace core::utils uart.printf(text); } - bool MenuItem::equals(MenuItem it2) + void MenuItem::replace(MenuItem* newItem) { - char* option2 = it2.getOption(); - char* text2 = it2.getText(); - callback_t cb2 = it2.getcb(); - void* ctx2 = it2.getctx(); + option = newItem->getOption(); + text = newItem->getText(); + cb = newItem->getcb(); + ctx = newItem->getctx(); + } + + bool MenuItem::equals(MenuItem* it2) + { + char* option2 = it2->getOption(); + char* text2 = it2->getText(); + callback_t cb2 = it2->getcb(); + void* ctx2 = it2->getctx(); if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { return false; diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index f7ef2447a..ab2e81b2f 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -4,7 +4,7 @@ namespace core::utils { - SubMenu::SubMenu(char* option, char* text, callback_t cb, void* ctx, MenuItem* items) : option(option), text(text), cb(cb), ctx(ctx), items(items), MenuItem(option, text, cb, ctx)//SubMenu(option, text, cb, ctx, items) + SubMenu::SubMenu(char* option, char* text, callback_t cb, void* ctx, MenuItem** items) : option(option), text(text), cb(cb), ctx(ctx), items(items), MenuItem(option, text, cb, ctx)//SubMenu(option, text, cb, ctx, items) { } @@ -21,23 +21,23 @@ namespace core::utils { for(int c = 0; c < itemCount; c ++) { - if(items[c].getOption() == "null") + if(items[c]->getOption() == "null") { return; } - items[c].printStr(uart); + items[c]->printStr(uart); } } - bool SubMenu::equals(SubMenu sub2) + bool SubMenu::equals(SubMenu* sub2) { //check equivalence of attributes besides items //this section is identical to menuItem - char* option2 = sub2.getOption(); - char* text2 = sub2.getText(); - callback_t cb2 = sub2.getcb(); - void* ctx2 = sub2.getctx(); - MenuItem* items2 = sub2.getItems(); + char* option2 = sub2->getOption(); + char* text2 = sub2->getText(); + callback_t cb2 = sub2->getcb(); + void* ctx2 = sub2->getctx(); + MenuItem** items2 = sub2->getItems(); if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { return false; @@ -46,7 +46,7 @@ namespace core::utils //Check items equivalence; for (int i = 0; i < itemCount; i ++) { - if(!(items[i].equals(items2[i]))) + if(!(items[i]->equals(items2[i]))) { return false; } diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index 09225c520..eea55f943 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -26,58 +26,22 @@ namespace core::utils uart.printf(message); } - char** Terminal::recieve(char** holder) + void Terminal::recieve(char* holder[10]) { char buffer[100]; char* argN[10]; uart.gets(buffer, 100); - argN[0] = strtok(buffer, " "); for(int i = 1; i < 10; i ++) { argN[i] = strtok(NULL," "); } - - // uart.gets(buffer, 100); - - // int c = 0; - // char hold[20]; - // int h = 0; - // for(int i = 0; i < 100; i ++) - // { - // if(buffer[i] == '\0') - // { - // break; - // } - - // if(c >= 10) - // { - // uart.printf("ERROR TOO MANY ARGUMENTS"); - // break; - // } - - // if(buffer[i] != ' ') - // { - // hold[h] = buffer[i]; - // h ++; - // } - // else - // { - // argN[c] = hold; - // for (int i = 0; i < 20; i++) - // { - // hold[i] = '\0'; - // } - // h = 0; - // c ++; - // } - - // } - holder = argN; - return holder; - + for(int i = 0; i < 10; i++) + { + holder[i] = argN[i]; + } } } \ No newline at end of file From 6629b972af03a55191cd6f28339ce223f0004f3a Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Mon, 17 Feb 2025 20:24:00 -0500 Subject: [PATCH 15/30] arguments passing into cb functions properly sample still needs finished --- include/core/utils/terminal/menuItem.hpp | 2 +- include/core/utils/terminal/terminal.hpp | 2 +- samples/terminal/main.cpp | 73 ++++++++++++++++++------ src/core/utils/terminal/terminal.cpp | 12 +++- 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 85ef230e8..c8b47d56a 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -4,7 +4,7 @@ //macro for max initial item count of submenus -using callback_t = void (*)(core::io::UART&, void*); +using callback_t = void (*)(core::io::UART&, void**); namespace core::utils { diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 835984c36..32f17be54 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -39,7 +39,7 @@ namespace core::utils * sets current submenu to provided value * @param sub the submenu to replace the current one with */ - void setCurrent(SubMenu* sub){current = sub;} + void setCurrent(SubMenu* sub); /** * checks if the terminal is still on the main menu diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index 7ce270da2..b0870054a 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -10,31 +10,55 @@ namespace io = core::io; namespace utils = core::utils; -void printCB(io::UART& uart, void* args) +enum dataType { - uart.printf("Test Message\n\r"); + CHAR, + ITEM, + TERMINAL +}; + +void printCB(io::UART& uart, void** args) +{ + uart.printf("\n\rTest Message\n\r"); } -void sendCB(io::UART& uart, void* args) +void sendCB(io::UART& uart, void** args) { - void* argsArray = static_cast(args); - char* message = static_cast(argsArray); - uart.printf(message); + uart.printf("\n\r"); + for(int i = 1; i < 10; i ++) + { + if(args[i] == nullptr) + { + i = 10; + continue; + } + uart.printf((char*)args[i]); + uart.printf(" "); + } } -void enterCB(io::UART& uart, void* args) +void enterCB(io::UART& uart, void** args) { - // Cast the void pointer to an array of void pointers - void** argsArray = static_cast(args); - - // Cast the first element of the array to SubMenu* - utils::SubMenu* sub = static_cast(argsArray[0]); - - // Cast the second element of the array to Terminal* - utils::Terminal* term = static_cast(argsArray[1]); + utils::Terminal* term = (utils::Terminal*)(args[10]); + + utils::MenuItem** items = term->getMenu()->getItems(); - // Set the current submenu - term->setCurrent(sub); + utils::MenuItem* chosen; + char* tag = (char*)args[0]; + for(int i = 0; i < 10; i ++) + { + char* op = items[i]->getOption(); + if(strcmp(op, "null") == 0) + { + break; + } + if(strcmp(tag,op) == 0) + { + chosen = (items[i]); + break; + } + } + term->setCurrent((utils::SubMenu*)chosen); } //This print is specifically for MENUITEMS, SUBMENUS, and MENUS @@ -56,6 +80,7 @@ void print(io::UART& uart, utils::SubMenu item) //TERMINAL specific print function void printTerm(io::UART& uart, core::utils::Terminal term) { + uart.printf("\n\r"); if(term.isMain()) { utils::Menu* mnu = term.getMenu(); @@ -100,6 +125,18 @@ int main() char* inputList[10]; term.recieve(inputList); char* tag = inputList[0]; + void* args[11]; + for(int i = 0; i < 10; i ++) + { + if(inputList[i] == "\0") + { + args[i-1] = nullptr; + break; + } + args[i-1] = inputList[i]; + } + args[10] = &term; + bool m = term.isMain(); utils::Menu* men = term.getMenu(); @@ -151,7 +188,7 @@ int main() } cb = chosen->getcb(); - cb(uart, nullptr); + cb(uart, args); printTerm(uart, term); } diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index eea55f943..02d552af3 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -26,17 +26,23 @@ namespace core::utils uart.printf(message); } + void Terminal::setCurrent(SubMenu* sub) + { + current = sub; + m = false; + } + void Terminal::recieve(char* holder[10]) { - char buffer[100]; + char buffer[99]; char* argN[10]; - uart.gets(buffer, 100); + uart.gets(buffer, 99); argN[0] = strtok(buffer, " "); for(int i = 1; i < 10; i ++) { - argN[i] = strtok(NULL," "); + argN[i] = strtok(NULL, " "); } for(int i = 0; i < 10; i++) From 55303df20aa7a1ae59e875cad00c65536fa756f0 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Thu, 20 Feb 2025 20:51:55 -0500 Subject: [PATCH 16/30] hiding code in classes from sample --- include/core/utils/terminal/terminal.hpp | 2 + samples/terminal/main.cpp | 56 ++---------------------- src/core/utils/terminal/terminal.cpp | 39 +++++++++++++++++ 3 files changed, 45 insertions(+), 52 deletions(-) diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 32f17be54..ffddc058f 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -57,6 +57,8 @@ namespace core::utils */ void recieve(char** holder); + void process(utils::MenuItem* holder, char* tag); + private: // menu instance Menu* menu; diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index b0870054a..8cf331939 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -81,6 +81,7 @@ void print(io::UART& uart, utils::SubMenu item) void printTerm(io::UART& uart, core::utils::Terminal term) { uart.printf("\n\r"); + uart.printf("Terminal:"); if(term.isMain()) { utils::Menu* mnu = term.getMenu(); @@ -111,17 +112,13 @@ int main() utils::MenuItem* items2[4] = {&print, &send, &sub, &nul}; utils::Menu menu = utils::Menu(items2); utils::Terminal term = utils::Terminal(uart, &menu); - + printTerm(uart, term); callback_t cb; while(true) { - utils::MenuItem* mitems; - - int c = 0; - char* inputList[10]; term.recieve(inputList); char* tag = inputList[0]; @@ -137,55 +134,10 @@ int main() } args[10] = &term; - bool m = term.isMain(); - - utils::Menu* men = term.getMenu(); - - utils::MenuItem** subitemsM = men->getItems(); - - utils::SubMenu* cur = term.getCurrent(); - - utils::MenuItem** subitemsC; - + utils::MenuItem* chosen = &nul; - if(!m) - { - subitemsC = cur->getItems(); - } + term.process(chosen, tag); - utils::MenuItem* chosen = &nul; - if(m) - { - for(int i = 0; i < 10; i ++) - { - char* op = subitemsM[i]->getOption(); - if(strcmp(op, "null") == 0) - { - break; - } - if(strcmp(tag,op) == 0) - { - chosen = (subitemsM[i]); - break; - } - } - } - else - { - for(int i = 0; i < 10; i ++) - { - char* op = subitemsC[i]->getOption(); - if(strcmp(op, "null") == 0) - { - break; - } - if(strcmp(tag,op) == 0) - { - chosen = (subitemsC[i]); - break; - } - } - } cb = chosen->getcb(); cb(uart, args); diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index 02d552af3..a375c6918 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -50,4 +50,43 @@ namespace core::utils holder[i] = argN[i]; } } + + void Terminal::process(utils::MenuItem* holder, char* tag) + { + utils::MenuItem** subitemsM = menu->getItems(); + utils::MenuItem** subitemsC = current->getItems(); + + if(m) + { + for(int i = 0; i < 10; i ++) + { + char* op = subitemsM[i]->getOption(); + if(strcmp(op, "null") == 0) + { + break; + } + if(strcmp(tag,op) == 0) + { + holder = (subitemsM[i]); + break; + } + } + } + else + { + for(int i = 0; i < 10; i ++) + { + char* op = subitemsC[i]->getOption(); + if(strcmp(op, "null") == 0) + { + break; + } + if(strcmp(tag,op) == 0) + { + holder = (subitemsC[i]); + break; + } + } + } + } } \ No newline at end of file From 5e2c4e10c502c0da40478645823161be3e1076da Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Sat, 1 Mar 2025 11:09:50 -0500 Subject: [PATCH 17/30] i broke it but all functionality beyond callabck and user input is within classes now --- include/core/utils/terminal/menu.hpp | 12 +++- include/core/utils/terminal/menuItem.hpp | 35 ++++++++- include/core/utils/terminal/terminal.hpp | 7 +- samples/terminal/main.cpp | 92 ++++++++++-------------- src/core/utils/terminal/menu.cpp | 34 ++++++++- src/core/utils/terminal/menuItem.cpp | 2 +- src/core/utils/terminal/subMenu.cpp | 38 +++++++++- src/core/utils/terminal/terminal.cpp | 40 +++++++++-- 8 files changed, 192 insertions(+), 68 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 17908680f..a8ae87475 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -25,12 +25,18 @@ namespace core::utils /** * returns number of items in menu */ - int getCount(){return itemCount;} + int getCount(){return itemCount;} + + void replace(Menu m); /** * returns list of items in menu */ - MenuItem** getItems() {return items;} + MenuItem** getItems() {return items;} + + void addItem(utils::MenuItem* item); + + void delItem(int index); /** @@ -38,7 +44,7 @@ namespace core::utils * true if every menu item is equal * @param mnu2 the menu to compare to */ - bool equals(Menu* mnu2); + bool equals(Menu* mnu2); private: diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index c8b47d56a..2fa281eff 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -20,7 +20,7 @@ namespace core::utils * @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(char* option, char* text, callback_t cb, void* ctx = nullptr); + MenuItem(void* head, void* term, char* option, char* text, callback_t cb, void* ctx = nullptr); char* getOption(){return option;} @@ -41,7 +41,7 @@ namespace core::utils */ bool equals(MenuItem* it2); - protected: + private: /** * key value for item, this is used to select it in your commands */ @@ -61,6 +61,17 @@ namespace core::utils * context for this item, void* because it is of an abstract type */ void* ctx; + + + /** + * submenu or menu this item is in + */ + void* head; + + /** + * terminal this is in + */ + void* term; }; class SubMenu : public MenuItem @@ -69,7 +80,7 @@ namespace core::utils /** * constructor for sub-menu sub-class */ - SubMenu(char* option, char* text, callback_t cb, void* ctx, MenuItem** items); + SubMenu(void* head, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** items); /** * unique overridden printStr() method for sub-menus @@ -93,12 +104,20 @@ namespace core::utils * returns itemCount */ int getCount(){return itemCount;} + + SubMenu* getHead(); + + void setItems(MenuItem** itms); /** * returns a list of all items contained in the submenu */ MenuItem** getItems(){return items;} + void enter(io::UART& uart, void** args); + + void exit(io::UART& uart, void** args); + private: /** * key value for item, this is used to select it in your commands @@ -128,6 +147,16 @@ namespace core::utils * list of all items inside of the sub-menu */ MenuItem** items; + + /** + * submenu or menu this item is in + */ + void* head; + + /** + * terminal this is in + */ + void* term; }; } diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index ffddc058f..d975a982b 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -48,9 +48,11 @@ namespace core::utils /** * Sends a provided message over UART + * also replaces menu with provided menu * @param message a string message to send via UART + * @param menu the menu to replace the current head node with */ - void update(char* message); + void update(char* message, utils::Menu menu); /** * proccesses incoming UART messages @@ -59,6 +61,9 @@ namespace core::utils void process(utils::MenuItem* holder, char* tag); + //TERMINAL specific print function + void printTerm(io::UART& uart); + private: // menu instance Menu* menu; diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index 8cf331939..64bcad947 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -17,11 +17,13 @@ enum dataType TERMINAL }; +//example callback that prints a test message void printCB(io::UART& uart, void** args) { uart.printf("\n\rTest Message\n\r"); } +//example callback that echos user input(called send because e char is taken by exit) void sendCB(io::UART& uart, void** args) { uart.printf("\n\r"); @@ -37,30 +39,6 @@ void sendCB(io::UART& uart, void** args) } } -void enterCB(io::UART& uart, void** args) -{ - utils::Terminal* term = (utils::Terminal*)(args[10]); - - utils::MenuItem** items = term->getMenu()->getItems(); - - utils::MenuItem* chosen; - char* tag = (char*)args[0]; - for(int i = 0; i < 10; i ++) - { - char* op = items[i]->getOption(); - if(strcmp(op, "null") == 0) - { - break; - } - if(strcmp(tag,op) == 0) - { - chosen = (items[i]); - break; - } - } - term->setCurrent((utils::SubMenu*)chosen); -} - //This print is specifically for MENUITEMS, SUBMENUS, and MENUS void print(io::UART& uart, utils::Menu item) { @@ -77,22 +55,6 @@ void print(io::UART& uart, utils::SubMenu item) item.printStr(uart); } -//TERMINAL specific print function -void printTerm(io::UART& uart, core::utils::Terminal term) -{ - uart.printf("\n\r"); - uart.printf("Terminal:"); - if(term.isMain()) - { - utils::Menu* mnu = term.getMenu(); - mnu->printStr(uart); - } - else - { - term.getCurrent()->printMStr(uart); - } -} - int main() { //core setup @@ -102,28 +64,52 @@ int main() io::UART& uart = io::getUART(9600); //item list end item - utils::MenuItem nul = utils::MenuItem("null","LIST END", nullptr, nullptr); + utils::MenuItem nul = utils::MenuItem(nullptr,nullptr,"null","LIST END", nullptr, nullptr); //make some items - utils::MenuItem print = utils::MenuItem("p","Print, takes only key\n\r", printCB, nullptr); - utils::MenuItem send = utils::MenuItem("s","Send, takes key and message\n\r", sendCB, nullptr); - utils::MenuItem* items[3] = {&print, &send, &nul}; - utils::SubMenu sub = utils::SubMenu("b", "SubMenu\n\r", enterCB, nullptr, items); - utils::MenuItem* items2[4] = {&print, &send, &sub, &nul}; - utils::Menu menu = utils::Menu(items2); - utils::Terminal term = utils::Terminal(uart, &menu); - - printTerm(uart, term); + utils::MenuItem* items[10]; + for(int i = 0; i < 10; i ++) + { + items[i] = nullptr; + } + utils::Menu menu = utils::Menu(items); + utils::Terminal term = utils::Terminal(uart, &menu); + void* m = &menu; + void* t = &term; + //a nullptr in the head parameter means it is part of the main menu + utils::MenuItem mainPrint = utils::MenuItem(nullptr,t,"p","print test",printCB,nullptr); + utils::MenuItem mainSend = utils::MenuItem(nullptr,t,"s","echo",sendCB,nullptr); + //submenu cb is exit cb struct, ctx is void* to enter function cb struct. these are NOT responsible for moving menus, just for operations done duirng a menu move + utils::SubMenu sub = utils::SubMenu(nullptr,t,"sb","sub menu",nullptr,nullptr,items); + void* s = ⊂ + utils::MenuItem subPrint = utils::MenuItem(s,t,"p","print test",printCB,nullptr); + utils::MenuItem subSend = utils::MenuItem(s,t,"s","echo",sendCB,nullptr); + + //create submenu + int c = menu.getCount(); + utils::MenuItem* subs[c]; + subs[0] = &subPrint; + subs[1] = &subSend; + subs[2] = &nul; + sub.setItems(subs); + + //add main menu items to main menu + menu.addItem(&mainPrint); + menu.addItem(&mainSend); + menu.addItem(&sub); + menu.addItem(&nul); + + term.printTerm(uart); + uart.printf("Usage: flagChar argument argument | max 9 arguments"); callback_t cb; - while(true) { char* inputList[10]; term.recieve(inputList); char* tag = inputList[0]; void* args[11]; - for(int i = 0; i < 10; i ++) + for(int i = 0; i < c; i ++) { if(inputList[i] == "\0") { @@ -142,7 +128,7 @@ int main() cb(uart, args); - printTerm(uart, term); + term.printTerm(uart); } return 0; diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 443dfe526..bd269ff98 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -15,12 +15,44 @@ namespace core::utils { if(items[c]->getOption() == "null") { - return; + return; } items[c]->printStr(uart); } } + void Menu::addItem(utils::MenuItem* item) + { + int c =0; + for(int i = 0; i < itemCount; i ++) + { + if (items[i] == nullptr) + { + c = i+1; + break; + } + } + if(c >= itemCount) + { + return; + } + items[c] = item; + } + + void Menu::delItem(int index) + { + items[index] = nullptr; + } + + void Menu::replace(Menu m) + { + utils::MenuItem** i2 = m.getItems(); + for(int i = 0; i < itemCount; i ++) + { + items[i] = i2[i]; + } + } + bool Menu::equals(Menu* mnu2) { MenuItem** items2 = mnu2->getItems(); diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index e377b2235..0befe5126 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -3,7 +3,7 @@ namespace core::utils { - MenuItem::MenuItem(char* option, char* text, callback_t cb, void* ctx) : option(option), text(text), cb(cb), ctx(ctx) + MenuItem::MenuItem(void* head, void* term, char* option, char* text, callback_t cb, void* ctx) : head(head), term(term), option(option), text(text), cb(cb), ctx(ctx) { } diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index ab2e81b2f..d9a9af097 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -4,7 +4,7 @@ namespace core::utils { - SubMenu::SubMenu(char* option, char* text, callback_t cb, void* ctx, MenuItem** items) : option(option), text(text), cb(cb), ctx(ctx), items(items), MenuItem(option, text, cb, ctx)//SubMenu(option, text, cb, ctx, items) + SubMenu::SubMenu(void* head, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** items) : head(head), term(term), option(option), text(text), cb(cb), ctx(ctx), items(items), MenuItem(head, term, option, text, cb, ctx)//SubMenu(option, text, cb, ctx, items) { } @@ -27,6 +27,29 @@ namespace core::utils } items[c]->printStr(uart); } + uart.printf("e|exit\n\r"); + } + + SubMenu* SubMenu::getHead() + { + if(head) + { + return (utils::SubMenu*)head; + } + return nullptr; + + } + + void SubMenu::setItems(MenuItem** itms) + { + for(int i = 0; i < itemCount; i++) + { + if(itms[i] == nullptr) + { + break; + } + items[i] = itms[i]; + } } bool SubMenu::equals(SubMenu* sub2) @@ -53,4 +76,17 @@ namespace core::utils } return true; } + + void SubMenu::enter(io::UART& uart, void** args) + { + if(ctx){ + callback_t cb = (callback_t)ctx; + cb(uart, args); + } + } + + void SubMenu::exit(io::UART& uart, void** args) + { + cb(uart, args); + } } \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index a375c6918..f7b4f3a72 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -15,10 +15,12 @@ namespace core::utils Terminal::Terminal(io::UART& uart, utils::Menu* menu) : menu(menu), uart(uart) { uart.printf("\n\rStarting Terminal...\n\r"); + m = true; } - void Terminal::update(char* message) + void Terminal::update(char* message, utils::Menu m = nullptr) { + menu->replace(m); for(int i = 0; i < 5; i ++) { uart.printf("\n\r"); @@ -51,6 +53,22 @@ namespace core::utils } } + //TERMINAL specific print function + void Terminal::printTerm(io::UART& uart) + { + uart.printf("\n\r"); + uart.printf("Terminal:\n\r"); + if(m) + { + menu->printStr(uart); + } + else + { + current->printMStr(uart); + uart.printf("e|exit\n\r"); + } + } + void Terminal::process(utils::MenuItem* holder, char* tag) { utils::MenuItem** subitemsM = menu->getItems(); @@ -63,12 +81,13 @@ namespace core::utils char* op = subitemsM[i]->getOption(); if(strcmp(op, "null") == 0) { - break; + holder = nullptr; + return; } if(strcmp(tag,op) == 0) { holder = (subitemsM[i]); - break; + return; } } } @@ -79,14 +98,25 @@ namespace core::utils char* op = subitemsC[i]->getOption(); if(strcmp(op, "null") == 0) { - break; + holder = nullptr; + return; } if(strcmp(tag,op) == 0) { holder = (subitemsC[i]); - break; + return; } } } + + if(strcmp(tag, "e") == 0) + { + current->exit(uart, nullptr); + utils::SubMenu* h = current->getHead(); + if(h != nullptr) + { + current->replace(h); + } + } } } \ No newline at end of file From d7c16a9a7392e905ea81b2cdda8a29cc9bbe8415 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Sat, 1 Mar 2025 12:39:34 -0500 Subject: [PATCH 18/30] almost printing right again, missing final item callback execution broken --- include/core/utils/terminal/terminal.hpp | 2 +- samples/terminal/main.cpp | 60 +++++++++--------------- src/core/utils/terminal/terminal.cpp | 33 +++++++------ 3 files changed, 42 insertions(+), 53 deletions(-) diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index d975a982b..de80c0973 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -59,7 +59,7 @@ namespace core::utils */ void recieve(char** holder); - void process(utils::MenuItem* holder, char* tag); + utils::MenuItem* process(utils::MenuItem* holder, char* tag); //TERMINAL specific print function void printTerm(io::UART& uart); diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index 64bcad947..fe3dca7f7 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -10,13 +10,6 @@ namespace io = core::io; namespace utils = core::utils; -enum dataType -{ - CHAR, - ITEM, - TERMINAL -}; - //example callback that prints a test message void printCB(io::UART& uart, void** args) { @@ -39,22 +32,6 @@ void sendCB(io::UART& uart, void** args) } } -//This print is specifically for MENUITEMS, SUBMENUS, and MENUS -void print(io::UART& uart, utils::Menu item) -{ - item.printStr(uart); -} - -void print(io::UART& uart, utils::MenuItem item) -{ - item.printStr(uart); -} - -void print(io::UART& uart, utils::SubMenu item) -{ - item.printStr(uart); -} - int main() { //core setup @@ -64,7 +41,7 @@ int main() io::UART& uart = io::getUART(9600); //item list end item - utils::MenuItem nul = utils::MenuItem(nullptr,nullptr,"null","LIST END", nullptr, nullptr); + utils::MenuItem nul = utils::MenuItem(nullptr,nullptr,"null","LIST END\n\r", nullptr, nullptr); //make some items utils::MenuItem* items[10]; @@ -78,13 +55,13 @@ int main() void* m = &menu; void* t = &term; //a nullptr in the head parameter means it is part of the main menu - utils::MenuItem mainPrint = utils::MenuItem(nullptr,t,"p","print test",printCB,nullptr); - utils::MenuItem mainSend = utils::MenuItem(nullptr,t,"s","echo",sendCB,nullptr); + utils::MenuItem mainPrint = utils::MenuItem(nullptr,t,"p","print test\n\r",printCB,nullptr); + utils::MenuItem mainSend = utils::MenuItem(nullptr,t,"s","echo\n\r",sendCB,nullptr); //submenu cb is exit cb struct, ctx is void* to enter function cb struct. these are NOT responsible for moving menus, just for operations done duirng a menu move - utils::SubMenu sub = utils::SubMenu(nullptr,t,"sb","sub menu",nullptr,nullptr,items); + utils::SubMenu sub = utils::SubMenu(nullptr,t,"sb","sub menu\n\r",nullptr,nullptr,items); void* s = ⊂ - utils::MenuItem subPrint = utils::MenuItem(s,t,"p","print test",printCB,nullptr); - utils::MenuItem subSend = utils::MenuItem(s,t,"s","echo",sendCB,nullptr); + utils::MenuItem subPrint = utils::MenuItem(s,t,"p","print test\n\r",printCB,nullptr); + utils::MenuItem subSend = utils::MenuItem(s,t,"s","echo\n\r",sendCB,nullptr); //create submenu int c = menu.getCount(); @@ -100,35 +77,42 @@ int main() menu.addItem(&sub); menu.addItem(&nul); - term.printTerm(uart); - uart.printf("Usage: flagChar argument argument | max 9 arguments"); - callback_t cb; + //printTerm is how you display the current state of the terminal + + uart.printf("Usage: flagChar argument argument | max 9 arguments\n\r"); + + //forever while loop to constantly wait for user input while(true) { + term.printTerm(uart); + callback_t cb; char* inputList[10]; term.recieve(inputList); char* tag = inputList[0]; void* args[11]; - for(int i = 0; i < c; i ++) + for(int i = 0; i < 10; i ++) { if(inputList[i] == "\0") { - args[i-1] = nullptr; + args[i] = nullptr; break; } - args[i-1] = inputList[i]; + args[i] = inputList[i]; } args[10] = &term; utils::MenuItem* chosen = &nul; - term.process(chosen, tag); + chosen->printStr(uart); + + if(chosen == nullptr || strcmp(chosen->getOption(),"EXIT") == 0) + { + continue; + } cb = chosen->getcb(); cb(uart, args); - - term.printTerm(uart); } return 0; diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index f7b4f3a72..94d06ae51 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -69,54 +69,59 @@ namespace core::utils } } - void Terminal::process(utils::MenuItem* holder, char* tag) + utils::MenuItem* Terminal::process(utils::MenuItem* holder, char* tag) { - utils::MenuItem** subitemsM = menu->getItems(); - utils::MenuItem** subitemsC = current->getItems(); - if(m) { - for(int i = 0; i < 10; i ++) + utils::MenuItem** subitemsM = menu->getItems(); + + for(int i = 0; i < menu->getCount(); i ++) { char* op = subitemsM[i]->getOption(); if(strcmp(op, "null") == 0) { holder = nullptr; - return; + return holder; } if(strcmp(tag,op) == 0) { holder = (subitemsM[i]); - return; + return holder; } } } else { - for(int i = 0; i < 10; i ++) + utils::MenuItem** subitemsC = current->getItems(); + + for(int i = 0; i < menu->getCount(); i ++) { char* op = subitemsC[i]->getOption(); if(strcmp(op, "null") == 0) { holder = nullptr; - return; + return holder; } if(strcmp(tag,op) == 0) { holder = (subitemsC[i]); - return; + return holder; } } } if(strcmp(tag, "e") == 0) { - current->exit(uart, nullptr); - utils::SubMenu* h = current->getHead(); - if(h != nullptr) + current->exit(uart,nullptr); + void* h = current->getHead(); + if(h) { - current->replace(h); + current->replace((utils::SubMenu*)h); } + + utils::MenuItem out = utils::MenuItem(nullptr,nullptr,"EXIT","EXIT",nullptr,nullptr); + holder->replace(&out); + return holder; } } } \ No newline at end of file From bb893201c24ba14f596c71a1c81f014b074ec0e6 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Sat, 22 Mar 2025 11:27:46 -0400 Subject: [PATCH 19/30] running commands properly, not taking input well --- .gitmodules | 3 ++ include/core/utils/terminal/menu.hpp | 2 + include/core/utils/terminal/menuItem.hpp | 6 +-- include/core/utils/terminal/terminal.hpp | 6 ++- samples/terminal/main.cpp | 64 +++++++++--------------- src/core/utils/terminal/menu.cpp | 10 +++- src/core/utils/terminal/subMenu.cpp | 8 +-- src/core/utils/terminal/terminal.cpp | 27 +++++++--- 8 files changed, 70 insertions(+), 56 deletions(-) diff --git a/.gitmodules b/.gitmodules index 7eb25cc08..bfb04dd50 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ + + + [submodule "libs/canopen"] path = libs/canopen url = ../canopen-stack.git diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index a8ae87475..f799b2b62 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -36,6 +36,8 @@ namespace core::utils void addItem(utils::MenuItem* item); + void newItems(utils::MenuItem** itms); + void delItem(int index); diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 2fa281eff..d696375e3 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -4,7 +4,7 @@ //macro for max initial item count of submenus -using callback_t = void (*)(core::io::UART&, void**); +using callback_t = void (*)(core::io::UART&, char** inputList, void*); namespace core::utils { @@ -114,9 +114,9 @@ namespace core::utils */ MenuItem** getItems(){return items;} - void enter(io::UART& uart, void** args); + void enter(io::UART& uart, char** args); - void exit(io::UART& uart, void** args); + void exit(io::UART& uart, char** args); private: /** diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index de80c0973..2d739e4c2 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -59,7 +59,11 @@ namespace core::utils */ void recieve(char** holder); - utils::MenuItem* process(utils::MenuItem* holder, char* tag); + /** + * 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(io::UART& uart); diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index fe3dca7f7..e33fbda47 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -11,23 +11,18 @@ namespace io = core::io; namespace utils = core::utils; //example callback that prints a test message -void printCB(io::UART& uart, void** args) +void printCB(io::UART& uart, char** inputList, void* term) { uart.printf("\n\rTest Message\n\r"); } //example callback that echos user input(called send because e char is taken by exit) -void sendCB(io::UART& uart, void** args) +void sendCB(io::UART& uart, char** inputList, void* term) { uart.printf("\n\r"); for(int i = 1; i < 10; i ++) { - if(args[i] == nullptr) - { - i = 10; - continue; - } - uart.printf((char*)args[i]); + uart.printf(inputList[i]); uart.printf(" "); } } @@ -72,48 +67,39 @@ int main() sub.setItems(subs); //add main menu items to main menu - menu.addItem(&mainPrint); - menu.addItem(&mainSend); - menu.addItem(&sub); - menu.addItem(&nul); + items[0] = &mainPrint; + items[1] = &mainSend; + items[2] = ⊂ + items[3] = &nul; + + menu.newItems(items); //printTerm is how you display the current state of the terminal uart.printf("Usage: flagChar argument argument | max 9 arguments\n\r"); //forever while loop to constantly wait for user input + char* inputList[10]; + char* tag; while(true) { term.printTerm(uart); - callback_t cb; - char* inputList[10]; + term.recieve(inputList); - char* tag = inputList[0]; - void* args[11]; - for(int i = 0; i < 10; i ++) - { - if(inputList[i] == "\0") - { - args[i] = nullptr; - break; - } - args[i] = inputList[i]; - } - args[10] = &term; - - utils::MenuItem* chosen = &nul; - term.process(chosen, tag); - - chosen->printStr(uart); - - if(chosen == nullptr || strcmp(chosen->getOption(),"EXIT") == 0) - { - continue; - } - cb = chosen->getcb(); - - cb(uart, args); + + tag = inputList[0]; + //void* args[11]; + // for(int i = 0; i < 10; i ++) + // { + // args[i] = inputList[i]; + // } + //args[10] = &term; + + //term.process(tag, args); + + term.process(tag,inputList); } + return 0; } \ No newline at end of file diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index bd269ff98..62f208d30 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -36,7 +36,15 @@ namespace core::utils { return; } - items[c] = item; + items[c-1] = item; + } + + void Menu::newItems(utils::MenuItem** itms) + { + for(int i = 0; i < itemCount; i ++) + { + items[i] = itms[i]; + } } void Menu::delItem(int index) diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index d9a9af097..d4f160ece 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -77,16 +77,16 @@ namespace core::utils return true; } - void SubMenu::enter(io::UART& uart, void** args) + void SubMenu::enter(io::UART& uart, char** args) { if(ctx){ callback_t cb = (callback_t)ctx; - cb(uart, args); + cb(uart, args, term); } } - void SubMenu::exit(io::UART& uart, void** args) + void SubMenu::exit(io::UART& uart, char** args) { - cb(uart, args); + cb(uart, args, term); } } \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index 94d06ae51..e9642f966 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -69,24 +69,27 @@ namespace core::utils } } - utils::MenuItem* Terminal::process(utils::MenuItem* holder, char* tag) + void Terminal::process(char* tag, char** args) { + + utils::MenuItem* holder; if(m) { utils::MenuItem** subitemsM = menu->getItems(); - + for(int i = 0; i < menu->getCount(); i ++) { char* op = subitemsM[i]->getOption(); + if(strcmp(op, "null") == 0) { holder = nullptr; - return holder; + break; } - if(strcmp(tag,op) == 0) + else if(strcmp(tag,op) == 0) { holder = (subitemsM[i]); - return holder; + break; } } } @@ -100,12 +103,12 @@ namespace core::utils if(strcmp(op, "null") == 0) { holder = nullptr; - return holder; + break; } if(strcmp(tag,op) == 0) { holder = (subitemsC[i]); - return holder; + break; } } } @@ -121,7 +124,15 @@ namespace core::utils utils::MenuItem out = utils::MenuItem(nullptr,nullptr,"EXIT","EXIT",nullptr,nullptr); holder->replace(&out); - return holder; } + + if(holder == nullptr || strcmp(holder->getOption(),"EXIT") == 0) + { + return; + } + + callback_t cb = holder->getcb(); + + cb(uart, args, (void*)this); } } \ No newline at end of file From d3bebcb90d7cceaa003a029cb766162d23eea348 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Sat, 22 Mar 2025 13:41:47 -0400 Subject: [PATCH 20/30] submenus working, input fixed, needs cleranup --- include/core/utils/terminal/menuItem.hpp | 4 +- include/core/utils/terminal/terminal.hpp | 7 ++- samples/terminal/main.cpp | 19 ++++--- src/core/utils/terminal/subMenu.cpp | 11 ++-- src/core/utils/terminal/terminal.cpp | 66 +++++++++++++++--------- 5 files changed, 66 insertions(+), 41 deletions(-) diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index d696375e3..4e16d2a33 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -112,7 +112,7 @@ namespace core::utils /** * returns a list of all items contained in the submenu */ - MenuItem** getItems(){return items;} + MenuItem** getItems(){return sitems;} void enter(io::UART& uart, char** args); @@ -146,7 +146,7 @@ namespace core::utils /** * list of all items inside of the sub-menu */ - MenuItem** items; + MenuItem** sitems; /** * submenu or menu this item is in diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 2d739e4c2..cc7f756ca 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -35,6 +35,8 @@ namespace core::utils { return current; } + + void enterSub(io::UART& uart, char** args, void* thing); /** * sets current submenu to provided value * @param sub the submenu to replace the current one with @@ -57,7 +59,7 @@ namespace core::utils /** * proccesses incoming UART messages */ - void recieve(char** holder); + bool recieve(char** holder); /** * processes chosen menu item and runs its callback(if exit, exits into higher menu) @@ -66,7 +68,7 @@ namespace core::utils void process(char* tag, char** args); //TERMINAL specific print function - void printTerm(io::UART& uart); + void printTerm(); private: // menu instance @@ -78,6 +80,7 @@ namespace core::utils core::io::UART& uart; + char buffer[99]; // UART instance }; diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index e33fbda47..98013db08 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -27,6 +27,12 @@ void sendCB(io::UART& uart, char** inputList, void* term) } } +void subCB(io::UART& uart, char** inputList, void* term) +{ + utils::Terminal* t = (utils::Terminal*) term; + t->enterSub(uart,inputList,term); +} + int main() { //core setup @@ -38,29 +44,30 @@ int main() //item list end item utils::MenuItem nul = utils::MenuItem(nullptr,nullptr,"null","LIST END\n\r", nullptr, nullptr); - //make some items + //make some item lists utils::MenuItem* items[10]; for(int i = 0; i < 10; i ++) { items[i] = nullptr; } + utils::MenuItem* subs[3]; + //make empty main menu and terminal containing it utils::Menu menu = utils::Menu(items); utils::Terminal term = utils::Terminal(uart, &menu); void* m = &menu; void* t = &term; + //make items to fill terminal with //a nullptr in the head parameter means it is part of the main menu utils::MenuItem mainPrint = utils::MenuItem(nullptr,t,"p","print test\n\r",printCB,nullptr); utils::MenuItem mainSend = utils::MenuItem(nullptr,t,"s","echo\n\r",sendCB,nullptr); //submenu cb is exit cb struct, ctx is void* to enter function cb struct. these are NOT responsible for moving menus, just for operations done duirng a menu move - utils::SubMenu sub = utils::SubMenu(nullptr,t,"sb","sub menu\n\r",nullptr,nullptr,items); + utils::SubMenu sub = utils::SubMenu(nullptr,t,"sb","sub menu\n\r",subCB,nullptr,subs); void* s = ⊂ utils::MenuItem subPrint = utils::MenuItem(s,t,"p","print test\n\r",printCB,nullptr); utils::MenuItem subSend = utils::MenuItem(s,t,"s","echo\n\r",sendCB,nullptr); - //create submenu - int c = menu.getCount(); - utils::MenuItem* subs[c]; + //filling a submenu subs[0] = &subPrint; subs[1] = &subSend; subs[2] = &nul; @@ -83,7 +90,7 @@ int main() char* tag; while(true) { - term.printTerm(uart); + term.printTerm(); term.recieve(inputList); diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index d4f160ece..b32ab0251 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -4,7 +4,7 @@ namespace core::utils { - SubMenu::SubMenu(void* head, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** items) : head(head), term(term), option(option), text(text), cb(cb), ctx(ctx), items(items), MenuItem(head, term, option, text, cb, ctx)//SubMenu(option, text, cb, ctx, items) + SubMenu::SubMenu(void* head, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** sitems) : head(head), term(term), option(option), text(text), cb(cb), ctx(ctx), sitems(sitems), MenuItem(head, term, option, text, cb, ctx)//SubMenu(option, text, cb, ctx, items) { } @@ -21,13 +21,12 @@ namespace core::utils { for(int c = 0; c < itemCount; c ++) { - if(items[c]->getOption() == "null") + if(sitems[c]->getOption() == "null") { return; } - items[c]->printStr(uart); + sitems[c]->printStr(uart); } - uart.printf("e|exit\n\r"); } SubMenu* SubMenu::getHead() @@ -48,7 +47,7 @@ namespace core::utils { break; } - items[i] = itms[i]; + sitems[i] = itms[i]; } } @@ -69,7 +68,7 @@ namespace core::utils //Check items equivalence; for (int i = 0; i < itemCount; i ++) { - if(!(items[i]->equals(items2[i]))) + if(!(sitems[i]->equals(items2[i]))) { return false; } diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index e9642f966..dfde46792 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -34,30 +34,38 @@ namespace core::utils m = false; } - void Terminal::recieve(char* holder[10]) + bool Terminal::recieve(char* holder[10]) { - char buffer[99]; - char* argN[10]; - - uart.gets(buffer, 99); - - argN[0] = strtok(buffer, " "); - for(int i = 1; i < 10; i ++) + for(int i = 0; i < 99; i ++) { - argN[i] = strtok(NULL, " "); + buffer[i] = '\0'; } + uart.gets(buffer, 99); - for(int i = 0; i < 10; i++) + holder[0] = strtok(buffer, " "); + for(int i = 1; i < 10; i ++) { - holder[i] = argN[i]; + holder[i] = strtok(NULL, " "); } + return true; + // for(int i = 0; i < 10; i++) + // { + // holder[i] = argN[i]; + // } } //TERMINAL specific print function - void Terminal::printTerm(io::UART& uart) + void Terminal::printTerm() { uart.printf("\n\r"); uart.printf("Terminal:\n\r"); + uart.printf("is main: "); + if(m) + { + uart.printf("true\n\r"); + } + else{uart.printf("false\n\r");} + if(m) { menu->printStr(uart); @@ -65,13 +73,29 @@ namespace core::utils else { current->printMStr(uart); - uart.printf("e|exit\n\r"); + uart.printf("q|QUIT\n\r"); + } + } + + void Terminal::enterSub(io::UART& uart, char** args, void* thing) + { + utils::MenuItem** items = menu->getItems(); + char* name = args[0]; + int c = menu->getCount(); + utils::MenuItem* item; + for(int i = 0; i < c; i ++) + { + item = items[i]; + if(strcmp(name, item->getOption()) == 0) + { + setCurrent((utils::SubMenu*)item); + break; + } } } void Terminal::process(char* tag, char** args) { - utils::MenuItem* holder; if(m) { @@ -113,20 +137,12 @@ namespace core::utils } } - if(strcmp(tag, "e") == 0) + if(strcmp(tag, "q") == 0) { - current->exit(uart,nullptr); - void* h = current->getHead(); - if(h) - { - current->replace((utils::SubMenu*)h); - } - - utils::MenuItem out = utils::MenuItem(nullptr,nullptr,"EXIT","EXIT",nullptr,nullptr); - holder->replace(&out); + m = true; } - if(holder == nullptr || strcmp(holder->getOption(),"EXIT") == 0) + if(holder == nullptr || strcmp(holder->getOption(),"QUIT") == 0) { return; } From 1c21acda855eede28332dee18cc4380d09623825 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Thu, 27 Mar 2025 19:26:43 -0400 Subject: [PATCH 21/30] header comments done, still need some comments and need to tidy up sections --- include/core/utils/terminal/menu.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index f799b2b62..204b3c04d 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -27,6 +27,10 @@ namespace core::utils */ 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); /** @@ -34,10 +38,22 @@ namespace core::utils */ 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); From e9c339b285ad1e4729aeef1c4f606f54cf1f827e Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Thu, 27 Mar 2025 19:27:14 -0400 Subject: [PATCH 22/30] why is it making me put a second message? --- include/core/utils/terminal/menuItem.hpp | 65 +++++++++++++++++++++--- include/core/utils/terminal/terminal.hpp | 29 +++++++++-- src/core/utils/terminal/terminal.cpp | 2 +- 3 files changed, 83 insertions(+), 13 deletions(-) diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 4e16d2a33..f899f4fd9 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -4,6 +4,8 @@ //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* to the function executed using callback_t = void (*)(core::io::UART&, char** inputList, void*); namespace core::utils @@ -15,23 +17,45 @@ namespace core::utils * 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* head, void* term, char* option, char* text, callback_t cb, void* ctx = nullptr); + MenuItem(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx = nullptr); + /** + * option acessor + */ char* getOption(){return option;} - + + /** + * 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); /** @@ -66,7 +90,7 @@ namespace core::utils /** * submenu or menu this item is in */ - void* head; + void* parent; /** * terminal this is in @@ -80,7 +104,7 @@ namespace core::utils /** * constructor for sub-menu sub-class */ - SubMenu(void* head, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** items); + SubMenu(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** items); /** * unique overridden printStr() method for sub-menus @@ -104,9 +128,16 @@ namespace core::utils * returns itemCount */ int getCount(){return itemCount;} + + /** + * returns parent + */ + SubMenu* getParent(){return parent;} - SubMenu* getHead(); - + /** + * replaces current item list with provided one + * @param itms items to replace current list with + */ void setItems(MenuItem** itms); /** @@ -114,8 +145,28 @@ namespace core::utils */ 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 ctx 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 cb 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: @@ -151,7 +202,7 @@ namespace core::utils /** * submenu or menu this item is in */ - void* head; + void* parent; /** * terminal this is in diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index cc7f756ca..c11b5f6b0 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -21,22 +21,37 @@ namespace core::utils */ Terminal(io::UART& uart, Menu* menu); + /** + * returns uart instance used + */ io::UART& getUART() { return uart; } + /** + * returns main menu + */ Menu* getMenu() { return menu; } + /** + * returns current, will be nullptr if are not in a submenu + */ SubMenu* getCurrent() { return current; } - void enterSub(io::UART& uart, char** args, void* thing); + /** + * 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 submenu to provided value * @param sub the submenu to replace the current one with @@ -58,6 +73,7 @@ namespace core::utils /** * proccesses incoming UART messages + * @param holder list to store tokenized strings in, should be full of "/0" to start */ bool recieve(char** holder); @@ -74,14 +90,17 @@ namespace core::utils // menu instance Menu* menu; + //current submenu, if there is one, nullptr otherwise SubMenu* current; - + + //true if currently in main menu bool m; + //uart instance this terminal is using core::io::UART& uart; - - char buffer[99]; - // UART instance + + //char buffer for UART entry + char buffer[99]; }; } diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index dfde46792..6a37645e2 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -77,7 +77,7 @@ namespace core::utils } } - void Terminal::enterSub(io::UART& uart, char** args, void* thing) + void Terminal::enterSub(io::UART& uart, char** args, void* term) { utils::MenuItem** items = menu->getItems(); char* name = args[0]; From 9afb8ad8f654b4e4c10061558affe29590fd9ccf Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Thu, 27 Mar 2025 19:47:06 -0400 Subject: [PATCH 23/30] main file has instruction comments now --- include/core/utils/terminal/menuItem.hpp | 12 +++++- samples/terminal/main.cpp | 50 +++++++++++++++++------- src/core/utils/terminal/menu.cpp | 2 +- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index f899f4fd9..1b9e819db 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -5,7 +5,8 @@ //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* to the function executed +//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*); namespace core::utils @@ -24,7 +25,7 @@ namespace core::utils * @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 = nullptr); + MenuItem(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx); /** * option acessor @@ -103,6 +104,13 @@ namespace core::utils 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); diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index 98013db08..aa1d11a2e 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -27,8 +27,10 @@ void sendCB(io::UART& uart, char** inputList, void* term) } } +//example submenu entercb void subCB(io::UART& uart, char** inputList, void* term) { + //enters submenu with no other behavior utils::Terminal* t = (utils::Terminal*) term; t->enterSub(uart,inputList,term); } @@ -46,24 +48,42 @@ int main() //make some item lists utils::MenuItem* items[10]; + + //this is really optional for(int i = 0; i < 10; i ++) { items[i] = nullptr; } + + //list of submenu items utils::MenuItem* subs[3]; //make empty main menu and terminal containing it + utils::Menu menu = utils::Menu(items); utils::Terminal term = utils::Terminal(uart, &menu); + + //void* to both for use in thier nodes void* m = &menu; void* t = &term; + //make items to fill terminal with //a nullptr in the head parameter means it is part of the main menu utils::MenuItem mainPrint = utils::MenuItem(nullptr,t,"p","print test\n\r",printCB,nullptr); utils::MenuItem mainSend = utils::MenuItem(nullptr,t,"s","echo\n\r",sendCB,nullptr); - //submenu cb is exit cb struct, ctx is void* to enter function cb struct. these are NOT responsible for moving menus, just for operations done duirng a menu move + + /** + * submenu cb is exit cb struct, ctx is void* to enter function cb struct/function. + * these are NOT responsible for moving menus, just for operations done duirng a menu move + * this example: when we enter sub, subCB is executed, this is a cb defined in main that just runs enterCB, this is just the bare minimum + * but it ideally would also contain your custom enterence behavior + * when we exit, ctx is nullptr so only the normal submenu exit is used + * this could also be a void* to a cb struct/function + */ utils::SubMenu sub = utils::SubMenu(nullptr,t,"sb","sub menu\n\r",subCB,nullptr,subs); void* s = ⊂ + + //more example menuitems, this time for the submenu utils::MenuItem subPrint = utils::MenuItem(s,t,"p","print test\n\r",printCB,nullptr); utils::MenuItem subSend = utils::MenuItem(s,t,"s","echo\n\r",sendCB,nullptr); @@ -71,6 +91,8 @@ int main() subs[0] = &subPrint; subs[1] = &subSend; subs[2] = &nul; + + //set submenu to updated list sub.setItems(subs); //add main menu items to main menu @@ -79,34 +101,34 @@ int main() items[2] = ⊂ items[3] = &nul; + //set menu items to the updated list menu.newItems(items); - //printTerm is how you display the current state of the terminal - + //usage instructions to print before terminal fully starts uart.printf("Usage: flagChar argument argument | max 9 arguments\n\r"); - //forever while loop to constantly wait for user input + //forever while loop to constantly wait for user input inside of recieve() + //create empty list of strings char* inputList[10]; + + //empty string to store input for item selection char* tag; + while(true) { + //print current state of terminal term.printTerm(); + //recieve input term.recieve(inputList); + //set tag to inputted selection string tag = inputList[0]; - //void* args[11]; - // for(int i = 0; i < 10; i ++) - // { - // args[i] = inputList[i]; - // } - //args[10] = &term; - - //term.process(tag, args); - + + //process input, this uses input to find chosen item, and execute/enter it term.process(tag,inputList); } - + //this should never happen return 0; } \ No newline at end of file diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 62f208d30..55b7eff41 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -6,7 +6,7 @@ namespace core::utils { Menu::Menu(MenuItem** items) : items(items) { - + //constructor } void Menu::printStr(core::io::UART& uart) From 4299ff3f0c7042afb6e10504b4f9dfefeed21bd3 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Sat, 29 Mar 2025 12:27:27 -0400 Subject: [PATCH 24/30] comments done --- include/core/utils/terminal/menuItem.hpp | 13 ++++++--- include/core/utils/terminal/terminal.hpp | 1 + src/core/utils/terminal/menu.cpp | 21 +++++++++++--- src/core/utils/terminal/menuItem.cpp | 8 ++++-- src/core/utils/terminal/subMenu.cpp | 35 ++++++++++-------------- src/core/utils/terminal/terminal.cpp | 26 ++++++++++++++++-- 6 files changed, 71 insertions(+), 33 deletions(-) diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 1b9e819db..a66fd3cb5 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -31,6 +31,11 @@ namespace core::utils * option acessor */ char* getOption(){return option;} + + /** + * returns parent + */ + void* getParent(){return parent;} /** * text acessor @@ -140,7 +145,7 @@ namespace core::utils /** * returns parent */ - SubMenu* getParent(){return parent;} + void* getParent(){return parent;} /** * replaces current item list with provided one @@ -162,7 +167,7 @@ namespace core::utils /** * automatic callback executor for entering * custom behavor is stored in ctx void* - * if ctx is empty will do nothing + * if cb is empty will do nothing * @param uart uart instance to use for cb * @param args arguments for cb */ @@ -170,8 +175,8 @@ namespace core::utils /** * automatic callback executor for exiting - * custom behavor is stored in cb - * if cb is nullptr will do nothing + * 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 */ diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index c11b5f6b0..6ce7126eb 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -66,6 +66,7 @@ namespace core::utils /** * Sends a provided message over UART * also replaces menu with provided menu + * used to reset menu to desired state and send a message * @param message a string message to send via UART * @param menu the menu to replace the current head node with */ diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 55b7eff41..af59f7e16 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -11,6 +11,7 @@ namespace core::utils void Menu::printStr(core::io::UART& uart) { + //loops through item list, stops when it hits an item with an option string of "null" for(int c = 0; c < itemCount; c ++) { if(items[c]->getOption() == "null") @@ -23,6 +24,7 @@ namespace core::utils void Menu::addItem(utils::MenuItem* item) { + //find next empty space int c =0; for(int i = 0; i < itemCount; i ++) { @@ -32,15 +34,18 @@ namespace core::utils break; } } + //make sure next space is within bounds if(c >= itemCount) { return; } + //if in bounds place in list, if not return nothing items[c-1] = item; } void Menu::newItems(utils::MenuItem** itms) { + //replace each item in list with corresponding one in new list for(int i = 0; i < itemCount; i ++) { items[i] = itms[i]; @@ -49,24 +54,32 @@ namespace core::utils void Menu::delItem(int index) { + //set desired index to null items[index] = nullptr; + //shift every higher index down + for(int i = index; i < itemCount-1; i ++) + { + items[index] = items[index + 1]; + }\ + //set highest index to null to ensure there is no duplicate final item + items[itemCount-1] = nullptr; } void Menu::replace(Menu m) { + //get item list of new menu utils::MenuItem** i2 = m.getItems(); - for(int i = 0; i < itemCount; i ++) - { - items[i] = i2[i]; - } + newItems(i2); } bool Menu::equals(Menu* mnu2) { + //get second list MenuItem** items2 = mnu2->getItems(); for (int i = 0; i < itemCount; i ++) { + //check if corresponding items are equal, if one fail return false if(!(items[i]->equals(items2[i]))) { return false; diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index 0befe5126..1072d5a26 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -3,13 +3,14 @@ namespace core::utils { - MenuItem::MenuItem(void* head, void* term, char* option, char* text, callback_t cb, void* ctx) : head(head), term(term), option(option), text(text), cb(cb), ctx(ctx) + MenuItem::MenuItem(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx) : parent(parent), term(term), option(option), text(text), cb(cb), ctx(ctx) { - + //constructor } void MenuItem::printStr(core::io::UART& uart) { + //print in form of op | text uart.printf(option); uart.printf("|"); uart.printf(text); @@ -17,6 +18,7 @@ namespace core::utils void MenuItem::replace(MenuItem* newItem) { + //replace with new items info option = newItem->getOption(); text = newItem->getText(); cb = newItem->getcb(); @@ -25,10 +27,12 @@ namespace core::utils bool MenuItem::equals(MenuItem* it2) { + //get option string, text, cb, and ctx of 2nd item char* option2 = it2->getOption(); char* text2 = it2->getText(); callback_t cb2 = it2->getcb(); void* ctx2 = it2->getctx(); + //if any corresponding arent equal, return false if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { return false; diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index b32ab0251..dcc304bc9 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -4,14 +4,15 @@ namespace core::utils { - SubMenu::SubMenu(void* head, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** sitems) : head(head), term(term), option(option), text(text), cb(cb), ctx(ctx), sitems(sitems), MenuItem(head, term, option, text, cb, ctx)//SubMenu(option, text, cb, ctx, items) + SubMenu::SubMenu(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** sitems) : parent(parent), term(term), option(option), text(text), cb(cb), ctx(ctx), sitems(sitems), MenuItem(parent, term, option, text, cb, ctx) { - + //constructor } void SubMenu::printStr(core::io::UART& uart) { //this is exclusively for displaying the submenu inside of a menu list + // option | text uart.printf(option); uart.printf("|"); uart.printf(text); @@ -19,6 +20,8 @@ namespace core::utils void SubMenu::printMStr(core::io::UART& uart) { + //used for printing this as the current menu state + //prints all items in list until it hits one with option string "null" for(int c = 0; c < itemCount; c ++) { if(sitems[c]->getOption() == "null") @@ -29,24 +32,12 @@ namespace core::utils } } - SubMenu* SubMenu::getHead() - { - if(head) - { - return (utils::SubMenu*)head; - } - return nullptr; - - } - void SubMenu::setItems(MenuItem** itms) { + //replaces corresponding items in sitems with itms + //used to replace item list, or certain items by inputing an altered item list for(int i = 0; i < itemCount; i++) { - if(itms[i] == nullptr) - { - break; - } sitems[i] = itms[i]; } } @@ -78,14 +69,16 @@ namespace core::utils void SubMenu::enter(io::UART& uart, char** args) { - if(ctx){ - callback_t cb = (callback_t)ctx; - cb(uart, args, term); - } + cb(uart,args,term); } void SubMenu::exit(io::UART& uart, char** args) { - cb(uart, args, term); + //if ctx has value use it as a callback + if(ctx) + { + callback_t cb = (callback_t)ctx; + cb(uart, args, term); + } } } \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index 6a37645e2..bce6100d0 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -14,39 +14,49 @@ namespace core::utils { Terminal::Terminal(io::UART& uart, utils::Menu* menu) : menu(menu), uart(uart) { + //start message uart.printf("\n\rStarting Terminal...\n\r"); + //set main menu flag to true m = true; } - void Terminal::update(char* message, utils::Menu m = nullptr) + void Terminal::update(char* message, utils::Menu m) { + //replace menu with provided menu->replace(m); + //print spacer lines for(int i = 0; i < 5; i ++) { uart.printf("\n\r"); } + //print uart.printf(message); } void Terminal::setCurrent(SubMenu* sub) { + //sets current to input, sets main menu flag to false current = sub; m = false; } bool Terminal::recieve(char* holder[10]) { + //fill buffer with null chars for(int i = 0; i < 99; i ++) { buffer[i] = '\0'; } + //fill buffer uart.gets(buffer, 99); + //tokenize holder[0] = strtok(buffer, " "); for(int i = 1; i < 10; i ++) { holder[i] = strtok(NULL, " "); } + //return true, this is placeholder for ensuring a complete message return true; // for(int i = 0; i < 10; i++) // { @@ -57,6 +67,7 @@ namespace core::utils //TERMINAL specific print function void Terminal::printTerm() { + //print header and some info uart.printf("\n\r"); uart.printf("Terminal:\n\r"); uart.printf("is main: "); @@ -66,10 +77,12 @@ namespace core::utils } else{uart.printf("false\n\r");} + //print menu if(m) { menu->printStr(uart); } + //or submenu else { current->printMStr(uart); @@ -83,11 +96,13 @@ namespace core::utils char* name = args[0]; int c = menu->getCount(); utils::MenuItem* item; + //find chosen item for(int i = 0; i < c; i ++) { item = items[i]; if(strcmp(name, item->getOption()) == 0) { + //cast item to submenu and place in current setCurrent((utils::SubMenu*)item); break; } @@ -96,9 +111,11 @@ namespace core::utils void Terminal::process(char* tag, char** args) { + //different behavior for menu and submenu utils::MenuItem* holder; if(m) { + //find item in menu utils::MenuItem** subitemsM = menu->getItems(); for(int i = 0; i < menu->getCount(); i ++) @@ -119,6 +136,7 @@ namespace core::utils } else { + //find item in submenu utils::MenuItem** subitemsC = current->getItems(); for(int i = 0; i < menu->getCount(); i ++) @@ -137,16 +155,20 @@ namespace core::utils } } + //check if quit flag, if quit set main menu flag to true + //this brings us back to main menu, current sill still have a value but this gets overwritten next menu move if(strcmp(tag, "q") == 0) { m = true; } - if(holder == nullptr || strcmp(holder->getOption(),"QUIT") == 0) + //if quit flag, kick out of process, + if(holder == nullptr || strcmp(holder->getOption(),"q") == 0) { return; } + //get and run callback of chosen item callback_t cb = holder->getcb(); cb(uart, args, (void*)this); From 21f3d7ed7818be34052ba84fb7ef3990c825f967 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 <112026704+RyanLeifer04@users.noreply.github.com> Date: Sat, 29 Mar 2025 14:29:00 -0400 Subject: [PATCH 25/30] Update terminal.cpp, include was capitalized wrong --- src/core/utils/terminal/terminal.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index bce6100d0..c6cca3bc2 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -4,8 +4,8 @@ #include #include #include -#include -#include +#include +#include #include namespace io = core::io; @@ -173,4 +173,4 @@ namespace core::utils cb(uart, args, (void*)this); } -} \ No newline at end of file +} From d76b916b77f2ccf656469a0537d5128b1c922979 Mon Sep 17 00:00:00 2001 From: GitHub Build Date: Sat, 29 Mar 2025 18:30:28 +0000 Subject: [PATCH 26/30] Applied Formatting Changes During GitHub Build --- include/core/utils/terminal/menu.hpp | 147 ++++---- include/core/utils/terminal/menuItem.hpp | 447 ++++++++++++----------- include/core/utils/terminal/terminal.hpp | 193 +++++----- samples/terminal/main.cpp | 124 +++---- src/core/utils/terminal/menu.cpp | 130 +++---- src/core/utils/terminal/menuItem.cpp | 67 ++-- src/core/utils/terminal/subMenu.cpp | 122 +++---- src/core/utils/terminal/terminal.cpp | 261 ++++++------- 8 files changed, 712 insertions(+), 779 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 204b3c04d..741bc8462 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -1,82 +1,79 @@ #ifndef EVT_TERM_MENU #define EVT_TERM_MENU -//macro for max initial item count of a main menu +// macro for max initial item count of a main menu #include - -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 { +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 \ No newline at end of file diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index a66fd3cb5..9c7016f8f 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -2,226 +2,237 @@ #define EVT_TERM_MENUITEM #include -//macro for max initial item count of submenus +// 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*) +// 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*); -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); - - /** - * 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 - { - 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); - - /** - * 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 - */ - void* parent; - - /** - * terminal this is in - */ - void* term; - }; -} +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); + + /** + * 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 { +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); + + /** + * 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 + */ + void* parent; + + /** + * terminal this is in + */ + void* term; +}; +} // namespace core::utils #endif \ No newline at end of file diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 6ce7126eb..d53594f31 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -7,103 +7,100 @@ #include #include -namespace core::utils -{ - - class Terminal - { - 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 baud 9600 or 115200 - */ - Terminal(io::UART& uart, Menu* menu); - - /** - * returns uart instance used - */ - io::UART& getUART() - { - return uart; - } - - /** - * returns main menu - */ - Menu* getMenu() - { - return menu; - } - - /** - * returns current, will be nullptr if are not in a submenu - */ - SubMenu* getCurrent() - { - return current; - } - - /** - * 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 submenu to provided value - * @param sub the submenu to replace the current one with - */ - void setCurrent(SubMenu* sub); - - /** - * checks if the terminal is still on the main menu - */ - bool isMain(){return m;} - - /** - * Sends a provided message over UART - * also replaces menu with provided menu - * used to reset menu to desired state and send a message - * @param message a string message to send via UART - * @param menu the menu to replace the current head node 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 - SubMenu* 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 { + +class Terminal { +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 baud 9600 or 115200 + */ + Terminal(io::UART& uart, Menu* menu); + + /** + * returns uart instance used + */ + io::UART& getUART() { + return uart; + } + + /** + * returns main menu + */ + Menu* getMenu() { + return menu; + } + + /** + * returns current, will be nullptr if are not in a submenu + */ + SubMenu* getCurrent() { + return current; + } + + /** + * 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 submenu to provided value + * @param sub the submenu to replace the current one with + */ + void setCurrent(SubMenu* sub); + + /** + * checks if the terminal is still on the main menu + */ + bool isMain() { + return m; + } + + /** + * Sends a provided message over UART + * also replaces menu with provided menu + * used to reset menu to desired state and send a message + * @param message a string message to send via UART + * @param menu the menu to replace the current head node 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 + SubMenu* 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 \ No newline at end of file diff --git a/samples/terminal/main.cpp b/samples/terminal/main.cpp index aa1d11a2e..74de84876 100644 --- a/samples/terminal/main.cpp +++ b/samples/terminal/main.cpp @@ -1,134 +1,126 @@ -#include -#include -#include +#include #include #include #include +#include +#include +#include #include -#include -namespace io = core::io; -namespace utils = core::utils; +namespace io = core::io; +namespace utils = core::utils; -//example callback that prints a test message -void printCB(io::UART& uart, char** inputList, void* term) -{ +// example callback that prints a test message +void printCB(io::UART& uart, char** inputList, void* term) { uart.printf("\n\rTest Message\n\r"); } -//example callback that echos user input(called send because e char is taken by exit) -void sendCB(io::UART& uart, char** inputList, void* term) -{ +// example callback that echos user input(called send because e char is taken by exit) +void sendCB(io::UART& uart, char** inputList, void* term) { uart.printf("\n\r"); - for(int i = 1; i < 10; i ++) - { + for (int i = 1; i < 10; i++) { uart.printf(inputList[i]); uart.printf(" "); } } -//example submenu entercb -void subCB(io::UART& uart, char** inputList, void* term) -{ - //enters submenu with no other behavior +// example submenu entercb +void subCB(io::UART& uart, char** inputList, void* term) { + // enters submenu with no other behavior utils::Terminal* t = (utils::Terminal*) term; - t->enterSub(uart,inputList,term); + t->enterSub(uart, inputList, term); } -int main() -{ - //core setup +int main() { + // core setup core::platform::init(); - //UART setup + // UART setup io::UART& uart = io::getUART(9600); - //item list end item - utils::MenuItem nul = utils::MenuItem(nullptr,nullptr,"null","LIST END\n\r", nullptr, nullptr); + // item list end item + utils::MenuItem nul = utils::MenuItem(nullptr, nullptr, "null", "LIST END\n\r", nullptr, nullptr); - //make some item lists + // make some item lists utils::MenuItem* items[10]; - //this is really optional - for(int i = 0; i < 10; i ++) - { + // this is really optional + for (int i = 0; i < 10; i++) { items[i] = nullptr; } - //list of submenu items + // list of submenu items utils::MenuItem* subs[3]; - - //make empty main menu and terminal containing it - utils::Menu menu = utils::Menu(items); + // make empty main menu and terminal containing it + + utils::Menu menu = utils::Menu(items); utils::Terminal term = utils::Terminal(uart, &menu); - //void* to both for use in thier nodes + // void* to both for use in thier nodes void* m = &menu; void* t = &term; - //make items to fill terminal with - //a nullptr in the head parameter means it is part of the main menu - utils::MenuItem mainPrint = utils::MenuItem(nullptr,t,"p","print test\n\r",printCB,nullptr); - utils::MenuItem mainSend = utils::MenuItem(nullptr,t,"s","echo\n\r",sendCB,nullptr); + // make items to fill terminal with + // a nullptr in the head parameter means it is part of the main menu + utils::MenuItem mainPrint = utils::MenuItem(nullptr, t, "p", "print test\n\r", printCB, nullptr); + utils::MenuItem mainSend = utils::MenuItem(nullptr, t, "s", "echo\n\r", sendCB, nullptr); /** - * submenu cb is exit cb struct, ctx is void* to enter function cb struct/function. + * submenu cb is exit cb struct, ctx is void* to enter function cb struct/function. * these are NOT responsible for moving menus, just for operations done duirng a menu move - * this example: when we enter sub, subCB is executed, this is a cb defined in main that just runs enterCB, this is just the bare minimum - * but it ideally would also contain your custom enterence behavior - * when we exit, ctx is nullptr so only the normal submenu exit is used - * this could also be a void* to a cb struct/function + * this example: when we enter sub, subCB is executed, this is a cb defined in main that just runs enterCB, this is + * just the bare minimum but it ideally would also contain your custom enterence behavior when we exit, ctx is + * nullptr so only the normal submenu exit is used this could also be a void* to a cb struct/function */ - utils::SubMenu sub = utils::SubMenu(nullptr,t,"sb","sub menu\n\r",subCB,nullptr,subs); - void* s = ⊂ + utils::SubMenu sub = utils::SubMenu(nullptr, t, "sb", "sub menu\n\r", subCB, nullptr, subs); + void* s = ⊂ - //more example menuitems, this time for the submenu - utils::MenuItem subPrint = utils::MenuItem(s,t,"p","print test\n\r",printCB,nullptr); - utils::MenuItem subSend = utils::MenuItem(s,t,"s","echo\n\r",sendCB,nullptr); + // more example menuitems, this time for the submenu + utils::MenuItem subPrint = utils::MenuItem(s, t, "p", "print test\n\r", printCB, nullptr); + utils::MenuItem subSend = utils::MenuItem(s, t, "s", "echo\n\r", sendCB, nullptr); - //filling a submenu + // filling a submenu subs[0] = &subPrint; subs[1] = &subSend; subs[2] = &nul; - //set submenu to updated list + // set submenu to updated list sub.setItems(subs); - //add main menu items to main menu + // add main menu items to main menu items[0] = &mainPrint; items[1] = &mainSend; items[2] = ⊂ items[3] = &nul; - //set menu items to the updated list + // set menu items to the updated list menu.newItems(items); - //usage instructions to print before terminal fully starts + // usage instructions to print before terminal fully starts uart.printf("Usage: flagChar argument argument | max 9 arguments\n\r"); - //forever while loop to constantly wait for user input inside of recieve() - //create empty list of strings + // forever while loop to constantly wait for user input inside of recieve() + // create empty list of strings char* inputList[10]; - //empty string to store input for item selection + // empty string to store input for item selection char* tag; - - while(true) - { - //print current state of terminal + + while (true) { + // print current state of terminal term.printTerm(); - - //recieve input + + // recieve input term.recieve(inputList); - //set tag to inputted selection string + // set tag to inputted selection string tag = inputList[0]; - //process input, this uses input to find chosen item, and execute/enter it - term.process(tag,inputList); + // process input, this uses input to find chosen item, and execute/enter it + term.process(tag, inputList); } - //this should never happen + // this should never happen return 0; } \ No newline at end of file diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index af59f7e16..3033d5fe1 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -1,90 +1,72 @@ -#include -#include #include +#include +#include -namespace core::utils -{ - Menu::Menu(MenuItem** items) : items(items) - { - //constructor - } - - void Menu::printStr(core::io::UART& uart) - { - //loops through item list, stops when it hits an item with an option string of "null" - for(int c = 0; c < itemCount; c ++) - { - if(items[c]->getOption() == "null") - { - return; - } - items[c]->printStr(uart); - } - } +namespace core::utils { +Menu::Menu(MenuItem** items) : items(items) { + // constructor +} - void Menu::addItem(utils::MenuItem* item) - { - //find next empty space - int c =0; - for(int i = 0; i < itemCount; i ++) - { - if (items[i] == nullptr) - { - c = i+1; - break; - } - } - //make sure next space is within bounds - if(c >= itemCount) - { +void Menu::printStr(core::io::UART& uart) { + // loops through item list, stops when it hits an item with an option string of "null" + for (int c = 0; c < itemCount; c++) { + if (items[c]->getOption() == "null") { return; } - //if in bounds place in list, if not return nothing - items[c-1] = item; + items[c]->printStr(uart); } +} - void Menu::newItems(utils::MenuItem** itms) - { - //replace each item in list with corresponding one in new list - for(int i = 0; i < itemCount; i ++) - { - items[i] = itms[i]; +void Menu::addItem(utils::MenuItem* item) { + // find next empty space + int c = 0; + for (int i = 0; i < itemCount; i++) { + if (items[i] == nullptr) { + c = i + 1; + break; } } - - void Menu::delItem(int index) - { - //set desired index to null - items[index] = nullptr; - //shift every higher index down - for(int i = index; i < itemCount-1; i ++) - { - items[index] = items[index + 1]; - }\ - //set highest index to null to ensure there is no duplicate final item - items[itemCount-1] = nullptr; + // make sure next space is within bounds + if (c >= itemCount) { + return; } + // if in bounds place in list, if not return nothing + items[c - 1] = item; +} - void Menu::replace(Menu m) - { - //get item list of new menu - utils::MenuItem** i2 = m.getItems(); - newItems(i2); +void Menu::newItems(utils::MenuItem** itms) { + // replace each item in list with corresponding one in new list + for (int i = 0; i < itemCount; i++) { + items[i] = itms[i]; } +} + +void Menu::delItem(int index) { + // set desired index to null + items[index] = nullptr; + // shift every higher index down + for (int i = index; i < itemCount - 1; i++) { + items[index] = items[index + 1]; + } // set highest index to null to ensure there is no duplicate final item + items[itemCount - 1] = nullptr; +} + +void Menu::replace(Menu m) { + // get item list of new menu + utils::MenuItem** i2 = m.getItems(); + newItems(i2); +} - bool Menu::equals(Menu* mnu2) - { - //get second list - MenuItem** items2 = mnu2->getItems(); +bool Menu::equals(Menu* mnu2) { + // get second list + MenuItem** items2 = mnu2->getItems(); - for (int i = 0; i < itemCount; i ++) - { - //check if corresponding items are equal, if one fail return false - if(!(items[i]->equals(items2[i]))) - { - return false; - } + for (int i = 0; i < itemCount; i++) { + // check if corresponding items are equal, if one fail return false + if (!(items[i]->equals(items2[i]))) { + return false; } - return true; } -} \ No newline at end of file + return true; +} +} // namespace core::utils \ No newline at end of file diff --git a/src/core/utils/terminal/menuItem.cpp b/src/core/utils/terminal/menuItem.cpp index 1072d5a26..73ae15c5b 100644 --- a/src/core/utils/terminal/menuItem.cpp +++ b/src/core/utils/terminal/menuItem.cpp @@ -1,42 +1,37 @@ -#include #include -namespace core::utils -{ +#include +namespace core::utils { - MenuItem::MenuItem(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx) : parent(parent), term(term), option(option), text(text), cb(cb), ctx(ctx) - { - //constructor - } +MenuItem::MenuItem(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx) + : parent(parent), term(term), option(option), text(text), cb(cb), ctx(ctx) { + // constructor +} - void MenuItem::printStr(core::io::UART& uart) - { - //print in form of op | text - uart.printf(option); - uart.printf("|"); - uart.printf(text); - } +void MenuItem::printStr(core::io::UART& uart) { + // print in form of op | text + uart.printf(option); + uart.printf("|"); + uart.printf(text); +} - void MenuItem::replace(MenuItem* newItem) - { - //replace with new items info - option = newItem->getOption(); - text = newItem->getText(); - cb = newItem->getcb(); - ctx = newItem->getctx(); - } +void MenuItem::replace(MenuItem* newItem) { + // replace with new items info + option = newItem->getOption(); + text = newItem->getText(); + cb = newItem->getcb(); + ctx = newItem->getctx(); +} - bool MenuItem::equals(MenuItem* it2) - { - //get option string, text, cb, and ctx of 2nd item - char* option2 = it2->getOption(); - char* text2 = it2->getText(); - callback_t cb2 = it2->getcb(); - void* ctx2 = it2->getctx(); - //if any corresponding arent equal, return false - if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) - { - return false; - } - return true; +bool MenuItem::equals(MenuItem* it2) { + // get option string, text, cb, and ctx of 2nd item + char* option2 = it2->getOption(); + char* text2 = it2->getText(); + callback_t cb2 = it2->getcb(); + void* ctx2 = it2->getctx(); + // if any corresponding arent equal, return false + if (option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { + return false; } -} \ No newline at end of file + return true; +} +} // namespace core::utils \ No newline at end of file diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp index dcc304bc9..f3391411a 100644 --- a/src/core/utils/terminal/subMenu.cpp +++ b/src/core/utils/terminal/subMenu.cpp @@ -1,84 +1,70 @@ -#include #include +#include +namespace core::utils { +SubMenu::SubMenu(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** sitems) + : parent(parent), term(term), option(option), text(text), cb(cb), ctx(ctx), sitems(sitems), + MenuItem(parent, term, option, text, cb, ctx) { + // constructor +} -namespace core::utils -{ - SubMenu::SubMenu(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** sitems) : parent(parent), term(term), option(option), text(text), cb(cb), ctx(ctx), sitems(sitems), MenuItem(parent, term, option, text, cb, ctx) - { - //constructor - } +void SubMenu::printStr(core::io::UART& uart) { + // this is exclusively for displaying the submenu inside of a menu list + // option | text + uart.printf(option); + uart.printf("|"); + uart.printf(text); +} - void SubMenu::printStr(core::io::UART& uart) - { - //this is exclusively for displaying the submenu inside of a menu list - // option | text - uart.printf(option); - uart.printf("|"); - uart.printf(text); +void SubMenu::printMStr(core::io::UART& uart) { + // used for printing this as the current menu state + // prints all items in list until it hits one with option string "null" + for (int c = 0; c < itemCount; c++) { + if (sitems[c]->getOption() == "null") { + return; + } + sitems[c]->printStr(uart); } +} - void SubMenu::printMStr(core::io::UART& uart) - { - //used for printing this as the current menu state - //prints all items in list until it hits one with option string "null" - for(int c = 0; c < itemCount; c ++) - { - if(sitems[c]->getOption() == "null") - { - return; - } - sitems[c]->printStr(uart); - } +void SubMenu::setItems(MenuItem** itms) { + // replaces corresponding items in sitems with itms + // used to replace item list, or certain items by inputing an altered item list + for (int i = 0; i < itemCount; i++) { + sitems[i] = itms[i]; } +} - void SubMenu::setItems(MenuItem** itms) - { - //replaces corresponding items in sitems with itms - //used to replace item list, or certain items by inputing an altered item list - for(int i = 0; i < itemCount; i++) - { - sitems[i] = itms[i]; - } +bool SubMenu::equals(SubMenu* sub2) { + // check equivalence of attributes besides items + // this section is identical to menuItem + char* option2 = sub2->getOption(); + char* text2 = sub2->getText(); + callback_t cb2 = sub2->getcb(); + void* ctx2 = sub2->getctx(); + MenuItem** items2 = sub2->getItems(); + if (option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { + return false; } - bool SubMenu::equals(SubMenu* sub2) - { - //check equivalence of attributes besides items - //this section is identical to menuItem - char* option2 = sub2->getOption(); - char* text2 = sub2->getText(); - callback_t cb2 = sub2->getcb(); - void* ctx2 = sub2->getctx(); - MenuItem** items2 = sub2->getItems(); - if(option != option2 || text != text2 || cb != cb2 || ctx != ctx2) - { + // Check items equivalence; + for (int i = 0; i < itemCount; i++) { + if (!(sitems[i]->equals(items2[i]))) { return false; } - - //Check items equivalence; - for (int i = 0; i < itemCount; i ++) - { - if(!(sitems[i]->equals(items2[i]))) - { - return false; - } - } - return true; } + return true; +} - void SubMenu::enter(io::UART& uart, char** args) - { - cb(uart,args,term); - } +void SubMenu::enter(io::UART& uart, char** args) { + cb(uart, args, term); +} - void SubMenu::exit(io::UART& uart, char** args) - { - //if ctx has value use it as a callback - if(ctx) - { - callback_t cb = (callback_t)ctx; - cb(uart, args, term); - } +void SubMenu::exit(io::UART& uart, char** args) { + // if ctx has value use it as a callback + if (ctx) { + callback_t cb = (callback_t) ctx; + cb(uart, args, term); } -} \ No newline at end of file +} +} // namespace core::utils \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminal.cpp index c6cca3bc2..ce242b245 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminal.cpp @@ -1,176 +1,149 @@ /** - * + * */ #include #include -#include #include #include +#include #include namespace io = core::io; -namespace core::utils -{ - Terminal::Terminal(io::UART& uart, utils::Menu* menu) : menu(menu), uart(uart) - { - //start message - uart.printf("\n\rStarting Terminal...\n\r"); - //set main menu flag to true - m = true; +namespace core::utils { +Terminal::Terminal(io::UART& uart, utils::Menu* menu) : menu(menu), uart(uart) { + // start message + uart.printf("\n\rStarting Terminal...\n\r"); + // set main menu flag to true + m = true; +} + +void Terminal::update(char* message, utils::Menu m) { + // replace menu with provided + menu->replace(m); + // print spacer lines + for (int i = 0; i < 5; i++) { + uart.printf("\n\r"); } + // print + uart.printf(message); +} - void Terminal::update(char* message, utils::Menu m) - { - //replace menu with provided - menu->replace(m); - //print spacer lines - for(int i = 0; i < 5; i ++) - { - uart.printf("\n\r"); - } - //print - uart.printf(message); +void Terminal::setCurrent(SubMenu* sub) { + // sets current to input, sets main menu flag to false + current = sub; + m = false; +} + +bool Terminal::recieve(char* holder[10]) { + // fill buffer with null chars + for (int i = 0; i < 99; i++) { + buffer[i] = '\0'; } + // fill buffer + uart.gets(buffer, 99); - void Terminal::setCurrent(SubMenu* sub) - { - //sets current to input, sets main menu flag to false - current = sub; - m = false; + // tokenize + holder[0] = strtok(buffer, " "); + for (int i = 1; i < 10; i++) { + holder[i] = strtok(NULL, " "); } + // return true, this is placeholder for ensuring a complete message + return true; + // for(int i = 0; i < 10; i++) + // { + // holder[i] = argN[i]; + // } +} - bool Terminal::recieve(char* holder[10]) - { - //fill buffer with null chars - for(int i = 0; i < 99; i ++) - { - buffer[i] = '\0'; - } - //fill buffer - uart.gets(buffer, 99); - - //tokenize - holder[0] = strtok(buffer, " "); - for(int i = 1; i < 10; i ++) - { - holder[i] = strtok(NULL, " "); - } - //return true, this is placeholder for ensuring a complete message - return true; - // for(int i = 0; i < 10; i++) - // { - // holder[i] = argN[i]; - // } +// TERMINAL specific print function +void Terminal::printTerm() { + // print header and some info + uart.printf("\n\r"); + uart.printf("Terminal:\n\r"); + uart.printf("is main: "); + if (m) { + uart.printf("true\n\r"); + } else { + uart.printf("false\n\r"); } - //TERMINAL specific print function - void Terminal::printTerm() - { - //print header and some info - uart.printf("\n\r"); - uart.printf("Terminal:\n\r"); - uart.printf("is main: "); - if(m) - { - uart.printf("true\n\r"); - } - else{uart.printf("false\n\r");} - - //print menu - if(m) - { - menu->printStr(uart); - } - //or submenu - else - { - current->printMStr(uart); - uart.printf("q|QUIT\n\r"); + // print menu + if (m) { + menu->printStr(uart); + } + // or submenu + else { + current->printMStr(uart); + uart.printf("q|QUIT\n\r"); + } +} + +void Terminal::enterSub(io::UART& uart, char** args, void* term) { + utils::MenuItem** items = menu->getItems(); + char* name = args[0]; + int c = menu->getCount(); + utils::MenuItem* item; + // find chosen item + for (int i = 0; i < c; i++) { + item = items[i]; + if (strcmp(name, item->getOption()) == 0) { + // cast item to submenu and place in current + setCurrent((utils::SubMenu*) item); + break; } } +} + +void Terminal::process(char* tag, char** args) { + // different behavior for menu and submenu + utils::MenuItem* holder; + if (m) { + // find item in menu + utils::MenuItem** subitemsM = menu->getItems(); + + for (int i = 0; i < menu->getCount(); i++) { + char* op = subitemsM[i]->getOption(); - void Terminal::enterSub(io::UART& uart, char** args, void* term) - { - utils::MenuItem** items = menu->getItems(); - char* name = args[0]; - int c = menu->getCount(); - utils::MenuItem* item; - //find chosen item - for(int i = 0; i < c; i ++) - { - item = items[i]; - if(strcmp(name, item->getOption()) == 0) - { - //cast item to submenu and place in current - setCurrent((utils::SubMenu*)item); + if (strcmp(op, "null") == 0) { + holder = nullptr; + break; + } else if (strcmp(tag, op) == 0) { + holder = (subitemsM[i]); break; } } - } + } else { + // find item in submenu + utils::MenuItem** subitemsC = current->getItems(); - void Terminal::process(char* tag, char** args) - { - //different behavior for menu and submenu - utils::MenuItem* holder; - if(m) - { - //find item in menu - utils::MenuItem** subitemsM = menu->getItems(); - - for(int i = 0; i < menu->getCount(); i ++) - { - char* op = subitemsM[i]->getOption(); - - if(strcmp(op, "null") == 0) - { - holder = nullptr; - break; - } - else if(strcmp(tag,op) == 0) - { - holder = (subitemsM[i]); - break; - } + for (int i = 0; i < menu->getCount(); i++) { + char* op = subitemsC[i]->getOption(); + if (strcmp(op, "null") == 0) { + holder = nullptr; + break; } - } - else - { - //find item in submenu - utils::MenuItem** subitemsC = current->getItems(); - - for(int i = 0; i < menu->getCount(); i ++) - { - char* op = subitemsC[i]->getOption(); - if(strcmp(op, "null") == 0) - { - holder = nullptr; - break; - } - if(strcmp(tag,op) == 0) - { - holder = (subitemsC[i]); - break; - } + if (strcmp(tag, op) == 0) { + holder = (subitemsC[i]); + break; } } + } - //check if quit flag, if quit set main menu flag to true - //this brings us back to main menu, current sill still have a value but this gets overwritten next menu move - if(strcmp(tag, "q") == 0) - { - m = true; - } - - //if quit flag, kick out of process, - if(holder == nullptr || strcmp(holder->getOption(),"q") == 0) - { - return; - } + // check if quit flag, if quit set main menu flag to true + // this brings us back to main menu, current sill still have a value but this gets overwritten next menu move + if (strcmp(tag, "q") == 0) { + m = true; + } - //get and run callback of chosen item - callback_t cb = holder->getcb(); - - cb(uart, args, (void*)this); + // if quit flag, kick out of process, + if (holder == nullptr || strcmp(holder->getOption(), "q") == 0) { + return; } + + // get and run callback of chosen item + callback_t cb = holder->getcb(); + + cb(uart, args, (void*) this); } +} // namespace core::utils From 100bac1cbec1ee10f33fe0605f1d70d632483840 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Sat, 12 Apr 2025 14:05:40 -0400 Subject: [PATCH 27/30] added terminalManager, making structural changes --- .gitmodules | 3 - include/core/utils/terminal/menuItem.hpp | 2 +- include/core/utils/terminal/terminal.hpp | 122 +++--------------- .../core/utils/terminal/terminalManager.hpp | 106 +++++++++++++++ 4 files changed, 128 insertions(+), 105 deletions(-) create mode 100644 include/core/utils/terminal/terminalManager.hpp diff --git a/.gitmodules b/.gitmodules index bfb04dd50..7eb25cc08 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ - - - [submodule "libs/canopen"] path = libs/canopen url = ../canopen-stack.git diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 9c7016f8f..3adf3bf63 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -1,6 +1,7 @@ #ifndef EVT_TERM_MENUITEM #define EVT_TERM_MENUITEM #include +#include // macro for max initial item count of submenus @@ -8,7 +9,6 @@ // 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*); namespace core::utils { class MenuItem { diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index d53594f31..b9a7340af 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -1,106 +1,26 @@ -#ifndef EVT_TERMINAL -#define EVT_TERMINAL - -#include -#include -#include -#include -#include - -namespace core::utils { - -class Terminal { -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 baud 9600 or 115200 - */ - Terminal(io::UART& uart, Menu* menu); - - /** - * returns uart instance used - */ - io::UART& getUART() { - return uart; - } - - /** - * returns main menu - */ - Menu* getMenu() { - return menu; - } - - /** - * returns current, will be nullptr if are not in a submenu - */ - SubMenu* getCurrent() { - return current; - } - - /** - * 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 submenu to provided value - * @param sub the submenu to replace the current one with - */ - void setCurrent(SubMenu* sub); - - /** - * checks if the terminal is still on the main menu - */ - bool isMain() { - return m; +#ifndef EVT_TERM_STRUCTS +#define EVT_TERM_STRUCTS + +struct TerminalInterface +{ + std::function func1; + std::function func2; + std::function func3; + + FunctionRunner(std::function f1, std::function f2, std::function f3) + : func1(f1), func2(f2), func3(f3) {} + + void runAll() { + std::cout << "Running func1...\n"; + func1(); + std::cout << "Running func2...\n"; + func2(); + std::cout << "Running func3...\n"; + func3(); } +}; - /** - * Sends a provided message over UART - * also replaces menu with provided menu - * used to reset menu to desired state and send a message - * @param message a string message to send via UART - * @param menu the menu to replace the current head node 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 - SubMenu* current; - - // true if currently in main menu - bool m; - - // uart instance this terminal is using - core::io::UART& uart; +using callback_t = void (*)(core::io::UART&, char** inputList, void*); - // char buffer for UART entry - char buffer[99]; -}; -} // namespace core::utils #endif \ No newline at end of file diff --git a/include/core/utils/terminal/terminalManager.hpp b/include/core/utils/terminal/terminalManager.hpp new file mode 100644 index 000000000..d53594f31 --- /dev/null +++ b/include/core/utils/terminal/terminalManager.hpp @@ -0,0 +1,106 @@ +#ifndef EVT_TERMINAL +#define EVT_TERMINAL + +#include +#include +#include +#include +#include + +namespace core::utils { + +class Terminal { +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 baud 9600 or 115200 + */ + Terminal(io::UART& uart, Menu* menu); + + /** + * returns uart instance used + */ + io::UART& getUART() { + return uart; + } + + /** + * returns main menu + */ + Menu* getMenu() { + return menu; + } + + /** + * returns current, will be nullptr if are not in a submenu + */ + SubMenu* getCurrent() { + return current; + } + + /** + * 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 submenu to provided value + * @param sub the submenu to replace the current one with + */ + void setCurrent(SubMenu* sub); + + /** + * checks if the terminal is still on the main menu + */ + bool isMain() { + return m; + } + + /** + * Sends a provided message over UART + * also replaces menu with provided menu + * used to reset menu to desired state and send a message + * @param message a string message to send via UART + * @param menu the menu to replace the current head node 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 + SubMenu* 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 \ No newline at end of file From 9199f7386c4323a88e6768c5e9df9adc6cf862d0 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Sat, 19 Apr 2025 11:43:07 -0400 Subject: [PATCH 28/30] saving changes --- include/core/utils/terminal/menu.hpp | 110 ++++++++++++---- include/core/utils/terminal/menuItem.hpp | 124 +----------------- .../{terminal.cpp => terminalManager.cpp} | 17 +-- 3 files changed, 92 insertions(+), 159 deletions(-) rename src/core/utils/terminal/{terminal.cpp => terminalManager.cpp} (93%) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 741bc8462..1a91822b0 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -5,74 +5,128 @@ #include namespace core::utils { -class Menu { + + +class Menu : public MenuItem{ public: /** - * Basic constructor takes a list a menu items - * @param items a list of menu items + * 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 */ - Menu(MenuItem** items); + SubMenu(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** items); /** - * creates a string representation of a menu, with each menu item on its own line + * unique overridden printStr() method for sub-menus */ void printStr(io::UART& uart); /** - * returns number of items in menu + * 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; } /** - * replaces this instance of a menu with another menu - * @param m the menu you want to replace it with + * returns parent */ - void replace(Menu m); + void* getParent() { + return parent; + } + + /** + * replaces current item list with provided one + * @param itms items to replace current list with + */ + void setItems(MenuItem** itms); /** - * returns list of items in menu + * returns a list of all items contained in the submenu */ MenuItem** getItems() { - return items; + return sitems; } + // # + // # USE THESE TO DO ANY NEEDED SETUP/CLEANUP WHEN ENTERING/EXITING A SUB MENU: + // # + /** - * adds an item to the menu - * @param item the item to add + * 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 addItem(utils::MenuItem* item); + void enter(io::UART& uart, char** args); /** - * replaces the current list of items with a new one - * @param itms the list to replace current with + * 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 newItems(utils::MenuItem** itms); + void exit(io::UART& uart, char** args); +private: /** - * delete an item from the list - * @param index the index of the item in the list(top of list when printing is 0) + * key value for item, this is used to select it in your commands */ - void delItem(int index); + char* option; /** - * checks if this menu is equivalent to another menu - * true if every menu item is equal - * @param mnu2 the menu to compare to + * description/name of item */ - bool equals(Menu* mnu2); + char* text; -private: /** - * list of all items contained in the menu + * pointer to callback method for this item */ - MenuItem** items; + callback_t cb; /** - * maximum number of items allowed in any menu + * 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 + */ + void* parent; + + /** + * terminal this is in + */ + void* term; }; } // namespace core::utils diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 3adf3bf63..3baf828d5 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -110,129 +110,7 @@ class MenuItem { * terminal this is in */ void* term; -}; - -class SubMenu : 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(void* 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 - */ - void* parent; - - /** - * terminal this is in - */ - void* term; -}; +}; } // namespace core::utils #endif \ No newline at end of file diff --git a/src/core/utils/terminal/terminal.cpp b/src/core/utils/terminal/terminalManager.cpp similarity index 93% rename from src/core/utils/terminal/terminal.cpp rename to src/core/utils/terminal/terminalManager.cpp index ce242b245..a8db3c05e 100644 --- a/src/core/utils/terminal/terminal.cpp +++ b/src/core/utils/terminal/terminalManager.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include namespace io = core::io; @@ -61,13 +61,14 @@ void Terminal::printTerm() { // print header and some info uart.printf("\n\r"); uart.printf("Terminal:\n\r"); - uart.printf("is main: "); - if (m) { - uart.printf("true\n\r"); - } else { - uart.printf("false\n\r"); - } - + // uart.printf("is main: "); + // if (m) { + // uart.printf("true\n\r"); + // } else { + // uart.printf("false\n\r"); + // } + + uart.printf("==========\n\r"); // print menu if (m) { menu->printStr(uart); From a6bcd5617c29b5249f764fe3e76799403d516808 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Sat, 19 Apr 2025 13:13:54 -0400 Subject: [PATCH 29/30] making updates to printing and terminal interface --- include/core/utils/terminal/menu.hpp | 6 ++++-- include/core/utils/terminal/menuItem.hpp | 2 +- include/core/utils/terminal/terminal.hpp | 15 ++++----------- include/core/utils/terminal/terminalManager.hpp | 3 ++- src/core/utils/terminal/terminalManager.cpp | 4 ++-- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/include/core/utils/terminal/menu.hpp b/include/core/utils/terminal/menu.hpp index 1a91822b0..220f7da07 100644 --- a/include/core/utils/terminal/menu.hpp +++ b/include/core/utils/terminal/menu.hpp @@ -3,6 +3,7 @@ // macro for max initial item count of a main menu #include +#include namespace core::utils { @@ -19,7 +20,7 @@ class Menu : public MenuItem{ * @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); + SubMenu(Menu* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** items); /** * unique overridden printStr() method for sub-menus @@ -120,8 +121,9 @@ class Menu : public MenuItem{ /** * submenu or menu this item is in + * if nullptr the current node is the head */ - void* parent; + Menu* parent; /** * terminal this is in diff --git a/include/core/utils/terminal/menuItem.hpp b/include/core/utils/terminal/menuItem.hpp index 3baf828d5..0f4bab723 100644 --- a/include/core/utils/terminal/menuItem.hpp +++ b/include/core/utils/terminal/menuItem.hpp @@ -1,7 +1,7 @@ #ifndef EVT_TERM_MENUITEM #define EVT_TERM_MENUITEM #include -#include +#include // macro for max initial item count of submenus diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index b9a7340af..47a718e97 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -3,20 +3,13 @@ struct TerminalInterface { - std::function func1; - std::function func2; - std::function func3; + //fill with list full of pointers to terminalManager functions to circumvent + void** funclist; - FunctionRunner(std::function f1, std::function f2, std::function f3) - : func1(f1), func2(f2), func3(f3) {} + FunctionRunner(void** funclist) + : funclist(funclist) {} void runAll() { - std::cout << "Running func1...\n"; - func1(); - std::cout << "Running func2...\n"; - func2(); - std::cout << "Running func3...\n"; - func3(); } }; diff --git a/include/core/utils/terminal/terminalManager.hpp b/include/core/utils/terminal/terminalManager.hpp index d53594f31..d5d4f8563 100644 --- a/include/core/utils/terminal/terminalManager.hpp +++ b/include/core/utils/terminal/terminalManager.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include namespace core::utils { @@ -90,7 +91,7 @@ class Terminal { Menu* menu; // current submenu, if there is one, nullptr otherwise - SubMenu* current; + Menu* current; // true if currently in main menu bool m; diff --git a/src/core/utils/terminal/terminalManager.cpp b/src/core/utils/terminal/terminalManager.cpp index a8db3c05e..974a145bd 100644 --- a/src/core/utils/terminal/terminalManager.cpp +++ b/src/core/utils/terminal/terminalManager.cpp @@ -67,8 +67,8 @@ void Terminal::printTerm() { // } else { // uart.printf("false\n\r"); // } - - uart.printf("==========\n\r"); + uart.printf(current->getText()); //this will eventually be changed to brint the nodes above current up to the head as well like head>between>current + uart.printf("===================\n\r"); // print menu if (m) { menu->printStr(uart); From b92320709a1440d832b7dcd75502e3c121722fa4 Mon Sep 17 00:00:00 2001 From: RyanLeifer04 Date: Fri, 9 May 2025 16:30:00 -0400 Subject: [PATCH 30/30] beginning on terminalInterface struct --- include/core/utils/terminal/terminal.hpp | 8 +- .../core/utils/terminal/terminalManager.hpp | 20 ++--- src/core/utils/terminal/menu.cpp | 90 +++++++++---------- src/core/utils/terminal/subMenu.cpp | 70 --------------- src/core/utils/terminal/terminalManager.cpp | 2 +- 5 files changed, 57 insertions(+), 133 deletions(-) delete mode 100644 src/core/utils/terminal/subMenu.cpp diff --git a/include/core/utils/terminal/terminal.hpp b/include/core/utils/terminal/terminal.hpp index 47a718e97..a209a18ae 100644 --- a/include/core/utils/terminal/terminal.hpp +++ b/include/core/utils/terminal/terminal.hpp @@ -1,15 +1,17 @@ #ifndef EVT_TERM_STRUCTS #define EVT_TERM_STRUCTS +#include struct TerminalInterface { //fill with list full of pointers to terminalManager functions to circumvent void** funclist; - FunctionRunner(void** funclist) - : funclist(funclist) {} + FunctionRunner(void** funclist): funclist(funclist) {} - void runAll() { + void runAll() + { + } }; diff --git a/include/core/utils/terminal/terminalManager.hpp b/include/core/utils/terminal/terminalManager.hpp index d5d4f8563..d8e78185d 100644 --- a/include/core/utils/terminal/terminalManager.hpp +++ b/include/core/utils/terminal/terminalManager.hpp @@ -10,13 +10,13 @@ namespace core::utils { -class Terminal { +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 baud 9600 or 115200 + * @param menu head node of terminal structure */ Terminal(io::UART& uart, Menu* menu); @@ -34,13 +34,6 @@ class Terminal { return menu; } - /** - * returns current, will be nullptr if are not in a submenu - */ - SubMenu* getCurrent() { - return current; - } - /** * enters the given submenu * @param uart the uart instance to use @@ -50,13 +43,14 @@ class Terminal { */ void enterSub(io::UART& uart, char** args, void* term); /** - * sets current submenu to provided value + * sets current menu/submenu to provided value * @param sub the submenu to replace the current one with */ - void setCurrent(SubMenu* sub); + 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; @@ -65,9 +59,9 @@ class Terminal { /** * Sends a provided message over UART * also replaces menu with provided menu - * used to reset menu to desired state and send a message + * 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 head node with + * @param menu the menu to replace the current menu with */ void update(char* message, utils::Menu menu); diff --git a/src/core/utils/terminal/menu.cpp b/src/core/utils/terminal/menu.cpp index 3033d5fe1..f3391411a 100644 --- a/src/core/utils/terminal/menu.cpp +++ b/src/core/utils/terminal/menu.cpp @@ -1,72 +1,70 @@ #include -#include #include namespace core::utils { -Menu::Menu(MenuItem** items) : items(items) { +SubMenu::SubMenu(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** sitems) + : parent(parent), term(term), option(option), text(text), cb(cb), ctx(ctx), sitems(sitems), + MenuItem(parent, term, option, text, cb, ctx) { // constructor } -void Menu::printStr(core::io::UART& uart) { - // loops through item list, stops when it hits an item with an option string of "null" +void SubMenu::printStr(core::io::UART& uart) { + // this is exclusively for displaying the submenu inside of a menu list + // option | text + uart.printf(option); + uart.printf("|"); + uart.printf(text); +} + +void SubMenu::printMStr(core::io::UART& uart) { + // used for printing this as the current menu state + // prints all items in list until it hits one with option string "null" for (int c = 0; c < itemCount; c++) { - if (items[c]->getOption() == "null") { + if (sitems[c]->getOption() == "null") { return; } - items[c]->printStr(uart); + sitems[c]->printStr(uart); } } -void Menu::addItem(utils::MenuItem* item) { - // find next empty space - int c = 0; +void SubMenu::setItems(MenuItem** itms) { + // replaces corresponding items in sitems with itms + // used to replace item list, or certain items by inputing an altered item list for (int i = 0; i < itemCount; i++) { - if (items[i] == nullptr) { - c = i + 1; - break; - } - } - // make sure next space is within bounds - if (c >= itemCount) { - return; + sitems[i] = itms[i]; } - // if in bounds place in list, if not return nothing - items[c - 1] = item; } -void Menu::newItems(utils::MenuItem** itms) { - // replace each item in list with corresponding one in new list - for (int i = 0; i < itemCount; i++) { - items[i] = itms[i]; +bool SubMenu::equals(SubMenu* sub2) { + // check equivalence of attributes besides items + // this section is identical to menuItem + char* option2 = sub2->getOption(); + char* text2 = sub2->getText(); + callback_t cb2 = sub2->getcb(); + void* ctx2 = sub2->getctx(); + MenuItem** items2 = sub2->getItems(); + if (option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { + return false; } -} - -void Menu::delItem(int index) { - // set desired index to null - items[index] = nullptr; - // shift every higher index down - for (int i = index; i < itemCount - 1; i++) { - items[index] = items[index + 1]; - } // set highest index to null to ensure there is no duplicate final item - items[itemCount - 1] = nullptr; -} - -void Menu::replace(Menu m) { - // get item list of new menu - utils::MenuItem** i2 = m.getItems(); - newItems(i2); -} - -bool Menu::equals(Menu* mnu2) { - // get second list - MenuItem** items2 = mnu2->getItems(); + // Check items equivalence; for (int i = 0; i < itemCount; i++) { - // check if corresponding items are equal, if one fail return false - if (!(items[i]->equals(items2[i]))) { + if (!(sitems[i]->equals(items2[i]))) { return false; } } return true; } + +void SubMenu::enter(io::UART& uart, char** args) { + cb(uart, args, term); +} + +void SubMenu::exit(io::UART& uart, char** args) { + // if ctx has value use it as a callback + if (ctx) { + callback_t cb = (callback_t) ctx; + cb(uart, args, term); + } +} } // namespace core::utils \ No newline at end of file diff --git a/src/core/utils/terminal/subMenu.cpp b/src/core/utils/terminal/subMenu.cpp deleted file mode 100644 index f3391411a..000000000 --- a/src/core/utils/terminal/subMenu.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include - -namespace core::utils { -SubMenu::SubMenu(void* parent, void* term, char* option, char* text, callback_t cb, void* ctx, MenuItem** sitems) - : parent(parent), term(term), option(option), text(text), cb(cb), ctx(ctx), sitems(sitems), - MenuItem(parent, term, option, text, cb, ctx) { - // constructor -} - -void SubMenu::printStr(core::io::UART& uart) { - // this is exclusively for displaying the submenu inside of a menu list - // option | text - uart.printf(option); - uart.printf("|"); - uart.printf(text); -} - -void SubMenu::printMStr(core::io::UART& uart) { - // used for printing this as the current menu state - // prints all items in list until it hits one with option string "null" - for (int c = 0; c < itemCount; c++) { - if (sitems[c]->getOption() == "null") { - return; - } - sitems[c]->printStr(uart); - } -} - -void SubMenu::setItems(MenuItem** itms) { - // replaces corresponding items in sitems with itms - // used to replace item list, or certain items by inputing an altered item list - for (int i = 0; i < itemCount; i++) { - sitems[i] = itms[i]; - } -} - -bool SubMenu::equals(SubMenu* sub2) { - // check equivalence of attributes besides items - // this section is identical to menuItem - char* option2 = sub2->getOption(); - char* text2 = sub2->getText(); - callback_t cb2 = sub2->getcb(); - void* ctx2 = sub2->getctx(); - MenuItem** items2 = sub2->getItems(); - if (option != option2 || text != text2 || cb != cb2 || ctx != ctx2) { - return false; - } - - // Check items equivalence; - for (int i = 0; i < itemCount; i++) { - if (!(sitems[i]->equals(items2[i]))) { - return false; - } - } - return true; -} - -void SubMenu::enter(io::UART& uart, char** args) { - cb(uart, args, term); -} - -void SubMenu::exit(io::UART& uart, char** args) { - // if ctx has value use it as a callback - if (ctx) { - callback_t cb = (callback_t) ctx; - cb(uart, args, term); - } -} -} // namespace core::utils \ No newline at end of file diff --git a/src/core/utils/terminal/terminalManager.cpp b/src/core/utils/terminal/terminalManager.cpp index 974a145bd..da2d536ec 100644 --- a/src/core/utils/terminal/terminalManager.cpp +++ b/src/core/utils/terminal/terminalManager.cpp @@ -11,7 +11,7 @@ namespace io = core::io; namespace core::utils { -Terminal::Terminal(io::UART& uart, utils::Menu* menu) : menu(menu), uart(uart) { +Terminal::TerminalManager(io::UART& uart, utils::Menu* menu) : menu(menu), uart(uart) { // start message uart.printf("\n\rStarting Terminal...\n\r"); // set main menu flag to true