working_modes.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. unsigned char new_file(void) {
  90. System.open_new_file = 1;
  91. return MODE_NO_CHANGE;
  92. }
  93. const char *new_file_get_name(void) {
  94. return PSTR("> Nowy plik");
  95. }
  96. __flash const struct main_menu_pos_s main_menu[MAIN_MENU_MAXPOS+1] = {
  97. {
  98. .func = save_point,
  99. .get_name = save_point_get_name,
  100. },
  101. {
  102. .func = enter_settings,
  103. .get_name = enter_settings_get_name,
  104. },
  105. {
  106. .func = tracking_pause,
  107. .get_name = pause_tracking_get_name,
  108. },
  109. {
  110. .func = new_file,
  111. .get_name = new_file_get_name,
  112. },
  113. };
  114. struct menu_params_s mp;
  115. void change_display_mode(signed char dir) {
  116. display_mode_index += dir;
  117. if (display_mode_index < 0)
  118. display_mode_index = sizeof(main_display_modes) - 1;
  119. if (display_mode_index >= (signed char)sizeof(main_display_modes))
  120. display_mode_index = 0;
  121. }
  122. unsigned char working_mode_default(unsigned char k) {
  123. switch (k) {
  124. case K_UP:
  125. change_display_mode(-1);
  126. break;
  127. case K_DOWN:
  128. change_display_mode(1);
  129. break;
  130. case K_RIGHT:
  131. return MODE_MAIN_MENU;
  132. }
  133. display_state(main_display_modes[display_mode_index]);
  134. return MODE_NO_CHANGE;
  135. }
  136. unsigned char working_mode_main_menu(unsigned char k) {
  137. switch (k) {
  138. case K_LEFT:
  139. return MODE_DEFAULT;
  140. case K_RIGHT:
  141. if (main_menu[mp.main_menu_pos].func)
  142. return main_menu[mp.main_menu_pos].func();
  143. break;
  144. case K_DOWN:
  145. if (mp.main_menu_pos < MAIN_MENU_MAXPOS)
  146. mp.main_menu_pos++;
  147. break;
  148. case K_UP:
  149. if (mp.main_menu_pos > 0)
  150. mp.main_menu_pos--;
  151. break;
  152. }
  153. display_state(DISPLAY_STATE_MAIN_MENU);
  154. return MODE_NO_CHANGE;
  155. }
  156. unsigned char working_mode_settings_menu(unsigned char k) {
  157. switch (k) {
  158. case K_LEFT:
  159. if (settings_menu[mp.settings_menu_pos].type == SETTINGS_TYPE_BACK)
  160. return MODE_MAIN_MENU;
  161. /* fall through */
  162. case K_RIGHT:
  163. switch (settings_menu[mp.settings_menu_pos].type) {
  164. case SETTINGS_TYPE_BOOL: settings_display_and_modify_bool(mp.settings_menu_pos, k); break;
  165. case SETTINGS_TYPE_U8: settings_display_and_modify_u8(mp.settings_menu_pos, k); break;
  166. }
  167. break;
  168. case K_DOWN:
  169. if (mp.settings_menu_pos < SETTINGS_MENU_MAXPOS)
  170. mp.settings_menu_pos++;
  171. break;
  172. case K_UP:
  173. if (mp.settings_menu_pos > 0)
  174. mp.settings_menu_pos--;
  175. break;
  176. }
  177. return MODE_NO_CHANGE;
  178. }
  179. void display_main_menu_item(void) {
  180. strcpy_P(disp.line1, PSTR(" *** MENU *** "));
  181. strcpy_P(disp.line2, main_menu[mp.main_menu_pos].get_name());
  182. }
  183. void display_settings_menu_item(void) {
  184. switch (settings_menu[mp.settings_menu_pos].type) {
  185. case SETTINGS_TYPE_BOOL:
  186. settings_display_and_modify_bool(mp.settings_menu_pos, 0);
  187. break;
  188. case SETTINGS_TYPE_U8:
  189. settings_display_and_modify_u8(mp.settings_menu_pos, 0);
  190. break;
  191. case SETTINGS_TYPE_BACK:
  192. strcpy_P(disp.line1, PSTR("* Ustawienia *"));
  193. strcpy_P(disp.line2, settings_menu[mp.settings_menu_pos].name);
  194. if (HAVE_NEXT_SETTING_POSITION)
  195. strcat_P(disp.line2, PSTR(" \x01")); /* down arrow */
  196. if (HAVE_PREV_SETTING_POSITION)
  197. strcat_P(disp.line2, PSTR(" \x02")); /* up arrow */
  198. break;
  199. };
  200. }
  201. unsigned char (*__flash const working_modes[])(unsigned char) = {
  202. working_mode_default,
  203. working_mode_main_menu,
  204. working_mode_settings_menu,
  205. };
  206. void key_process(void) {
  207. unsigned char k = getkey();
  208. unsigned char newmode = working_modes[System.working_mode](k);
  209. if (newmode != MODE_NO_CHANGE && newmode != System.working_mode) {
  210. LCD_Clear();
  211. System.working_mode = newmode;
  212. }
  213. }
  214. unsigned char enter_settings(void) {
  215. display_state(DISPLAY_STATE_SETTINGS_MENU);
  216. return MODE_SETTINGS_MENU;
  217. }