working_modes.c 5.9 KB

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