working_modes.c 5.6 KB

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