Settings_view.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "UnitempViews.h"
  16. #include <gui/modules/variable_item_list.h>
  17. //Текущий вид
  18. static View* view;
  19. //Список
  20. static VariableItemList* variable_item_list;
  21. static const char states[2][9] = {"Auto", "Infinity"};
  22. static const char temp_units[UT_TEMP_COUNT][3] = {"*C", "*F"};
  23. static const char pressure_units[UT_PRESSURE_COUNT][6] = {"mm Hg", "in Hg", "kPa"};
  24. //Элемент списка - бесконечная подсветка
  25. VariableItem* infinity_backlight_item;
  26. //Единица измерения температуры
  27. VariableItem* temperature_unit_item;
  28. //Единица измерения давления
  29. VariableItem* pressure_unit_item;
  30. #define VIEW_ID UnitempViewSettings
  31. /**
  32. * @brief Функция обработки нажатия кнопки "Назад"
  33. *
  34. * @param context Указатель на данные приложения
  35. * @return ID вида в который нужно переключиться
  36. */
  37. static uint32_t _exit_callback(void* context) {
  38. UNUSED(context);
  39. //Костыль с зависающей подсветкой
  40. if((bool)variable_item_get_current_value_index(infinity_backlight_item) !=
  41. app->settings.infinityBacklight) {
  42. if((bool)variable_item_get_current_value_index(infinity_backlight_item)) {
  43. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  44. } else {
  45. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  46. }
  47. }
  48. app->settings.infinityBacklight =
  49. (bool)variable_item_get_current_value_index(infinity_backlight_item);
  50. app->settings.temp_unit = variable_item_get_current_value_index(temperature_unit_item);
  51. app->settings.pressure_unit = variable_item_get_current_value_index(pressure_unit_item);
  52. unitemp_saveSettings();
  53. unitemp_loadSettings();
  54. //Возврат предыдущий вид
  55. return UnitempViewMainMenu;
  56. }
  57. /**
  58. * @brief Функция обработки нажатия средней кнопки
  59. *
  60. * @param context Указатель на данные приложения
  61. * @param index На каком элементе списка была нажата кнопка
  62. */
  63. static void _enter_callback(void* context, uint32_t index) {
  64. UNUSED(context);
  65. UNUSED(index);
  66. }
  67. static void _setting_change_callback(VariableItem* item) {
  68. if(item == infinity_backlight_item) {
  69. variable_item_set_current_value_text(
  70. infinity_backlight_item,
  71. states[variable_item_get_current_value_index(infinity_backlight_item)]);
  72. }
  73. if(item == temperature_unit_item) {
  74. variable_item_set_current_value_text(
  75. temperature_unit_item,
  76. temp_units[variable_item_get_current_value_index(temperature_unit_item)]);
  77. }
  78. if(item == pressure_unit_item) {
  79. variable_item_set_current_value_text(
  80. pressure_unit_item,
  81. pressure_units[variable_item_get_current_value_index(pressure_unit_item)]);
  82. }
  83. }
  84. /**
  85. * @brief Создание меню редактирования настроек
  86. */
  87. void unitemp_Settings_alloc(void) {
  88. variable_item_list = variable_item_list_alloc();
  89. //Сброс всех элементов меню
  90. variable_item_list_reset(variable_item_list);
  91. infinity_backlight_item = variable_item_list_add(
  92. variable_item_list, "Backlight time", UT_TEMP_COUNT, _setting_change_callback, app);
  93. temperature_unit_item =
  94. variable_item_list_add(variable_item_list, "Temp. unit", 2, _setting_change_callback, app);
  95. pressure_unit_item = variable_item_list_add(
  96. variable_item_list, "Press. unit", UT_PRESSURE_COUNT, _setting_change_callback, app);
  97. //Добавление колбека на нажатие средней кнопки
  98. variable_item_list_set_enter_callback(variable_item_list, _enter_callback, app);
  99. //Создание вида из списка
  100. view = variable_item_list_get_view(variable_item_list);
  101. //Добавление колбека на нажатие кнопки "Назад"
  102. view_set_previous_callback(view, _exit_callback);
  103. //Добавление вида в диспетчер
  104. view_dispatcher_add_view(app->view_dispatcher, VIEW_ID, view);
  105. }
  106. void unitemp_Settings_switch(void) {
  107. //Обнуление последнего выбранного пункта
  108. variable_item_list_set_selected_item(variable_item_list, 0);
  109. variable_item_set_current_value_index(
  110. infinity_backlight_item, (uint8_t)app->settings.infinityBacklight);
  111. variable_item_set_current_value_text(
  112. infinity_backlight_item,
  113. states[variable_item_get_current_value_index(infinity_backlight_item)]);
  114. variable_item_set_current_value_index(temperature_unit_item, (uint8_t)app->settings.temp_unit);
  115. variable_item_set_current_value_text(
  116. temperature_unit_item,
  117. temp_units[variable_item_get_current_value_index(temperature_unit_item)]);
  118. variable_item_set_current_value_index(
  119. pressure_unit_item, (uint8_t)app->settings.pressure_unit);
  120. variable_item_set_current_value_text(
  121. pressure_unit_item,
  122. pressure_units[variable_item_get_current_value_index(pressure_unit_item)]);
  123. view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID);
  124. }
  125. void unitemp_Settings_free(void) {
  126. //Очистка списка элементов
  127. variable_item_list_free(variable_item_list);
  128. //Очистка вида
  129. view_free(view);
  130. //Удаление вида после обработки
  131. view_dispatcher_remove_view(app->view_dispatcher, VIEW_ID);
  132. }