floatpump-firmware/Middlewares/floatpump/Inc/Menu_Controller.h

126 lines
3.9 KiB
C++

//
// Created by robtor on 05.01.23.
//
#ifndef FLOATPUMP_MENU_CONTROLLER_H
#define FLOATPUMP_MENU_CONTROLLER_H
#include "Menu.h"
namespace floatpump {
namespace menu {
class Menu_Controller {
public:
Menu_Controller(Menu *menu, LCD_I2C_Driver &driver): m_menu(menu), m_driver(driver) {};
enum Event {Increase, Decrease, Push};
void execute() {
displayMenu(m_menu);
}
void pushEvent(Event ev) {
switch(ev) {
case Increase:
increase(m_menu); break;
case Decrease:
decrease(m_menu); break;
case Push:
keypress(m_menu); break;
}
}
private:
bool isEntry() {
}
void displayMenu(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:
LCD_I2C_Driver &m_driver;
int m_current_index = 0;
Menu *m_menu;
};
} // floatpump
} // menu
#endif //FLOATPUMP_MENU_CONTROLLER_H