-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsealhat_menu.c
73 lines (61 loc) · 1.96 KB
/
sealhat_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
/*
*
*/
#include "sealhat_menu.h"
static inline void menu_cursorTop() {
usb_write("\e[H", 3);
}
static inline void menu_clearScreen() {
usb_write("\e[2J", 3);
}
static inline void menu_eraseAbove() {
usb_write("\e[1J", 4);
}
static int32_t menu_print(void* outData, uint32_t BUFFER_SIZE) {
int32_t err;
int32_t timeout = 0;
do {
err = usb_write(outData, BUFFER_SIZE);
delay_ms(1);
timeout++;
} while(err < 0 && timeout < BLOCK_TIME);
return err;
}
void menu_display(const MENU_t *mnu, char* prompt)
{
int i; // for iterating through sub-menus
char mnuTitle[MAX_TITLE_LENGTH + 8];
snprintf(mnuTitle, MAX_TITLE_LENGTH + 8, "** %s **\n", mnu->title);
menu_print(mnuTitle, strlen(mnuTitle));
for(i = 0; i < mnu->num_submenus; i++) {
snprintf(mnuTitle, MAX_TITLE_LENGTH + 8, "%d: %s\n", i+1, mnu->submenu[i]->title);
menu_print(mnuTitle, strlen(mnuTitle));
}
if(prompt != NULL) {
menu_print(prompt, strlen(prompt));
}
}
const MENU_t* menu_navigate(const MENU_t* mnu, const uint32_t selection)
{
const MENU_t* nextMnu; // variable to hold the selected menu
static const int ERR_LENGTH = 48; // max error message length
char errorMsg[ERR_LENGTH]; // for possible error messages
// if a valid submenu is chosen return its pointer, otherwise return the same one
if ((selection > 0) && (selection <= mnu->num_submenus)) {
// 0 index so subtract 1 and get its pointer from the array
nextMnu = mnu->submenu[selection-1];
// if the menu chosen has no submenus, then it is a command
// execute the command and return the origional menu.
if(nextMnu->num_submenus == 0 && nextMnu->command != NULL) {
nextMnu->command(nextMnu->data_payload);
}
else {
mnu = nextMnu;
}
}
else {
snprintf(errorMsg, ERR_LENGTH, "Error: %lud is not a valid option.\n\n", selection);
menu_print(errorMsg, strlen(errorMsg));
}
return mnu;
}