uart_terminal_app.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "uart_terminal_app_i.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <expansion/expansion.h>
  5. static bool uart_terminal_app_custom_event_callback(void* context, uint32_t event) {
  6. furi_assert(context);
  7. UART_TerminalApp* app = context;
  8. return scene_manager_handle_custom_event(app->scene_manager, event);
  9. }
  10. static bool uart_terminal_app_back_event_callback(void* context) {
  11. furi_assert(context);
  12. UART_TerminalApp* app = context;
  13. return scene_manager_handle_back_event(app->scene_manager);
  14. }
  15. static void uart_terminal_app_tick_event_callback(void* context) {
  16. furi_assert(context);
  17. UART_TerminalApp* app = context;
  18. scene_manager_handle_tick_event(app->scene_manager);
  19. }
  20. UART_TerminalApp* uart_terminal_app_alloc() {
  21. UART_TerminalApp* app = malloc(sizeof(UART_TerminalApp));
  22. app->gui = furi_record_open(RECORD_GUI);
  23. app->view_dispatcher = view_dispatcher_alloc();
  24. app->scene_manager = scene_manager_alloc(&uart_terminal_scene_handlers, app);
  25. view_dispatcher_enable_queue(app->view_dispatcher);
  26. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  27. view_dispatcher_set_custom_event_callback(
  28. app->view_dispatcher, uart_terminal_app_custom_event_callback);
  29. view_dispatcher_set_navigation_event_callback(
  30. app->view_dispatcher, uart_terminal_app_back_event_callback);
  31. view_dispatcher_set_tick_event_callback(
  32. app->view_dispatcher, uart_terminal_app_tick_event_callback, 100);
  33. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  34. app->var_item_list = variable_item_list_alloc();
  35. view_dispatcher_add_view(
  36. app->view_dispatcher,
  37. UART_TerminalAppViewVarItemList,
  38. variable_item_list_get_view(app->var_item_list));
  39. for(int i = 0; i < START_MENU_ITEMS; ++i) {
  40. app->selected_option_index[i] = 0;
  41. }
  42. for(int i = 0; i < SETUP_MENU_ITEMS; ++i) {
  43. app->setup_selected_option_index[i] = 0;
  44. }
  45. app->widget = widget_alloc();
  46. view_dispatcher_add_view(
  47. app->view_dispatcher, UART_TerminalAppViewHelp, widget_get_view(app->widget));
  48. app->text_box = text_box_alloc();
  49. view_dispatcher_add_view(
  50. app->view_dispatcher, UART_TerminalAppViewConsoleOutput, text_box_get_view(app->text_box));
  51. app->text_box_store = furi_string_alloc();
  52. furi_string_reserve(app->text_box_store, UART_TERMINAL_TEXT_BOX_STORE_SIZE);
  53. app->text_input = text_input_alloc();
  54. view_dispatcher_add_view(
  55. app->view_dispatcher, UART_TerminalAppViewTextInput, text_input_get_view(app->text_input));
  56. app->hex_input = uart_hex_input_alloc();
  57. view_dispatcher_add_view(
  58. app->view_dispatcher,
  59. UART_TerminalAppViewHexInput,
  60. uart_hex_input_get_view(app->hex_input));
  61. app->setup_selected_option_index[BAUDRATE_ITEM_IDX] = DEFAULT_BAUDRATE_OPT_IDX;
  62. scene_manager_next_scene(app->scene_manager, UART_TerminalSceneStart);
  63. return app;
  64. }
  65. void uart_terminal_app_free(UART_TerminalApp* app) {
  66. furi_assert(app);
  67. // Views
  68. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewVarItemList);
  69. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewHelp);
  70. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewConsoleOutput);
  71. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewTextInput);
  72. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewHexInput);
  73. variable_item_list_free(app->var_item_list);
  74. widget_free(app->widget);
  75. text_box_free(app->text_box);
  76. furi_string_free(app->text_box_store);
  77. text_input_free(app->text_input);
  78. uart_hex_input_free(app->hex_input);
  79. // View dispatcher
  80. view_dispatcher_free(app->view_dispatcher);
  81. scene_manager_free(app->scene_manager);
  82. uart_terminal_uart_free(app->uart);
  83. // Close records
  84. furi_record_close(RECORD_GUI);
  85. free(app);
  86. }
  87. int32_t uart_terminal_app(void* p) {
  88. UNUSED(p);
  89. // Disable expansion protocol to avoid interference with UART Handle
  90. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  91. expansion_disable(expansion);
  92. UART_TerminalApp* uart_terminal_app = uart_terminal_app_alloc();
  93. uart_terminal_app->uart = uart_terminal_uart_init(uart_terminal_app);
  94. view_dispatcher_run(uart_terminal_app->view_dispatcher);
  95. uart_terminal_app_free(uart_terminal_app);
  96. // Return previous state of expansion
  97. expansion_enable(expansion);
  98. furi_record_close(RECORD_EXPANSION);
  99. return 0;
  100. }