-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_menu_app.c
263 lines (231 loc) · 6.29 KB
/
default_menu_app.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
#include <stdio.h>
#include "default_menu_app.h"
#include "colors.h"
#include "button.h"
#include "framebuffer.h"
#define DEFAULT_MENU_FG_COLOR WHITE
#define DEFAULT_MENU_BG_COLOR BLACK
struct default_menu_app_context context_stack[MAX_APP_STACK_DEPTH];
int current_menu_stack_idx = -1;
struct default_menu_app_context *current_context = NULL;
void init_default_menu_app_context(struct default_menu_app_context *c, struct menu_t *m)
{
c->menu = m;
c->top_item = 0;
c->current_item = 0;
c->selected_item = -1;
c->screen_changed = 1;
}
void default_menu_app_cb(struct badge_app *app);
struct badge_app default_menu_app = {
.app_func = default_menu_app_cb,
.app_context = 0,
.wake_up = 1,
/* these are set in do_selection just before calling app_func() */
.menu = 0,
.current_selection = 0,
};
/* Program states. Initial state is DEFAULT_MENU_APP_INIT */
enum default_menu_app_state_t {
DEFAULT_MENU_APP_INIT,
DEFAULT_MENU_APP_RUN,
DEFAULT_MENU_APP_EXIT,
};
static enum default_menu_app_state_t default_menu_app_state = DEFAULT_MENU_APP_INIT;
static void default_menu_app_init(void)
{
FbInit();
FbClear();
default_menu_app_state = DEFAULT_MENU_APP_RUN;
current_context->screen_changed = 1;
}
static int count_menu_items(struct menu_t *m)
{
for (int i = 0; ; i++) {
if (m[i].attrib & LAST_ITEM)
return i + 1;
}
}
static void move_up(void)
{
if (current_context->current_item > 0) {
current_context->current_item--;
if (current_context->top_item > current_context->current_item)
current_context->top_item--;
current_context->screen_changed = 1;
}
}
static void move_down(void)
{
int nitems = count_menu_items(current_context->menu);
if (current_context->current_item < nitems - 1) {
current_context->current_item++;
if (current_context->top_item < current_context->current_item - 15)
current_context->top_item++;
current_context->screen_changed = 1;
}
}
static void go_back(void)
{
if (current_menu_stack_idx > -1) {
current_menu_stack_idx--;
pop_app();
if (current_menu_stack_idx >= 0)
current_context = &context_stack[current_menu_stack_idx];
else
current_context = default_menu_app.app_context;
}
}
static void display_menu_item_description(struct badge_app *app)
{
static int screen_changed = 1;
if (app->wake_up)
screen_changed = 1;
if (screen_changed) {
struct menu_t *m = app->app_context;
FbColor(CYAN);
FbBackgroundColor(BLACK);
FbClear();
FbMove(0, 0);
FbWriteString(m->data.description);
FbSwapBuffers();
screen_changed = 0;
}
int down_latches = button_down_latches();
if (BUTTON_PRESSED(BADGE_BUTTON_A, down_latches) ||
BUTTON_PRESSED(BADGE_BUTTON_B, down_latches) ||
BUTTON_PRESSED(BADGE_BUTTON_LEFT, down_latches) ||
BUTTON_PRESSED(BADGE_BUTTON_RIGHT, down_latches) ||
BUTTON_PRESSED(BADGE_BUTTON_UP, down_latches) ||
BUTTON_PRESSED(BADGE_BUTTON_DOWN, down_latches)) {
screen_changed = 1;
pop_app();
}
}
static void do_selection(void)
{
struct menu_t *m = current_context->menu;
enum menu_item_type t = m[current_context->current_item].type;
struct badge_app app;
switch (t) {
case MENU:
if (current_menu_stack_idx < MAX_APP_STACK_DEPTH - 1) {
current_menu_stack_idx++;
app.app_func = default_menu_app_cb;
app.wake_up = 1;
app.app_context = &context_stack[current_menu_stack_idx];
init_default_menu_app_context(app.app_context,
(struct menu_t *) &m[current_context->current_item].data.menu[0]);
app.menu = m;
app.current_selection = current_context->current_item;
push_app(app);
}
break;
case BACK:
go_back();
break;
case FUNCTION:
app.app_func = m[current_context->current_item].data.func;
app.app_context = 0;
app.wake_up = 1;
app.menu = m;
app.current_selection = current_context->current_item;
push_app(app);
break;
case ITEM_DESC:
app.app_func = display_menu_item_description;
app.app_context = &m[current_context->current_item];
app.menu = m;
app.current_selection = current_context->current_item;
push_app(app);
break;
case TEXT:
/* Doesn't do anything if selected. */
/* This is only used in conjunction with SKIP_ITEM attribute anyway. */
break;
}
}
static void check_buttons(void)
{
int down_latches = button_down_latches();
if (BUTTON_PRESSED(BADGE_BUTTON_LEFT, down_latches)) {
} else if (BUTTON_PRESSED(BADGE_BUTTON_RIGHT, down_latches)) {
} else if (BUTTON_PRESSED(BADGE_BUTTON_UP, down_latches)) {
move_up();
} else if (BUTTON_PRESSED(BADGE_BUTTON_DOWN, down_latches)) {
move_down();
} else if (BUTTON_PRESSED(BADGE_BUTTON_A, down_latches)) {
do_selection();
} else if (BUTTON_PRESSED(BADGE_BUTTON_B, down_latches)) {
go_back();
}
/* skip items tagged to be skipped */
while (current_context->menu[current_context->current_item].attrib & SKIP_ITEM)
move_down();
}
static void draw_screen(void)
{
struct menu_t *m = current_context->menu;
if (!current_context->screen_changed)
return;
FbColor(DEFAULT_MENU_FG_COLOR);
FbBackgroundColor(DEFAULT_MENU_BG_COLOR);
FbClear();
int nitems = count_menu_items(current_context->menu);
int x = 0;
int y = 0;
for (int i = 0; i < nitems; i++) {
if (i < current_context->top_item) /* skip to the top item */
continue;
FbMove(x, y);
if (i == current_context->current_item) {
FbColor(DEFAULT_MENU_FG_COLOR);
FbFilledRectangle(LCD_XSIZE, 10);
FbColor(DEFAULT_MENU_BG_COLOR);
FbBackgroundColor(DEFAULT_MENU_FG_COLOR);
} else {
FbColor(DEFAULT_MENU_BG_COLOR);
FbFilledRectangle(LCD_XSIZE, 10);
FbColor(DEFAULT_MENU_FG_COLOR);
FbBackgroundColor(DEFAULT_MENU_BG_COLOR);
}
FbMove(x, y);
FbWriteString(m[i].name);
if (m[i].attrib & LAST_ITEM)
break;
y += 10;
if (y > 150)
break;
}
FbSwapBuffers();
current_context->screen_changed = 0;
}
static void default_menu_app_run(void)
{
check_buttons();
draw_screen();
}
static void default_menu_app_exit(void)
{
default_menu_app_state = DEFAULT_MENU_APP_INIT; /* So that when we start again, we do not immediately exit */
(void) pop_app();
}
void default_menu_app_cb(struct badge_app *app)
{
current_context = app->app_context;
if (app->wake_up)
current_context->screen_changed = 1;
switch (default_menu_app_state) {
case DEFAULT_MENU_APP_INIT:
default_menu_app_init();
break;
case DEFAULT_MENU_APP_RUN:
default_menu_app_run();
break;
case DEFAULT_MENU_APP_EXIT:
default_menu_app_exit();
break;
default:
break;
}
}