menu.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #define MENU_TYPE_SETTING_BOOL 0
  3. #define MENU_TYPE_SETTING_U8 1
  4. #define MENU_TYPE_DISPLAY 2
  5. #define MENU_TYPE_FUNCTION 3
  6. #define MENU_TYPE_FUNCTION_WITH_KEY 4
  7. #define IS_SETTING(x) (x<3)
  8. #define MENU_DISPLAY_TYPE_DEFAULT 0 // specific for setting type if IS_SETTING(), MENU_DISPLAY_STRING otherwise
  9. #define MENU_DISPLAY_TYPE_STRING 1
  10. #define MENU_DISPLAY_TYPE_FUNCTION 2
  11. #define MENU_DISPLAY_TYPE_NAME_FUNCTION 3
  12. #define MENU_DISPLAY_TYPE_NAME_CSFUNCTION 4
  13. #define MENU_DISPLAY_TYPE_U8_METERS 5
  14. #define MENU_DISPLAY_TYPE_U8_SECONDS 6
  15. #define MENU_DISPLAY_TYPE_U8_KMH 7
  16. #define menu_push(x) { if(__menu_num<DATA_NUM) __menu_data[__menu_num++] = x; } // stack commands
  17. #define menu_pop() (--__menu_num)
  18. #define menu_get() (&__menu_data[__menu_num-1])
  19. #define no_menu() (__menu_num == 0)
  20. #define DATA_NUM 3 // maximum menu depth
  21. #define HAVE_NEXT_SETTING_POSITION (menu_get()->ind < (menu_get()->num-1))
  22. #define HAVE_PREV_SETTING_POSITION (menu_get()->ind > 0)
  23. struct menu_pos {
  24. unsigned char type;
  25. unsigned char display_type;
  26. __flash const char *name; // used for line 1 when MENU_DISPLAY_TYPE_STRING or MENU_DISPLAY_TYPE_NAME_(CS)FUNCTION
  27. union {
  28. __flash const char *value; // used for line 2 when MENU_DISPLAY_TYPE_STRING
  29. void (* display)(void); // used for line 2 when MENU_DISPLAY_TYPE_NAME_FUNCTION; used for both lines when MENU_DISPLAY_TYPE_FUNCTION
  30. __flash const char * (* csdisplay)(void); // used for line 2 when MENU_DISPLAY_TYPE_NAME_CSFUNCTION
  31. };
  32. unsigned char index; // index when IS_SETTING()
  33. void (* changed)(void); // what to call on changed value when IS_SETTING()
  34. union {
  35. unsigned char (* func)(void); // what to call on MENU_TYPE_FUNCTION; returns true if display refresh is needed
  36. unsigned char (* func_key)(unsigned char k); // what to call on MENU_TYPE_FUNCTION_WITH_KEY; returns true if display refresh is needed
  37. };
  38. unsigned char allow_back; // left arrow will return to level up
  39. };
  40. struct menu_struct {
  41. __flash const struct menu_pos *list; // list of elements
  42. unsigned char num; // count of elements
  43. signed int ind:6; // current index/position
  44. };
  45. extern unsigned char __menu_num;
  46. extern struct menu_struct __menu_data[DATA_NUM];
  47. extern void (*func_enter_table[])(struct menu_pos *); // to be defined in the application
  48. unsigned char menu(void);