working_modes.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "main.h"
  2. #include "working_modes.h"
  3. #include "display.h"
  4. #include "HD44780-I2C.h"
  5. #include "xprintf.h"
  6. #include "settings.h"
  7. #include "nmea.h"
  8. #include "gpx.h"
  9. static signed char display_mode_index;
  10. __flash const unsigned char main_display_modes[] = {
  11. DISPLAY_STATE_MAIN_DEFAULT,
  12. DISPLAY_STATE_COORD,
  13. DISPLAY_STATE_ELE_SAT,
  14. DISPLAY_STATE_DIST_TIME,
  15. DISPLAY_STATE_SPEED,
  16. DISPLAY_STATE_TIME,
  17. };
  18. const char *enter_settings_get_name(void) {
  19. return PSTR("> Ustawienia");
  20. }
  21. unsigned char tracking_pause(void) {
  22. System.tracking_paused = !System.tracking_paused;
  23. if (System.tracking_paused)
  24. LEDB_ON();
  25. else
  26. LEDB_OFF();
  27. return MODE_NO_CHANGE;
  28. }
  29. const char *pause_tracking_get_name(void) {
  30. static unsigned char state = STATE_PAUSE_TRACKING_NOTPAUSED;
  31. switch (state) {
  32. default:
  33. case STATE_PAUSE_TRACKING_NOTPAUSED:
  34. if (System.tracking_paused) {
  35. set_timer(info_display, 2000);
  36. state = STATE_PAUSE_TRACKING_JUSTPAUSED;
  37. }
  38. return PSTR("> Wstrzymaj rej.");
  39. case STATE_PAUSE_TRACKING_JUSTPAUSED:
  40. if (timer_expired(info_display))
  41. state = STATE_PAUSE_TRACKING_PAUSED;
  42. if (!System.tracking_paused) {
  43. set_timer(info_display, 2000);
  44. state = STATE_PAUSE_TRACKING_JUSTUNPAUSED;
  45. }
  46. return PSTR("Wstrzymano!");
  47. case STATE_PAUSE_TRACKING_PAUSED:
  48. if (!System.tracking_paused) {
  49. set_timer(info_display, 2000);
  50. state = STATE_PAUSE_TRACKING_JUSTUNPAUSED;
  51. }
  52. return PSTR("> Wznow rejestr.");
  53. case STATE_PAUSE_TRACKING_JUSTUNPAUSED:
  54. if (System.tracking_paused) {
  55. set_timer(info_display, 2000);
  56. state = STATE_PAUSE_TRACKING_JUSTPAUSED;
  57. }
  58. if (timer_expired(info_display))
  59. state = STATE_PAUSE_TRACKING_NOTPAUSED;
  60. return PSTR("Wznowiono!");
  61. }
  62. }
  63. const char *save_point_get_name(void) {
  64. switch (mp.point_save_state) {
  65. default:
  66. return PSTR("Zapisz punkt");
  67. case STATE_POINT_SAVE_NOT_DONE:
  68. if (timer_expired(info_display))
  69. mp.point_save_state = STATE_POINT_SAVE_READY;
  70. return PSTR("Nie zapisano!");
  71. case STATE_POINT_SAVE_DONE:
  72. if (timer_expired(info_display))
  73. mp.point_save_state = STATE_POINT_SAVE_READY;
  74. return PSTR("Zapisano!");
  75. }
  76. }
  77. unsigned char save_point(void) {
  78. if (timer_expired(info_display)) { /* don't save too often */
  79. if (System.location_valid) {
  80. gpx_save_single_point(&location);
  81. mp.point_save_state = STATE_POINT_SAVE_DONE;
  82. } else {
  83. mp.point_save_state = STATE_POINT_SAVE_NOT_DONE;
  84. }
  85. }
  86. set_timer(info_display, 2000);
  87. return MODE_NO_CHANGE;
  88. }
  89. __flash const struct main_menu_pos_s main_menu[MAIN_MENU_MAXPOS+1] = {
  90. {
  91. .func = save_point,
  92. .get_name = save_point_get_name,
  93. },
  94. {
  95. .func = enter_settings,
  96. .get_name = enter_settings_get_name,
  97. },
  98. {
  99. .func = tracking_pause,
  100. .get_name = pause_tracking_get_name,
  101. },
  102. };
  103. struct menu_params_s mp;
  104. void change_display_mode(signed char dir) {
  105. display_mode_index += dir;
  106. if (display_mode_index < 0)
  107. display_mode_index = sizeof(main_display_modes) - 1;
  108. if (display_mode_index >= (signed char)sizeof(main_display_modes))
  109. display_mode_index = 0;
  110. }
  111. unsigned char working_mode_default(unsigned char k) {
  112. switch (k) {
  113. case K_UP:
  114. change_display_mode(-1);
  115. break;
  116. case K_DOWN:
  117. change_display_mode(1);
  118. break;
  119. case K_RIGHT:
  120. return MODE_MAIN_MENU;
  121. }
  122. display_state(main_display_modes[display_mode_index]);
  123. return MODE_NO_CHANGE;
  124. }
  125. unsigned char working_mode_main_menu(unsigned char k) {
  126. switch (k) {
  127. case K_LEFT:
  128. return MODE_DEFAULT;
  129. case K_RIGHT:
  130. if (main_menu[mp.main_menu_pos].func)
  131. return main_menu[mp.main_menu_pos].func();
  132. break;
  133. case K_DOWN:
  134. if (mp.main_menu_pos < MAIN_MENU_MAXPOS)
  135. mp.main_menu_pos++;
  136. break;
  137. case K_UP:
  138. if (mp.main_menu_pos > 0)
  139. mp.main_menu_pos--;
  140. break;
  141. }
  142. display_state(DISPLAY_STATE_MAIN_MENU);
  143. return MODE_NO_CHANGE;
  144. }
  145. unsigned char working_mode_settings_menu(unsigned char k) {
  146. switch (k) {
  147. case K_LEFT:
  148. if (settings_menu[mp.settings_menu_pos].type == SETTINGS_TYPE_BACK)
  149. return MODE_MAIN_MENU;
  150. /* fall through */
  151. case K_RIGHT:
  152. switch (settings_menu[mp.settings_menu_pos].type) {
  153. case SETTINGS_TYPE_BOOL: settings_display_and_modify_bool(mp.settings_menu_pos, k); break;
  154. case SETTINGS_TYPE_U8: settings_display_and_modify_u8(mp.settings_menu_pos, k); break;
  155. }
  156. break;
  157. case K_DOWN:
  158. if (mp.settings_menu_pos < SETTINGS_MENU_MAXPOS)
  159. mp.settings_menu_pos++;
  160. break;
  161. case K_UP:
  162. if (mp.settings_menu_pos > 0)
  163. mp.settings_menu_pos--;
  164. break;
  165. }
  166. return MODE_NO_CHANGE;
  167. }
  168. void display_main_menu_item(void) {
  169. strcpy_P(disp.line1, PSTR(" *** MENU *** "));
  170. strcpy_P(disp.line2, main_menu[mp.main_menu_pos].get_name());
  171. }
  172. void display_settings_menu_item(void) {
  173. switch (settings_menu[mp.settings_menu_pos].type) {
  174. case SETTINGS_TYPE_BOOL:
  175. settings_display_and_modify_bool(mp.settings_menu_pos, 0);
  176. break;
  177. case SETTINGS_TYPE_U8:
  178. settings_display_and_modify_u8(mp.settings_menu_pos, 0);
  179. break;
  180. case SETTINGS_TYPE_BACK:
  181. strcpy_P(disp.line1, PSTR("* Ustawienia *"));
  182. strcpy_P(disp.line2, settings_menu[mp.settings_menu_pos].name);
  183. if (HAVE_NEXT_SETTING_POSITION)
  184. strcat_P(disp.line2, PSTR(" \x01")); /* down arrow */
  185. if (HAVE_PREV_SETTING_POSITION)
  186. strcat_P(disp.line2, PSTR(" \x02")); /* up arrow */
  187. break;
  188. };
  189. }
  190. unsigned char (*__flash const working_modes[])(unsigned char) = {
  191. working_mode_default,
  192. working_mode_main_menu,
  193. working_mode_settings_menu,
  194. };
  195. void key_process(void) {
  196. unsigned char k = getkey();
  197. unsigned char newmode = working_modes[System.working_mode](k);
  198. if (newmode != MODE_NO_CHANGE && newmode != System.working_mode) {
  199. LCD_Clear();
  200. System.working_mode = newmode;
  201. }
  202. }
  203. unsigned char enter_settings(void) {
  204. display_state(DISPLAY_STATE_SETTINGS_MENU);
  205. return MODE_SETTINGS_MENU;
  206. }