mp_flipper_app_settings.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <gui/gui.h>
  2. #include <gui/modules/variable_item_list.h>
  3. #include <gui/view_dispatcher.h>
  4. #include <lib/toolbox/value_index.h>
  5. #include "py_app.h"
  6. typedef struct {
  7. Gui* gui;
  8. PyApp* settings;
  9. ViewDispatcher* view_dispatcher;
  10. VariableItemList* items;
  11. } PyAppSettings;
  12. #define STACK_SIZE 3
  13. const char* const stack_size_text[STACK_SIZE] = {
  14. "512",
  15. "1024",
  16. "2048",
  17. };
  18. const int32_t stack_size[STACK_SIZE] = {
  19. 512,
  20. 1024,
  21. 2048,
  22. };
  23. #define HEAP_SIZE 5
  24. const char* const heap_size_text[HEAP_SIZE] = {
  25. "10%",
  26. "20%",
  27. "30%",
  28. "40%",
  29. "50%",
  30. };
  31. const float heap_size[HEAP_SIZE] = {
  32. 0.1,
  33. 0.2,
  34. 0.3,
  35. 0.4,
  36. 0.5,
  37. };
  38. static void set_stack_size(VariableItem* item) {
  39. PyAppSettings* app = variable_item_get_context(item);
  40. uint8_t index = variable_item_get_current_value_index(item);
  41. variable_item_set_current_value_text(item, stack_size_text[index]);
  42. app->settings->stack_size = stack_size[index];
  43. }
  44. static void set_heap_size(VariableItem* item) {
  45. PyAppSettings* app = variable_item_get_context(item);
  46. uint8_t index = variable_item_get_current_value_index(item);
  47. variable_item_set_current_value_text(item, heap_size_text[index]);
  48. app->settings->heap_size = heap_size[index];
  49. }
  50. static uint32_t py_app_settings_exit(void* context) {
  51. UNUSED(context);
  52. return VIEW_NONE;
  53. }
  54. static PyAppSettings* alloc_settings(void) {
  55. PyAppSettings* app = malloc(sizeof(PyAppSettings));
  56. app->gui = furi_record_open(RECORD_GUI);
  57. app->settings = py_app_load_settings();
  58. app->items = variable_item_list_alloc();
  59. View* view = variable_item_list_get_view(app->items);
  60. view_set_previous_callback(view, py_app_settings_exit);
  61. VariableItem* item;
  62. uint8_t value_index;
  63. item = variable_item_list_add(app->items, "Stack Size", STACK_SIZE, set_stack_size, app);
  64. value_index = value_index_int32(app->settings->stack_size, stack_size, STACK_SIZE);
  65. variable_item_set_current_value_index(item, value_index);
  66. variable_item_set_current_value_text(item, stack_size_text[value_index]);
  67. item = variable_item_list_add(app->items, "Heap Size", HEAP_SIZE, set_heap_size, app);
  68. value_index = value_index_float(app->settings->heap_size, heap_size, HEAP_SIZE);
  69. variable_item_set_current_value_index(item, value_index);
  70. variable_item_set_current_value_text(item, heap_size_text[value_index]);
  71. app->view_dispatcher = view_dispatcher_alloc();
  72. view_dispatcher_enable_queue(app->view_dispatcher);
  73. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  74. view_dispatcher_add_view(app->view_dispatcher, 0, view);
  75. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  76. return app;
  77. }
  78. static void free_settings(PyAppSettings* app) {
  79. view_dispatcher_remove_view(app->view_dispatcher, 0);
  80. variable_item_list_free(app->items);
  81. view_dispatcher_free(app->view_dispatcher);
  82. free(app->settings);
  83. furi_record_close(RECORD_GUI);
  84. free(app);
  85. }
  86. int32_t py_app_settings_app(void* p) {
  87. UNUSED(p);
  88. PyAppSettings* app = alloc_settings();
  89. view_dispatcher_run(app->view_dispatcher);
  90. py_app_save_settings(app->settings);
  91. free_settings(app);
  92. return 0;
  93. }