mag.c 7.5 KB

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