uhf_app.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "uhf_app_i.h"
  2. // empty callback
  3. void empty_rx_callback(UartIrqEvent event, uint8_t data, void* ctx) {
  4. UNUSED(event);
  5. UNUSED(data);
  6. UNUSED(ctx);
  7. }
  8. char* convertToHexString(const uint8_t* array, size_t length) {
  9. if(array == NULL || length == 0) {
  10. return NULL;
  11. }
  12. FuriString* temp_str = furi_string_alloc();
  13. for(size_t i = 0; i < length; i++) {
  14. furi_string_cat_printf(temp_str, "%02X ", array[i]);
  15. }
  16. const char* furi_str = furi_string_get_cstr(temp_str);
  17. size_t str_len = strlen(furi_str);
  18. char* str = (char*)malloc(sizeof(char) * str_len);
  19. memcpy(str, furi_str, str_len);
  20. furi_string_free(temp_str);
  21. return str;
  22. }
  23. bool uhf_custom_event_callback(void* ctx, uint32_t event) {
  24. furi_assert(ctx);
  25. UHFApp* uhf_app = ctx;
  26. return scene_manager_handle_custom_event(uhf_app->scene_manager, event);
  27. }
  28. bool uhf_back_event_callback(void* ctx) {
  29. furi_assert(ctx);
  30. UHFApp* uhf_app = ctx;
  31. return scene_manager_handle_back_event(uhf_app->scene_manager);
  32. }
  33. void uhf_tick_event_callback(void* ctx) {
  34. furi_assert(ctx);
  35. UHFApp* uhf_app = ctx;
  36. scene_manager_handle_tick_event(uhf_app->scene_manager);
  37. }
  38. UHFApp* uhf_alloc() {
  39. UHFApp* uhf_app = (UHFApp*)malloc(sizeof(UHFApp));
  40. uhf_app->view_dispatcher = view_dispatcher_alloc();
  41. uhf_app->scene_manager = scene_manager_alloc(&uhf_scene_handlers, uhf_app);
  42. view_dispatcher_enable_queue(uhf_app->view_dispatcher);
  43. view_dispatcher_set_event_callback_context(uhf_app->view_dispatcher, uhf_app);
  44. view_dispatcher_set_custom_event_callback(uhf_app->view_dispatcher, uhf_custom_event_callback);
  45. view_dispatcher_set_navigation_event_callback(
  46. uhf_app->view_dispatcher, uhf_back_event_callback);
  47. view_dispatcher_set_tick_event_callback(
  48. uhf_app->view_dispatcher, uhf_tick_event_callback, 100);
  49. // Open GUI record
  50. uhf_app->gui = furi_record_open(RECORD_GUI);
  51. view_dispatcher_attach_to_gui(
  52. uhf_app->view_dispatcher, uhf_app->gui, ViewDispatcherTypeFullscreen);
  53. //worker
  54. uhf_app->worker = uhf_worker_alloc();
  55. // device
  56. uhf_app->uhf_device = uhf_device_alloc();
  57. UHFTag* uhf_tag = uhf_tag_alloc();
  58. // point tag object to worker
  59. uhf_app->worker->uhf_tag = uhf_tag;
  60. uhf_app->uhf_device->uhf_tag = uhf_tag;
  61. // Open Notification record
  62. uhf_app->notifications = furi_record_open(RECORD_NOTIFICATION);
  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. // Worker
  103. uhf_worker_stop(uhf_app->worker);
  104. uhf_worker_free(uhf_app->worker);
  105. // Tag
  106. uhf_tag_free(uhf_app->worker->uhf_tag);
  107. // View Dispatcher
  108. view_dispatcher_free(uhf_app->view_dispatcher);
  109. // Scene Manager
  110. scene_manager_free(uhf_app->scene_manager);
  111. // GUI
  112. furi_record_close(RECORD_GUI);
  113. uhf_app->gui = NULL;
  114. // UHFDevice
  115. uhf_device_free(uhf_app->uhf_device);
  116. // Notifications
  117. furi_record_close(RECORD_NOTIFICATION);
  118. uhf_app->notifications = NULL;
  119. free(uhf_app);
  120. }
  121. static const NotificationSequence uhf_sequence_blink_start_cyan = {
  122. &message_blink_start_10,
  123. &message_blink_set_color_cyan,
  124. &message_do_not_reset,
  125. NULL,
  126. };
  127. static const NotificationSequence uhf_sequence_blink_stop = {
  128. &message_blink_stop,
  129. NULL,
  130. };
  131. void uhf_blink_start(UHFApp* uhf_app) {
  132. notification_message(uhf_app->notifications, &uhf_sequence_blink_start_cyan);
  133. }
  134. void uhf_blink_stop(UHFApp* uhf_app) {
  135. notification_message(uhf_app->notifications, &uhf_sequence_blink_stop);
  136. }
  137. void uhf_show_loading_popup(void* ctx, bool show) {
  138. UHFApp* uhf_app = ctx;
  139. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  140. if(show) {
  141. // Raise timer priority so that animations can play
  142. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  143. view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewLoading);
  144. } else {
  145. // Restore default timer priority
  146. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  147. }
  148. }
  149. int32_t uhf_app_main(void* ctx) {
  150. UNUSED(ctx);
  151. UHFApp* uhf_app = uhf_alloc();
  152. // enable 5v pin
  153. furi_hal_power_enable_otg();
  154. scene_manager_next_scene(uhf_app->scene_manager, UHFSceneVerify);
  155. view_dispatcher_run(uhf_app->view_dispatcher);
  156. // disable 5v pin
  157. furi_hal_power_disable_otg();
  158. // set uart callback to none
  159. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, empty_rx_callback, NULL);
  160. // exit app
  161. uhf_free(uhf_app);
  162. return 0;
  163. }