-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmenu.cpp
More file actions
112 lines (96 loc) · 2.92 KB
/
menu.cpp
File metadata and controls
112 lines (96 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "menu.h"
#include <avr/pgmspace.h>
menu::menu(byte pageSize, byte maxOpts) {
_pageSize = pageSize;
_itemCount = 0;
_selected = 0;
_topItem = 0;
_maxOpts = maxOpts;
_menuItems = (menuItem *) malloc(_maxOpts * sizeof(menuItem));
}
menu::~menu() {
free(_menuItems);
}
/* Adds or updates a menu item (based on unique value) */
void menu::setItem(char disp[], byte value) {
byte index = this->getIndexByValue(value);
if (index >= _maxOpts) return;
strcpy(_menuItems[index].name, disp);
_menuItems[index].value = value;
if (index == _itemCount) _itemCount++;
}
void menu::setItem_P(const char *disp, byte value) {
byte index = this->getIndexByValue(value);
if (index >= _maxOpts) return;
strcpy_P(_menuItems[index].name, disp);
_menuItems[index].value = value;
if (index == _itemCount) _itemCount++;
}
/* Appends text to an existing menu item */
void menu::appendItem(char disp[], byte value) {
byte index = this->getIndexByValue(value);
if (index == _itemCount) return;
strcat(_menuItems[index].name, disp);
}
void menu::appendItem_P(const char *disp, byte value) {
byte index = this->getIndexByValue(value);
if (index == _itemCount) return;
strcat_P(_menuItems[index].name, disp);
}
/* Set selected by specifying index */
void menu::setSelected(byte index) {
_selected = index;
}
/* Select by menu item value */
void menu::setSelectedByValue(byte value) {
_selected = this->getIndexByValue(value);
}
/* Get selected menu item index */
byte menu::getSelected(void) {
return _selected;
}
/* Update _topItem based on selection and pageSize */
boolean menu::refreshDisp(void) {
if (_selected < _topItem) {
_topItem = _selected;
return 1;
}
if (_selected >= _topItem + _pageSize) {
_topItem = _selected - _pageSize + 1;
return 1;
}
return 0;
}
/* Get specified row's menu item text based on _topItem and _pageSize */
void menu::getVisibleRow(byte row, char retString[]) {
this->refreshDisp();
if (_topItem + row < _itemCount) strcpy(retString, _menuItems[_topItem + row].name);
else strcpy(retString, "");
}
/* Get menu item text for currently selected item */
char* menu::getSelectedRow(char retString[]) {
strcpy(retString, _menuItems[_selected].name);
return (char*)_menuItems[_selected].name;
}
/* Get the value for the currently selected menu item */
byte menu::getValue() {
return _menuItems[_selected].value;
}
/* Get the cursor position based on current selection, _topItem and _pageSize */
byte menu::getCursor(void) {
this->refreshDisp();
return _selected - _topItem;
}
/* Get total number of defined menu items */
byte menu::getItemCount(void) {
return _itemCount;
}
/* Get menu item index based on specified menu item value */
byte menu::getIndexByValue(byte val) {
if (_itemCount) {
for (byte i = 0; i < _itemCount; i++) {
if (_menuItems[i].value == val) return i;
}
}
return _itemCount;
}