uhf_app.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // Submenu
  91. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewMenu);
  92. submenu_free(uhf_app->submenu);
  93. // Popup
  94. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewPopup);
  95. popup_free(uhf_app->popup);
  96. // Loading
  97. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewLoading);
  98. loading_free(uhf_app->loading);
  99. // TextInput
  100. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewTextInput);
  101. text_input_free(uhf_app->text_input);
  102. // Custom Widget
  103. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget);
  104. widget_free(uhf_app->widget);
  105. // Tag
  106. uhf_tag_wrapper_free(uhf_app->worker->uhf_tag_wrapper);
  107. // Worker
  108. uhf_worker_stop(uhf_app->worker);
  109. uhf_worker_free(uhf_app->worker);
  110. // Device
  111. uhf_device_free(uhf_app->uhf_device);
  112. // View Dispatcher
  113. view_dispatcher_free(uhf_app->view_dispatcher);
  114. // Scene Manager
  115. scene_manager_free(uhf_app->scene_manager);
  116. // GUI
  117. furi_record_close(RECORD_GUI);
  118. uhf_app->gui = NULL;
  119. // Variable Item List
  120. variable_item_list_free(uhf_app->variable_item_list);
  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. // Disable expansion protocol to avoid interference with UART Handle
  156. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  157. expansion_disable(expansion);
  158. UHFApp* uhf_app = uhf_alloc();
  159. // enable 5v pin
  160. uint8_t attempts = 0;
  161. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  162. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  163. furi_hal_power_enable_otg();
  164. furi_delay_ms(10);
  165. }
  166. furi_delay_ms(200);
  167. // init pin a2
  168. // furi_hal_gpio_init_simple(&gpio_ext_pa7, GpioModeOutputPushPull);
  169. scene_manager_next_scene(uhf_app->scene_manager, UHFSceneModuleInfo);
  170. view_dispatcher_run(uhf_app->view_dispatcher);
  171. // disable 5v pin
  172. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  173. furi_hal_power_disable_otg();
  174. }
  175. // furi_hal_gpio_disable_int_callback()
  176. // exit app
  177. uhf_free(uhf_app);
  178. // Return previous state of expansion
  179. expansion_enable(expansion);
  180. furi_record_close(RECORD_EXPANSION);
  181. return 0;
  182. }