working_modes.c 3.8 KB

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