system_settings.c 3.6 KB

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