-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.c
More file actions
728 lines (615 loc) · 23 KB
/
menu.c
File metadata and controls
728 lines (615 loc) · 23 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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/**
* @file menu.c
* @brief Menu system library for Raspberry Pi Pico
*
* This library provides a hierarchical menu system for displays.
*
* @author Turi Scandurra
*/
#include "menu.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
/**
* @brief Create a new menu
* @param title Menu title
* @return Pointer to menu_t structure, or NULL on failure
*/
menu_t *menu_create(const char *title) {
if (!title) return NULL;
menu_t *menu = (menu_t *)malloc(sizeof(menu_t));
if (!menu) return NULL;
menu->title = title;
menu->item_count = 0;
menu->parent = NULL;
menu->selected_index = 0;
menu->first_visible_index = 0;
memset(menu->items, 0, sizeof(menu->items));
return menu;
}
/**
* @brief Add an action item to a menu
* @param menu Pointer to menu_t structure
* @param label Item label
* @param callback Callback function to execute when selected
* @return 0 on success, -1 on failure
*/
int menu_add_action(menu_t *menu, const char *label, menu_callback_t callback) {
if (!menu || !label) return -1;
if (menu->item_count >= MENU_MAX_ITEMS) return -1;
menu_item_t *item = &menu->items[menu->item_count];
item->label = label;
item->callback = callback;
item->submenu = NULL;
item->type = MENU_ITEM_ACTION;
item->value_change_callback = NULL;
menu->item_count++;
return 0;
}
/**
* @brief Add a submenu item to a menu
* @param parent_menu Pointer to parent menu_t structure
* @param label Item label
* @param submenu Pointer to submenu menu_t structure
* @return 0 on success, -1 on failure
*/
int menu_add_submenu(menu_t *parent_menu, const char *label, menu_t *submenu) {
if (!parent_menu || !label || !submenu) return -1;
if (parent_menu->item_count >= MENU_MAX_ITEMS) return -1;
menu_item_t *item = &parent_menu->items[parent_menu->item_count];
item->label = label;
item->callback = NULL;
item->submenu = submenu;
item->type = MENU_ITEM_SUBMENU;
item->value_change_callback = NULL;
submenu->parent = parent_menu;
parent_menu->item_count++;
return 0;
}
/**
* @brief Initialize menu system
* @param menu_system Pointer to menu_system_t structure
* @param root_menu Root menu
* @param visible_count Number of visible items on display
* @param draw_callback Callback function to draw menu
*/
void menu_system_init(menu_system_t *menu_system, menu_t *root_menu,
uint8_t visible_count, void (*draw_callback)(menu_system_t *)) {
if (!menu_system || !root_menu) return;
menu_system->root_menu = root_menu;
menu_system->current_menu = root_menu;
menu_system->selected_index = root_menu->selected_index;
menu_system->first_visible_index = root_menu->first_visible_index;
menu_system->visible_count = visible_count;
menu_system->draw_callback = draw_callback;
}
/**
* @brief Navigate up in menu hierarchy
* @param menu_system Pointer to menu_system_t structure
*/
void menu_system_up(menu_system_t *menu_system) {
if (!menu_system || !menu_system->current_menu) return;
if (menu_system->selected_index > 0) {
menu_system->selected_index--;
if (menu_system->selected_index < menu_system->first_visible_index) {
menu_system->first_visible_index = menu_system->selected_index;
}
} else {
menu_system->selected_index = menu_system->current_menu->item_count - 1;
if (menu_system->current_menu->item_count > menu_system->visible_count) {
menu_system->first_visible_index = menu_system->selected_index - menu_system->visible_count + 1;
}
}
menu_system->current_menu->selected_index = menu_system->selected_index;
menu_system->current_menu->first_visible_index = menu_system->first_visible_index;
if (menu_system->draw_callback) {
menu_system->draw_callback(menu_system);
}
}
/**
* @brief Navigate down in menu hierarchy
* @param menu_system Pointer to menu_system_t structure
*/
void menu_system_down(menu_system_t *menu_system) {
if (!menu_system || !menu_system->current_menu) return;
if (menu_system->selected_index < menu_system->current_menu->item_count - 1) {
menu_system->selected_index++;
if (menu_system->selected_index >= menu_system->first_visible_index + menu_system->visible_count) {
menu_system->first_visible_index = menu_system->selected_index - menu_system->visible_count + 1;
}
} else {
menu_system->selected_index = 0;
menu_system->first_visible_index = 0;
}
menu_system->current_menu->selected_index = menu_system->selected_index;
menu_system->current_menu->first_visible_index = menu_system->first_visible_index;
if (menu_system->draw_callback) {
menu_system->draw_callback(menu_system);
}
}
/**
* @brief Select current menu item
* @param menu_system Pointer to menu_system_t structure
*/
void menu_system_select(menu_system_t *menu_system) {
if (!menu_system || !menu_system->current_menu) return;
menu_item_t *item = menu_system_get_selected(menu_system);
if (!item) return;
menu_system->current_menu->selected_index = menu_system->selected_index;
menu_system->current_menu->first_visible_index = menu_system->first_visible_index;
if (item->submenu) {
menu_system->current_menu = item->submenu;
menu_system->selected_index = menu_system->current_menu->selected_index;
menu_system->first_visible_index = menu_system->current_menu->first_visible_index;
if (menu_system->draw_callback) {
menu_system->draw_callback(menu_system);
}
} else if (item->type == MENU_ITEM_RADIO || item->type == MENU_ITEM_AB) {
menu_item_toggle(item);
if (menu_system->draw_callback) {
menu_system->draw_callback(menu_system);
}
} else if (item->callback) {
item->callback();
}
}
/**
* @brief Go back to parent menu
* @param menu_system Pointer to menu_system_t structure
*/
void menu_system_back(menu_system_t *menu_system) {
if (!menu_system || !menu_system->current_menu) return;
if (menu_system->current_menu->parent) {
menu_system->current_menu->selected_index = menu_system->selected_index;
menu_system->current_menu->first_visible_index = menu_system->first_visible_index;
menu_system->current_menu = menu_system->current_menu->parent;
menu_system->selected_index = menu_system->current_menu->selected_index;
menu_system->first_visible_index = menu_system->current_menu->first_visible_index;
if (menu_system->draw_callback) {
menu_system->draw_callback(menu_system);
}
}
}
/**
* @brief Get currently selected item
* @param menu_system Pointer to menu_system_t structure
* @return Pointer to menu_item_t, or NULL if none selected
*/
menu_item_t *menu_system_get_selected(menu_system_t *menu_system) {
if (!menu_system || !menu_system->current_menu) return NULL;
if (menu_system->selected_index >= menu_system->current_menu->item_count) return NULL;
return &menu_system->current_menu->items[menu_system->selected_index];
}
/**
* @brief Refresh menu display
* @param menu_system Pointer to menu_system_t structure
*/
void menu_system_refresh(menu_system_t *menu_system) {
if (!menu_system) return;
if (menu_system->draw_callback) {
menu_system->draw_callback(menu_system);
}
}
/**
* @brief Add a uint8_t value item to a menu
*/
int menu_add_value_uint8(menu_t *menu, const char *label, uint8_t initial_value,
uint8_t min_value, uint8_t max_value, uint8_t step,
menu_value_change_callback_t callback) {
if (!menu || !label) return -1;
if (menu->item_count >= MENU_MAX_ITEMS) return -1;
if (min_value > max_value) return -1;
if (initial_value < min_value || initial_value > max_value) return -1;
menu_item_t *item = &menu->items[menu->item_count];
item->label = label;
item->callback = NULL;
item->submenu = NULL;
item->type = MENU_ITEM_VALUE_UINT8;
item->value.value_uint8 = initial_value;
item->min_value.min_uint8 = min_value;
item->max_value.max_uint8 = max_value;
item->step.step_uint8 = step;
item->value_change_callback = callback;
menu->item_count++;
return 0;
}
/**
* @brief Add an int16_t value item to a menu
*/
int menu_add_value_int16(menu_t *menu, const char *label, int16_t initial_value,
int16_t min_value, int16_t max_value, int16_t step,
menu_value_change_callback_t callback) {
if (!menu || !label) return -1;
if (menu->item_count >= MENU_MAX_ITEMS) return -1;
if (min_value > max_value) return -1;
if (initial_value < min_value || initial_value > max_value) return -1;
menu_item_t *item = &menu->items[menu->item_count];
item->label = label;
item->callback = NULL;
item->submenu = NULL;
item->type = MENU_ITEM_VALUE_INT16;
item->value.value_int16 = initial_value;
item->min_value.min_int16 = min_value;
item->max_value.max_int16 = max_value;
item->step.step_int16 = step;
item->value_change_callback = callback;
menu->item_count++;
return 0;
}
/**
* @brief Add a string value item to a menu
*/
int menu_add_value_string(menu_t *menu, const char *label, const char *options[],
uint8_t option_count, uint8_t initial_index,
menu_value_change_callback_t callback) {
if (!menu || !label || !options) return -1;
if (menu->item_count >= MENU_MAX_ITEMS) return -1;
if (option_count == 0 || option_count > MENU_MAX_STRING_OPTIONS) return -1;
if (initial_index >= option_count) return -1;
menu_item_t *item = &menu->items[menu->item_count];
item->label = label;
item->callback = NULL;
item->submenu = NULL;
item->type = MENU_ITEM_VALUE_STRING;
item->string_option_count = option_count;
item->string_option_index = initial_index;
item->value.value_string = options[initial_index];
item->value_change_callback = callback;
for (uint8_t i = 0; i < option_count; i++) {
item->string_options[i] = options[i];
}
menu->item_count++;
return 0;
}
/**
* @brief Add a radio button item to a menu
*/
int menu_add_radio(menu_t *menu, const char *label, bool initial_value,
menu_value_change_callback_t callback) {
if (!menu || !label) return -1;
if (menu->item_count >= MENU_MAX_ITEMS) return -1;
menu_item_t *item = &menu->items[menu->item_count];
item->label = label;
item->callback = NULL;
item->submenu = NULL;
item->type = MENU_ITEM_RADIO;
item->value.value_bool = initial_value;
item->value_change_callback = callback;
menu->item_count++;
return 0;
}
/**
* @brief Add an A/B toggle item to a menu
*/
int menu_add_ab(menu_t *menu, const char *label, const char *option_a,
const char *option_b, bool initial_value,
menu_value_change_callback_t callback) {
if (!menu || !label || !option_a || !option_b) return -1;
if (menu->item_count >= MENU_MAX_ITEMS) return -1;
menu_item_t *item = &menu->items[menu->item_count];
item->label = label;
item->callback = NULL;
item->submenu = NULL;
item->type = MENU_ITEM_AB;
item->value.value_bool = initial_value;
item->string_options[0] = option_a;
item->string_options[1] = option_b;
item->string_option_count = 2;
item->value_change_callback = callback;
menu->item_count++;
return 0;
}
/**
* @brief Format a menu item as a text string
*/
int menu_item_format_text(menu_item_t *item, char *buffer, size_t buffer_size, bool selected) {
if (!item || !buffer || buffer_size == 0) return -1;
const char *prefix = selected ? "> " : " ";
int written = 0;
switch (item->type) {
case MENU_ITEM_ACTION:
case MENU_ITEM_SUBMENU:
written = snprintf(buffer, buffer_size, "%s%s", prefix, item->label);
break;
case MENU_ITEM_VALUE_UINT8:
if (selected) {
written = snprintf(buffer, buffer_size, "%s<%d>", prefix, item->value.value_uint8);
} else {
written = snprintf(buffer, buffer_size, "%s%s: %d", prefix, item->label, item->value.value_uint8);
}
break;
case MENU_ITEM_VALUE_INT16:
if (selected) {
int16_t val = item->value.value_int16;
written = snprintf(buffer, buffer_size, "%s<%s%d>", prefix,
(val >= 0) ? "+" : "", val);
} else {
int16_t val = item->value.value_int16;
written = snprintf(buffer, buffer_size, "%s%s: %s%d", prefix, item->label,
(val >= 0) ? "+" : "", val);
}
break;
case MENU_ITEM_VALUE_STRING:
if (selected) {
written = snprintf(buffer, buffer_size, "%s<%s>", prefix, item->value.value_string);
} else {
written = snprintf(buffer, buffer_size, "%s%s: %s", prefix, item->label, item->value.value_string);
}
break;
case MENU_ITEM_RADIO:
if (selected) {
written = snprintf(buffer, buffer_size, "%s%s [%c]", prefix, item->label,
item->value.value_bool ? 'X' : ' ');
} else {
written = snprintf(buffer, buffer_size, "%s%s [%c]", prefix, item->label,
item->value.value_bool ? 'X' : ' ');
}
break;
case MENU_ITEM_AB:
if (selected) {
const char *opt = item->value.value_bool ? item->string_options[1] : item->string_options[0];
written = snprintf(buffer, buffer_size, "%s%s", prefix, opt);
} else {
const char *opt = item->value.value_bool ? item->string_options[1] : item->string_options[0];
written = snprintf(buffer, buffer_size, "%s%s: %s", prefix, item->label, opt);
}
break;
default:
written = snprintf(buffer, buffer_size, "%s%s", prefix, item->label);
break;
}
return (written < 0 || (size_t)written >= buffer_size) ? -1 : written;
}
/**
* @brief Get display string for a menu item
*/
int menu_item_get_display_string(menu_item_t *item, char *buffer, size_t buffer_size) {
if (!item || !buffer || buffer_size == 0) return -1;
switch (item->type) {
case MENU_ITEM_ACTION:
case MENU_ITEM_SUBMENU:
return snprintf(buffer, buffer_size, "%s", item->label);
case MENU_ITEM_VALUE_UINT8:
return snprintf(buffer, buffer_size, "%s: %d", item->label, item->value.value_uint8);
case MENU_ITEM_VALUE_INT16: {
int16_t val = item->value.value_int16;
return snprintf(buffer, buffer_size, "%s: %s%d", item->label,
(val >= 0) ? "+" : "", val);
}
case MENU_ITEM_VALUE_STRING:
return snprintf(buffer, buffer_size, "%s: %s", item->label, item->value.value_string);
case MENU_ITEM_RADIO:
return snprintf(buffer, buffer_size, "%s [%c]", item->label,
item->value.value_bool ? 'X' : ' ');
case MENU_ITEM_AB: {
const char *opt = item->value.value_bool ? item->string_options[1] : item->string_options[0];
return snprintf(buffer, buffer_size, "%s: %s", item->label, opt);
}
default:
return snprintf(buffer, buffer_size, "%s", item->label);
}
}
/**
* @brief Increment a menu item's value
*/
int menu_item_increment(menu_item_t *item) {
if (!item) return -1;
bool changed = false;
switch (item->type) {
case MENU_ITEM_VALUE_UINT8: {
uint8_t new_value = item->value.value_uint8 + item->step.step_uint8;
if (new_value <= item->max_value.max_uint8) {
item->value.value_uint8 = new_value;
changed = true;
}
break;
}
case MENU_ITEM_VALUE_INT16: {
int16_t new_value = item->value.value_int16 + item->step.step_int16;
if (new_value <= item->max_value.max_int16) {
item->value.value_int16 = new_value;
changed = true;
}
break;
}
case MENU_ITEM_VALUE_STRING: {
if (item->string_option_index < item->string_option_count - 1) {
item->string_option_index++;
item->value.value_string = item->string_options[item->string_option_index];
changed = true;
}
break;
}
default:
return -1;
}
if (changed && item->value_change_callback) {
item->value_change_callback();
}
return changed ? 0 : -1;
}
/**
* @brief Decrement a menu item's value
*/
int menu_item_decrement(menu_item_t *item) {
if (!item) return -1;
bool changed = false;
switch (item->type) {
case MENU_ITEM_VALUE_UINT8: {
uint8_t new_value;
if (item->value.value_uint8 >= item->step.step_uint8) {
new_value = item->value.value_uint8 - item->step.step_uint8;
} else {
new_value = item->min_value.min_uint8;
}
if (new_value >= item->min_value.min_uint8) {
item->value.value_uint8 = new_value;
changed = true;
}
break;
}
case MENU_ITEM_VALUE_INT16: {
int16_t new_value = item->value.value_int16 - item->step.step_int16;
if (new_value >= item->min_value.min_int16) {
item->value.value_int16 = new_value;
changed = true;
}
break;
}
case MENU_ITEM_VALUE_STRING: {
if (item->string_option_index > 0) {
item->string_option_index--;
item->value.value_string = item->string_options[item->string_option_index];
changed = true;
}
break;
}
default:
return -1;
}
if (changed && item->value_change_callback) {
item->value_change_callback();
}
return changed ? 0 : -1;
}
/**
* @brief Toggle a boolean/radio/ab menu item's value
*/
int menu_item_toggle(menu_item_t *item) {
if (!item) return -1;
bool changed = false;
switch (item->type) {
case MENU_ITEM_RADIO:
case MENU_ITEM_AB:
item->value.value_bool = !item->value.value_bool;
if (item->type == MENU_ITEM_AB) {
item->value.value_string = item->value.value_bool ?
item->string_options[1] : item->string_options[0];
}
changed = true;
break;
default:
return -1;
}
if (changed && item->value_change_callback) {
item->value_change_callback();
}
return 0;
}
/**
* @brief Set a menu item's value directly
*/
int menu_item_set_value(menu_item_t *item, const void *value_ptr) {
if (!item || !value_ptr) return -1;
bool changed = false;
switch (item->type) {
case MENU_ITEM_VALUE_UINT8: {
uint8_t val = *(const uint8_t *)value_ptr;
if (val >= item->min_value.min_uint8 && val <= item->max_value.max_uint8) {
item->value.value_uint8 = val;
changed = true;
}
break;
}
case MENU_ITEM_VALUE_INT16: {
int16_t val = *(const int16_t *)value_ptr;
if (val >= item->min_value.min_int16 && val <= item->max_value.max_int16) {
item->value.value_int16 = val;
changed = true;
}
break;
}
case MENU_ITEM_VALUE_STRING: {
const char *val = *(const char **)value_ptr;
for (uint8_t i = 0; i < item->string_option_count; i++) {
if (item->string_options[i] == val || strcmp(item->string_options[i], val) == 0) {
item->string_option_index = i;
item->value.value_string = item->string_options[i];
changed = true;
break;
}
}
break;
}
case MENU_ITEM_RADIO:
case MENU_ITEM_AB: {
bool val = *(const bool *)value_ptr;
item->value.value_bool = val;
if (item->type == MENU_ITEM_AB) {
item->value.value_string = val ? item->string_options[1] : item->string_options[0];
}
changed = true;
break;
}
default:
return -1;
}
if (changed && item->value_change_callback) {
item->value_change_callback();
}
return changed ? 0 : -1;
}
/**
* @brief Get a menu item's value
*/
const void *menu_item_get_value(menu_item_t *item) {
if (!item) return NULL;
switch (item->type) {
case MENU_ITEM_VALUE_UINT8:
return &item->value.value_uint8;
case MENU_ITEM_VALUE_INT16:
return &item->value.value_int16;
case MENU_ITEM_VALUE_STRING:
return &item->value.value_string;
case MENU_ITEM_RADIO:
case MENU_ITEM_AB:
return &item->value.value_bool;
default:
return NULL;
}
}
/**
* @brief Enter edit mode for current menu item (increment/decrement)
*/
void menu_system_edit(menu_system_t *menu_system, bool increment) {
if (!menu_system) return;
menu_item_t *item = menu_system_get_selected(menu_system);
if (!item) return;
bool changed = false;
switch (item->type) {
case MENU_ITEM_VALUE_UINT8:
case MENU_ITEM_VALUE_INT16:
case MENU_ITEM_VALUE_STRING:
if (increment) {
changed = (menu_item_increment(item) == 0);
} else {
changed = (menu_item_decrement(item) == 0);
}
break;
case MENU_ITEM_RADIO:
case MENU_ITEM_AB:
if (increment || !increment) {
changed = (menu_item_toggle(item) == 0);
}
break;
default:
break;
}
if (changed && menu_system->draw_callback) {
menu_system->draw_callback(menu_system);
}
}
/**
* @brief Destroy menu and free resources
* @param menu Pointer to menu_t structure
*/
void menu_destroy(menu_t *menu) {
if (!menu) return;
for (uint8_t i = 0; i < menu->item_count; i++) {
if (menu->items[i].submenu) {
menu_destroy(menu->items[i].submenu);
}
}
free(menu);
}