system_settings.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "system_settings.h"
  2. #include <loader/loader.h>
  3. #include <lib/toolbox/value_index.h>
  4. #include <locale/locale.h>
  5. const char* const log_level_text[] = {
  6. "Default",
  7. "None",
  8. "Error",
  9. "Warning",
  10. "Info",
  11. "Debug",
  12. "Trace",
  13. };
  14. const uint32_t log_level_value[] = {
  15. FuriLogLevelDefault,
  16. FuriLogLevelNone,
  17. FuriLogLevelError,
  18. FuriLogLevelWarn,
  19. FuriLogLevelInfo,
  20. FuriLogLevelDebug,
  21. FuriLogLevelTrace,
  22. };
  23. static void log_level_changed(VariableItem* item) {
  24. // SystemSettings* app = variable_item_get_context(item);
  25. uint8_t index = variable_item_get_current_value_index(item);
  26. variable_item_set_current_value_text(item, log_level_text[index]);
  27. furi_hal_rtc_set_log_level(log_level_value[index]);
  28. }
  29. const char* const debug_text[] = {
  30. "OFF",
  31. "ON",
  32. };
  33. static void debug_changed(VariableItem* item) {
  34. uint8_t index = variable_item_get_current_value_index(item);
  35. variable_item_set_current_value_text(item, debug_text[index]);
  36. if(index) {
  37. furi_hal_rtc_set_flag(FuriHalRtcFlagDebug);
  38. } else {
  39. furi_hal_rtc_reset_flag(FuriHalRtcFlagDebug);
  40. }
  41. loader_update_menu();
  42. }
  43. const char* const heap_trace_mode_text[] = {
  44. "None",
  45. "Main",
  46. #if FURI_DEBUG
  47. "Tree",
  48. "All",
  49. #endif
  50. };
  51. const uint32_t heap_trace_mode_value[] = {
  52. FuriHalRtcHeapTrackModeNone,
  53. FuriHalRtcHeapTrackModeMain,
  54. #if FURI_DEBUG
  55. FuriHalRtcHeapTrackModeTree,
  56. FuriHalRtcHeapTrackModeAll,
  57. #endif
  58. };
  59. static void heap_trace_mode_changed(VariableItem* item) {
  60. // SystemSettings* app = variable_item_get_context(item);
  61. uint8_t index = variable_item_get_current_value_index(item);
  62. variable_item_set_current_value_text(item, heap_trace_mode_text[index]);
  63. furi_hal_rtc_set_heap_track_mode(heap_trace_mode_value[index]);
  64. }
  65. const char* const mesurement_units_text[] = {
  66. "Metric",
  67. "Imperial",
  68. };
  69. const uint32_t mesurement_units_value[] = {
  70. LocaleMeasurementUnitsMetric,
  71. LocaleMeasurementUnitsImperial,
  72. };
  73. static void mesurement_units_changed(VariableItem* item) {
  74. // SystemSettings* app = variable_item_get_context(item);
  75. uint8_t index = variable_item_get_current_value_index(item);
  76. variable_item_set_current_value_text(item, mesurement_units_text[index]);
  77. locale_set_measurement_unit(mesurement_units_value[index]);
  78. }
  79. const char* const time_format_text[] = {
  80. "24h",
  81. "12h",
  82. };
  83. const uint32_t time_format_value[] = {
  84. LocaleTimeFormat24h,
  85. LocaleTimeFormat12h,
  86. };
  87. static void time_format_changed(VariableItem* item) {
  88. // SystemSettings* app = variable_item_get_context(item);
  89. uint8_t index = variable_item_get_current_value_index(item);
  90. variable_item_set_current_value_text(item, time_format_text[index]);
  91. locale_set_time_format(time_format_value[index]);
  92. }
  93. const char* const date_format_text[] = {
  94. "D/M/Y",
  95. "M/D/Y",
  96. "Y/M/D",
  97. };
  98. const uint32_t date_format_value[] = {
  99. LocaleDateFormatDMY,
  100. LocaleDateFormatMDY,
  101. LocaleDateFormatYMD,
  102. };
  103. static void date_format_changed(VariableItem* item) {
  104. // SystemSettings* app = variable_item_get_context(item);
  105. uint8_t index = variable_item_get_current_value_index(item);
  106. variable_item_set_current_value_text(item, date_format_text[index]);
  107. locale_set_date_format(date_format_value[index]);
  108. }
  109. static uint32_t system_settings_exit(void* context) {
  110. UNUSED(context);
  111. return VIEW_NONE;
  112. }
  113. SystemSettings* system_settings_alloc() {
  114. SystemSettings* app = malloc(sizeof(SystemSettings));
  115. // Load settings
  116. app->gui = furi_record_open(RECORD_GUI);
  117. app->view_dispatcher = view_dispatcher_alloc();
  118. view_dispatcher_enable_queue(app->view_dispatcher);
  119. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  120. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  121. VariableItem* item;
  122. uint8_t value_index;
  123. app->var_item_list = variable_item_list_alloc();
  124. item = variable_item_list_add(
  125. app->var_item_list,
  126. "Units",
  127. COUNT_OF(mesurement_units_text),
  128. mesurement_units_changed,
  129. app);
  130. value_index = value_index_uint32(
  131. locale_get_measurement_unit(), mesurement_units_value, COUNT_OF(mesurement_units_value));
  132. variable_item_set_current_value_index(item, value_index);
  133. variable_item_set_current_value_text(item, mesurement_units_text[value_index]);
  134. item = variable_item_list_add(
  135. app->var_item_list, "Time Format", COUNT_OF(time_format_text), time_format_changed, app);
  136. value_index = value_index_uint32(
  137. locale_get_time_format(), time_format_value, COUNT_OF(time_format_value));
  138. variable_item_set_current_value_index(item, value_index);
  139. variable_item_set_current_value_text(item, time_format_text[value_index]);
  140. item = variable_item_list_add(
  141. app->var_item_list, "Date Format", COUNT_OF(date_format_text), date_format_changed, app);
  142. value_index = value_index_uint32(
  143. locale_get_date_format(), date_format_value, COUNT_OF(date_format_value));
  144. variable_item_set_current_value_index(item, value_index);
  145. variable_item_set_current_value_text(item, date_format_text[value_index]);
  146. item = variable_item_list_add(
  147. app->var_item_list, "Log Level", COUNT_OF(log_level_text), log_level_changed, app);
  148. value_index = value_index_uint32(
  149. furi_hal_rtc_get_log_level(), log_level_value, COUNT_OF(log_level_text));
  150. variable_item_set_current_value_index(item, value_index);
  151. variable_item_set_current_value_text(item, log_level_text[value_index]);
  152. item = variable_item_list_add(
  153. app->var_item_list, "Debug", COUNT_OF(debug_text), debug_changed, app);
  154. value_index = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug) ? 1 : 0;
  155. variable_item_set_current_value_index(item, value_index);
  156. variable_item_set_current_value_text(item, debug_text[value_index]);
  157. item = variable_item_list_add(
  158. app->var_item_list,
  159. "Heap Trace",
  160. COUNT_OF(heap_trace_mode_text),
  161. heap_trace_mode_changed,
  162. app);
  163. value_index = value_index_uint32(
  164. furi_hal_rtc_get_heap_track_mode(), heap_trace_mode_value, COUNT_OF(heap_trace_mode_text));
  165. furi_hal_rtc_set_heap_track_mode(heap_trace_mode_value[value_index]);
  166. variable_item_set_current_value_index(item, value_index);
  167. variable_item_set_current_value_text(item, heap_trace_mode_text[value_index]);
  168. view_set_previous_callback(
  169. variable_item_list_get_view(app->var_item_list), system_settings_exit);
  170. view_dispatcher_add_view(
  171. app->view_dispatcher,
  172. SystemSettingsViewVarItemList,
  173. variable_item_list_get_view(app->var_item_list));
  174. view_dispatcher_switch_to_view(app->view_dispatcher, SystemSettingsViewVarItemList);
  175. return app;
  176. }
  177. void system_settings_free(SystemSettings* app) {
  178. furi_assert(app);
  179. // Variable item list
  180. view_dispatcher_remove_view(app->view_dispatcher, SystemSettingsViewVarItemList);
  181. variable_item_list_free(app->var_item_list);
  182. // View dispatcher
  183. view_dispatcher_free(app->view_dispatcher);
  184. // Records
  185. furi_record_close(RECORD_GUI);
  186. free(app);
  187. }
  188. int32_t system_settings_app(void* p) {
  189. UNUSED(p);
  190. SystemSettings* app = system_settings_alloc();
  191. view_dispatcher_run(app->view_dispatcher);
  192. system_settings_free(app);
  193. return 0;
  194. }