-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_menu.c
255 lines (200 loc) · 5.37 KB
/
test_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
/**
* @file Test: User menu using cu_ui module
*
* @author CieNTi
*/
#include "cu_ui.h"
#include <stdio.h>
#include <stdbool.h>
/* ------------
* Menu actions
*/
/***/
int m_entry_nr1(void)
{
int res = 1;
PRINTF("\n>> Called m_entry_nr1\n");
/* Good boy */
res = 0;
return res;
}
/***/
int m_entry_nr2(void)
{
int res = 1;
PRINTF("\n>> Called m_entry_nr2\n");
/* Good boy */
res = 0;
return res;
}
/***/
int m_entry_nr3(void)
{
int res = 1;
PRINTF("\n>> Called m_entry_nr3\n");
/* Good boy */
res = 0;
return res;
}
/***/
int m_entry_nr4(void)
{
int res = 1;
PRINTF("\n>> Called m_entry_nr4\n");
/* Good boy */
res = 0;
return res;
}
/***/
int m_entry_nr5(void)
{
int res = 1;
PRINTF("\n>> Called m_entry_nr5\n");
/* Good boy */
res = 0;
return res;
}
/* -------------------------
* Menu creation and control
*/
/***/
int show_main_menu()
{
/* Always failing function by default */
static int res = 1;
res = 1;
/* Selected option holder */
static int sel_item = 0;
sel_item = 0;
/* Options */
__root static const struct menu_item_st menu[] =
{
{ M_H, "First, more decorated header", m_entry_header },
{ M_H, "Not the first header, less deco", m_entry_header },
{ '1', "User entry nr.1", m_entry_nr1 },
{ '2', "User entry nr.2", m_entry_nr2 },
{ '3', "User entry nr.3", m_entry_nr3 },
{ '4', "User entry nr.4", m_entry_nr4 },
{ '5', "User entry nr.5", m_entry_nr5 },
{ M_H, "Not the first header, less deco", m_entry_header },
{ '6', "User entry nr.1", m_entry_nr1 },
{ '7', "User entry nr.2", m_entry_nr2 },
{ '8', "User entry nr.3", m_entry_nr3 },
{ '9', "User entry nr.4", m_entry_nr4 },
{ '0', "User entry nr.5", m_entry_nr5 },
{ 'x', "Exit", NULL }
};
/* Show menu and wait for user interaction */
res = display_menu(menu, &sel_item, false);
/* Call action if all went fine */
if ((!res) && (menu[sel_item].cb != NULL))
{
res = menu[sel_item].cb();
}
/* 0 ok, otherwise fails */
return res;
}
/* ---------------------- Hierarchical menu members ---------------------- */
/*
* --------------------------
* *** Menus and submenus ***
* --------------------------
*/
int ui_submenu_nr1(void);
int ui_submenu_nr2(void);
/***/
int ui_menu_main(void)
{
/* Options */
__root static const struct menu_item_st menu[] =
{
{ M_H, "Hierarchical menu", m_entry_header },
{ '1', "Go to submenu #1", ui_submenu_nr1 },
{ '2', "Go to submenu #2", ui_submenu_nr2 },
{ 'x', "Exit", NULL }
};
/* 0 ok, otherwise fail */
return display_hmenu(menu);
}
/***/
int ui_submenu_nr1(void)
{
/* Options */
__root static const struct menu_item_st menu[] =
{
{ M_H, "Submenu #1", m_entry_header },
{ '1', "Call to end-point nr.1", m_entry_nr1 },
{ '2', "Call to end-point nr.2", m_entry_nr2 },
{ '3', "Call to end-point nr.3", m_entry_nr3 },
{ '4', "Call to end-point nr.4", m_entry_nr4 },
{ 'x', "Exit", NULL }
};
/* 0 ok, otherwise fail */
return display_hmenu(menu);
}
/***/
int ui_submenu_nr2(void)
{
/* Options */
__root static const struct menu_item_st menu[] =
{
{ M_H, "Submenu #2", m_entry_header },
{ '1', "Call to end-point nr.1", m_entry_nr1 },
{ '2', "Call to end-point nr.2", m_entry_nr2 },
{ '3', "Call to end-point nr.3", m_entry_nr3 },
{ '4', "Call to end-point nr.4", m_entry_nr4 },
{ '5', "Call to end-point nr.5", m_entry_nr5 },
{ 'x', "Exit", NULL }
};
/* 0 ok, otherwise fail */
return display_hmenu(menu);
}
/* ------------------------------ main loop ------------------------------ */
int main()
{
/* Always fail by default */
static int res = 1;
res = 1;
#if defined(CU_MOCK_IS_ALIVE)
PRINTF("\n***\n* C-Utils mocking enabled\n***\n\n");
#endif
#if defined(CU_MOCK_IS_ALIVE) && defined(CU_MOCK_FGETC_NONBLOCK)
set_fgetc_mock_type(CU_MOCK_TYPE_FLOAT);
#endif
/* Test for comparing fgets() with uart_fgets() */
char my_str[15];
PRINTF("Expecting string here: ");
if (FGETS(my_str, 15) == my_str)
{
/* Received */
PRINTF("String received: %s\n", my_str);
}
/* Test parsed string against a float */
float my_float = 0;
PRINTF("sscanf() exits with 0x%X\n",
sscanf(my_str, "%f", &my_float));
PRINTF("my_float is %f\n", my_float);
/* Questions example */
int q_int = 0;
float q_float = 0.0;
char q_str[10];
display_question("Type an integer", m_type_int, &q_int);
PRINTF("q_int is: %i\n", q_int);
display_question("Type a float", m_type_float, &q_float);
PRINTF("q_float is: %f\n", q_float);
display_question("Type a string", m_type_string, &q_str, 10);
PRINTF("q_str is: %s\n", q_str);
/* Simple menu example */
/* Uncomment for infinite loop */
//res = 0;
//while (!res)
{
PRINTF(">> Sleeping for 1s\n");
usleep(1000000);
res = show_main_menu();
}
/* Hierarchical menu creation and control example */
res = start_hmenu(ui_menu_main);
PRINTF("\nExiting now ...\n\n");
return res;
}