-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcu_ui.c
More file actions
380 lines (330 loc) · 8.32 KB
/
Copy pathcu_ui.c
File metadata and controls
380 lines (330 loc) · 8.32 KB
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
/**
* @file Some tools to ease recurrent tasks
*
* @author CieNTi
* @version 1.3.4
*/
#include "cu_ui.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
/* ----------------------------------------
* Module variables
*/
/**
* Entries calls hierarchy index
*/
static int menu_idx = 0;
/**
* Entries calls hierarchy array
*/
static menu_action *menu_lst[MAX_HMENU_LST];
/* ------------------------------------------------------------------------
* --------------------- Read cu_ui.h for documentation -------------------
* ------------------------------------------------------------------------ */
/**/
int m_entry_header(void)
{
}
/**/
int display_menu(const struct menu_item_st *menu,
int *sel_item,
bool wait_only)
{
char menu_choice = 0;
bool menu_disabled = false;
bool header_sent;
int i;
/* Function fails by default */
int res = 1;
/* Wait the choice */
while (1)
{
/* Wait for user interaction only or print menu too? */
i = 0;
header_sent = false;
do
{
if ((!wait_only) && (!menu_disabled))
{
/* Heading element */
if (menu[i].key == M_H && menu[i].cb == m_entry_header)
{
/* Head text */
if (!header_sent)
{
PRINTF("\n\n#*");
}
PRINTF("\n# %s\n", menu[i].text);
if (!header_sent)
{
PRINTF("#*\n\n");
}
header_sent = true;
continue;
}
/* Last element, used to exit, is put away from user menu entries */
if (menu[i].cb == NULL)
{
PRINTF("\n");
}
/* Print menu entry */
PRINTF(" %c. %s\n", menu[i].key, menu[i].text);
}
}
while (menu[i++].cb != NULL);
/* Default selected item is the last, exit one, so the callback still can
* be checked against NULL */
*sel_item = i - 1;
/* Tell the user to interact xD */
if (!menu_disabled)
{
PRINTF("\n-- Select an option: ");
}
/* menu_disabled is used to disable menu+question for 1 cycle */
menu_disabled = false;
/* Use a custom function if no stdin is present */
menu_choice = FGETC;
if (menu_choice == '\n' || menu_choice == '\r')
{
menu_disabled = true;
continue;
}
/* Assuming both non-blocking and blocking situations: 0 = 'no key' */
if (menu_choice != 0)
{
/* Search for the pressed key in menu elements keys */
i = 0;
do
{
/* Assign a menu action if choice is found */
if (menu[i].key == menu_choice)
{
/* Exit search loop after action is triggered */
*sel_item = i;
res = 0;
#if defined(M_FGETS_ECHO_ON)
PRINTF("%c", menu_choice);
#endif
break;
}
}
while (menu[i++].cb != NULL);
/* Wrong key? */
if ((i > 1) && (menu[i - 1].cb == NULL))
{
PRINTF("\nxx Key %c unknown, please try again\n\n\n\n", menu_choice);
}
else
{
/* Exit main wait loop */
break;
}
}
else
{
menu_disabled = true;
}
/* TODO/IMPROVEMENT: OS wait to avoid high CPU use */
// OS_wait_function();
}
#if defined(M_FGETS_ECHO_ON)
PRINTF("\n");
#endif
/* 0 ok, otherwise fails */
return res;
}
/**/
int start_hmenu(menu_action *first_parent)
{
/* Always failing function by default */
int res = 1;
/* Reset list */
for (menu_idx = MAX_HMENU_LST; menu_idx > 0; menu_idx--)
{
menu_lst[menu_idx - 1] = NULL;
}
/* First element */
menu_lst[menu_idx++] = first_parent;
/*
* Starting menu parsing:
* ---------------------
* Every menu_lst call will update menu_lst[menu_idx] before exit. If no more
* action is required (exit or endpoint entry) a NULL is used.
* Index check and update is done here, and NULL is used as end-of-list.
*/
res = 0;
while ((menu_idx > 0) && // Enough low side index?
(menu_idx <= MAX_HMENU_LST) && // Enough high side index ?
(menu_lst[menu_idx - 1] != NULL) && // Is a function ?
(!(res = menu_lst[menu_idx - 1]()))) // Call the function! No error ?
{
/* Ok, safe zone, let's check what to do */
if (menu_lst[menu_idx] == NULL)
{
/* Go back if possible */
if (menu_idx > 0)
{
menu_idx--;
}
}
else
{
/* New member! */
if (++menu_idx == MAX_HMENU_LST)
{
PRINTF("\nxx Max depth reached, please increase MAX_HMENU_LST\n");
PRINTF("xx Entering an infinite loop here now ...\n");
while (1)
{
/* Dear programmer:
* -- Update MAX_HMENU_LST and try again! --
* Thanks
*/
}
}
/* Ensures a brand new callback */
menu_lst[menu_idx] = NULL;
}
}
/* Errors? */
if (res)
{
PRINTF("\nxx Exiting with errors:\n");
PRINTF("xx menu_idx: %i, menu_lst[menu_idx]: %08X\n",
menu_idx,
menu_lst[menu_idx]);
}
/* 0 ok, otherwise fails */
return res;
}
/**/
int display_hmenu(const struct menu_item_st *menu)
{
/* Always failing function by default */
int res = 1;
/* Selected option holder */
int sel_item = 0;
/* Show menu and wait for user interaction */
res = display_menu(menu, &sel_item, false);
/* Call action if all went fine */
if (!res)
{
menu_lst[menu_idx] = menu[sel_item].cb;
}
/* 0 ok, otherwise fail */
return res;
}
/**/
char *uart_fgets(char *str, int num)
{
int i = 0;
while (((str[i] = FGETC) != '\n') && (str[i] != '\r'))
{
/* Go out 0x00! */
if (str[i] == 0x00)
{
continue;
}
#if defined(M_FGETS_ECHO_ON)
/* We don't want backspace or delete to be printed if there is no data */
if (((i == 0) && (str[i] == '\b')) ||
((i == 0) && (str[i] == 0x7F)))
{
continue;
}
if (((str[i] > 0x1F) && // 0x20 = Space ' '
(str[i] < 0x7F)) || // 0x7E = Tilde '~'
(str[i] == '\b') || // Backspace and delete keys are allowed
(str[i] == 0x7F)) // once accepted by first filter
{
PRINTF("%c", str[i]);
}
#endif
/* Let's see if user was a good boy */
switch (str[i])
{
/* Backspace and Del key (Del can be an escape sequence -> will exit) */
case 0x7F:
case '\b':
i = (i > 0)?i - 1:0;
str[i] = 0x00;
break;
/* Escape key */
case 0x1B:
/* Exit with EOF equivalent (not exactly an error, just canceled) */
return NULL;
break;
default:
/* Printable/typeable characters */
if ((str[i] > 0x1F) && // 0x20 = Space ' '
(str[i] < 0x7F)) // 0x7E = Tilde '~'
{
/* As EOF is not an option to end parsing, '\n' is needed. Therefore
* string will end in ['\n', 0x00] */
if (i < (num - 2))
{
/* Accept it */
i++;
}
}
break;
}
}
#if defined(M_FGETS_ECHO_ON)
PRINTF("\n");
#endif
/* Ensure valid string with newline (overwriting carriage return, if one) */
str[i] = '\n';
str[++i] = 0x00;
/* All fine */
return str;
}
int display_question(char *question, enum menu_data_type dtype, ...)
{
int res = 1;
char tmp_str[Q_ANSWER_SIZE] = { 0x00 };
char *dst_string = tmp_str;
void *scan_data = NULL;
char *scan_fmt = NULL;
int max_data = Q_ANSWER_SIZE;
/* Create and initialize variable argument list */
static va_list args;
va_start(args, dtype);
scan_data = va_arg(args, void *);
if (dtype == m_type_string)
{
/* If expecting a string, use it directly */
dst_string = scan_data;
max_data = va_arg(args, int);
}
va_end(args);
/* Display question and wait the user to type something */
PRINTF("?? %s: ", question);
if (FGETS(dst_string, max_data) == dst_string)
{
res = 0;
switch (dtype)
{
case m_type_int:
scan_fmt = "%i";
break;
case m_type_float:
scan_fmt = "%f";
break;
case m_type_string:
break;
default:
/* Oops */
res = 1;
break;
}
/* Last error check */
if ((scan_fmt != NULL )&& (sscanf(tmp_str, scan_fmt, scan_data) == 0))
{
/* No elements, invalid scan */
res = 1;
}
}
return res;
}