From 241123e6c99c927fd7faa838ceddb559f2f80fcf Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Wed, 19 Jan 2022 09:13:53 +0100 Subject: [PATCH] Implement 75% off menu --- doc/Notes and TODOs/TODO allgemein.txt | 9 +-- lib/Menu/menu.cpp | 80 +++++++++++++++++++--- lib/Menu/menu.h | 12 +++- lib/Menu/menuEntry.cpp | 21 +++++- lib/Menu/menuEntry.h | 11 +++- src/main.cpp | 91 +++++++++++++++++++++++++- 6 files changed, 197 insertions(+), 27 deletions(-) diff --git a/doc/Notes and TODOs/TODO allgemein.txt b/doc/Notes and TODOs/TODO allgemein.txt index 49256b0..61449f3 100644 --- a/doc/Notes and TODOs/TODO allgemein.txt +++ b/doc/Notes and TODOs/TODO allgemein.txt @@ -1,9 +1,2 @@ -Meine Libs von andren Abhängigkeiten lösen - - Die loop funmktions mit eigenem Timer oder ohne Timer - - Alle config.h und nicht allgemeinen Header entfernen +Menu hat manchmal speicher leaks :/ -Speedometer: - - Mitrechnen durchschnittliche aufrufzeit - -Alle loops Nachgucken ob last_millis aktualisiert wird. -Mqtt kram auslagern \ No newline at end of file diff --git a/lib/Menu/menu.cpp b/lib/Menu/menu.cpp index 37f7e8a..8cedc42 100644 --- a/lib/Menu/menu.cpp +++ b/lib/Menu/menu.cpp @@ -10,13 +10,47 @@ */ #include "menu.h" -Menu::Menu(MenuEntry* firstEntry) { - entrys.push_back(firstEntry); +Menu::Menu() { this->it = this->entrys.begin(); } +void Menu::setParentMenu(Menu* menu) { + this->parentMenu = menu; +} + +void Menu::addEntry(MenuEntry* entry) { + this->entrys.push_back(entry); + + // TODO: Same shit here. Why I get the first element after an increment. + static uint8_t inc = 0; + inc++; + if (inc == 2) { + this->it = this->entrys.begin(); + } +} + +void Menu::printMenu() { + Serial.println("\n\n"); + for (std::list::iterator iter = this->entrys.begin(); iter != this->entrys.end(); iter++) { + MenuEntry* selectedEntry = *(iter); + if (this->it == iter) + Serial.print(" "); + Serial.println(selectedEntry->getName()); + } + this->inSubmenu = false; +} + void Menu::nextEntry() { - if (this->it != this->entrys.end()) + if (this->inSubmenu) { + MenuEntry* selectedEntry = *(this->it); + selectedEntry->getMenu()->nextEntry(); + return; + } + + // TODO: Why I need the fucking next two lines? here and in the next function + std::list::iterator iter = this->it; + iter++; + if (iter != this->entrys.end()) this->it++; else this->it = this->entrys.begin(); @@ -24,19 +58,47 @@ void Menu::nextEntry() { } void Menu::prevEntry() { - if (this->it != this->entrys.begin()) + if (this->inSubmenu) { + MenuEntry* selectedEntry = *(this->it); + selectedEntry->getMenu()->prevEntry(); + return; + } + std::list::iterator iter = this->entrys.end(); + iter--; + if (this->it != this->entrys.begin()) { + Serial.println("--"); this->it--; - else - this->it = this->entrys.end(); + } + else { + Serial.println("begin"); + this->it = iter; + } this->printMenu(); } void Menu::enterEntry() { - // TODO: find out why i cannot call run directly from the iterator + // TODO: find out why i cannot call runAction() directly from the iterator MenuEntry* selectedEntry = *(this->it); - selectedEntry->run(); + + if (this->inSubmenu) { + selectedEntry->getMenu()->enterEntry(); + return; + } + + if (selectedEntry->getIsMenu()) { + this->inSubmenu = true; + selectedEntry->getMenu()->setParentMenu(this); + } + selectedEntry->runAction(); } void Menu::enterParentMenu() { - this->parentMenu->printMenu(); + if (this->inSubmenu) { + MenuEntry* selectedEntry = *(this->it); + selectedEntry->getMenu()->enterParentMenu(); + return; + } + + if (parentMenu) + this->parentMenu->printMenu(); } \ No newline at end of file diff --git a/lib/Menu/menu.h b/lib/Menu/menu.h index 1830803..9b75e68 100644 --- a/lib/Menu/menu.h +++ b/lib/Menu/menu.h @@ -13,12 +13,20 @@ #include +// Only for print in consol +// have to be deleted after display installation +#include + #include "menuEntry.h" +class MenuEntry; class Menu { public: - Menu(MenuEntry* firstEntry); + Menu(); + void setParentMenu(Menu* menu); + + void addEntry(MenuEntry* entry); void printMenu(); void nextEntry(); @@ -27,7 +35,7 @@ class Menu { void enterParentMenu(); private: - bool active = false; + bool inSubmenu = false; Menu* parentMenu; diff --git a/lib/Menu/menuEntry.cpp b/lib/Menu/menuEntry.cpp index 708f2c7..3380ae9 100644 --- a/lib/Menu/menuEntry.cpp +++ b/lib/Menu/menuEntry.cpp @@ -19,20 +19,37 @@ MenuEntry::MenuEntry(const char* name, void (*function) (), void (*callback) ()) MenuEntry::MenuEntry(const char* name, void (*function) ()) { this->name = name; this->function = function; + this->callback = nullptr; } MenuEntry::MenuEntry(const char* name, Menu* menu) { this->name = name; this->menu = menu; this->isMenu = true; + this->callback = nullptr; } -void MenuEntry::run() { +void MenuEntry::runAction() { if (isMenu) - {} + this->menu->printMenu(); else this->function(); if (this->callback) this->callback(); +} + +const char* MenuEntry::getName() { + return this->name; +} + +bool MenuEntry::getIsMenu() { + return this->isMenu; +} + +Menu* MenuEntry::getMenu() { + if (isMenu) + return this->menu; + else + return nullptr; } \ No newline at end of file diff --git a/lib/Menu/menuEntry.h b/lib/Menu/menuEntry.h index 69f073c..cb2af02 100644 --- a/lib/Menu/menuEntry.h +++ b/lib/Menu/menuEntry.h @@ -13,20 +13,25 @@ #include "menu.h" +class Menu; class MenuEntry { public: MenuEntry(const char* name, void (*function) (), void (*callback) ()); MenuEntry(const char* name, void (*function) ()); MenuEntry(const char* name, Menu* menu); - void run(); + void runAction(); + + const char* getName(); + bool getIsMenu(); + Menu* getMenu(); private: const char* name; void (*function) (); - void (*callback) (); + void (*callback) () = nullptr; - bool isMenu; + bool isMenu = false; Menu *menu; }; diff --git a/src/main.cpp b/src/main.cpp index 7a407b7..63dadb7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,13 +8,23 @@ #include "driveModi/driveManager.h" #include "moveControl.h" #include "network.h" +#include "menu.h" +#include "menuEntry.h" + +// Platzhalter, muss irgendwann weg +void dummy(){Serial.println("Dummy in Action");} void callbackControllerAction(); void callbackControllerConnect(); void controllerPrintBattery(); +void p_f(); +void i_f(); +void d_f(); +int32_t displaySelectValue(); MoveControl moveController; DriveManager driveManager(&moveController); +Menu* main_m; void setup() { Serial.begin(115200); @@ -29,9 +39,40 @@ void setup() { Serial.println("\nReady to connect a PS3 Controller... \n"); Ps3.begin(); - driveManager.changeModus(Modi::ManualControl); - Serial1.begin(GPS_BAUD, SERIAL_8N1, GPS_RX, GPS_TX); + + // Create Menu + main_m = new Menu(); + Menu* mode_m = new Menu(); + Menu* pid_m = new Menu(); + + // Entry for the main menu + MenuEntry* mode_e = new MenuEntry("Mode", mode_m); + MenuEntry* gps_e = new MenuEntry("GPS", dummy); + MenuEntry* pid_e = new MenuEntry("PID", pid_m); + main_m->addEntry(mode_e); + main_m->addEntry(gps_e); + main_m->addEntry(pid_e); + + // Entry for the mode Menu + MenuEntry* autopilot_e = new MenuEntry("Autopilot", dummy); + MenuEntry* captureRoute_e = new MenuEntry("Capture Route", dummy); + MenuEntry* consolControl_e = new MenuEntry("Consol Control", dummy); + MenuEntry* manualControl_e = new MenuEntry("Manual Control", dummy); + MenuEntry* testMode_e = new MenuEntry("Test Mode", dummy); + mode_m->addEntry(autopilot_e); + mode_m->addEntry(captureRoute_e); + mode_m->addEntry(consolControl_e); + mode_m->addEntry(manualControl_e); + mode_m->addEntry(testMode_e); + + // Entry for the PID Menu + MenuEntry* p_e = new MenuEntry("P", dummy); + MenuEntry* i_e = new MenuEntry("I", dummy); + MenuEntry* d_e = new MenuEntry("D", dummy); + pid_m->addEntry(p_e); + pid_m->addEntry(i_e); + pid_m->addEntry(d_e); } void loop() { @@ -41,8 +82,36 @@ void loop() { Serial.print(Serial1.read()); } -void callbackControllerAction() { +bool isDelayReached() { + static uint32_t lastMillis = 0; + const uint16_t delay = 200; + bool res = false; + if (millis() - lastMillis > delay) { + res = true; + lastMillis = millis(); + } + + return res; +} + +void callbackControllerAction() { + // Menu controlling + + if (isDelayReached()) { + if (Ps3.data.button.up) + main_m->prevEntry(); + else if (Ps3.data.button.down) + main_m->nextEntry(); + else if (Ps3.data.button.left) + main_m->enterParentMenu(); + else if (Ps3.data.button.right) + main_m->enterEntry(); + else if (Ps3.data.button.select) + main_m->printMenu(); + else if (Ps3.data.button.ps) + controllerPrintBattery(); + } } void callbackControllerConnect() { @@ -64,3 +133,19 @@ void controllerPrintBattery() { else if( controller_battery == ps3_status_battery_shutdown ) Serial.println("SHUTDOWN"); else Serial.println("UNDEFINED"); } + +void p_f() { + +} + +void i_f() { + +} + +void d_f() { + +} + +int32_t displaySelectValue() { + +}