-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.c
239 lines (197 loc) · 7.08 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
//
// Created by hamster on 7/18/18.
//
#include "common.h"
/**
* Draw the menu items
* Beware, there's no real check if your start row is beyond the end of the number of items
* @param menu items
* @param startY start position
* @param numItems how many rows
* @param startRow which row offset to start
*/
static void drawMenu(MENU *menu, uint16_t startY, uint8_t numItems, uint8_t startRow){
uint8_t spacing = util_gfx_font_height() - 5;
util_gfx_fill_rect(0, startY, GFX_WIDTH, spacing * numItems, COLOR_BLACK);
for(int i = 0; i < numItems; i++){
util_gfx_set_cursor(15, startY);
util_gfx_print(menu[i + startRow].name);
startY = startY + spacing;
}
}
/**
* Convince the user to select a menu item or timeout
* @param menu menu items to display
* @param startY start position
* @param numItems how many items total in menu
* @param numRows number of rows to show
* @param timeout should we give up after a certain time?
* @param updateTemplate Should we keep the top bar updated?
* @return
*/
int getMenuSelection(MENU *menu, uint16_t startY, uint8_t numItems, uint8_t numRows, uint16_t timeout, bool updateTemplate){
uint8_t oldBatteryPercent = 0;
uint32_t oldScore = 0;
int oldModifier = 0;
uint8_t selected = 0;
uint8_t cursor = 0;
uint16_t counter = 0;
uint8_t startRow = 0;
if(numItems < numRows){
numRows = numItems;
}
if(updateTemplate) {
drawScreenTemplate();
updateBatteryIcon(getBatteryPercent());
}
util_gfx_set_font(FONT_COMPUTER_12PT);
util_gfx_set_color(COLOR_WHITE);
drawMenu(menu, startY, numRows, 0);
uint8_t spacing = util_gfx_font_height() - 5;
drawCaret(startY, startY);
while(true){
switch(getButton(false)){
case USER_BUTTON_UP:
// Are we at the top of the list of items?
if(selected > 0){
// Do we need to scroll up?
if(cursor == 0){
// Need to scroll
drawMenu(menu, startY, numRows, --startRow);
drawCaret((cursor * spacing) + startY, (cursor * spacing) + startY);
}
else {
// No scroll, just move up
drawCaret((cursor * spacing) + startY, ((cursor - 1) * spacing) + startY);
cursor--;
}
selected--;
}
while(getButton(false) == USER_BUTTON_UP);
counter = 0;
break;
case USER_BUTTON_DOWN:
// Are we at the bottom of the list of items?
if(selected < numItems - 1){
// Do we need to scroll down?
if(cursor == numRows - 1){
// Need to scroll
drawMenu(menu, startY, numRows, ++startRow);
drawCaret((cursor * spacing) + startY, (cursor * spacing) + startY);
}
else {
// No scroll, just move down
drawCaret((cursor * spacing) + startY, ((cursor + 1) * spacing) + startY);
cursor++;
}
selected++;
}
while(getButton(false) == USER_BUTTON_DOWN);
counter = 0;
break;
case USER_BUTTON_A:
while(getButton(false) == USER_BUTTON_A);
return selected;
case USER_BUTTON_B:
while(getButton(false) == USER_BUTTON_B);
return -1;
default:
break;
}
if(timeout > 0) {
counter += 20;
if (counter >= timeout) {
return -1;
}
}
nrf_delay_ms(20);
if(updateTemplate) {
if(oldModifier != getTotalScoreModifier() || oldScore != user.score){
oldModifier = getTotalScoreModifier();
if(oldScore != user.score){
// Save the user data, can't do this in the interrupt
storeUser();
}
oldScore = user.score;
drawScreenTemplate();
}
if (getBatteryPercent() != oldBatteryPercent) {
oldBatteryPercent = getBatteryPercent();
updateBatteryIcon(oldBatteryPercent);
}
}
}
}
/**
* Update the battery icon based on the battery percentage
* @param percent
*/
void updateBatteryIcon(uint8_t percent){
util_gfx_fill_rect(GFX_WIDTH - 20, 0, 20, 12, COLOR_BLACK);
switch(percent){
case 255:
util_gfx_draw_raw_file("SYSTEM/AC.RAW", GFX_WIDTH - 20, 0, 20, 12, NULL, false, NULL);
break;
case 100:
util_gfx_draw_raw_file("SYSTEM/BAT100.RAW", GFX_WIDTH - 20, 0, 20, 12, NULL, false, NULL);
break;
case 75:
util_gfx_draw_raw_file("SYSTEM/BAT75.RAW", GFX_WIDTH - 20, 0, 20, 12, NULL, false, NULL);
break;
case 50:
util_gfx_draw_raw_file("SYSTEM/BAT50.RAW", GFX_WIDTH - 20, 0, 20, 12, NULL, false, NULL);
break;
case 25:
util_gfx_draw_raw_file("SYSTEM/BAT25.RAW", GFX_WIDTH - 20, 0, 20, 12, NULL, false, NULL);
break;
case 1:
util_gfx_draw_raw_file("SYSTEM/BATOUT.RAW", GFX_WIDTH - 20, 0, 20, 12, NULL, false, NULL);
break;
default:
case 0:
util_gfx_draw_raw_file("SYSTEM/BATINIT.RAW", GFX_WIDTH - 20, 0, 20, 12, NULL, false, NULL);
break;
}
}
/**
* Draw a caret at the start of the y value given
* @param y where to start the row
*/
void drawCaret(uint16_t oldY, uint16_t newY){
//uint16_t oldColor = util_gfx_color_get();
util_gfx_set_font(FONT_COMPUTER_12PT);
util_gfx_set_color(COLOR_BLACK);
util_gfx_set_cursor(0, oldY);
util_gfx_print(">");
util_gfx_set_color(COLOR_GREENYELLOW);
util_gfx_set_cursor(0, newY);
util_gfx_print(">");
util_gfx_set_color(COLOR_WHITE);
//util_gfx_set_color(oldColor);
}
/**
* Draw the screen template, for the top of the screen
*/
void drawScreenTemplate(void){
// Setup the main screen
//util_gfx_fill_screen(COLOR_BLACK);
util_gfx_fill_rect(0, 0, GFX_WIDTH, 25, COLOR_BLACK);
util_gfx_cursor_area_reset();
// Show the username
util_gfx_set_font(FONT_MONO55_8PT);
util_gfx_set_color(COLOR_WHITE);
uint16_t w, h;
/*util_gfx_get_text_bounds(user.name, 0, 0, &w, &h);
util_gfx_set_cursor(64 - (w / 2), 2);
util_gfx_print(user.name);
util_gfx_get_text_bounds(getClanName(user.clan), 0, 0, &w, &h);
util_gfx_set_cursor(64 - (w / 2), 15);
util_gfx_print(getClanName(user.clan));*/
util_gfx_set_font(FONT_GAMEPLAY_5PT);
util_gfx_set_color(COLOR_BLUE);
util_gfx_set_cursor(2, 2);
char score[11];
snprintf(score, 11, "%u", user.score);
util_gfx_print(score);
util_gfx_draw_line(0, 15, GFX_WIDTH, 15, COLOR_WHITE);
}