uhf_app.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "uhf_app_i.h"
  2. #include "expansion/expansion.h"
  3. char* convertToHexString(uint8_t* array, size_t length) {
  4. if(array == NULL || length == 0) {
  5. return " ";
  6. }
  7. FuriString* temp_str = furi_string_alloc();
  8. for(size_t i = 0; i < length; i++) {
  9. furi_string_cat_printf(temp_str, "%02X ", array[i]);
  10. }
  11. const char* furi_str = furi_string_get_cstr(temp_str);
  12. size_t str_len = strlen(furi_str);
  13. char* str = (char*)malloc(sizeof(char) * str_len);
  14. memcpy(str, furi_str, str_len);
  15. furi_string_free(temp_str);
  16. return str;
  17. }
  18. bool uhf_custom_event_callback(void* ctx, uint32_t event) {
  19. furi_assert(ctx);
  20. UHFApp* uhf_app = ctx;
  21. return scene_manager_handle_custom_event(uhf_app->scene_manager, event);
  22. }
  23. bool uhf_back_event_callback(void* ctx) {
  24. furi_assert(ctx);
  25. UHFApp* uhf_app = ctx;
  26. return scene_manager_handle_back_event(uhf_app->scene_manager);
  27. }
  28. void uhf_tick_event_callback(void* ctx) {
  29. furi_assert(ctx);
  30. UHFApp* uhf_app = ctx;
  31. scene_manager_handle_tick_event(uhf_app->scene_manager);
  32. }
  33. UHFApp* uhf_alloc() {
  34. UHFApp* uhf_app = (UHFApp*)malloc(sizeof(UHFApp));
  35. uhf_app->view_dispatcher = view_dispatcher_alloc();
  36. uhf_app->scene_manager = scene_manager_alloc(&uhf_scene_handlers, uhf_app);
  37. view_dispatcher_set_event_callback_context(uhf_app->view_dispatcher, uhf_app);
  38. view_dispatcher_set_custom_event_callback(uhf_app->view_dispatcher, uhf_custom_event_callback);
  39. view_dispatcher_set_navigation_event_callback(
  40. uhf_app->view_dispatcher, uhf_back_event_callback);
  41. view_dispatcher_set_tick_event_callback(
  42. uhf_app->view_dispatcher, uhf_tick_event_callback, 100);
  43. // Open GUI record
  44. uhf_app->gui = furi_record_open(RECORD_GUI);
  45. view_dispatcher_attach_to_gui(
  46. uhf_app->view_dispatcher, uhf_app->gui, ViewDispatcherTypeFullscreen);
  47. // Variable Item List
  48. uhf_app->variable_item_list = variable_item_list_alloc();
  49. //worker
  50. uhf_app->worker = uhf_worker_alloc();
  51. // device
  52. uhf_app->uhf_device = uhf_device_alloc();
  53. UHFTagWrapper* uhf_tag_wrapper = uhf_tag_wrapper_alloc();
  54. // // point tag object to worker
  55. uhf_app->worker->uhf_tag_wrapper = uhf_tag_wrapper;
  56. uhf_app->uhf_device->uhf_tag_wrapper = uhf_tag_wrapper;
  57. // Open Notification record
  58. uhf_app->notifications = furi_record_open(RECORD_NOTIFICATION);
  59. // Variable Item List
  60. uhf_app->variable_item_list = variable_item_list_alloc();
  61. view_dispatcher_add_view(
  62. uhf_app->view_dispatcher,
  63. UHFViewVariableItemList,
  64. variable_item_list_get_view(uhf_app->variable_item_list));
  65. // Submenu
  66. uhf_app->submenu = submenu_alloc();
  67. view_dispatcher_add_view(
  68. uhf_app->view_dispatcher, UHFViewMenu, submenu_get_view(uhf_app->submenu));
  69. // Popup
  70. uhf_app->popup = popup_alloc();
  71. view_dispatcher_add_view(
  72. uhf_app->view_dispatcher, UHFViewPopup, popup_get_view(uhf_app->popup));
  73. // Loading
  74. uhf_app->loading = loading_alloc();
  75. view_dispatcher_add_view(
  76. uhf_app->view_dispatcher, UHFViewLoading, loading_get_view(uhf_app->loading));
  77. // Text Input
  78. uhf_app->text_input = text_input_alloc();
  79. view_dispatcher_add_view(
  80. uhf_app->view_dispatcher, UHFViewTextInput, text_input_get_view(uhf_app->text_input));
  81. // Custom Widget
  82. uhf_app->widget = widget_alloc();
  83. view_dispatcher_add_view(
  84. uhf_app->view_dispatcher, UHFViewWidget, widget_get_view(uhf_app->widget));
  85. return uhf_app;
  86. }
  87. void uhf_free(UHFApp* uhf_app) {
  88. furi_assert(uhf_app);
  89. // Submenu
  90. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewMenu);
  91. submenu_free(uhf_app->submenu);
  92. // Popup
  93. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewPopup);
  94. popup_free(uhf_app->popup);
  95. // Loading
  96. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewLoading);
  97. loading_free(uhf_app->loading);
  98. // TextInput
  99. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewTextInput);
  100. text_input_free(uhf_app->text_input);
  101. // Custom Widget
  102. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget);
  103. widget_free(uhf_app->widget);
  104. // Variable Item List
  105. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewVariableItemList);
  106. variable_item_list_free(uhf_app->variable_item_list);
  107. // Tag
  108. uhf_tag_wrapper_free(uhf_app->worker->uhf_tag_wrapper);
  109. // Worker
  110. uhf_worker_stop(uhf_app->worker);
  111. uhf_worker_free(uhf_app->worker);
  112. // Device
  113. uhf_device_free(uhf_app->uhf_device);
  114. // View Dispatcher
  115. view_dispatcher_free(uhf_app->view_dispatcher);
  116. // Scene Manager
  117. scene_manager_free(uhf_app->scene_manager);
  118. // GUI
  119. furi_record_close(RECORD_GUI);
  120. uhf_app->gui = NULL;
  121. // Notifications
  122. furi_record_close(RECORD_NOTIFICATION);
  123. uhf_app->notifications = NULL;
  124. free(uhf_app);
  125. }
  126. static const NotificationSequence uhf_sequence_blink_start_cyan = {
  127. &message_blink_start_10,
  128. &message_blink_set_color_cyan,
  129. &message_do_not_reset,
  130. NULL,
  131. };
  132. static const NotificationSequence uhf_sequence_blink_stop = {
  133. &message_blink_stop,
  134. NULL,
  135. };
  136. void uhf_blink_start(UHFApp* uhf_app) {
  137. notification_message(uhf_app->notifications, &uhf_sequence_blink_start_cyan);
  138. }
  139. void uhf_blink_stop(UHFApp* uhf_app) {
  140. notification_message(uhf_app->notifications, &uhf_sequence_blink_stop);
  141. }
  142. void uhf_show_loading_popup(void* ctx, bool show) {
  143. UHFApp* uhf_app = ctx;
  144. if(show) {
  145. // Raise timer priority so that animations can play
  146. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  147. view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewLoading);
  148. } else {
  149. // Restore default timer priority
  150. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  151. }
  152. }
  153. int32_t uhf_app_main(void* ctx) {
  154. UNUSED(ctx);
  155. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  156. expansion_disable(expansion);
  157. bool is_5v_enabled_by_app = false;
  158. // enable 5v pin if not enabled
  159. if(!furi_hal_power_is_otg_enabled()) {
  160. furi_hal_power_enable_otg();
  161. is_5v_enabled_by_app = true;
  162. }
  163. UHFApp* uhf_app = uhf_alloc();
  164. // enter app
  165. scene_manager_next_scene(uhf_app->scene_manager, UHFSceneModuleInfo);
  166. view_dispatcher_run(uhf_app->view_dispatcher);
  167. // disable 5v pin if enabled by app
  168. if(is_5v_enabled_by_app) {
  169. furi_hal_power_disable_otg();
  170. }
  171. // exit app
  172. uhf_free(uhf_app);
  173. expansion_enable(expansion);
  174. furi_record_close(RECORD_EXPANSION);
  175. return 0;
  176. }