menu.c 4.8 KB

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