menu.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "main.h"
  2. #include "menu.h"
  3. #include "settings.h"
  4. #include "display.h"
  5. #include "xprintf.h"
  6. unsigned char __menu_num; // Menu stack variables
  7. struct menu_struct __menu_data[DATA_NUM];
  8. __flash const char _NULL_STRING[] = "(NULL)";
  9. void settings_display_bool(unsigned char index) {
  10. unsigned char val = get_flag(index);
  11. if (val)
  12. strcpy_P(disp.line2, PSTR("< Tak > "));
  13. else
  14. strcpy_P(disp.line2, PSTR("< Nie > "));
  15. strcat_P(disp.line2, HAVE_NEXT_SETTING_POSITION?PSTR(" \x01"):PSTR(" ")); /* down arrow */
  16. strcat_P(disp.line2, HAVE_PREV_SETTING_POSITION?PSTR(" \x02"):PSTR(" ")); /* up arrow */
  17. }
  18. void settings_display_u8(unsigned char index) {
  19. unsigned char val = System.conf.conf_u8[index];
  20. xsprintf(disp.line2, PSTR("%d"), (int)val);
  21. strcat_P(disp.line2, HAVE_NEXT_SETTING_POSITION?PSTR(" \x01"):PSTR(" ")); /* down arrow */
  22. strcat_P(disp.line2, HAVE_PREV_SETTING_POSITION?PSTR(" \x02"):PSTR(" ")); /* up arrow */
  23. }
  24. void settings_change_bool(struct menu_pos pos, unsigned char k) {
  25. unsigned char index = pos.index;
  26. unsigned char val = get_flag(index);
  27. if (k == K_LEFT || k == K_RIGHT) { /* change value */
  28. val = !val;
  29. set_flag(index, val);
  30. if (pos.changed != NULL)
  31. pos.changed();
  32. }
  33. }
  34. void settings_change_u8(struct menu_pos pos, unsigned char k) {
  35. unsigned char index = pos.index;
  36. unsigned char val = System.conf.conf_u8[index];
  37. if (k == K_LEFT) {
  38. if (val)
  39. val--;
  40. }
  41. if (k == K_RIGHT) {
  42. if (val < limits_max_u8[index])
  43. val++;
  44. }
  45. if (k == K_LEFT || k == K_RIGHT) {
  46. System.conf.conf_u8[index] = val;
  47. if (pos.changed != NULL)
  48. pos.changed();
  49. }
  50. }
  51. unsigned char menu(void) {
  52. struct menu_struct *curr;
  53. struct menu_pos pos;
  54. unsigned char display_line1_as_string = 0;
  55. unsigned char display_changed = 0;
  56. unsigned char k = getkey();
  57. curr = menu_get();
  58. pos = curr->list[curr->ind];
  59. switch (k) {
  60. case K_UP:
  61. if (curr->ind > 0) {
  62. curr->ind--;
  63. pos = curr->list[curr->ind];
  64. display_changed = 1;
  65. }
  66. break;
  67. case K_DOWN:
  68. if (curr->ind < curr->num-1) {
  69. curr->ind++;
  70. pos = curr->list[curr->ind];
  71. display_changed = 1;
  72. }
  73. break;
  74. }
  75. switch (pos.display_type) {
  76. case MENU_DISPLAY_TYPE_DEFAULT:
  77. if (IS_SETTING(pos.type))
  78. break;
  79. /* fall through */
  80. case MENU_DISPLAY_TYPE_STRING:
  81. display_line1_as_string = 1;
  82. if (pos.value) {
  83. strcpy_P(disp.line2, pos.value);
  84. } else {
  85. strcpy_P(disp.line2, _NULL_STRING);
  86. }
  87. break;
  88. case MENU_DISPLAY_TYPE_NAME_FUNCTION:
  89. display_line1_as_string = 1;
  90. /* fall through */
  91. case MENU_DISPLAY_TYPE_FUNCTION:
  92. pos.display();
  93. break;
  94. case MENU_DISPLAY_TYPE_NAME_CSFUNCTION:
  95. display_line1_as_string = 1;
  96. strcpy_P(disp.line2, pos.csdisplay());
  97. break;
  98. default: /* bad data */
  99. break;
  100. }
  101. switch (pos.type) {
  102. case MENU_TYPE_SETTING_BOOL:
  103. if (pos.display_type == MENU_DISPLAY_TYPE_DEFAULT) {
  104. settings_display_bool(pos.index);
  105. display_line1_as_string = 1;
  106. }
  107. if (k) {
  108. settings_change_bool(pos, k);
  109. display_changed = 1;
  110. }
  111. break;
  112. case MENU_TYPE_SETTING_U8:
  113. if (pos.display_type == MENU_DISPLAY_TYPE_DEFAULT) {
  114. settings_display_u8(pos.index);
  115. display_line1_as_string = 1;
  116. }
  117. if (k) {
  118. settings_change_u8(pos, k);
  119. display_changed = 1;
  120. }
  121. break;
  122. case MENU_TYPE_DISPLAY:
  123. /* nothing special to do */
  124. break;
  125. case MENU_TYPE_FUNCTION:
  126. if (k == K_RIGHT) {
  127. pos.func();
  128. display_changed = 1;
  129. }
  130. break;
  131. }
  132. if (pos.allow_back && k == K_LEFT) {
  133. menu_pop();
  134. return 0;
  135. }
  136. if (display_line1_as_string) {
  137. if (pos.name) {
  138. strcpy_P(disp.line1, pos.name);
  139. } else {
  140. strcpy_P(disp.line1, _NULL_STRING);
  141. }
  142. }
  143. if (display_changed)
  144. set_timer(lcd, 50); /* ensure update on next iteration */
  145. return 1;
  146. }