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

90 lines
2.6 KiB
C++

//
// Created by robtor on 09.12.22.
//
#ifndef FLOATPUMP_MENU_H
#define FLOATPUMP_MENU_H
#include <vector>
#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 displayMenu() {
int page = m_first_index / 4;
int pageindex = m_first_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;
if( m_entries.size() > entry_index ) {
std::string dspstring = m_entries[entry_index].printLine();
if(dspstring.length() > 19) {
m_driver.LCDSendCString("-------------------");
} else {
dspstring.append((19 - dspstring.length()), ' ');
m_driver.LCDSendCString(const_cast<char *>(dspstring.c_str()));
}
} else {
m_driver.LCDSendCString("-------------------");
}
}
}
void keypress() {
if(m_first_index < m_entries.size()) {
m_entries[m_first_index].action_press();
}
}
void increase() {
if(m_entries[m_first_index].isEntered()) {
m_entries[m_first_index].action_increase();
} else if(m_first_index < m_entries.size() - 1 && !m_entries[m_first_index].isEntered()) {
m_first_index++;
}
}
void decrease() {
if(m_entries[m_first_index].isEntered()) {
m_entries[m_first_index].action_decrease();
} else if(m_first_index > 0) {
m_first_index--;
}
}
private:
LCD_I2C_Driver &m_driver;
std::vector<Menu_Entry> m_entries;
int m_first_index = 0;
};
} // floatpump
} // menu
#endif //FLOATPUMP_MENU_H