Settings_view.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022-2023 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", "hPA"};
  24. static const char heat_index_bool[2][4] = {"OFF", "ON"};
  25. //Элемент списка - бесконечная подсветка
  26. VariableItem* infinity_backlight_item;
  27. //Единица измерения температуры
  28. VariableItem* temperature_unit_item;
  29. //Единица измерения давления
  30. VariableItem* pressure_unit_item;
  31. VariableItem* heat_index_item;
  32. #define VIEW_ID UnitempViewSettings
  33. /**
  34. * @brief Функция обработки нажатия кнопки "Назад"
  35. *
  36. * @param context Указатель на данные приложения
  37. * @return ID вида в который нужно переключиться
  38. */
  39. static uint32_t _exit_callback(void* context) {
  40. UNUSED(context);
  41. //Костыль с зависающей подсветкой
  42. if((bool)variable_item_get_current_value_index(infinity_backlight_item) !=
  43. app->settings.infinityBacklight) {
  44. if((bool)variable_item_get_current_value_index(infinity_backlight_item)) {
  45. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  46. } else {
  47. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  48. }
  49. }
  50. app->settings.infinityBacklight =
  51. (bool)variable_item_get_current_value_index(infinity_backlight_item);
  52. app->settings.temp_unit = variable_item_get_current_value_index(temperature_unit_item);
  53. app->settings.pressure_unit = variable_item_get_current_value_index(pressure_unit_item);
  54. app->settings.heat_index = variable_item_get_current_value_index(heat_index_item);
  55. unitemp_saveSettings();
  56. unitemp_loadSettings();
  57. //Возврат предыдущий вид
  58. return UnitempViewMainMenu;
  59. }
  60. /**
  61. * @brief Функция обработки нажатия средней кнопки
  62. *
  63. * @param context Указатель на данные приложения
  64. * @param index На каком элементе списка была нажата кнопка
  65. */
  66. static void _enter_callback(void* context, uint32_t index) {
  67. UNUSED(context);
  68. UNUSED(index);
  69. }
  70. static void _setting_change_callback(VariableItem* item) {
  71. if(item == infinity_backlight_item) {
  72. variable_item_set_current_value_text(
  73. infinity_backlight_item,
  74. states[variable_item_get_current_value_index(infinity_backlight_item)]);
  75. }
  76. if(item == temperature_unit_item) {
  77. variable_item_set_current_value_text(
  78. temperature_unit_item,
  79. temp_units[variable_item_get_current_value_index(temperature_unit_item)]);
  80. }
  81. if(item == pressure_unit_item) {
  82. variable_item_set_current_value_text(
  83. pressure_unit_item,
  84. pressure_units[variable_item_get_current_value_index(pressure_unit_item)]);
  85. }
  86. if(item == heat_index_item) {
  87. variable_item_set_current_value_text(
  88. heat_index_item,
  89. heat_index_bool[variable_item_get_current_value_index(heat_index_item)]);
  90. }
  91. }
  92. /**
  93. * @brief Создание меню редактирования настроек
  94. */
  95. void unitemp_Settings_alloc(void) {
  96. variable_item_list = variable_item_list_alloc();
  97. //Сброс всех элементов меню
  98. variable_item_list_reset(variable_item_list);
  99. infinity_backlight_item = variable_item_list_add(
  100. variable_item_list, "Backlight time", UT_TEMP_COUNT, _setting_change_callback, app);
  101. temperature_unit_item =
  102. variable_item_list_add(variable_item_list, "Temp. unit", 2, _setting_change_callback, app);
  103. pressure_unit_item = variable_item_list_add(
  104. variable_item_list, "Press. unit", UT_PRESSURE_COUNT, _setting_change_callback, app);
  105. heat_index_item = variable_item_list_add(
  106. variable_item_list, "Calc. heat index", 2, _setting_change_callback, app);
  107. //Добавление колбека на нажатие средней кнопки
  108. variable_item_list_set_enter_callback(variable_item_list, _enter_callback, app);
  109. //Создание вида из списка
  110. view = variable_item_list_get_view(variable_item_list);
  111. //Добавление колбека на нажатие кнопки "Назад"
  112. view_set_previous_callback(view, _exit_callback);
  113. //Добавление вида в диспетчер
  114. view_dispatcher_add_view(app->view_dispatcher, VIEW_ID, view);
  115. }
  116. void unitemp_Settings_switch(void) {
  117. //Обнуление последнего выбранного пункта
  118. variable_item_list_set_selected_item(variable_item_list, 0);
  119. variable_item_set_current_value_index(
  120. infinity_backlight_item, (uint8_t)app->settings.infinityBacklight);
  121. variable_item_set_current_value_text(
  122. infinity_backlight_item,
  123. states[variable_item_get_current_value_index(infinity_backlight_item)]);
  124. variable_item_set_current_value_index(temperature_unit_item, (uint8_t)app->settings.temp_unit);
  125. variable_item_set_current_value_text(
  126. temperature_unit_item,
  127. temp_units[variable_item_get_current_value_index(temperature_unit_item)]);
  128. variable_item_set_current_value_index(
  129. pressure_unit_item, (uint8_t)app->settings.pressure_unit);
  130. variable_item_set_current_value_text(
  131. pressure_unit_item,
  132. pressure_units[variable_item_get_current_value_index(pressure_unit_item)]);
  133. variable_item_set_current_value_index(heat_index_item, (uint8_t)app->settings.heat_index);
  134. variable_item_set_current_value_text(
  135. heat_index_item, heat_index_bool[variable_item_get_current_value_index(heat_index_item)]);
  136. view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID);
  137. }
  138. void unitemp_Settings_free(void) {
  139. //Удаление вида после обработки
  140. view_dispatcher_remove_view(app->view_dispatcher, VIEW_ID);
  141. //Очистка списка элементов
  142. variable_item_list_free(variable_item_list);
  143. }