FEATURE: finished menu structure driver

This commit is contained in:
Robin Dietzel 2023-01-06 12:12:37 +01:00
parent a5e10bf0ea
commit 025c5e17d3
3 changed files with 71 additions and 56 deletions

View File

@ -7,6 +7,6 @@
// Auto generated header file containing the last git revision
#define GIT_HASH "5232017"
#define GIT_HASH "3e3f570"
#endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H

View File

@ -28,43 +28,57 @@ namespace floatpump {
m_submenus.push_back(submenu);
}
bool isSubmenu(int index) {
if(index > 0 && index < m_submenus.size()) {
return true;
}
return false;
std::string printLine() {
return m_name;
}
bool isEntry(int index) {
if(index >= m_submenus.size() && index < (m_entries.size() + m_submenus.size())) {
return true;
std::vector<std::string> getDisplayList() {
std::vector<std::string> list;
//first show submenus
for(auto it = m_submenus.begin(); it != m_submenus.end(); ++it) {
list.push_back((*it)->printLine());
}
return false;
for(auto it = m_entries.begin(); it != m_entries.end(); ++it) {
list.push_back(it->printLine());
}
if(m_parent != nullptr) {
list.push_back("<- Go Back");
}
return list;
}
Menu_Entry *getEntry(int index) {
if(isEntry(index)) {
return &m_entries[index - m_submenus.size()];
} else {
return nullptr;
}
int getAllEntriesNum() {
return (m_entries.size() + m_submenus.size()) + ((m_parent != nullptr)? 1 : 0);
}
Menu *getSubmenu(int index) {
if(isSubmenu(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:

View File

@ -31,6 +31,12 @@ namespace floatpump {
}
}
private:
bool isEntry() {
}
void displayMenu(Menu *m) {
int page = m_current_index / 4;
int pageindex = m_current_index % 4;
@ -47,69 +53,64 @@ namespace floatpump {
m_driver.LCDSetCursor(1, i);
int entry_index = (page * 4) + i;
//Display submenus first and then entries
if (entry_index < m->m_submenus.size()) {
std::string dspstring = "submenu";
m_driver.LCDSendCString("submenu");
//TODO: display submenus contentname
//Display entries
} else if (entry_index < (m->m_entries.size() + m->m_submenus.size())) {
std::string dspstring = m->m_entries[entry_index - m->m_submenus.size()].printLine();
auto menutexts = m->getDisplayList();
if(entry_index < m->getAllEntriesNum()) {
std::string dspstring = menutexts[entry_index];
if (dspstring.length() > 19) {
m_driver.LCDSendCString("-------------------");
dspstring = "-- ERR --";
} else {
dspstring.append((19 - dspstring.length()), ' ');
m_driver.LCDSendCString(const_cast<char *>(dspstring.c_str()));
}
} else if (entry_index == (m->m_entries.size() + m->m_submenus.size()) && m->m_parent != nullptr) {
m_driver.LCDSendCString("Go Back");
} else { //Show separator at end of menu
//TODO: make this look better
m_driver.LCDSendCString(const_cast<char *>(dspstring.c_str()));
} else {
m_driver.LCDSendCString(" ");
}
}
}
void keypress(Menu *m) {
//enter submenu
if (m_current_index < m->m_submenus.size()) {
m_menu = m->m_submenus[m_current_index];
//forward press to entry
} else if (m_current_index < (m->m_entries.size() + m->m_submenus.size())) {
m->m_entries[m_current_index - m->m_submenus.size()].action_press();
} else if (m_current_index == (m->m_submenus.size() + m->m_entries.size()) && m->m_parent != nullptr) {
m_menu = m->m_parent;
//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) {
//always increase
if (m_current_index < m->m_submenus.size()) {
m_current_index++;
//increase when not in entry entered state
} else if (m_current_index < (m->m_submenus.size() + m->m_entries.size())) {
if (m->m_entries[m_current_index - m->m_submenus.size()].isEntered()) {
m->m_entries[m_current_index - m->m_submenus.size()].action_increase();
} else if (m_current_index < (m->m_submenus.size() + m->m_entries.size()) && m->m_parent != nullptr) {
m_current_index++;
} else if (m_current_index < (m->m_submenus.size() + m->m_entries.size()) - 1) {
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 > m->m_submenus.size()) {
if (m->m_entries[m_current_index - m->m_submenus.size()].isEntered()) {
m->m_entries[m_current_index - m->m_submenus.size()].action_decrease();
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--;
}
} else if (m_current_index > 0) {
m_current_index--;
}
}
private: