2023-01-05 15:32:42 +00:00
|
|
|
//
|
|
|
|
// 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 {
|
2023-01-05 16:42:39 +00:00
|
|
|
public:
|
|
|
|
Menu_Controller(Menu *menu, LCD_I2C_Driver &driver): m_menu(menu), m_driver(driver) {};
|
2023-01-05 15:32:42 +00:00
|
|
|
|
2023-01-05 16:42:39 +00:00
|
|
|
enum Event {Increase, Decrease, Push};
|
2023-01-05 15:32:42 +00:00
|
|
|
|
2023-01-05 16:42:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 11:12:37 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
bool isEntry() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:42:39 +00:00
|
|
|
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;
|
2023-01-06 11:12:37 +00:00
|
|
|
|
|
|
|
auto menutexts = m->getDisplayList();
|
|
|
|
|
|
|
|
if(entry_index < m->getAllEntriesNum()) {
|
|
|
|
std::string dspstring = menutexts[entry_index];
|
2023-01-05 15:32:42 +00:00
|
|
|
|
2023-01-05 16:42:39 +00:00
|
|
|
if (dspstring.length() > 19) {
|
2023-01-06 11:12:37 +00:00
|
|
|
dspstring = "-- ERR --";
|
2023-01-05 16:42:39 +00:00
|
|
|
} else {
|
|
|
|
dspstring.append((19 - dspstring.length()), ' ');
|
|
|
|
}
|
2023-01-06 11:12:37 +00:00
|
|
|
|
|
|
|
m_driver.LCDSendCString(const_cast<char *>(dspstring.c_str()));
|
|
|
|
} else {
|
2023-01-05 16:42:39 +00:00
|
|
|
m_driver.LCDSendCString(" ");
|
|
|
|
}
|
2023-01-06 11:12:37 +00:00
|
|
|
|
2023-01-05 16:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void keypress(Menu *m) {
|
2023-01-06 11:12:37 +00:00
|
|
|
//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;
|
2023-01-05 16:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void increase(Menu *m) {
|
2023-01-06 11:12:37 +00:00
|
|
|
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 {
|
2023-01-05 16:42:39 +00:00
|
|
|
m_current_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void decrease(Menu *m) {
|
2023-01-06 11:12:37 +00:00
|
|
|
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--;
|
|
|
|
}
|
2023-01-05 16:42:39 +00:00
|
|
|
} else {
|
|
|
|
m_current_index--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LCD_I2C_Driver &m_driver;
|
|
|
|
int m_current_index = 0;
|
|
|
|
Menu *m_menu;
|
2023-01-05 15:32:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // floatpump
|
|
|
|
} // menu
|
|
|
|
|
|
|
|
#endif //FLOATPUMP_MENU_CONTROLLER_H
|