menu.c 4.2 KB

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