mag.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "mag_i.h"
  2. #define TAG "Mag"
  3. static bool mag_debug_custom_event_callback(void* context, uint32_t event) {
  4. furi_assert(context);
  5. Mag* mag = context;
  6. return scene_manager_handle_custom_event(mag->scene_manager, event);
  7. }
  8. static bool mag_debug_back_event_callback(void* context) {
  9. furi_assert(context);
  10. Mag* mag = context;
  11. return scene_manager_handle_back_event(mag->scene_manager);
  12. }
  13. static Mag* mag_alloc() {
  14. Mag* mag = malloc(sizeof(Mag));
  15. mag->storage = furi_record_open(RECORD_STORAGE);
  16. mag->dialogs = furi_record_open(RECORD_DIALOGS);
  17. mag->file_name = furi_string_alloc();
  18. mag->file_path = furi_string_alloc_set(MAG_APP_FOLDER);
  19. mag->args = furi_string_alloc();
  20. mag->view_dispatcher = view_dispatcher_alloc();
  21. mag->scene_manager = scene_manager_alloc(&mag_scene_handlers, mag);
  22. view_dispatcher_enable_queue(mag->view_dispatcher);
  23. view_dispatcher_set_event_callback_context(mag->view_dispatcher, mag);
  24. view_dispatcher_set_custom_event_callback(
  25. mag->view_dispatcher, mag_debug_custom_event_callback);
  26. view_dispatcher_set_navigation_event_callback(
  27. mag->view_dispatcher, mag_debug_back_event_callback);
  28. mag->mag_dev = mag_device_alloc();
  29. mag_state_load(&mag->state);
  30. // Open GUI record
  31. mag->gui = furi_record_open(RECORD_GUI);
  32. // Open Notification record
  33. mag->notifications = furi_record_open(RECORD_NOTIFICATION);
  34. // Submenu
  35. mag->submenu = submenu_alloc();
  36. view_dispatcher_add_view(mag->view_dispatcher, MagViewSubmenu, submenu_get_view(mag->submenu));
  37. // Dialog
  38. mag->dialog_ex = dialog_ex_alloc();
  39. view_dispatcher_add_view(
  40. mag->view_dispatcher, MagViewDialogEx, dialog_ex_get_view(mag->dialog_ex));
  41. // Popup
  42. mag->popup = popup_alloc();
  43. view_dispatcher_add_view(mag->view_dispatcher, MagViewPopup, popup_get_view(mag->popup));
  44. // Loading
  45. mag->loading = loading_alloc();
  46. view_dispatcher_add_view(mag->view_dispatcher, MagViewLoading, loading_get_view(mag->loading));
  47. // Widget
  48. mag->widget = widget_alloc();
  49. view_dispatcher_add_view(mag->view_dispatcher, MagViewWidget, widget_get_view(mag->widget));
  50. // Variable Item List
  51. mag->variable_item_list = variable_item_list_alloc();
  52. view_dispatcher_add_view(
  53. mag->view_dispatcher,
  54. MagViewVariableItemList,
  55. variable_item_list_get_view(mag->variable_item_list));
  56. // Text Input
  57. mag->text_input = text_input_alloc();
  58. view_dispatcher_add_view(
  59. mag->view_dispatcher, MagViewTextInput, text_input_get_view(mag->text_input));
  60. // Disable expansion protocol to avoid interference with UART Handle
  61. mag->expansion = furi_record_open(RECORD_EXPANSION);
  62. expansion_disable(mag->expansion);
  63. // Move UART here? conditional upon setting?
  64. return mag;
  65. }
  66. static void mag_free(Mag* mag) {
  67. furi_assert(mag);
  68. furi_string_free(mag->file_name);
  69. furi_string_free(mag->file_path);
  70. furi_string_free(mag->args);
  71. // Mag device
  72. mag_device_free(mag->mag_dev);
  73. mag->mag_dev = NULL;
  74. // Submenu
  75. view_dispatcher_remove_view(mag->view_dispatcher, MagViewSubmenu);
  76. submenu_free(mag->submenu);
  77. // DialogEx
  78. view_dispatcher_remove_view(mag->view_dispatcher, MagViewDialogEx);
  79. dialog_ex_free(mag->dialog_ex);
  80. // Popup
  81. view_dispatcher_remove_view(mag->view_dispatcher, MagViewPopup);
  82. popup_free(mag->popup);
  83. // Loading
  84. view_dispatcher_remove_view(mag->view_dispatcher, MagViewLoading);
  85. loading_free(mag->loading);
  86. // Widget
  87. view_dispatcher_remove_view(mag->view_dispatcher, MagViewWidget);
  88. widget_free(mag->widget);
  89. // Variable Item List
  90. view_dispatcher_remove_view(mag->view_dispatcher, MagViewVariableItemList);
  91. variable_item_list_free(mag->variable_item_list);
  92. // TextInput
  93. view_dispatcher_remove_view(mag->view_dispatcher, MagViewTextInput);
  94. text_input_free(mag->text_input);
  95. // View Dispatcher
  96. view_dispatcher_free(mag->view_dispatcher);
  97. // Scene Manager
  98. scene_manager_free(mag->scene_manager);
  99. // GUI
  100. furi_record_close(RECORD_GUI);
  101. mag->gui = NULL;
  102. // Notifications
  103. furi_record_close(RECORD_NOTIFICATION);
  104. mag->notifications = NULL;
  105. // Return previous state of expansion
  106. expansion_enable(mag->expansion);
  107. furi_record_close(RECORD_EXPANSION);
  108. furi_record_close(RECORD_STORAGE);
  109. furi_record_close(RECORD_DIALOGS);
  110. free(mag);
  111. }
  112. // entry point for app
  113. int32_t mag_app(void* p) {
  114. const char* args = p;
  115. Mag* mag = mag_alloc();
  116. if(args && strlen(args)) {
  117. furi_string_set(mag->args, args);
  118. }
  119. mag_make_app_folder(mag);
  120. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  121. uint8_t attempts = 0;
  122. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  123. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  124. furi_hal_power_enable_otg();
  125. furi_delay_ms(10);
  126. }
  127. view_dispatcher_attach_to_gui(mag->view_dispatcher, mag->gui, ViewDispatcherTypeFullscreen);
  128. if(furi_string_empty(mag->args)) {
  129. scene_manager_next_scene(mag->scene_manager, MagSceneStart);
  130. } else {
  131. mag_device_load_data(mag->mag_dev, mag->args, true);
  132. scene_manager_next_scene(mag->scene_manager, MagSceneEmulate);
  133. }
  134. view_dispatcher_run(mag->view_dispatcher);
  135. // Disable 5v power
  136. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  137. furi_hal_power_disable_otg();
  138. }
  139. mag_free(mag);
  140. return 0;
  141. }
  142. void mag_make_app_folder(Mag* mag) {
  143. furi_assert(mag);
  144. if(!storage_simply_mkdir(mag->storage, MAG_APP_FOLDER)) {
  145. dialog_message_show_storage_error(mag->dialogs, "Cannot create\napp folder");
  146. }
  147. }
  148. void mag_text_store_set(Mag* mag, const char* text, ...) {
  149. furi_assert(mag);
  150. va_list args;
  151. va_start(args, text);
  152. vsnprintf(mag->text_store, MAG_TEXT_STORE_SIZE, text, args);
  153. va_end(args);
  154. }
  155. void mag_text_store_clear(Mag* mag) {
  156. furi_assert(mag);
  157. memset(mag->text_store, 0, sizeof(mag->text_store));
  158. }
  159. void mag_popup_timeout_callback(void* context) {
  160. Mag* mag = context;
  161. view_dispatcher_send_custom_event(mag->view_dispatcher, MagEventPopupClosed);
  162. }
  163. void mag_widget_callback(GuiButtonType result, InputType type, void* context) {
  164. Mag* mag = context;
  165. if(type == InputTypeShort) {
  166. view_dispatcher_send_custom_event(mag->view_dispatcher, result);
  167. }
  168. }
  169. void mag_text_input_callback(void* context) {
  170. Mag* mag = context;
  171. view_dispatcher_send_custom_event(mag->view_dispatcher, MagEventNext);
  172. }
  173. void mag_show_loading_popup(void* context, bool show) {
  174. Mag* mag = context;
  175. if(show) {
  176. // Raise timer priority so that animations can play
  177. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  178. view_dispatcher_switch_to_view(mag->view_dispatcher, MagViewLoading);
  179. } else {
  180. // Restore default timer priority
  181. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  182. }
  183. }