nfc_magic.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "nfc_magic_i.h"
  2. bool nfc_magic_custom_event_callback(void* context, uint32_t event) {
  3. furi_assert(context);
  4. NfcMagic* nfc_magic = context;
  5. return scene_manager_handle_custom_event(nfc_magic->scene_manager, event);
  6. }
  7. bool nfc_magic_back_event_callback(void* context) {
  8. furi_assert(context);
  9. NfcMagic* nfc_magic = context;
  10. return scene_manager_handle_back_event(nfc_magic->scene_manager);
  11. }
  12. void nfc_magic_tick_event_callback(void* context) {
  13. furi_assert(context);
  14. NfcMagic* nfc_magic = context;
  15. scene_manager_handle_tick_event(nfc_magic->scene_manager);
  16. }
  17. void nfc_magic_show_loading_popup(void* context, bool show) {
  18. NfcMagic* nfc_magic = context;
  19. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  20. if(show) {
  21. // Raise timer priority so that animations can play
  22. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  23. view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewLoading);
  24. } else {
  25. // Restore default timer priority
  26. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  27. }
  28. }
  29. NfcMagic* nfc_magic_alloc() {
  30. NfcMagic* nfc_magic = malloc(sizeof(NfcMagic));
  31. nfc_magic->worker = nfc_magic_worker_alloc();
  32. nfc_magic->view_dispatcher = view_dispatcher_alloc();
  33. nfc_magic->scene_manager = scene_manager_alloc(&nfc_magic_scene_handlers, nfc_magic);
  34. view_dispatcher_enable_queue(nfc_magic->view_dispatcher);
  35. view_dispatcher_set_event_callback_context(nfc_magic->view_dispatcher, nfc_magic);
  36. view_dispatcher_set_custom_event_callback(
  37. nfc_magic->view_dispatcher, nfc_magic_custom_event_callback);
  38. view_dispatcher_set_navigation_event_callback(
  39. nfc_magic->view_dispatcher, nfc_magic_back_event_callback);
  40. view_dispatcher_set_tick_event_callback(
  41. nfc_magic->view_dispatcher, nfc_magic_tick_event_callback, 100);
  42. // Nfc device
  43. nfc_magic->dev = malloc(sizeof(NfcMagicDevice));
  44. nfc_magic->source_dev = nfc_device_alloc();
  45. furi_string_set(nfc_magic->source_dev->folder, NFC_APP_FOLDER);
  46. // Open GUI record
  47. nfc_magic->gui = furi_record_open(RECORD_GUI);
  48. view_dispatcher_attach_to_gui(
  49. nfc_magic->view_dispatcher, nfc_magic->gui, ViewDispatcherTypeFullscreen);
  50. // Open Notification record
  51. nfc_magic->notifications = furi_record_open(RECORD_NOTIFICATION);
  52. // Submenu
  53. nfc_magic->submenu = submenu_alloc();
  54. view_dispatcher_add_view(
  55. nfc_magic->view_dispatcher, NfcMagicViewMenu, submenu_get_view(nfc_magic->submenu));
  56. // Popup
  57. nfc_magic->popup = popup_alloc();
  58. view_dispatcher_add_view(
  59. nfc_magic->view_dispatcher, NfcMagicViewPopup, popup_get_view(nfc_magic->popup));
  60. // Loading
  61. nfc_magic->loading = loading_alloc();
  62. view_dispatcher_add_view(
  63. nfc_magic->view_dispatcher, NfcMagicViewLoading, loading_get_view(nfc_magic->loading));
  64. // Text Input
  65. nfc_magic->text_input = text_input_alloc();
  66. view_dispatcher_add_view(
  67. nfc_magic->view_dispatcher,
  68. NfcMagicViewTextInput,
  69. text_input_get_view(nfc_magic->text_input));
  70. // Byte Input
  71. nfc_magic->byte_input = byte_input_alloc();
  72. view_dispatcher_add_view(
  73. nfc_magic->view_dispatcher,
  74. NfcMagicViewByteInput,
  75. byte_input_get_view(nfc_magic->byte_input));
  76. // Custom Widget
  77. nfc_magic->widget = widget_alloc();
  78. view_dispatcher_add_view(
  79. nfc_magic->view_dispatcher, NfcMagicViewWidget, widget_get_view(nfc_magic->widget));
  80. return nfc_magic;
  81. }
  82. void nfc_magic_free(NfcMagic* nfc_magic) {
  83. furi_assert(nfc_magic);
  84. // Nfc device
  85. free(nfc_magic->dev);
  86. nfc_device_free(nfc_magic->source_dev);
  87. // Submenu
  88. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewMenu);
  89. submenu_free(nfc_magic->submenu);
  90. // Popup
  91. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewPopup);
  92. popup_free(nfc_magic->popup);
  93. // Loading
  94. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewLoading);
  95. loading_free(nfc_magic->loading);
  96. // Text Input
  97. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewTextInput);
  98. text_input_free(nfc_magic->text_input);
  99. // Byte Input
  100. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewByteInput);
  101. byte_input_free(nfc_magic->byte_input);
  102. // Custom Widget
  103. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewWidget);
  104. widget_free(nfc_magic->widget);
  105. // Worker
  106. nfc_magic_worker_stop(nfc_magic->worker);
  107. nfc_magic_worker_free(nfc_magic->worker);
  108. // View Dispatcher
  109. view_dispatcher_free(nfc_magic->view_dispatcher);
  110. // Scene Manager
  111. scene_manager_free(nfc_magic->scene_manager);
  112. // GUI
  113. furi_record_close(RECORD_GUI);
  114. nfc_magic->gui = NULL;
  115. // Notifications
  116. furi_record_close(RECORD_NOTIFICATION);
  117. nfc_magic->notifications = NULL;
  118. free(nfc_magic);
  119. }
  120. static const NotificationSequence nfc_magic_sequence_blink_start_cyan = {
  121. &message_blink_start_10,
  122. &message_blink_set_color_cyan,
  123. &message_do_not_reset,
  124. NULL,
  125. };
  126. static const NotificationSequence nfc_magic_sequence_blink_stop = {
  127. &message_blink_stop,
  128. NULL,
  129. };
  130. void nfc_magic_blink_start(NfcMagic* nfc_magic) {
  131. notification_message(nfc_magic->notifications, &nfc_magic_sequence_blink_start_cyan);
  132. }
  133. void nfc_magic_blink_stop(NfcMagic* nfc_magic) {
  134. notification_message(nfc_magic->notifications, &nfc_magic_sequence_blink_stop);
  135. }
  136. int32_t nfc_magic_app(void* p) {
  137. UNUSED(p);
  138. NfcMagic* nfc_magic = nfc_magic_alloc();
  139. scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneStart);
  140. view_dispatcher_run(nfc_magic->view_dispatcher);
  141. magic_deactivate();
  142. nfc_magic_free(nfc_magic);
  143. return 0;
  144. }