menu.c 4.8 KB

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