2022-12-13 11:14:45 +00:00
|
|
|
//
|
|
|
|
// Created by robtor on 09.12.22.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef FLOATPUMP_MENU_H
|
|
|
|
#define FLOATPUMP_MENU_H
|
|
|
|
|
2023-01-04 12:22:45 +00:00
|
|
|
#include <vector>
|
2023-01-05 15:32:42 +00:00
|
|
|
#include <typeinfo>
|
2023-01-04 12:22:45 +00:00
|
|
|
#include "Menu_Entry.h"
|
|
|
|
#include "LCD_I2C_Driver.h"
|
|
|
|
|
2022-12-13 11:14:45 +00:00
|
|
|
namespace floatpump {
|
|
|
|
namespace menu {
|
|
|
|
|
|
|
|
class Menu {
|
|
|
|
|
2023-01-04 12:22:45 +00:00
|
|
|
public:
|
|
|
|
Menu(LCD_I2C_Driver &driver) : m_driver(driver) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEntry(Menu_Entry entry) {
|
|
|
|
m_entries.push_back(entry);
|
|
|
|
}
|
2023-01-05 15:32:42 +00:00
|
|
|
void updateMenu() {
|
|
|
|
int page = m_current_index / 4;
|
|
|
|
int pageindex = m_current_index % 4;
|
2023-01-04 12:22:45 +00:00
|
|
|
|
2023-01-05 15:32:42 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
m_driver.LCDSetCursor(0, i);
|
2023-01-04 12:22:45 +00:00
|
|
|
|
2023-01-05 15:32:42 +00:00
|
|
|
if (pageindex == i) {
|
|
|
|
m_driver.LCDSendChar(LCD_I2C_Driver::SpecialChars::RightArrow);
|
2023-01-04 13:54:51 +00:00
|
|
|
} else {
|
2023-01-05 15:32:42 +00:00
|
|
|
m_driver.LCDSendCString(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_driver.LCDSetCursor(1, i);
|
|
|
|
|
|
|
|
int entry_index = (page * 4) + i;
|
|
|
|
//Display submenus first and then entries
|
|
|
|
if (entry_index < m_submenus.size()) {
|
|
|
|
std::string dspstring = "submenu";
|
|
|
|
m_driver.LCDSendCString("submenu");
|
|
|
|
//TODO: display submenu contentsnames
|
|
|
|
//Display entries
|
|
|
|
} else if (entry_index < (m_entries.size() + m_submenus.size())) {
|
|
|
|
std::string dspstring = m_entries[entry_index - m_submenus.size()].printLine();
|
|
|
|
|
|
|
|
if (dspstring.length() > 19) {
|
|
|
|
m_driver.LCDSendCString("-------------------");
|
|
|
|
} else {
|
|
|
|
dspstring.append((19 - dspstring.length()), ' ');
|
|
|
|
m_driver.LCDSendCString(const_cast<char *>(dspstring.c_str()));
|
|
|
|
}
|
|
|
|
} else { //Show separator at end of menu
|
|
|
|
//TODO: make this look better
|
|
|
|
m_driver.LCDSendCString("-");
|
2023-01-04 13:54:51 +00:00
|
|
|
}
|
2023-01-04 12:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void keypress() {
|
2023-01-05 15:32:42 +00:00
|
|
|
//enter submenu
|
|
|
|
if (m_current_index < m_submenus.size()) {
|
|
|
|
//forward press to entry
|
|
|
|
} else if (m_current_index < (m_entries.size() + m_submenus.size())) {
|
|
|
|
m_entries[m_current_index - m_submenus.size()].action_press();
|
2023-01-04 12:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void increase() {
|
2023-01-05 15:32:42 +00:00
|
|
|
//always increase
|
|
|
|
if (m_current_index < m_submenus.size()) {
|
|
|
|
m_current_index++;
|
|
|
|
//increase when not in entry entered state
|
|
|
|
} else if (m_current_index < (m_submenus.size() + m_entries.size())) {
|
|
|
|
if (m_entries[m_current_index - m_submenus.size()].isEntered()) {
|
|
|
|
m_entries[m_current_index - m_submenus.size()].action_increase();
|
|
|
|
} else if (m_current_index < (m_submenus.size() + m_entries.size() - 1)) {
|
|
|
|
m_current_index++;
|
|
|
|
}
|
2023-01-04 13:54:51 +00:00
|
|
|
}
|
2023-01-04 12:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void decrease() {
|
2023-01-05 15:32:42 +00:00
|
|
|
if (m_current_index > m_submenus.size()) {
|
|
|
|
if (m_entries[m_current_index - m_submenus.size()].isEntered()) {
|
|
|
|
m_entries[m_current_index - m_submenus.size()].action_decrease();
|
|
|
|
} else {
|
|
|
|
m_current_index--;
|
|
|
|
}
|
|
|
|
} else if (m_current_index > 0) {
|
|
|
|
m_current_index--;
|
2023-01-04 13:54:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:22:45 +00:00
|
|
|
}
|
2023-01-05 15:32:42 +00:00
|
|
|
|
|
|
|
void addSubmenu(Menu *submenu) {
|
|
|
|
submenu->m_parent = this;
|
|
|
|
m_submenus.push_back(submenu);
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:22:45 +00:00
|
|
|
private:
|
|
|
|
LCD_I2C_Driver &m_driver;
|
|
|
|
std::vector<Menu_Entry> m_entries;
|
2023-01-05 15:32:42 +00:00
|
|
|
std::vector<Menu *> m_submenus;
|
|
|
|
Menu *m_parent = nullptr;
|
|
|
|
int m_current_index = 0;
|
2022-12-13 11:14:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // floatpump
|
|
|
|
} // menu
|
|
|
|
|
|
|
|
#endif //FLOATPUMP_MENU_H
|