working_modes.c 5.6 KB

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