mag.c 7.4 KB

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