nfc_magic.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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->nfc_dev = nfc_device_alloc();
  44. furi_string_set(nfc_magic->nfc_dev->folder, NFC_APP_FOLDER);
  45. // Open GUI record
  46. nfc_magic->gui = furi_record_open(RECORD_GUI);
  47. view_dispatcher_attach_to_gui(
  48. nfc_magic->view_dispatcher, nfc_magic->gui, ViewDispatcherTypeFullscreen);
  49. // Open Notification record
  50. nfc_magic->notifications = furi_record_open(RECORD_NOTIFICATION);
  51. // Submenu
  52. nfc_magic->submenu = submenu_alloc();
  53. view_dispatcher_add_view(
  54. nfc_magic->view_dispatcher, NfcMagicViewMenu, submenu_get_view(nfc_magic->submenu));
  55. // Popup
  56. nfc_magic->popup = popup_alloc();
  57. view_dispatcher_add_view(
  58. nfc_magic->view_dispatcher, NfcMagicViewPopup, popup_get_view(nfc_magic->popup));
  59. // Loading
  60. nfc_magic->loading = loading_alloc();
  61. view_dispatcher_add_view(
  62. nfc_magic->view_dispatcher, NfcMagicViewLoading, loading_get_view(nfc_magic->loading));
  63. // Text Input
  64. nfc_magic->text_input = text_input_alloc();
  65. view_dispatcher_add_view(
  66. nfc_magic->view_dispatcher,
  67. NfcMagicViewTextInput,
  68. text_input_get_view(nfc_magic->text_input));
  69. // Custom Widget
  70. nfc_magic->widget = widget_alloc();
  71. view_dispatcher_add_view(
  72. nfc_magic->view_dispatcher, NfcMagicViewWidget, widget_get_view(nfc_magic->widget));
  73. return nfc_magic;
  74. }
  75. void nfc_magic_free(NfcMagic* nfc_magic) {
  76. furi_assert(nfc_magic);
  77. // Nfc device
  78. nfc_device_free(nfc_magic->nfc_dev);
  79. // Submenu
  80. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewMenu);
  81. submenu_free(nfc_magic->submenu);
  82. // Popup
  83. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewPopup);
  84. popup_free(nfc_magic->popup);
  85. // Loading
  86. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewLoading);
  87. loading_free(nfc_magic->loading);
  88. // TextInput
  89. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewTextInput);
  90. text_input_free(nfc_magic->text_input);
  91. // Custom Widget
  92. view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewWidget);
  93. widget_free(nfc_magic->widget);
  94. // Worker
  95. nfc_magic_worker_stop(nfc_magic->worker);
  96. nfc_magic_worker_free(nfc_magic->worker);
  97. // View Dispatcher
  98. view_dispatcher_free(nfc_magic->view_dispatcher);
  99. // Scene Manager
  100. scene_manager_free(nfc_magic->scene_manager);
  101. // GUI
  102. furi_record_close(RECORD_GUI);
  103. nfc_magic->gui = NULL;
  104. // Notifications
  105. furi_record_close(RECORD_NOTIFICATION);
  106. nfc_magic->notifications = NULL;
  107. free(nfc_magic);
  108. }
  109. static const NotificationSequence nfc_magic_sequence_blink_start_cyan = {
  110. &message_blink_start_10,
  111. &message_blink_set_color_cyan,
  112. &message_do_not_reset,
  113. NULL,
  114. };
  115. static const NotificationSequence nfc_magic_sequence_blink_stop = {
  116. &message_blink_stop,
  117. NULL,
  118. };
  119. void nfc_magic_blink_start(NfcMagic* nfc_magic) {
  120. notification_message(nfc_magic->notifications, &nfc_magic_sequence_blink_start_cyan);
  121. }
  122. void nfc_magic_blink_stop(NfcMagic* nfc_magic) {
  123. notification_message(nfc_magic->notifications, &nfc_magic_sequence_blink_stop);
  124. }
  125. int32_t nfc_magic_app(void* p) {
  126. UNUSED(p);
  127. NfcMagic* nfc_magic = nfc_magic_alloc();
  128. scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneStart);
  129. view_dispatcher_run(nfc_magic->view_dispatcher);
  130. nfc_magic_free(nfc_magic);
  131. return 0;
  132. }