CLEANUP: Cleaned up menu code
This commit is contained in:
parent
025c5e17d3
commit
ff0393db48
@ -7,6 +7,6 @@
|
|||||||
|
|
||||||
// Auto generated header file containing the last git revision
|
// Auto generated header file containing the last git revision
|
||||||
|
|
||||||
#define GIT_HASH "3e3f570"
|
#define GIT_HASH "a8757b1"
|
||||||
|
|
||||||
#endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H
|
#endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H
|
@ -91,7 +91,7 @@ int main(void) {
|
|||||||
HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET);
|
||||||
|
|
||||||
using namespace floatpump::menu;
|
using namespace floatpump::menu;
|
||||||
Menu mainmenu(display);
|
Menu mainmenu("Hauptmenue");
|
||||||
Menu_Entry_Type_Checkable entry1bool(true);
|
Menu_Entry_Type_Checkable entry1bool(true);
|
||||||
entry1bool.linkConfig(globalConfig.testbool.getLink());
|
entry1bool.linkConfig(globalConfig.testbool.getLink());
|
||||||
Menu_Entry entry(&entry1bool, "test");
|
Menu_Entry entry(&entry1bool, "test");
|
||||||
@ -108,7 +108,7 @@ int main(void) {
|
|||||||
mainmenu.addEntry(entry2);
|
mainmenu.addEntry(entry2);
|
||||||
mainmenu.addEntry(entry3);
|
mainmenu.addEntry(entry3);
|
||||||
|
|
||||||
Menu submenu(display);
|
Menu submenu("Submenu1");
|
||||||
Menu_Entry_Type_Checkable entrysub(true);
|
Menu_Entry_Type_Checkable entrysub(true);
|
||||||
Menu_Entry sube(&entrysub, "yay dies sub!");
|
Menu_Entry sube(&entrysub, "yay dies sub!");
|
||||||
submenu.addEntry(sube);
|
submenu.addEntry(sube);
|
||||||
|
@ -5,86 +5,50 @@
|
|||||||
#ifndef FLOATPUMP_MENU_H
|
#ifndef FLOATPUMP_MENU_H
|
||||||
#define FLOATPUMP_MENU_H
|
#define FLOATPUMP_MENU_H
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
#include "Menu_Entry.h"
|
#include "Menu_Entry.h"
|
||||||
#include "LCD_I2C_Driver.h"
|
#include "LCD_I2C_Driver.h"
|
||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump::menu {
|
||||||
namespace menu {
|
|
||||||
|
|
||||||
class Menu {
|
class Menu {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Menu(LCD_I2C_Driver &driver) : m_driver(driver) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void addEntry(Menu_Entry entry) {
|
explicit Menu(std::string name) : m_name(std::move(name)) {}
|
||||||
m_entries.push_back(entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
void addSubmenu(Menu *submenu) {
|
//Add specific components
|
||||||
submenu->m_parent = this;
|
void addEntry(const Menu_Entry &entry);
|
||||||
m_submenus.push_back(submenu);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string printLine() {
|
void addSubmenu(Menu *submenu);
|
||||||
return m_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::string> getDisplayList() {
|
//Get this menu as string
|
||||||
std::vector<std::string> list;
|
[[nodiscard]] std::string printLine() const;
|
||||||
|
|
||||||
//first show submenus
|
//Get all submenus and entries as string vector
|
||||||
|
std::vector<std::string> getDisplayList();
|
||||||
|
|
||||||
for(auto it = m_submenus.begin(); it != m_submenus.end(); ++it) {
|
//Get number of all entries
|
||||||
list.push_back((*it)->printLine());
|
[[nodiscard]] unsigned int getAllEntriesNum() const;
|
||||||
}
|
|
||||||
|
|
||||||
for(auto it = m_entries.begin(); it != m_entries.end(); ++it) {
|
//Retrieve specific submenu, returns nullptr on error
|
||||||
list.push_back(it->printLine());
|
Menu *getSubmenu(int index);
|
||||||
}
|
|
||||||
|
|
||||||
|
//Retrieve specific entry, returns nullptr on error
|
||||||
|
Menu_Entry *getEntry(int index);
|
||||||
|
|
||||||
if(m_parent != nullptr) {
|
//Returns parent menu pointer or nullptr
|
||||||
list.push_back("<- Go Back");
|
Menu *getParent();
|
||||||
}
|
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getAllEntriesNum() {
|
|
||||||
return (m_entries.size() + m_submenus.size()) + ((m_parent != nullptr)? 1 : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
Menu *getSubmenu(int index) {
|
|
||||||
if(index >= 0 && index < m_submenus.size()) {
|
|
||||||
return m_submenus[index];
|
|
||||||
} else {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Menu_Entry *getEntry(int index) {
|
|
||||||
if(index >= m_submenus.size() && index < m_entries.size() + m_submenus.size()) {
|
|
||||||
return &m_entries[index - m_submenus.size()];
|
|
||||||
} else {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LCD_I2C_Driver &m_driver;
|
|
||||||
std::vector<Menu_Entry> m_entries;
|
|
||||||
std::vector<Menu *> m_submenus;
|
|
||||||
Menu *m_parent = nullptr;
|
|
||||||
int m_current_index = 0;
|
|
||||||
std::string m_name = "Menu";
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::vector<Menu_Entry> m_entries; //normal menu entries
|
||||||
|
std::vector<Menu *> m_submenus; //all submenu entries
|
||||||
|
Menu *m_parent = nullptr; //the parent menu, if it is a submenu
|
||||||
|
std::string m_name = "Menu"; //the name of this menu (displayed if it is an submenu)
|
||||||
};
|
};
|
||||||
|
|
||||||
} // floatpump
|
}
|
||||||
} // menu
|
|
||||||
|
|
||||||
#endif //FLOATPUMP_MENU_H
|
#endif //FLOATPUMP_MENU_H
|
||||||
|
@ -7,111 +7,32 @@
|
|||||||
|
|
||||||
#include "Menu.h"
|
#include "Menu.h"
|
||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump::menu {
|
||||||
namespace menu {
|
|
||||||
|
|
||||||
class Menu_Controller {
|
class Menu_Controller {
|
||||||
public:
|
public:
|
||||||
Menu_Controller(Menu *menu, LCD_I2C_Driver &driver): m_menu(menu), m_driver(driver) {};
|
Menu_Controller(Menu *menu, LCD_I2C_Driver &driver) : m_menu(menu), m_driver(driver) {};
|
||||||
|
|
||||||
enum Event {Increase, Decrease, Push};
|
//Possible User-Events that could be pushed to the controller
|
||||||
|
enum Event {
|
||||||
|
Increase, Decrease, Push
|
||||||
|
};
|
||||||
|
|
||||||
void execute() {
|
//Execute Menu (call this in a single loop to update the screen)
|
||||||
displayMenu(m_menu);
|
void execute();
|
||||||
}
|
|
||||||
|
|
||||||
void pushEvent(Event ev) {
|
//Push an Event to the Menu_Controller
|
||||||
switch(ev) {
|
void pushEvent(Event ev);
|
||||||
case Increase:
|
|
||||||
increase(m_menu); break;
|
|
||||||
case Decrease:
|
|
||||||
decrease(m_menu); break;
|
|
||||||
case Push:
|
|
||||||
keypress(m_menu); break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
//Used to interact with the screen
|
||||||
|
void displayMenu(Menu *m);
|
||||||
|
|
||||||
bool isEntry() {
|
void keypress(Menu *m);
|
||||||
|
|
||||||
}
|
void increase(Menu *m);
|
||||||
|
|
||||||
void displayMenu(Menu *m) {
|
void decrease(Menu *m);
|
||||||
int page = m_current_index / 4;
|
|
||||||
int pageindex = m_current_index % 4;
|
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
m_driver.LCDSetCursor(0, i);
|
|
||||||
|
|
||||||
if (pageindex == i) {
|
|
||||||
m_driver.LCDSendChar(LCD_I2C_Driver::SpecialChars::RightArrow);
|
|
||||||
} else {
|
|
||||||
m_driver.LCDSendCString(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
m_driver.LCDSetCursor(1, i);
|
|
||||||
|
|
||||||
int entry_index = (page * 4) + i;
|
|
||||||
|
|
||||||
auto menutexts = m->getDisplayList();
|
|
||||||
|
|
||||||
if(entry_index < m->getAllEntriesNum()) {
|
|
||||||
std::string dspstring = menutexts[entry_index];
|
|
||||||
|
|
||||||
if (dspstring.length() > 19) {
|
|
||||||
dspstring = "-- ERR --";
|
|
||||||
} else {
|
|
||||||
dspstring.append((19 - dspstring.length()), ' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
m_driver.LCDSendCString(const_cast<char *>(dspstring.c_str()));
|
|
||||||
} else {
|
|
||||||
m_driver.LCDSendCString(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void keypress(Menu *m) {
|
|
||||||
//Enter submenu if entry is submenu
|
|
||||||
if (m->getSubmenu(m_current_index) != nullptr) {
|
|
||||||
m_menu = m->getSubmenu(m_current_index);
|
|
||||||
//Forward press action if entry is entry
|
|
||||||
} else if (m->getEntry(m_current_index) != nullptr) {
|
|
||||||
m->getEntry(m_current_index)->action_press();
|
|
||||||
} else if(m_menu->m_parent != nullptr) {
|
|
||||||
m_menu = m_menu->m_parent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void increase(Menu *m) {
|
|
||||||
if(m_current_index < m->getAllEntriesNum() - 1) {
|
|
||||||
if(m->getEntry(m_current_index) != nullptr) {
|
|
||||||
if(m->getEntry(m_current_index)->isEntered()) {
|
|
||||||
m->getEntry(m_current_index)->action_increase();
|
|
||||||
} else {
|
|
||||||
m_current_index++;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
m_current_index++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void decrease(Menu *m) {
|
|
||||||
if(m_current_index > 0) {
|
|
||||||
if(m->getEntry(m_current_index) != nullptr) {
|
|
||||||
if(m->getEntry(m_current_index)->isEntered()) {
|
|
||||||
m->getEntry(m_current_index)->action_decrease();
|
|
||||||
} else {
|
|
||||||
m_current_index--;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
m_current_index--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LCD_I2C_Driver &m_driver;
|
LCD_I2C_Driver &m_driver;
|
||||||
@ -119,7 +40,6 @@ namespace floatpump {
|
|||||||
Menu *m_menu;
|
Menu *m_menu;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // floatpump
|
|
||||||
} // menu
|
} // menu
|
||||||
|
|
||||||
#endif //FLOATPUMP_MENU_CONTROLLER_H
|
#endif //FLOATPUMP_MENU_CONTROLLER_H
|
||||||
|
@ -8,43 +8,31 @@
|
|||||||
#include <Menu_Entry_Type_Delegate.h>
|
#include <Menu_Entry_Type_Delegate.h>
|
||||||
|
|
||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump::menu {
|
||||||
namespace menu {
|
|
||||||
|
|
||||||
class Menu_Entry {
|
class Menu_Entry {
|
||||||
public:
|
public:
|
||||||
menu::Menu_Entry_Type_Delegate *m_type;
|
|
||||||
|
|
||||||
Menu_Entry(Menu_Entry_Type_Delegate *type, std::string name) : m_type(type), m_name(name) {};
|
Menu_Entry(Menu_Entry_Type_Delegate *type, std::string name) : m_type(type), m_name(name) {};
|
||||||
|
|
||||||
std::string printLine() {
|
|
||||||
int spaces = 19 - (m_name.length() + m_type->toString().length());
|
|
||||||
std::string spacer;
|
|
||||||
spacer.append(spaces, ' ');
|
|
||||||
return m_name + spacer + m_type->toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
void action_press() {
|
|
||||||
m_type->u_press();
|
|
||||||
}
|
|
||||||
|
|
||||||
void action_increase() {
|
//Retreive string for putting on display
|
||||||
m_type->u_increase(1);
|
std::string printLine();
|
||||||
}
|
|
||||||
|
|
||||||
void action_decrease() {
|
void action_press();
|
||||||
m_type->u_decrease(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isEntered() {
|
void action_increase();
|
||||||
return m_type->isEntered();
|
|
||||||
}
|
void action_decrease();
|
||||||
|
|
||||||
|
//Check if currently entered
|
||||||
|
bool isEntered();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
|
menu::Menu_Entry_Type_Delegate *m_type; //Saves type of this menu entry
|
||||||
};
|
};
|
||||||
|
|
||||||
} // floatpump
|
}
|
||||||
} // menu
|
|
||||||
|
|
||||||
#endif //FLOATPUMP_MENU_ENTRY_H
|
#endif //FLOATPUMP_MENU_ENTRY_H
|
||||||
|
@ -7,44 +7,28 @@
|
|||||||
|
|
||||||
#include "Menu_Entry_Type_Delegate.h"
|
#include "Menu_Entry_Type_Delegate.h"
|
||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump::menu {
|
||||||
namespace menu {
|
|
||||||
|
|
||||||
class Menu_Entry_Type_Checkable : public Menu_Entry_Type_Delegate {
|
class Menu_Entry_Type_Checkable : public Menu_Entry_Type_Delegate {
|
||||||
public:
|
public:
|
||||||
Menu_Entry_Type_Checkable(bool mState) {
|
explicit Menu_Entry_Type_Checkable(bool mState);
|
||||||
m_state = new bool(mState);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string toString() override {
|
std::string toString() override;
|
||||||
return std::string((*m_state) ? "ON " : "OFF");
|
|
||||||
}
|
|
||||||
|
|
||||||
void u_press() override {
|
void u_press() override;
|
||||||
*m_state = !(*m_state);
|
|
||||||
}
|
|
||||||
|
|
||||||
void u_increase(uint16_t steps) override {
|
void u_increase(uint16_t steps) override;
|
||||||
|
|
||||||
}
|
void u_decrease(uint16_t steps) override;
|
||||||
|
|
||||||
void u_decrease(uint16_t steps) override {
|
bool isEntered() override;
|
||||||
|
|
||||||
}
|
void linkConfig(bool *link);
|
||||||
|
|
||||||
bool isEntered() override {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void linkConfig(bool *link) {
|
|
||||||
m_state = link;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool *m_state;
|
bool *m_state;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // floatpump
|
|
||||||
} // menu
|
} // menu
|
||||||
|
|
||||||
#endif //FLOATPUMP_MENU_ENTRY_TYPE_CHECKABLE_H
|
#endif //FLOATPUMP_MENU_ENTRY_TYPE_CHECKABLE_H
|
||||||
|
@ -13,6 +13,8 @@ namespace floatpump {
|
|||||||
|
|
||||||
class Menu_Entry_Type_Numeric : public Menu_Entry_Type_Delegate {
|
class Menu_Entry_Type_Numeric : public Menu_Entry_Type_Delegate {
|
||||||
public:
|
public:
|
||||||
|
explicit Menu_Entry_Type_Numeric(uint16_t mValue);
|
||||||
|
|
||||||
std::string toString() override;
|
std::string toString() override;
|
||||||
|
|
||||||
void u_press() override;
|
void u_press() override;
|
||||||
@ -23,8 +25,10 @@ namespace floatpump {
|
|||||||
|
|
||||||
bool isEntered() override;
|
bool isEntered() override;
|
||||||
|
|
||||||
|
void linkConfig(uint16_t *link);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t m_value = 0;
|
uint16_t *m_value = 0;
|
||||||
bool m_entered = false;
|
bool m_entered = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -7,13 +7,12 @@
|
|||||||
|
|
||||||
#include "Menu_Entry_Type_Delegate.h"
|
#include "Menu_Entry_Type_Delegate.h"
|
||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump::menu {
|
||||||
namespace menu {
|
|
||||||
|
|
||||||
|
|
||||||
class Menu_Entry_Type_Percent : public floatpump::menu::Menu_Entry_Type_Delegate{
|
class Menu_Entry_Type_Percent : public floatpump::menu::Menu_Entry_Type_Delegate {
|
||||||
public:
|
public:
|
||||||
Menu_Entry_Type_Percent(uint8_t val) : m_value(val) {};
|
explicit Menu_Entry_Type_Percent(uint8_t val);;
|
||||||
|
|
||||||
std::string toString() override;
|
std::string toString() override;
|
||||||
|
|
||||||
@ -25,12 +24,13 @@ namespace floatpump {
|
|||||||
|
|
||||||
bool isEntered() override;
|
bool isEntered() override;
|
||||||
|
|
||||||
|
void linkConfig(uint8_t *link);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t m_value = 0;
|
uint8_t *m_value = nullptr;
|
||||||
bool m_entered = false;
|
bool m_entered = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} //menu
|
|
||||||
} //floatpump
|
} //floatpump
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,61 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by robtor on 05.01.23.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "Menu_Entry_Type_Time.h"
|
|
||||||
|
|
||||||
namespace floatpump {
|
|
||||||
namespace menu {
|
|
||||||
std::string Menu_Entry_Type_Time::toString() {
|
|
||||||
switch(m_entered) {
|
|
||||||
case 0:
|
|
||||||
return std::string(" " + std::to_string(m_hour) + ":" + std::to_string(m_minute) + ":" + std::to_string(m_second)); break;
|
|
||||||
case 1:
|
|
||||||
return std::string("#" + std::to_string(m_hour) + ":" + std::to_string(m_minute) + ":" + std::to_string(m_second)); break;
|
|
||||||
case 2:
|
|
||||||
return std::string(" " + std::to_string(m_hour) + "#" + std::to_string(m_minute) + ":" + std::to_string(m_second)); break;
|
|
||||||
case 3:
|
|
||||||
return std::string(" " + std::to_string(m_hour) + ":" + std::to_string(m_minute) + "#" + std::to_string(m_second)); break;
|
|
||||||
}
|
|
||||||
return std::string();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Menu_Entry_Type_Time::u_press() {
|
|
||||||
if(m_entered < 3) {
|
|
||||||
m_entered++;
|
|
||||||
} else {
|
|
||||||
m_entered = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Menu_Entry_Type_Time::u_increase(uint16_t steps) {
|
|
||||||
switch(m_entered) {
|
|
||||||
case 1:
|
|
||||||
(m_hour < 23) ? m_hour++ : m_hour = 0; break;
|
|
||||||
case 2:
|
|
||||||
(m_minute < 59) ? m_minute++ : m_minute = 0; break;
|
|
||||||
case 3:
|
|
||||||
(m_second < 59) ? m_second++ : m_second = 0; break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Menu_Entry_Type_Time::u_decrease(uint16_t steps) {
|
|
||||||
switch(m_entered) {
|
|
||||||
case 1:
|
|
||||||
(m_hour > 0) ? m_hour-- : m_hour = 23; break;
|
|
||||||
case 2:
|
|
||||||
(m_minute > 0) ? m_minute-- : m_minute = 59; break;
|
|
||||||
case 3:
|
|
||||||
(m_second > 0) ? m_second-- : m_second = 59; break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Menu_Entry_Type_Time::isEntered() {
|
|
||||||
if(m_entered > 0) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // floatpump
|
|
||||||
} // menu
|
|
@ -7,12 +7,11 @@
|
|||||||
|
|
||||||
#include "Menu_Entry_Type_Delegate.h"
|
#include "Menu_Entry_Type_Delegate.h"
|
||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump::menu {
|
||||||
namespace menu {
|
|
||||||
|
|
||||||
class Menu_Entry_Type_Time : public Menu_Entry_Type_Delegate{
|
class Menu_Entry_Type_Time : public Menu_Entry_Type_Delegate{
|
||||||
public:
|
public:
|
||||||
Menu_Entry_Type_Time(uint8_t hour, uint8_t minute, uint8_t second) : m_hour(hour), m_minute(minute), m_second(second) {};
|
Menu_Entry_Type_Time(uint8_t hour, uint8_t minute, uint8_t second);;
|
||||||
|
|
||||||
std::string toString() override;
|
std::string toString() override;
|
||||||
|
|
||||||
@ -24,12 +23,13 @@ namespace floatpump {
|
|||||||
|
|
||||||
bool isEntered() override;
|
bool isEntered() override;
|
||||||
|
|
||||||
|
void linkConfig(uint8_t *c_hour, uint8_t *c_minute, uint8_t *c_second);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t m_entered = 0;
|
uint8_t m_entered = 0;
|
||||||
uint8_t m_hour, m_minute, m_second;
|
uint8_t *m_hour, *m_minute, *m_second = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // floatpump
|
} // menu
|
||||||
} // menu
|
|
||||||
|
|
||||||
#endif //FLOATPUMP_MENU_ENTRY_TYPE_TIME_H
|
#endif //FLOATPUMP_MENU_ENTRY_TYPE_TIME_H
|
||||||
|
@ -4,7 +4,66 @@
|
|||||||
|
|
||||||
#include "Menu.h"
|
#include "Menu.h"
|
||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump::menu {
|
||||||
namespace menu {
|
void Menu::addEntry(const Menu_Entry &entry) {
|
||||||
} // floatpump
|
m_entries.push_back(entry);
|
||||||
} // menu
|
}
|
||||||
|
|
||||||
|
void Menu::addSubmenu(Menu *submenu) {
|
||||||
|
//Always set this menu as parent of created submenus within this menu
|
||||||
|
submenu->m_parent = this;
|
||||||
|
m_submenus.push_back(submenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Menu::printLine() const {
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> Menu::getDisplayList() {
|
||||||
|
std::vector<std::string> list;
|
||||||
|
|
||||||
|
list.reserve(m_submenus.size() + m_entries.size());
|
||||||
|
|
||||||
|
//Append al Submenus
|
||||||
|
for (auto &m_submenu: m_submenus) {
|
||||||
|
list.push_back(m_submenu->printLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
//Append all Entries
|
||||||
|
for (auto &m_entry: m_entries) {
|
||||||
|
list.push_back(m_entry.printLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
//Append Go-Back-Entry if we are within a submenu
|
||||||
|
if (m_parent != nullptr) {
|
||||||
|
list.emplace_back("<- Go Back");
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int Menu::getAllEntriesNum() const {
|
||||||
|
//Add 1 to this if we have a parent menu -> makes Go-Back-Entry selectable
|
||||||
|
return (m_entries.size() + m_submenus.size()) + ((m_parent != nullptr) ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Menu *Menu::getSubmenu(int index) {
|
||||||
|
if (index >= 0 && index < m_submenus.size()) {
|
||||||
|
return m_submenus[index];
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Menu_Entry *Menu::getEntry(int index) {
|
||||||
|
if (index >= m_submenus.size() && index < m_entries.size() + m_submenus.size()) {
|
||||||
|
return &m_entries[index - m_submenus.size()];
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Menu *Menu::getParent() {
|
||||||
|
return m_parent;
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,108 @@
|
|||||||
|
|
||||||
#include "Menu_Controller.h"
|
#include "Menu_Controller.h"
|
||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump::menu {
|
||||||
namespace menu {
|
|
||||||
} // floatpump
|
void Menu_Controller::execute() {
|
||||||
|
displayMenu(m_menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Controller::pushEvent(Menu_Controller::Event ev) {
|
||||||
|
switch (ev) {
|
||||||
|
case Increase:
|
||||||
|
increase(m_menu);
|
||||||
|
break;
|
||||||
|
case Decrease:
|
||||||
|
decrease(m_menu);
|
||||||
|
break;
|
||||||
|
case Push:
|
||||||
|
keypress(m_menu);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Controller::displayMenu(Menu *m) {
|
||||||
|
int page = m_current_index / 4; //Currently displayed page
|
||||||
|
int pageindex = m_current_index % 4; //Current line on page
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
m_driver.LCDSetCursor(0, i);
|
||||||
|
|
||||||
|
//Set arrow marker for currently selected line
|
||||||
|
if (pageindex == i) {
|
||||||
|
m_driver.LCDSendChar(LCD_I2C_Driver::SpecialChars::RightArrow);
|
||||||
|
} else {
|
||||||
|
m_driver.LCDSendCString((char *) " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
m_driver.LCDSetCursor(1, i);
|
||||||
|
|
||||||
|
//Map screen line numbers to index
|
||||||
|
int entry_index = (page * 4) + i;
|
||||||
|
|
||||||
|
//Retreive menu contents
|
||||||
|
auto menutexts = m->getDisplayList();
|
||||||
|
|
||||||
|
if (entry_index < m->getAllEntriesNum()) {
|
||||||
|
std::string dspstring = menutexts[entry_index];
|
||||||
|
|
||||||
|
if (dspstring.length() > 19) {
|
||||||
|
dspstring = "-- ERR --";
|
||||||
|
} else {
|
||||||
|
dspstring.append((19 - dspstring.length()), ' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
m_driver.LCDSendCString(const_cast<char *>(dspstring.c_str()));
|
||||||
|
} else {
|
||||||
|
//Clear rest of screen if not fully containing a menu
|
||||||
|
m_driver.LCDSendCString((char *) " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Controller::keypress(Menu *m) {
|
||||||
|
//Enter submenu if entry is submenu
|
||||||
|
if (m->getSubmenu(m_current_index) != nullptr) {
|
||||||
|
//Replace containing menu with submenu
|
||||||
|
m_menu = m->getSubmenu(m_current_index);
|
||||||
|
m_current_index = 0;
|
||||||
|
//Forward press action if entry is entry
|
||||||
|
} else if (m->getEntry(m_current_index) != nullptr) {
|
||||||
|
m->getEntry(m_current_index)->action_press();
|
||||||
|
//If we are on the Go-Back-Entry and have a parent: replace menu with parent menu
|
||||||
|
} else if (m_menu->getParent() != nullptr) {
|
||||||
|
m_menu = m_menu->getParent();
|
||||||
|
m_current_index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Controller::increase(Menu *m) {
|
||||||
|
if (m_current_index < m->getAllEntriesNum() - 1) {
|
||||||
|
if (m->getEntry(m_current_index) != nullptr) {
|
||||||
|
//Forward increase action to entry if we have an entered entry
|
||||||
|
if (m->getEntry(m_current_index)->isEntered()) {
|
||||||
|
m->getEntry(m_current_index)->action_increase();
|
||||||
|
} else {
|
||||||
|
m_current_index++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
m_current_index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Controller::decrease(Menu *m) {
|
||||||
|
if (m_current_index > 0) {
|
||||||
|
if (m->getEntry(m_current_index) != nullptr) {
|
||||||
|
//Forward decrease action to entry if we have an entered entry
|
||||||
|
if (m->getEntry(m_current_index)->isEntered()) {
|
||||||
|
m->getEntry(m_current_index)->action_decrease();
|
||||||
|
} else {
|
||||||
|
m_current_index--;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
m_current_index--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} // menu
|
} // menu
|
@ -6,5 +6,29 @@
|
|||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump {
|
||||||
namespace menu {
|
namespace menu {
|
||||||
|
|
||||||
|
std::string Menu_Entry::printLine() {
|
||||||
|
//We have 19 characters width for displaying the entry -> fill with spaces
|
||||||
|
int spaces = 19 - (m_name.length() + m_type->toString().length());
|
||||||
|
std::string spacer;
|
||||||
|
spacer.append(spaces, ' ');
|
||||||
|
return m_name + spacer + m_type->toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry::action_press() {
|
||||||
|
m_type->u_press();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry::action_increase() {
|
||||||
|
m_type->u_increase(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry::action_decrease() {
|
||||||
|
m_type->u_decrease(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Menu_Entry::isEntered() {
|
||||||
|
return m_type->isEntered();
|
||||||
|
}
|
||||||
} // floatpump
|
} // floatpump
|
||||||
} // menu
|
} // menu
|
@ -8,5 +8,32 @@ namespace floatpump {
|
|||||||
namespace menu {
|
namespace menu {
|
||||||
|
|
||||||
|
|
||||||
|
Menu_Entry_Type_Checkable::Menu_Entry_Type_Checkable(bool mState) {
|
||||||
|
m_state = new bool(mState);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Menu_Entry_Type_Checkable::toString() {
|
||||||
|
return {(*m_state) ? "ON " : "OFF"};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry_Type_Checkable::u_press() {
|
||||||
|
*m_state = !(*m_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry_Type_Checkable::u_increase(uint16_t steps) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry_Type_Checkable::u_decrease(uint16_t steps) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Menu_Entry_Type_Checkable::isEntered() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry_Type_Checkable::linkConfig(bool *link) {
|
||||||
|
m_state = link;
|
||||||
|
}
|
||||||
} // floatpump
|
} // floatpump
|
||||||
} // menu
|
} // menu
|
@ -6,11 +6,16 @@
|
|||||||
|
|
||||||
namespace floatpump {
|
namespace floatpump {
|
||||||
namespace menu {
|
namespace menu {
|
||||||
|
|
||||||
|
Menu_Entry_Type_Numeric::Menu_Entry_Type_Numeric(uint16_t mValue) {
|
||||||
|
m_value = new uint16_t(mValue);
|
||||||
|
}
|
||||||
|
|
||||||
std::string Menu_Entry_Type_Numeric::toString() {
|
std::string Menu_Entry_Type_Numeric::toString() {
|
||||||
if(m_entered) {
|
if (m_entered) {
|
||||||
return "# " + std::to_string(m_value);
|
return "# " + std::to_string(*m_value);
|
||||||
} else {
|
} else {
|
||||||
return " " + std::to_string(m_value);
|
return " " + std::to_string(*m_value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,5 +34,9 @@ namespace floatpump {
|
|||||||
bool Menu_Entry_Type_Numeric::isEntered() {
|
bool Menu_Entry_Type_Numeric::isEntered() {
|
||||||
return m_entered;
|
return m_entered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Menu_Entry_Type_Numeric::linkConfig(uint16_t *link) {
|
||||||
|
m_value = link;
|
||||||
|
}
|
||||||
} // floatpump
|
} // floatpump
|
||||||
} // menu
|
} // menu
|
@ -5,10 +5,10 @@
|
|||||||
#include "Menu_Entry_Type_Percent.h"
|
#include "Menu_Entry_Type_Percent.h"
|
||||||
|
|
||||||
std::string floatpump::menu::Menu_Entry_Type_Percent::toString() {
|
std::string floatpump::menu::Menu_Entry_Type_Percent::toString() {
|
||||||
if(m_entered) {
|
if (m_entered) {
|
||||||
return std::string("# " + std::to_string(m_value) + "%");
|
return std::string("# " + std::to_string(*m_value) + "%");
|
||||||
} else {
|
} else {
|
||||||
return std::string(" " + std::to_string(m_value) + "%");
|
return std::string(" " + std::to_string(*m_value) + "%");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,16 +17,24 @@ void floatpump::menu::Menu_Entry_Type_Percent::u_press() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void floatpump::menu::Menu_Entry_Type_Percent::u_increase(uint16_t steps) {
|
void floatpump::menu::Menu_Entry_Type_Percent::u_increase(uint16_t steps) {
|
||||||
if(m_value < 100)
|
if (*m_value < 100)
|
||||||
m_value++;
|
(*m_value)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void floatpump::menu::Menu_Entry_Type_Percent::u_decrease(uint16_t steps) {
|
void floatpump::menu::Menu_Entry_Type_Percent::u_decrease(uint16_t steps) {
|
||||||
if(m_value > 0) {
|
if (*m_value > 0) {
|
||||||
m_value--;
|
(*m_value)--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool floatpump::menu::Menu_Entry_Type_Percent::isEntered() {
|
bool floatpump::menu::Menu_Entry_Type_Percent::isEntered() {
|
||||||
return m_entered;
|
return m_entered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
floatpump::menu::Menu_Entry_Type_Percent::Menu_Entry_Type_Percent(uint8_t val) {
|
||||||
|
m_value = new uint8_t(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void floatpump::menu::Menu_Entry_Type_Percent::linkConfig(uint8_t *link) {
|
||||||
|
m_value = link;
|
||||||
|
}
|
||||||
|
73
Middlewares/floatpump/Src/Menu_Entry_Type_Time.cpp
Normal file
73
Middlewares/floatpump/Src/Menu_Entry_Type_Time.cpp
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
//
|
||||||
|
// Created by robtor on 05.01.23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Menu_Entry_Type_Time.h"
|
||||||
|
|
||||||
|
namespace floatpump {
|
||||||
|
namespace menu {
|
||||||
|
std::string Menu_Entry_Type_Time::toString() {
|
||||||
|
switch(m_entered) {
|
||||||
|
case 0:
|
||||||
|
return std::string(" " + std::to_string(*m_hour) + ":" + std::to_string(*m_minute) + ":" + std::to_string(*m_second)); break;
|
||||||
|
case 1:
|
||||||
|
return std::string("#" + std::to_string(*m_hour) + ":" + std::to_string(*m_minute) + ":" + std::to_string(*m_second)); break;
|
||||||
|
case 2:
|
||||||
|
return std::string(" " + std::to_string(*m_hour) + "#" + std::to_string(*m_minute) + ":" + std::to_string(*m_second)); break;
|
||||||
|
case 3:
|
||||||
|
return std::string(" " + std::to_string(*m_hour) + ":" + std::to_string(*m_minute) + "#" + std::to_string(*m_second)); break;
|
||||||
|
}
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry_Type_Time::u_press() {
|
||||||
|
if(m_entered < 3) {
|
||||||
|
m_entered++;
|
||||||
|
} else {
|
||||||
|
m_entered = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry_Type_Time::u_increase(uint16_t steps) {
|
||||||
|
switch(m_entered) {
|
||||||
|
case 1:
|
||||||
|
(*m_hour < 23) ? (*m_hour)++ : *m_hour = 0; break;
|
||||||
|
case 2:
|
||||||
|
(*m_minute < 59) ? (*m_minute)++ : *m_minute = 0; break;
|
||||||
|
case 3:
|
||||||
|
(*m_second < 59) ? (*m_second)++ : *m_second = 0; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry_Type_Time::u_decrease(uint16_t steps) {
|
||||||
|
switch(m_entered) {
|
||||||
|
case 1:
|
||||||
|
(*m_hour > 0) ? (*m_hour)-- : *m_hour = 23; break;
|
||||||
|
case 2:
|
||||||
|
(*m_minute > 0) ? (*m_minute)-- : *m_minute = 59; break;
|
||||||
|
case 3:
|
||||||
|
(*m_second > 0) ? (*m_second)-- : *m_second = 59; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Menu_Entry_Type_Time::isEntered() {
|
||||||
|
if(m_entered > 0) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Menu_Entry_Type_Time::Menu_Entry_Type_Time(uint8_t hour, uint8_t minute, uint8_t second) {
|
||||||
|
m_hour = new uint8_t (hour);
|
||||||
|
m_minute = new uint8_t (minute);
|
||||||
|
m_second = new uint8_t (second);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu_Entry_Type_Time::linkConfig(uint8_t *c_hour, uint8_t *c_minute, uint8_t *c_second) {
|
||||||
|
m_hour = c_hour;
|
||||||
|
m_minute = c_minute;
|
||||||
|
m_second = c_second;
|
||||||
|
}
|
||||||
|
} // floatpump
|
||||||
|
} // menu
|
Loading…
Reference in New Issue
Block a user