-
Notifications
You must be signed in to change notification settings - Fork 22
/
menu.c
308 lines (249 loc) · 9.82 KB
/
menu.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
menu.c: openchronos-ng menu system
Copyright (C) 2012 Angelo Arrifano <[email protected]>
Copyright (C) 2016 Benjamin Sølberg <[email protected]>
http://github.com/BenjaminSoelberg/openchronos-ng-elf
This file is part of openchronos-ng.
openchronos-ng is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
openchronos-ng is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
// *************************************************************************************************
// Initialization and control of application.
// *************************************************************************************************
// *************************************************************************************************
// Include section
#include "menu.h"
#include "drivers/ports.h"
#include "drivers/display.h"
#define MENUMODE_IDLE_MAX_COUNT 10
#define MENU_EDITMODE_IDLE_MAX_COUNT 10
/* The head of the linked list holding menu items */
static struct menu *menu_head;
/* Menu mode stuff */
static struct {
uint8_t enabled:1; /* is menu mode enabled? */
struct menu *item; /* the currently active menu item */
struct menu *start_item; /* the original menu item */
uint8_t idle_count; /* number of idle polls counts */
} menumode;
/* Menu edit mode stuff */
static struct {
uint8_t enabled:1; /* is edit mode enabled? */
uint8_t pos:7; /* the position for selected item */
void (* complete_fn)(void); /* call this fn when editmode exits */
void (* cancel_fn)(void); /* call this fn when editmode is canceled */
struct menu_editmode_item *items; /* vector of editmode items */
uint8_t idle_count; /* number of idle polls counts */
} menu_editmode;
/***************************************************************************
************************ USER INPUT / MAIN MENU ***************************
**************************************************************************/
/***************************************************/
/******************** Edit mode ********************/
/***************************************************/
static void editmode_handler(void)
{
/* STAR button exits edit mode */
if (ports_button_pressed(PORTS_BTN_STAR, 0)) {
/* deselect item */
menu_editmode.items[menu_editmode.pos].deselect();
menu_editmode.complete_fn();
menu_editmode.enabled = 0;
menu_editmode.idle_count = 0;
} else if (ports_button_pressed(PORTS_BTN_NUM, 0)) {
/* deselect current item */
menu_editmode.items[menu_editmode.pos].deselect();
/* select next item */
menu_editmode.pos++;
if (! menu_editmode.items[menu_editmode.pos].set)
menu_editmode.pos = 0;
menu_editmode.items[menu_editmode.pos].select();
menu_editmode.idle_count = 0;
} else if (ports_button_pressed(PORTS_BTN_UP, 0)) {
menu_editmode.items[menu_editmode.pos].set(1);
menu_editmode.idle_count = 0;
} else if (ports_button_pressed(PORTS_BTN_DOWN, 0)) {
menu_editmode.items[menu_editmode.pos].set(-1);
menu_editmode.idle_count = 0;
} else if (menu_editmode.cancel_fn && menu_editmode.idle_count >= MENU_EDITMODE_IDLE_MAX_COUNT) {
/* deselect item */
menu_editmode.items[menu_editmode.pos].deselect();
menu_editmode.cancel_fn();
menu_editmode.enabled = 0;
menu_editmode.idle_count = 0;
}
}
/***************************************************/
/******************** Menu mode ********************/
/***************************************************/
static void menumode_select(void)
{
/* exit menu mode */
menumode.enabled = 0;
menumode.start_item = NULL;
menumode.idle_count = 0;
/* clear both lines but keep symbols! */
display_clear(0, 1);
display_clear(0, 2);
/* turn off up/down symbols */
display_symbol(0, LCD_SYMB_ARROW_UP, SEG_OFF);
display_symbol(0, LCD_SYMB_ARROW_DOWN, SEG_OFF);
/* stop blinking name of current selected module */
display_chars(0, LCD_SEG_L2_4_0, NULL, BLINK_OFF);
/* activate item */
if (menumode.item->activate_fn)
menumode.item->activate_fn();
}
void menumode_cancel(void)
{
// Restore to original menu item
menumode.item = menumode.start_item;
menumode_select();
}
static void menumode_start(void)
{
menumode.start_item = menumode.item;
menumode.idle_count = 0;
/* deactivate current menu item */
if (menumode.item->deactivate_fn)
menumode.item->deactivate_fn();
/* enable edit mode */
menumode.enabled = 1;
/* show MENU in the first line */
display_chars(0, LCD_SEG_L1_3_0, "MENU", SEG_SET);
/* turn on up/down symbols */
display_symbol(0, LCD_SYMB_ARROW_UP, SEG_ON);
display_symbol(0, LCD_SYMB_ARROW_DOWN, SEG_ON);
/* show up blinking name of current selected item */
display_chars(0, LCD_SEG_L2_4_0, NULL, BLINK_ON);
display_chars(0, LCD_SEG_L2_4_0, menumode.item->name, SEG_SET);
}
static void menumode_next(void) {
menumode.idle_count = 0;
menumode.item = menumode.item->next;
display_clear(0, 2);
display_chars(0, LCD_SEG_L2_4_0, menumode.item->name, SEG_SET);
}
static void menumode_prev(void) {
menumode.idle_count = 0;
menumode.item = menumode.item->prev;
display_clear(0, 2);
display_chars(0, LCD_SEG_L2_4_0, menumode.item->name, SEG_SET);
}
static void menumode_handler(void)
{
if (ports_button_pressed(PORTS_BTN_STAR, 0)) {
menumode_select();
} else if (ports_button_pressed(PORTS_BTN_UP, 0)) {
menumode_next();
} else if (ports_button_pressed(PORTS_BTN_DOWN, 0)) {
menumode_prev();
} else if (menumode.idle_count >= MENUMODE_IDLE_MAX_COUNT) {
menumode_cancel();
}
}
static void menuitem_handler(void) {
if (ports_button_pressed(PORTS_BTN_LSTAR, 1)) {
if (menumode.item->lstar_btn_fn)
menumode.item->lstar_btn_fn();
} else if (ports_button_pressed(PORTS_BTN_STAR, !!(menumode.item->lstar_btn_fn))) {
menumode_start();
} else if (ports_button_pressed(PORTS_BTN_LNUM, 1)) {
if (menumode.item->lnum_btn_fn)
menumode.item->lnum_btn_fn();
} else if (ports_button_pressed(PORTS_BTN_NUM, !!(menumode.item->lnum_btn_fn))) {
if (menumode.item->num_btn_fn)
menumode.item->num_btn_fn();
} else if (ports_button_pressed(PORTS_BTN_UP | PORTS_BTN_DOWN, 0)) {
if (menumode.item->updown_btn_fn)
menumode.item->updown_btn_fn();
} else if (ports_button_pressed(PORTS_BTN_UP, 0)) {
if (menumode.item->up_btn_fn)
menumode.item->up_btn_fn();
} else if (ports_button_pressed(PORTS_BTN_DOWN, 0)) {
if (menumode.item->down_btn_fn)
menumode.item->down_btn_fn();
}
}
/**********************************************************/
/******************** Public functions ********************/
/**********************************************************/
void menu_timeout_poll(void) {
if (menumode.enabled) {
menumode.idle_count++;
} else if (menu_editmode.enabled) {
menu_editmode.idle_count++;
}
}
void menu_check_buttons(void)
{
if (menu_editmode.enabled) {
editmode_handler();
} else if (menumode.enabled) {
menumode_handler();
} else {
menuitem_handler();
}
ports_buttons_clear();
}
struct menu* menu_add_entry(char const *name,
void (*up_btn_fn)(void),
void (*down_btn_fn)(void),
void (*num_btn_fn)(void),
void (*lstar_btn_fn)(void),
void (*lnum_btn_fn)(void),
void (*updown_btn_fn)(void),
void (*activate_fn)(void),
void (*deactivate_fn)(void))
{
struct menu **menu_hd = &menu_head;
struct menu *menu_p;
if (! *menu_hd) {
/* Head is empty, create new menu item linked to itself */
menu_p = (struct menu *) malloc(sizeof(struct menu));
menu_p->next = menu_p;
menu_p->prev = menu_p;
*menu_hd = menu_p;
/* There wasnt any menu active, so we activate this one */
menumode.item = menu_p;
activate_fn();
} else {
/* insert new item before head */
menu_p = (struct menu *) malloc(sizeof(struct menu));
menu_p->next = (*menu_hd);
menu_p->prev = (*menu_hd)->prev;
(*menu_hd)->prev = menu_p;
menu_p->prev->next = menu_p;
}
menu_p->name = name;
menu_p->up_btn_fn = up_btn_fn;
menu_p->down_btn_fn = down_btn_fn;
menu_p->num_btn_fn = num_btn_fn;
menu_p->lstar_btn_fn = lstar_btn_fn;
menu_p->lnum_btn_fn = lnum_btn_fn;
menu_p->updown_btn_fn = updown_btn_fn;
menu_p->activate_fn = activate_fn;
menu_p->deactivate_fn = deactivate_fn;
return menu_p;
}
void menu_editmode_start(void (* complete_fn)(void),
void (* cancel_fn)(void),
struct menu_editmode_item *items)
{
menu_editmode.pos = 0;
menu_editmode.items = items;
menu_editmode.complete_fn = complete_fn;
menu_editmode.cancel_fn = cancel_fn;
menu_editmode.enabled = 1;
menu_editmode.idle_count = 0;
/* select the first item */
menu_editmode.items[0].select();
}