uhf_app.c 6.3 KB

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