uhf_app.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_enable_queue(uhf_app->view_dispatcher);
  38. view_dispatcher_set_event_callback_context(uhf_app->view_dispatcher, uhf_app);
  39. view_dispatcher_set_custom_event_callback(uhf_app->view_dispatcher, uhf_custom_event_callback);
  40. view_dispatcher_set_navigation_event_callback(
  41. uhf_app->view_dispatcher, uhf_back_event_callback);
  42. view_dispatcher_set_tick_event_callback(
  43. uhf_app->view_dispatcher, uhf_tick_event_callback, 100);
  44. // Open GUI record
  45. uhf_app->gui = furi_record_open(RECORD_GUI);
  46. view_dispatcher_attach_to_gui(
  47. uhf_app->view_dispatcher, uhf_app->gui, ViewDispatcherTypeFullscreen);
  48. // Variable Item List
  49. uhf_app->variable_item_list = variable_item_list_alloc();
  50. //worker
  51. uhf_app->worker = uhf_worker_alloc();
  52. // device
  53. uhf_app->uhf_device = uhf_device_alloc();
  54. UHFTagWrapper* uhf_tag_wrapper = uhf_tag_wrapper_alloc();
  55. // // point tag object to worker
  56. uhf_app->worker->uhf_tag_wrapper = uhf_tag_wrapper;
  57. uhf_app->uhf_device->uhf_tag_wrapper = uhf_tag_wrapper;
  58. // Open Notification record
  59. uhf_app->notifications = furi_record_open(RECORD_NOTIFICATION);
  60. // Variable Item List
  61. uhf_app->variable_item_list = variable_item_list_alloc();
  62. view_dispatcher_add_view(
  63. uhf_app->view_dispatcher,
  64. UHFViewVariableItemList,
  65. variable_item_list_get_view(uhf_app->variable_item_list));
  66. // Submenu
  67. uhf_app->submenu = submenu_alloc();
  68. view_dispatcher_add_view(
  69. uhf_app->view_dispatcher, UHFViewMenu, submenu_get_view(uhf_app->submenu));
  70. // Popup
  71. uhf_app->popup = popup_alloc();
  72. view_dispatcher_add_view(
  73. uhf_app->view_dispatcher, UHFViewPopup, popup_get_view(uhf_app->popup));
  74. // Loading
  75. uhf_app->loading = loading_alloc();
  76. view_dispatcher_add_view(
  77. uhf_app->view_dispatcher, UHFViewLoading, loading_get_view(uhf_app->loading));
  78. // Text Input
  79. uhf_app->text_input = text_input_alloc();
  80. view_dispatcher_add_view(
  81. uhf_app->view_dispatcher, UHFViewTextInput, text_input_get_view(uhf_app->text_input));
  82. // Custom Widget
  83. uhf_app->widget = widget_alloc();
  84. view_dispatcher_add_view(
  85. uhf_app->view_dispatcher, UHFViewWidget, widget_get_view(uhf_app->widget));
  86. return uhf_app;
  87. }
  88. void uhf_free(UHFApp* uhf_app) {
  89. furi_assert(uhf_app);
  90. // Variable Item List
  91. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewVariableItemList);
  92. variable_item_list_free(uhf_app->variable_item_list);
  93. // Submenu
  94. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewMenu);
  95. submenu_free(uhf_app->submenu);
  96. // Popup
  97. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewPopup);
  98. popup_free(uhf_app->popup);
  99. // Loading
  100. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewLoading);
  101. loading_free(uhf_app->loading);
  102. // TextInput
  103. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewTextInput);
  104. text_input_free(uhf_app->text_input);
  105. // Custom Widget
  106. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget);
  107. widget_free(uhf_app->widget);
  108. // Tag
  109. uhf_tag_wrapper_free(uhf_app->worker->uhf_tag_wrapper);
  110. // Worker
  111. uhf_worker_stop(uhf_app->worker);
  112. uhf_worker_free(uhf_app->worker);
  113. // Device
  114. uhf_device_free(uhf_app->uhf_device);
  115. // View Dispatcher
  116. view_dispatcher_free(uhf_app->view_dispatcher);
  117. // Scene Manager
  118. scene_manager_free(uhf_app->scene_manager);
  119. // GUI
  120. furi_record_close(RECORD_GUI);
  121. uhf_app->gui = NULL;
  122. // Notifications
  123. furi_record_close(RECORD_NOTIFICATION);
  124. uhf_app->notifications = NULL;
  125. free(uhf_app);
  126. }
  127. static const NotificationSequence uhf_sequence_blink_start_cyan = {
  128. &message_blink_start_10,
  129. &message_blink_set_color_cyan,
  130. &message_do_not_reset,
  131. NULL,
  132. };
  133. static const NotificationSequence uhf_sequence_blink_stop = {
  134. &message_blink_stop,
  135. NULL,
  136. };
  137. void uhf_blink_start(UHFApp* uhf_app) {
  138. notification_message(uhf_app->notifications, &uhf_sequence_blink_start_cyan);
  139. }
  140. void uhf_blink_stop(UHFApp* uhf_app) {
  141. notification_message(uhf_app->notifications, &uhf_sequence_blink_stop);
  142. }
  143. void uhf_show_loading_popup(void* ctx, bool show) {
  144. UHFApp* uhf_app = ctx;
  145. if(show) {
  146. // Raise timer priority so that animations can play
  147. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  148. view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewLoading);
  149. } else {
  150. // Restore default timer priority
  151. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  152. }
  153. }
  154. int32_t uhf_app_main(void* ctx) {
  155. UNUSED(ctx);
  156. // Disable expansion protocol to avoid interference with UART Handle
  157. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  158. expansion_disable(expansion);
  159. UHFApp* uhf_app = uhf_alloc();
  160. // enable 5v pin
  161. uint8_t attempts = 0;
  162. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  163. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  164. furi_hal_power_enable_otg();
  165. furi_delay_ms(10);
  166. }
  167. // enter app
  168. scene_manager_next_scene(uhf_app->scene_manager, UHFSceneModuleInfo);
  169. view_dispatcher_run(uhf_app->view_dispatcher);
  170. // disable 5v pin
  171. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  172. furi_hal_power_disable_otg();
  173. }
  174. // exit app
  175. uhf_free(uhf_app);
  176. // Return previous state of expansion
  177. expansion_enable(expansion);
  178. furi_record_close(RECORD_EXPANSION);
  179. return 0;
  180. }