Menu selection is now data driven

Some major refactoring has been going on.
Added simple structs of data containing info about what action to take when a menu item is selected.

Finally removed all but one call to display.display() in menu-function. There might still be more than one call to it on a frame though, since some functions still calls it internally.
This commit is contained in:
Mikael Degerfält 2019-06-16 01:42:25 +02:00
parent 6a8e36635e
commit 9a0bb369ac
2 changed files with 253 additions and 447 deletions

File diff suppressed because it is too large Load diff

View file

@ -35,81 +35,3 @@ Controls setup. Opens a new menu.
*/
#include <cstring>
#include <Adafruit_SSD1306.h>
#include "numenu.h"
#include "menu.h"
NuMenu::NuMenu(Adafruit_SSD1306& display)
: _display(display)
{
}
bool NuMenu::init()
{
// memset(_pageStack, 0, sizeof(_pageStack));
// _rootMenu = MenuPageState(root, 0, 0);
_enabled = false;
return true;
}
void NuMenu::update(uint16_t buttonState)
{
if(_enabled)
{
// int
}
}
extern Adafruit_SSD1306 display;
void NuMenu::drawMenuItems(const char* title, const char* entries[], int count, int selection, int offset)
{
//Initialize display and draw menu header + line
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(title);
display.drawLine(0,MENU_ROW_HEIGHT,127,MENU_ROW_HEIGHT, WHITE);
int rowPixel = MENU_HEADER_OFFSET + MENU_ROW_HEIGHT;
for(int index = offset, count = 0; ((index-offset) < MENU_NUM_ROWS) && index < count; index++ )
{
// int rowPixel = (row+1)*MENU_ROW_HEIGHT + MENU_HEADER_OFFSET;
const char* lineText = entries[index];
display.setCursor(0,rowPixel);
rowPixel += (MENU_ROW_HEIGHT+1);
display.println(lineText);
}
// TODO: Fix cursor
// if(selection>=0)
// drawMenuCursor(selection, WHITE);
}
// This is for the SUB MENU
// void NuMenu::drawSelection(const char* title, const char* entries[], int count, int* selection)
// {
// _display.fillRect(63,11,64,52,BLACK);
// _display.drawRect(63,11,64,52,WHITE);
// _display.setTextColor(WHITE);
// _display.setTextSize(1);
// _display.setCursor(68,15);
// _display.println(title);
// const char* entryTxt = entries[*selection];
// int len = strlen(entryTxt);
// _display.setTextSize(2);
// _display.setCursor(91 - 4*len,33);
// _display.println(entryTxt);
// _display.display();
// }