system_settings.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "system_settings.h"
  2. #include <loader/loader.h>
  3. static uint8_t
  4. uint32_value_index(const uint32_t value, const uint32_t values[], uint8_t values_count) {
  5. int64_t last_value = INT64_MIN;
  6. uint8_t index = 0;
  7. for(uint8_t i = 0; i < values_count; i++) {
  8. if((value >= last_value) && (value <= values[i])) {
  9. index = i;
  10. break;
  11. }
  12. last_value = values[i];
  13. }
  14. return index;
  15. }
  16. const char* const log_level_text[] = {
  17. "Default",
  18. "None",
  19. "Error",
  20. "Warning",
  21. "Info",
  22. "Debug",
  23. "Trace",
  24. };
  25. const uint32_t log_level_value[] = {
  26. FuriLogLevelDefault,
  27. FuriLogLevelNone,
  28. FuriLogLevelError,
  29. FuriLogLevelWarn,
  30. FuriLogLevelInfo,
  31. FuriLogLevelDebug,
  32. FuriLogLevelTrace,
  33. };
  34. static void log_level_changed(VariableItem* item) {
  35. // SystemSettings* app = variable_item_get_context(item);
  36. uint8_t index = variable_item_get_current_value_index(item);
  37. variable_item_set_current_value_text(item, log_level_text[index]);
  38. furi_hal_rtc_set_log_level(log_level_value[index]);
  39. }
  40. const char* const debug_text[] = {
  41. "Disable",
  42. "Enable",
  43. };
  44. static void debug_changed(VariableItem* item) {
  45. uint8_t index = variable_item_get_current_value_index(item);
  46. variable_item_set_current_value_text(item, debug_text[index]);
  47. if(index) {
  48. furi_hal_rtc_set_flag(FuriHalRtcFlagDebug);
  49. } else {
  50. furi_hal_rtc_reset_flag(FuriHalRtcFlagDebug);
  51. }
  52. loader_update_menu();
  53. }
  54. static uint32_t system_settings_exit(void* context) {
  55. return VIEW_NONE;
  56. }
  57. SystemSettings* system_settings_alloc() {
  58. SystemSettings* app = furi_alloc(sizeof(SystemSettings));
  59. // Load settings
  60. app->gui = furi_record_open("gui");
  61. app->view_dispatcher = view_dispatcher_alloc();
  62. view_dispatcher_enable_queue(app->view_dispatcher);
  63. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  64. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  65. VariableItem* item;
  66. uint8_t value_index;
  67. app->var_item_list = variable_item_list_alloc();
  68. item = variable_item_list_add(
  69. app->var_item_list, "Log Level", COUNT_OF(log_level_text), log_level_changed, app);
  70. value_index = uint32_value_index(
  71. furi_hal_rtc_get_log_level(), log_level_value, COUNT_OF(log_level_text));
  72. variable_item_set_current_value_index(item, value_index);
  73. variable_item_set_current_value_text(item, log_level_text[value_index]);
  74. item = variable_item_list_add(
  75. app->var_item_list, "Debug", COUNT_OF(debug_text), debug_changed, app);
  76. value_index = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug) ? 1 : 0;
  77. variable_item_set_current_value_index(item, value_index);
  78. variable_item_set_current_value_text(item, debug_text[value_index]);
  79. view_set_previous_callback(
  80. variable_item_list_get_view(app->var_item_list), system_settings_exit);
  81. view_dispatcher_add_view(
  82. app->view_dispatcher,
  83. SystemSettingsViewVarItemList,
  84. variable_item_list_get_view(app->var_item_list));
  85. view_dispatcher_switch_to_view(app->view_dispatcher, SystemSettingsViewVarItemList);
  86. return app;
  87. }
  88. void system_settings_free(SystemSettings* app) {
  89. furi_assert(app);
  90. // Variable item list
  91. view_dispatcher_remove_view(app->view_dispatcher, SystemSettingsViewVarItemList);
  92. variable_item_list_free(app->var_item_list);
  93. // View dispatcher
  94. view_dispatcher_free(app->view_dispatcher);
  95. // Records
  96. furi_record_close("gui");
  97. free(app);
  98. }
  99. int32_t system_settings_app(void* p) {
  100. SystemSettings* app = system_settings_alloc();
  101. view_dispatcher_run(app->view_dispatcher);
  102. system_settings_free(app);
  103. return 0;
  104. }