-
Notifications
You must be signed in to change notification settings - Fork 51
/
menu.c
473 lines (435 loc) · 12.5 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "ff.h"
#include "config.h"
#include "comms.h"
#include "memstrings.h"
#include "fileutils.h"
#include "tapuino.h"
#include "lcd_interface.h"
#include "lcdutils.h"
#define MODE_PLAY 0
#define MODE_RECORD 1
#define MODE_OPTIONS 2
#define REC_MODE_MANUAL 0
#define REC_MODE_AUTO 1
#define OPTION_MACHINE_TYPE 0
#define OPTION_VIDEO_MODE 1
#define OPTION_SIGNAL 2
#define OPTION_KEY_REPEAT 3
#define OPTION_TICKER_SPEED 4
#define OPTION_TICKER_HOLD 5
#define OPTION_REC_FINALIZE 6
#define OPTION_REC_AUTO_FINALIZE 7
#define SELECT_MODE_EXIT 0xFF
int g_num_files = 0;
int g_cur_file_index = 0;
uint8_t get_cur_command() {
// this order of operations is very important
// first get an 'atomic' read of g_cur_command into a local
uint8_t cur_command = g_cur_command;
// then compare the _local_ against a non-IDLE command
if (cur_command != COMMAND_IDLE) {
// and clear the global i.e. only if the global was non-idle at the time of read
// this prevents clearing the global as a key is pressed and missing it
g_cur_command = COMMAND_IDLE;
}
return cur_command;
}
uint8_t handle_select_mode(const char* ptitle, const char** ppitems, uint8_t max, uint8_t cur_mode) {
uint8_t prev_mode = (cur_mode + 1) % max;
lcd_title_P(ptitle);
while (1) {
if (prev_mode != cur_mode) {
lcd_status_P(ppitems[cur_mode]);
prev_mode = cur_mode;
}
switch(get_cur_command()) {
case COMMAND_SELECT:
return cur_mode;
break;
case COMMAND_ABORT:
return SELECT_MODE_EXIT;
break;
case COMMAND_NEXT:
if (cur_mode == (max - 1)) {
cur_mode = 0;
} else {
cur_mode++;
}
break;
case COMMAND_PREVIOUS:
if (cur_mode == 0) {
cur_mode = max -1;
} else {
cur_mode--;
}
break;
}
}
}
void handle_play_mode(FILINFO* pfile_info) {
// reset to the root after a possible record operation
change_dir("/");
// refresh the file list to avoid blank entries bug
if ((g_num_files = get_num_files(pfile_info)) == 0) {
lcd_title_P(S_NO_FILES_FOUND);
lcd_busy_spinner();
return;
}
g_cur_file_index = 0;
if (!get_file_at_index(pfile_info, g_cur_file_index)) {
// shouldn't happen...
lcd_title_P(S_NO_FILES_FOUND);
lcd_busy_spinner();
return;
}
lcd_title_P(S_SELECT_FILE);
display_filename(pfile_info);
while (1) {
switch(get_cur_command()) {
case COMMAND_SELECT:
if (pfile_info->fattrib & AM_DIR) {
if (change_dir(pfile_info->fname) == FR_OK) {
g_num_files = get_num_files(pfile_info);
g_cur_file_index = 0;
get_file_at_index(pfile_info, g_cur_file_index);
display_filename(pfile_info);
} else {
lcd_status_P(S_CHDIR_FAILED);
}
} else {
display_filename(pfile_info);
play_file(pfile_info);
lcd_title_P(S_SELECT_FILE);
// buffer is used so get the file again
get_file_at_index(pfile_info, g_cur_file_index);
display_filename(pfile_info);
}
break;
case COMMAND_ABORT:
if (g_fs.cdir != 0) {
if (change_dir("..") == FR_OK) {
g_num_files = get_num_files(pfile_info);
g_cur_file_index = 0;
get_file_at_index(pfile_info, g_cur_file_index);
display_filename(pfile_info);
} else {
lcd_status_P(S_CHDIR_FAILED);
}
} else {
// back to main menu
return;
}
break;
case COMMAND_NEXT:
if (++g_cur_file_index >= g_num_files) {
g_cur_file_index = 0;
}
get_file_at_index(pfile_info, g_cur_file_index);
display_filename(pfile_info);
break;
case COMMAND_PREVIOUS:
if (--g_cur_file_index < 0) {
g_cur_file_index = g_num_files - 1;
}
get_file_at_index(pfile_info, g_cur_file_index);
display_filename(pfile_info);
break;
}
filename_ticker(pfile_info, get_timer_tick());
}
}
void handle_record_mode_ready(char* pfile_name) {
lcd_title_P(S_READY_RECORD);
lcd_status_P(S_PRESS_START);
while (1) {
switch(get_cur_command()) {
case COMMAND_SELECT:
record_file(pfile_name);
return;
break;
case COMMAND_ABORT:
// back to main menu
return;
break;
}
}
}
uint8_t handle_manual_filename(FILINFO* pfile_info) {
uint8_t cur_char_pos = 0;
uint8_t cursor_pos = 0;
uint8_t max_chars = strlen_P(S_FILENAME_CHARS);
uint8_t cur_char = 0;
lcd_title_P(S_ENTER_FILENAME);
lcd_status("");
lcd_cursor();
lcd_setCursor(0, 1);
// start with a nicely terminated string!
memset(pfile_info->lfname, 0, pfile_info->lfsize);
while (1) {
switch(get_cur_command()) {
case COMMAND_SELECT:
if (cursor_pos < (MAX_LCD_LINE_LEN - 1)) {
cur_char = pgm_read_byte(S_FILENAME_CHARS + cur_char_pos);
pfile_info->lfname[cursor_pos] = cur_char;
cursor_pos++;
lcd_setCursor(cursor_pos, 1);
cur_char_pos = 0;
}
break;
case COMMAND_SELECT_LONG:
strcat(pfile_info->lfname, ".tap");
lcd_noCursor();
// exit to previous menu, with accept
return 1;
break;
case COMMAND_ABORT:
if (cursor_pos != 0) {
pfile_info->lfname[cursor_pos] = 0;
lcd_setCursor(cursor_pos, 1);
lcd_write(' ');
cursor_pos--;
lcd_setCursor(cursor_pos, 1);
cur_char_pos = 0;
cur_char = pfile_info->lfname[cursor_pos];
while (pgm_read_byte(S_FILENAME_CHARS + cur_char_pos) != cur_char) {
cur_char_pos++;
}
}
break;
case COMMAND_ABORT_LONG:
lcd_title_P(S_OPERATION_ABORTED);
lcd_noCursor();
lcd_busy_spinner();
// exit to previous menu, with cancel
return 0;
break;
case COMMAND_NEXT:
cur_char_pos = (cur_char_pos + 1) % max_chars;
cur_char = pgm_read_byte(S_FILENAME_CHARS + cur_char_pos);
lcd_write(cur_char);
lcd_setCursor(cursor_pos, 1);
pfile_info->lfname[cursor_pos] = cur_char;
break;
case COMMAND_PREVIOUS:
if (cur_char_pos == 0) {
cur_char_pos = max_chars;
}
cur_char_pos--;
cur_char = pgm_read_byte(S_FILENAME_CHARS + cur_char_pos);
lcd_write(cur_char);
lcd_setCursor(cursor_pos, 1);
pfile_info->lfname[cursor_pos] = cur_char;
break;
}
}
}
void handle_record_mode(FILINFO* pfile_info) {
const char* ppitems[] = {S_REC_MODE_MANUAL, S_REC_MODE_AUTO};
uint8_t cur_mode = 0;
lcd_title_P(S_SELECT_RECORD_MODE);
// attempt to open the recording dir
strcpy_P((char*)g_fat_buffer, S_DEFAULT_RECORD_DIR);
// change to the recording dir
if (change_dir((char*)g_fat_buffer) != FR_OK) {
lcd_status_P(S_CHDIR_FAILED);
lcd_busy_spinner();
return;
}
while (1) {
cur_mode = handle_select_mode(S_SELECT_RECORD_MODE, ppitems, 2, cur_mode);
switch (cur_mode) {
case REC_MODE_AUTO:
handle_record_mode_ready(NULL);
return;
break;
case REC_MODE_MANUAL:
if (handle_manual_filename(pfile_info)) {
handle_record_mode_ready(pfile_info->lfname);
return;
}
break;
case SELECT_MODE_EXIT:
return;
break;
}
}
}
uint8_t handle_option_value(const char* poption, uint16_t* pcur_value, uint16_t min_value, uint16_t max_value, uint16_t step_value) {
char buffer[MAX_LCD_LINE_LEN + 1];
int32_t cur_value = *pcur_value;
if (cur_value < min_value || cur_value > max_value) {
cur_value = min_value;
}
lcd_title_P(poption);
utoa((uint16_t)cur_value, buffer, 10);
lcd_status(buffer);
while (1) {
switch(get_cur_command()) {
case COMMAND_SELECT:
*pcur_value = (uint16_t) cur_value;
return 1;
break;
case COMMAND_ABORT:
return 0;
break;
case COMMAND_NEXT:
cur_value += step_value;
if (cur_value > max_value) {
cur_value = max_value;
}
utoa((uint16_t)cur_value, buffer, 10);
lcd_status(buffer);
break;
case COMMAND_PREVIOUS:
cur_value -= step_value;
if (cur_value < min_value) {
cur_value = min_value;
}
utoa((uint16_t)cur_value, buffer, 10);
lcd_status(buffer);
break;
}
}
}
uint8_t handle_option_enum(const char* poption, uint16_t* pcur_value, uint16_t max_items, const char* ppitems[]) {
int32_t cur_value = *pcur_value;
lcd_title_P(poption);
lcd_status_P(ppitems[cur_value]);
while (1) {
switch(get_cur_command()) {
case COMMAND_SELECT:
*pcur_value = (uint16_t) cur_value;
return 1;
break;
case COMMAND_ABORT:
return 0;
break;
case COMMAND_NEXT:
cur_value++;
if (cur_value >= max_items) {
cur_value = 0;
}
lcd_status_P(ppitems[cur_value]);
break;
case COMMAND_PREVIOUS:
cur_value--;
if (cur_value < 0) {
cur_value = max_items - 1;
}
lcd_status_P(ppitems[cur_value]);
break;
}
}
}
void handle_mode_options() {
const char* ppitems[] = {S_OPTION_MACHINE_TYPE, S_OPTION_VIDEO_MODE, S_OPTION_SIGNAL, S_OPTION_KEY_REPEAT, S_OPTION_TICKER_SPEED, S_OPTION_TICKER_HOLD, S_OPTION_REC_FINALIZE, S_OPTION_REC_AUTO_FINALIZE};
uint16_t value = 0;
uint8_t save = 0;
uint8_t cur_mode = 0;
while (1) {
cur_mode = handle_select_mode(S_MODE_OPTIONS, ppitems, 8, cur_mode);
switch (cur_mode) {
case OPTION_MACHINE_TYPE:
{
const char* ppenum[] = {S_C64, S_VIC, S_C16};
value = g_machine_type;
if (handle_option_enum(S_OPTION_MACHINE_TYPE, &value, 3, ppenum)) {
save = 1;
g_machine_type = value;
}
}
break;
case OPTION_VIDEO_MODE:
{
const char* ppenum[] = {S_PAL, S_NTSC};
value = g_video_mode;
if (handle_option_enum(S_OPTION_VIDEO_MODE, &value, 2, ppenum)) {
save = 1;
g_video_mode = value;
}
}
break;
case OPTION_SIGNAL:
value = g_invert_signal;
if (handle_option_value(S_OPTION_SIGNAL, &value, 0, 1, 1)) {
g_invert_signal = value;
if (value) {
CONTROL_SET_BUS1();
} else {
CONTROL_SET_BUS0();
}
save = 1;
}
break;
case OPTION_KEY_REPEAT:
value = g_key_repeat_next * 10;
if (handle_option_value(S_OPTION_KEY_REPEAT, &value, 50, 500, 50)) {
g_key_repeat_next = value / 10;
save = 1;
}
break;
case OPTION_TICKER_SPEED:
value = g_ticker_rate * 10;
if (handle_option_value(S_OPTION_TICKER_SPEED, &value, 50, 500, 50)) {
g_ticker_rate = value / 10;
save = 1;
}
break;
case OPTION_TICKER_HOLD:
value = g_ticker_hold_rate * 10;
if (handle_option_value(S_OPTION_TICKER_HOLD, &value, 250, 2500, 250)) {
g_ticker_hold_rate = value / 10;
save = 1;
}
break;
case OPTION_REC_FINALIZE:
value = g_rec_finalize_time * 10;
if (handle_option_value(S_OPTION_REC_FINALIZE, &value, 500, 2500, 500)) {
g_rec_finalize_time = value / 10;
save = 1;
}
break;
case OPTION_REC_AUTO_FINALIZE:
value = g_rec_auto_finalize;
if (handle_option_value(S_OPTION_REC_AUTO_FINALIZE, &value, 0, 1, 1)) {
g_rec_auto_finalize = value;
save = 1;
}
break;
case SELECT_MODE_EXIT:
if (save) {
save_eeprom_data();
}
return;
break;
}
}
}
void main_menu(FILINFO* pfile_info) {
const char* ppitems[] = {S_MODE_PLAY, S_MODE_RECORD, S_MODE_OPTIONS};
uint8_t cur_mode = 0;
if ((g_num_files = get_num_files(pfile_info)) == 0) {
lcd_title_P(S_NO_FILES_FOUND);
return;
}
while (1) {
cur_mode = handle_select_mode(S_SELECT_MODE, ppitems, 3, cur_mode);
switch (cur_mode) {
case MODE_PLAY:
handle_play_mode(pfile_info);
break;
case MODE_RECORD:
handle_record_mode(pfile_info);
break;
case MODE_OPTIONS:
handle_mode_options();
break;
case SELECT_MODE_EXIT:
cur_mode = 0;
break;
}
}
}