menu.c 4.8 KB

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