-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.h
145 lines (122 loc) · 2.9 KB
/
menu.h
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*\
|*| menu.h
|*|
|*| (c) 2022 Trent M. Wyatt.
|*| companion file for Reverse Geocache Box project
|*|
\*/
#if !defined(MENU_H_INC)
#define MENU_H_INC
struct lcd_menu_t;
enum entry_t : uint8_t
{
FUNC = 0,
MENU = 1,
INT = 2
};
struct variant_t
{
union {
void (*func)();
lcd_menu_t *menu;
int ival;
} value;
entry_t type;
variant_t const & operator = (void (*func)())
{
(*this).value.func = func;
type = FUNC;
return *this;
}
variant_t const & operator = (lcd_menu_t *menu)
{
(*this).value.menu = menu;
type = MENU;
return *this;
}
variant_t const & operator = (int ival)
{
(*this).value.ival = ival;
type = INT;
return *this;
}
}; // variant_t
struct menu_t
{
char txt[17];
variant_t value;
int min;
int max;
menu_t()
{
txt[0] = '\0';
value = 0;
min = 0;
max = 0;
}
};
struct lcd_menu_t
{
menu_t menu[2];
uint8_t cur : 1;
lcd_menu_t()
{
for (menu_t &entry : menu) {
entry.txt[0] = '\0';
entry.value = 0;
entry.min = 0;
entry.max = 0;
}
}
lcd_menu_t(char *msg1, void(*func1)(), char *msg2, void(*func2)())
{
strncpy(menu[0].txt, msg1, sizeof(menu[0].txt));
menu[0].value = func1;
strncpy(menu[1].txt, msg2, sizeof(menu[1].txt));
menu[1].value = func2;
}
lcd_menu_t(char *msg1, lcd_menu_t *menu1, char *msg2, lcd_menu_t *menu2)
{
strncpy(menu[0].txt, msg1, sizeof(menu[0].txt));
menu[0].value = menu1;
strncpy(menu[1].txt, msg2, sizeof(menu[1].txt));
menu[1].value = menu2;
}
lcd_menu_t(char const *msg1, void(*func1)(), char const *msg2, void(*func2)())
{
strncpy(menu[0].txt, msg1, sizeof(menu[0].txt));
menu[0].value = func1;
strncpy(menu[1].txt, msg2, sizeof(menu[1].txt));
menu[1].value = func2;
}
lcd_menu_t(char const *msg1, lcd_menu_t *menu1, char const *msg2, lcd_menu_t *menu2)
{
strncpy(menu[0].txt, msg1, sizeof(menu[0].txt));
menu[0].value = menu1;
strncpy(menu[1].txt, msg2, sizeof(menu[1].txt));
menu[1].value = menu2;
}
int next()
{
return cur = !cur;
}
lcd_menu_t &exec() {
switch (menu[cur].value.type) {
case FUNC:
if (menu[cur].value.value.func != nullptr) {
menu[cur].value.value.func();
}
break;
case MENU:
if (menu[cur].value.value.menu != nullptr) {
*this = *(menu[cur].value.value.menu);
}
break;
case INT:
break;
}
return *this;
}
//
};
#endif // MENU_H_INC