uhf_app.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // Each byte takes 3 characters in the hex representation (2 characters + space), plus 1 for the null terminator
  13. size_t hexLength = (length * 3) + 1;
  14. char* hexArray = (char*)malloc(hexLength * sizeof(char));
  15. if(hexArray == NULL) {
  16. return NULL;
  17. }
  18. size_t index = 0;
  19. for(size_t i = 0; i < length; i++) {
  20. index += snprintf(&hexArray[index], hexLength - index, "%02x ", array[i]);
  21. }
  22. hexArray[hexLength - 1] = '\0';
  23. return hexArray;
  24. }
  25. bool uhf_custom_event_callback(void* ctx, uint32_t event) {
  26. furi_assert(ctx);
  27. UHFApp* uhf_app = ctx;
  28. return scene_manager_handle_custom_event(uhf_app->scene_manager, event);
  29. }
  30. bool uhf_back_event_callback(void* ctx) {
  31. furi_assert(ctx);
  32. UHFApp* uhf_app = ctx;
  33. return scene_manager_handle_back_event(uhf_app->scene_manager);
  34. }
  35. void uhf_tick_event_callback(void* ctx) {
  36. furi_assert(ctx);
  37. UHFApp* uhf_app = ctx;
  38. scene_manager_handle_tick_event(uhf_app->scene_manager);
  39. }
  40. UHFApp* uhf_alloc() {
  41. UHFApp* uhf_app = (UHFApp*)malloc(sizeof(UHFApp));
  42. uhf_app->view_dispatcher = view_dispatcher_alloc();
  43. uhf_app->scene_manager = scene_manager_alloc(&uhf_scene_handlers, uhf_app);
  44. view_dispatcher_enable_queue(uhf_app->view_dispatcher);
  45. view_dispatcher_set_event_callback_context(uhf_app->view_dispatcher, uhf_app);
  46. view_dispatcher_set_custom_event_callback(uhf_app->view_dispatcher, uhf_custom_event_callback);
  47. view_dispatcher_set_navigation_event_callback(
  48. uhf_app->view_dispatcher, uhf_back_event_callback);
  49. view_dispatcher_set_tick_event_callback(
  50. uhf_app->view_dispatcher, uhf_tick_event_callback, 100);
  51. // Open GUI record
  52. uhf_app->gui = furi_record_open(RECORD_GUI);
  53. view_dispatcher_attach_to_gui(
  54. uhf_app->view_dispatcher, uhf_app->gui, ViewDispatcherTypeFullscreen);
  55. //worker
  56. uhf_app->worker = uhf_worker_alloc();
  57. // device
  58. uhf_app->uhf_device = uhf_device_alloc();
  59. uhf_app->uhf_device->dev_data = uhf_app->worker->response_data;
  60. // Open Notification record
  61. uhf_app->notifications = furi_record_open(RECORD_NOTIFICATION);
  62. // Submenu
  63. uhf_app->submenu = submenu_alloc();
  64. view_dispatcher_add_view(
  65. uhf_app->view_dispatcher, UHFViewMenu, submenu_get_view(uhf_app->submenu));
  66. // Popup
  67. uhf_app->popup = popup_alloc();
  68. view_dispatcher_add_view(
  69. uhf_app->view_dispatcher, UHFViewPopup, popup_get_view(uhf_app->popup));
  70. // Loading
  71. uhf_app->loading = loading_alloc();
  72. view_dispatcher_add_view(
  73. uhf_app->view_dispatcher, UHFViewLoading, loading_get_view(uhf_app->loading));
  74. // Text Input
  75. uhf_app->text_input = text_input_alloc();
  76. view_dispatcher_add_view(
  77. uhf_app->view_dispatcher, UHFViewTextInput, text_input_get_view(uhf_app->text_input));
  78. // Custom Widget
  79. uhf_app->widget = widget_alloc();
  80. view_dispatcher_add_view(
  81. uhf_app->view_dispatcher, UHFViewWidget, widget_get_view(uhf_app->widget));
  82. return uhf_app;
  83. }
  84. void uhf_free(UHFApp* uhf_app) {
  85. furi_assert(uhf_app);
  86. // Submenu
  87. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewMenu);
  88. submenu_free(uhf_app->submenu);
  89. // Popup
  90. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewPopup);
  91. popup_free(uhf_app->popup);
  92. // Loading
  93. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewLoading);
  94. loading_free(uhf_app->loading);
  95. // TextInput
  96. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewTextInput);
  97. text_input_free(uhf_app->text_input);
  98. // Custom Widget
  99. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget);
  100. widget_free(uhf_app->widget);
  101. // Worker
  102. uhf_worker_stop(uhf_app->worker);
  103. uhf_worker_free(uhf_app->worker);
  104. // View Dispatcher
  105. view_dispatcher_free(uhf_app->view_dispatcher);
  106. // Scene Manager
  107. scene_manager_free(uhf_app->scene_manager);
  108. // GUI
  109. furi_record_close(RECORD_GUI);
  110. uhf_app->gui = NULL;
  111. // UHFDevice
  112. uhf_device_free(uhf_app->uhf_device);
  113. // Notifications
  114. furi_record_close(RECORD_NOTIFICATION);
  115. uhf_app->notifications = NULL;
  116. free(uhf_app);
  117. }
  118. static const NotificationSequence uhf_sequence_blink_start_cyan = {
  119. &message_blink_start_10,
  120. &message_blink_set_color_cyan,
  121. &message_do_not_reset,
  122. NULL,
  123. };
  124. static const NotificationSequence uhf_sequence_blink_stop = {
  125. &message_blink_stop,
  126. NULL,
  127. };
  128. void uhf_blink_start(UHFApp* uhf_app) {
  129. notification_message(uhf_app->notifications, &uhf_sequence_blink_start_cyan);
  130. }
  131. void uhf_blink_stop(UHFApp* uhf_app) {
  132. notification_message(uhf_app->notifications, &uhf_sequence_blink_stop);
  133. }
  134. void uhf_show_loading_popup(void* ctx, bool show) {
  135. UHFApp* uhf_app = ctx;
  136. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  137. if(show) {
  138. // Raise timer priority so that animations can play
  139. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  140. view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewLoading);
  141. } else {
  142. // Restore default timer priority
  143. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  144. }
  145. }
  146. int32_t uhf_app_main(void* ctx) {
  147. UNUSED(ctx);
  148. UHFApp* uhf_app = uhf_alloc();
  149. // enable 5v pin
  150. furi_hal_power_enable_otg();
  151. scene_manager_next_scene(uhf_app->scene_manager, UHFSceneVerify);
  152. view_dispatcher_run(uhf_app->view_dispatcher);
  153. // disable 5v pin
  154. furi_hal_power_disable_otg();
  155. // set uart callback to none
  156. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, empty_rx_callback, NULL);
  157. // exit app
  158. uhf_free(uhf_app);
  159. return 0;
  160. }