infrared_app_view_manager.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <gui/modules/button_menu.h>
  2. #include <gui/view_stack.h>
  3. #include <gui/modules/loading.h>
  4. #include <gui/modules/button_panel.h>
  5. #include <gui/modules/dialog_ex.h>
  6. #include <furi.h>
  7. #include <callback-connector.h>
  8. #include "infrared/infrared_app_view_manager.h"
  9. #include "infrared/view/infrared_progress_view.h"
  10. #include "infrared_app.h"
  11. #include "infrared/infrared_app_event.h"
  12. InfraredAppViewManager::InfraredAppViewManager() {
  13. event_queue = osMessageQueueNew(10, sizeof(InfraredAppEvent), NULL);
  14. view_dispatcher = view_dispatcher_alloc();
  15. auto callback = cbc::obtain_connector(this, &InfraredAppViewManager::previous_view_callback);
  16. gui = static_cast<Gui*>(furi_record_open("gui"));
  17. view_dispatcher_attach_to_gui(view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  18. button_menu = button_menu_alloc();
  19. submenu = submenu_alloc();
  20. popup = popup_alloc();
  21. dialog_ex = dialog_ex_alloc();
  22. text_input = text_input_alloc();
  23. button_panel = button_panel_alloc();
  24. progress_view = infrared_progress_view_alloc();
  25. loading_view = loading_alloc();
  26. universal_view_stack = view_stack_alloc();
  27. view_stack_add_view(universal_view_stack, button_panel_get_view(button_panel));
  28. view_set_orientation(view_stack_get_view(universal_view_stack), ViewOrientationVertical);
  29. add_view(ViewId::UniversalRemote, view_stack_get_view(universal_view_stack));
  30. add_view(ViewId::ButtonMenu, button_menu_get_view(button_menu));
  31. add_view(ViewId::Submenu, submenu_get_view(submenu));
  32. add_view(ViewId::Popup, popup_get_view(popup));
  33. add_view(ViewId::DialogEx, dialog_ex_get_view(dialog_ex));
  34. add_view(ViewId::TextInput, text_input_get_view(text_input));
  35. view_set_previous_callback(view_stack_get_view(universal_view_stack), callback);
  36. view_set_previous_callback(button_menu_get_view(button_menu), callback);
  37. view_set_previous_callback(submenu_get_view(submenu), callback);
  38. view_set_previous_callback(popup_get_view(popup), callback);
  39. view_set_previous_callback(dialog_ex_get_view(dialog_ex), callback);
  40. view_set_previous_callback(text_input_get_view(text_input), callback);
  41. }
  42. InfraredAppViewManager::~InfraredAppViewManager() {
  43. view_dispatcher_remove_view(
  44. view_dispatcher, static_cast<uint32_t>(InfraredAppViewManager::ViewId::UniversalRemote));
  45. view_dispatcher_remove_view(
  46. view_dispatcher, static_cast<uint32_t>(InfraredAppViewManager::ViewId::ButtonMenu));
  47. view_dispatcher_remove_view(
  48. view_dispatcher, static_cast<uint32_t>(InfraredAppViewManager::ViewId::TextInput));
  49. view_dispatcher_remove_view(
  50. view_dispatcher, static_cast<uint32_t>(InfraredAppViewManager::ViewId::DialogEx));
  51. view_dispatcher_remove_view(
  52. view_dispatcher, static_cast<uint32_t>(InfraredAppViewManager::ViewId::Submenu));
  53. view_dispatcher_remove_view(
  54. view_dispatcher, static_cast<uint32_t>(InfraredAppViewManager::ViewId::Popup));
  55. view_stack_remove_view(universal_view_stack, button_panel_get_view(button_panel));
  56. view_stack_free(universal_view_stack);
  57. button_panel_free(button_panel);
  58. submenu_free(submenu);
  59. popup_free(popup);
  60. button_menu_free(button_menu);
  61. dialog_ex_free(dialog_ex);
  62. text_input_free(text_input);
  63. infrared_progress_view_free(progress_view);
  64. loading_free(loading_view);
  65. view_dispatcher_free(view_dispatcher);
  66. furi_record_close("gui");
  67. osMessageQueueDelete(event_queue);
  68. }
  69. void InfraredAppViewManager::switch_to(ViewId type) {
  70. view_dispatcher_switch_to_view(view_dispatcher, static_cast<uint32_t>(type));
  71. }
  72. TextInput* InfraredAppViewManager::get_text_input() {
  73. return text_input;
  74. }
  75. DialogEx* InfraredAppViewManager::get_dialog_ex() {
  76. return dialog_ex;
  77. }
  78. Submenu* InfraredAppViewManager::get_submenu() {
  79. return submenu;
  80. }
  81. Popup* InfraredAppViewManager::get_popup() {
  82. return popup;
  83. }
  84. ButtonMenu* InfraredAppViewManager::get_button_menu() {
  85. return button_menu;
  86. }
  87. ButtonPanel* InfraredAppViewManager::get_button_panel() {
  88. return button_panel;
  89. }
  90. InfraredProgressView* InfraredAppViewManager::get_progress() {
  91. return progress_view;
  92. }
  93. Loading* InfraredAppViewManager::get_loading() {
  94. return loading_view;
  95. }
  96. ViewStack* InfraredAppViewManager::get_universal_view_stack() {
  97. return universal_view_stack;
  98. }
  99. osMessageQueueId_t InfraredAppViewManager::get_event_queue() {
  100. return event_queue;
  101. }
  102. void InfraredAppViewManager::clear_events() {
  103. InfraredAppEvent event;
  104. while(osMessageQueueGet(event_queue, &event, NULL, 0) == osOK)
  105. ;
  106. }
  107. void InfraredAppViewManager::receive_event(InfraredAppEvent* event) {
  108. if(osMessageQueueGet(event_queue, event, NULL, 100) != osOK) {
  109. event->type = InfraredAppEvent::Type::Tick;
  110. }
  111. }
  112. void InfraredAppViewManager::send_event(InfraredAppEvent* event) {
  113. uint32_t timeout = 0;
  114. /* Rapid button hammering on signal send scenes causes queue overflow - ignore it,
  115. * but try to keep button release event - it switches off INFRARED DMA sending. */
  116. if(event->type == InfraredAppEvent::Type::MenuSelectedRelease) {
  117. timeout = 200;
  118. }
  119. if((event->type == InfraredAppEvent::Type::DialogExSelected) &&
  120. (event->payload.dialog_ex_result == DialogExReleaseCenter)) {
  121. timeout = 200;
  122. }
  123. osMessageQueuePut(event_queue, event, 0, timeout);
  124. }
  125. uint32_t InfraredAppViewManager::previous_view_callback(void* context) {
  126. if(event_queue != NULL) {
  127. InfraredAppEvent event;
  128. event.type = InfraredAppEvent::Type::Back;
  129. send_event(&event);
  130. }
  131. return VIEW_IGNORE;
  132. }
  133. void InfraredAppViewManager::add_view(ViewId view_type, View* view) {
  134. view_dispatcher_add_view(view_dispatcher, static_cast<uint32_t>(view_type), view);
  135. }