menu.c 4.5 KB

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