floatpump-firmware/Middlewares/floatpump/Inc/Menu.h
2023-01-05 16:32:42 +01:00

117 lines
4.1 KiB
C++

//
// Created by robtor on 09.12.22.
//
#ifndef FLOATPUMP_MENU_H
#define FLOATPUMP_MENU_H
#include <vector>
#include <typeinfo>
#include "Menu_Entry.h"
#include "LCD_I2C_Driver.h"
namespace floatpump {
namespace menu {
class Menu {
public:
Menu(LCD_I2C_Driver &driver) : m_driver(driver) {
}
void addEntry(Menu_Entry entry) {
m_entries.push_back(entry);
}
void updateMenu() {
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;
//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("-");
}
}
}
void keypress() {
//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();
}
}
void increase() {
//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++;
}
}
}
void decrease() {
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--;
}
}
void addSubmenu(Menu *submenu) {
submenu->m_parent = this;
m_submenus.push_back(submenu);
}
private:
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;
};
} // floatpump
} // menu
#endif //FLOATPUMP_MENU_H