uhf_app.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. // Submenu
  58. uhf_app->submenu = submenu_alloc();
  59. view_dispatcher_add_view(
  60. uhf_app->view_dispatcher, UHFViewMenu, submenu_get_view(uhf_app->submenu));
  61. // Popup
  62. uhf_app->popup = popup_alloc();
  63. view_dispatcher_add_view(
  64. uhf_app->view_dispatcher, UHFViewPopup, popup_get_view(uhf_app->popup));
  65. // Loading
  66. uhf_app->loading = loading_alloc();
  67. view_dispatcher_add_view(
  68. uhf_app->view_dispatcher, UHFViewLoading, loading_get_view(uhf_app->loading));
  69. // Text Input
  70. uhf_app->text_input = text_input_alloc();
  71. view_dispatcher_add_view(
  72. uhf_app->view_dispatcher, UHFViewTextInput, text_input_get_view(uhf_app->text_input));
  73. // Custom Widget
  74. uhf_app->widget = widget_alloc();
  75. view_dispatcher_add_view(
  76. uhf_app->view_dispatcher, UHFViewWidget, widget_get_view(uhf_app->widget));
  77. return uhf_app;
  78. }
  79. void uhf_free(UHFApp* uhf_app) {
  80. furi_assert(uhf_app);
  81. // Submenu
  82. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewMenu);
  83. submenu_free(uhf_app->submenu);
  84. // Popup
  85. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewPopup);
  86. popup_free(uhf_app->popup);
  87. // Loading
  88. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewLoading);
  89. loading_free(uhf_app->loading);
  90. // TextInput
  91. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewTextInput);
  92. text_input_free(uhf_app->text_input);
  93. // Custom Widget
  94. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget);
  95. widget_free(uhf_app->widget);
  96. // Tag
  97. uhf_tag_wrapper_free(uhf_app->worker->uhf_tag_wrapper);
  98. // Worker
  99. uhf_worker_stop(uhf_app->worker);
  100. uhf_worker_free(uhf_app->worker);
  101. // Device
  102. uhf_device_free(uhf_app->uhf_device);
  103. // View Dispatcher
  104. view_dispatcher_free(uhf_app->view_dispatcher);
  105. // Scene Manager
  106. scene_manager_free(uhf_app->scene_manager);
  107. // GUI
  108. furi_record_close(RECORD_GUI);
  109. uhf_app->gui = NULL;
  110. // Notifications
  111. furi_record_close(RECORD_NOTIFICATION);
  112. uhf_app->notifications = NULL;
  113. free(uhf_app);
  114. }
  115. static const NotificationSequence uhf_sequence_blink_start_cyan = {
  116. &message_blink_start_10,
  117. &message_blink_set_color_cyan,
  118. &message_do_not_reset,
  119. NULL,
  120. };
  121. static const NotificationSequence uhf_sequence_blink_stop = {
  122. &message_blink_stop,
  123. NULL,
  124. };
  125. void uhf_blink_start(UHFApp* uhf_app) {
  126. notification_message(uhf_app->notifications, &uhf_sequence_blink_start_cyan);
  127. }
  128. void uhf_blink_stop(UHFApp* uhf_app) {
  129. notification_message(uhf_app->notifications, &uhf_sequence_blink_stop);
  130. }
  131. void uhf_show_loading_popup(void* ctx, bool show) {
  132. UHFApp* uhf_app = ctx;
  133. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  134. if(show) {
  135. // Raise timer priority so that animations can play
  136. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  137. view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewLoading);
  138. } else {
  139. // Restore default timer priority
  140. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  141. }
  142. }
  143. int32_t uhf_app_main(void* ctx) {
  144. UNUSED(ctx);
  145. UHFApp* uhf_app = uhf_alloc();
  146. // enable 5v pin
  147. furi_hal_power_enable_otg();
  148. // init pin a2
  149. // furi_hal_gpio_init_simple(&gpio_ext_pa7, GpioModeOutputPushPull);
  150. scene_manager_next_scene(uhf_app->scene_manager, UHFSceneVerify);
  151. view_dispatcher_run(uhf_app->view_dispatcher);
  152. // disable 5v pin
  153. furi_hal_power_disable_otg();
  154. // furi_hal_gpio_disable_int_callback()
  155. // exit app
  156. uhf_free(uhf_app);
  157. return 0;
  158. }