working_modes.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. static signed char display_mode_index;
  9. __flash const unsigned char main_display_modes[] = {
  10. DISPLAY_STATE_MAIN_DEFAULT,
  11. DISPLAY_STATE_COORD,
  12. DISPLAY_STATE_ELE_SAT,
  13. };
  14. __flash const struct main_menu_pos_s main_menu[] = {
  15. {
  16. .func = enter_settings,
  17. },
  18. };
  19. struct menu_params_s mp;
  20. void change_display_mode(signed char dir) {
  21. display_mode_index += dir;
  22. if (display_mode_index < 0)
  23. display_mode_index = sizeof(main_display_modes) - 1;
  24. if (display_mode_index >= (signed char)sizeof(main_display_modes))
  25. display_mode_index = 0;
  26. }
  27. unsigned char working_mode_default(unsigned char k) {
  28. switch (k) {
  29. case K_UP:
  30. change_display_mode(-1);
  31. break;
  32. case K_DOWN:
  33. change_display_mode(1);
  34. break;
  35. case K_RIGHT:
  36. return MODE_MAIN_MENU;
  37. }
  38. display_state(main_display_modes[display_mode_index]);
  39. return MODE_NO_CHANGE;
  40. }
  41. unsigned char working_mode_main_menu(unsigned char k) {
  42. switch (k) {
  43. case K_LEFT:
  44. return MODE_DEFAULT;
  45. case K_RIGHT:
  46. return main_menu_right_press();
  47. case K_DOWN: /* TODO next menu item */
  48. case K_UP: /* TODO prev menu item */
  49. }
  50. display_state(DISPLAY_STATE_MAIN_MENU);
  51. return MODE_NO_CHANGE;
  52. }
  53. unsigned char working_mode_settings_menu(unsigned char k) {
  54. switch (k) {
  55. case K_LEFT:
  56. if (settings_menu[mp.settings_menu_pos].type == SETTINGS_TYPE_BACK)
  57. return MODE_MAIN_MENU;
  58. /* fall through */
  59. case K_RIGHT:
  60. switch (settings_menu[mp.settings_menu_pos].type) {
  61. case SETTINGS_TYPE_BOOL: settings_display_and_modify_bool(mp.settings_menu_pos, k); break;
  62. case SETTINGS_TYPE_U8: settings_display_and_modify_u8(mp.settings_menu_pos, k); break;
  63. }
  64. break;
  65. case K_DOWN:
  66. if (mp.settings_menu_pos < SETTINGS_MENU_MAXPOS)
  67. mp.settings_menu_pos++;
  68. break;
  69. case K_UP:
  70. if (mp.settings_menu_pos > 0)
  71. mp.settings_menu_pos--;
  72. break;
  73. }
  74. return MODE_NO_CHANGE;
  75. }
  76. void display_settings_menu_item(void) {
  77. switch (settings_menu[mp.settings_menu_pos].type) {
  78. case SETTINGS_TYPE_BOOL:
  79. settings_display_and_modify_bool(mp.settings_menu_pos, 0);
  80. break;
  81. case SETTINGS_TYPE_U8:
  82. settings_display_and_modify_u8(mp.settings_menu_pos, 0);
  83. break;
  84. case SETTINGS_TYPE_BACK:
  85. strcpy_P(disp.line1, PSTR("* Ustawienia *"));
  86. strcpy_P(disp.line2, settings_menu[mp.settings_menu_pos].name);
  87. if (HAVE_NEXT_SETTING_POSITION)
  88. strcat_P(disp.line2, PSTR(" \x01")); /* down arrow */
  89. if (HAVE_PREV_SETTING_POSITION)
  90. strcat_P(disp.line2, PSTR(" \x02")); /* up arrow */
  91. break;
  92. };
  93. }
  94. unsigned char (*__flash const working_modes[])(unsigned char) = {
  95. working_mode_default,
  96. working_mode_main_menu,
  97. working_mode_settings_menu,
  98. };
  99. void key_process(void) {
  100. unsigned char k = getkey();
  101. unsigned char newmode = working_modes[System.working_mode](k);
  102. if (newmode != MODE_NO_CHANGE && newmode != System.working_mode) {
  103. LCD_Clear();
  104. System.working_mode = newmode;
  105. }
  106. }
  107. unsigned char main_menu_right_press(void) {
  108. if (main_menu[mp.main_menu_pos].func)
  109. return main_menu[mp.main_menu_pos].func();
  110. return MODE_NO_CHANGE;
  111. }
  112. unsigned char enter_settings(void) {
  113. display_state(DISPLAY_STATE_SETTINGS_MENU);
  114. return MODE_SETTINGS_MENU;
  115. }