uhf_app.c 5.7 KB

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