fome-fw/firmware/controllers/algo/lcd_menu_tree.cpp

85 lines
2.0 KiB
C++
Raw Normal View History

2015-01-06 19:07:46 -08:00
/**
* @file lcd_menu_tree.cpp
*
* @date Jan 6, 2015
2015-01-12 15:04:10 -08:00
* @author Andrey Belomutskiy, (c) 2012-2015
2015-01-06 19:07:46 -08:00
*/
#include "stddef.h"
#include "lcd_menu_tree.h"
#include "error_handling.h"
MenuTree::MenuTree(MenuItem *root) {
this->root = root;
current = NULL;
2015-02-26 15:09:02 -08:00
linesCount = 0;
topVisible = NULL;
2015-01-06 19:07:46 -08:00
}
void MenuTree::init(MenuItem *first, int linesCount) {
this->linesCount = linesCount;
current = first;
topVisible = first;
}
2015-01-06 20:03:36 -08:00
void MenuTree::enterSubMenu(void) {
if (current->firstChild != NULL) {
current = topVisible = current->firstChild;
2015-01-18 11:03:57 -08:00
} else if (current->callback != NULL) {
VoidCallback cb = current->callback;
cb();
2015-01-06 20:03:36 -08:00
}
}
void MenuTree::back(void) {
if (current->parent == root)
return; // we are on the top level already
current = topVisible = current->parent->topOfTheList;
}
2015-01-06 19:07:46 -08:00
void MenuTree::nextItem(void) {
if (current->next == NULL) {
2015-01-07 05:04:24 -08:00
current = topVisible = current->topOfTheList;
2015-01-06 19:07:46 -08:00
return;
}
current = current->next;
if (current->index - topVisible->index == linesCount)
topVisible = topVisible->next;
}
2015-02-26 15:09:02 -08:00
MenuItem::MenuItem(MenuItem * parent, const char *text, VoidCallback callback) : MenuItem(parent, LL_STRING, text, callback) {
2015-01-08 20:04:09 -08:00
}
2015-02-26 15:09:02 -08:00
MenuItem::MenuItem(MenuItem * parent, const char *text) : MenuItem(parent, LL_STRING, text, NULL) {
2015-01-07 05:04:24 -08:00
}
2015-02-26 15:09:02 -08:00
MenuItem::MenuItem(MenuItem * parent, lcd_line_e lcdLine) : MenuItem(parent, lcdLine, NULL, NULL) {
2015-01-07 05:04:24 -08:00
}
2015-02-26 15:09:02 -08:00
MenuItem::MenuItem(MenuItem * parent, lcd_line_e lcdLine, const char *text, VoidCallback callback) {
2015-01-07 05:04:24 -08:00
this->parent = parent;
2015-02-26 15:09:02 -08:00
this->lcdLine = lcdLine;
this->text = text;
this->callback = callback;
2015-01-06 20:03:36 -08:00
firstChild = NULL;
lastChild = NULL;
2015-02-26 15:09:02 -08:00
topOfTheList = NULL;
2015-01-06 20:03:36 -08:00
next = NULL;
2015-02-26 15:09:02 -08:00
index = 0;
2015-01-06 19:07:46 -08:00
// root element has NULL parent
if (parent != NULL) {
if (parent->firstChild == NULL) {
2015-01-06 20:03:36 -08:00
topOfTheList = this;
2015-02-26 15:09:02 -08:00
parent->firstChild = this;
2015-01-06 19:07:46 -08:00
}
if (parent->lastChild != NULL) {
index = parent->lastChild->index + 1;
2015-01-06 20:03:36 -08:00
topOfTheList = parent->lastChild->topOfTheList;
2015-01-06 19:07:46 -08:00
parent->lastChild->next = this;
}
parent->lastChild = this;
}
}