uart_terminal_app.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_set_event_callback_context(app->view_dispatcher, app);
  26. view_dispatcher_set_custom_event_callback(
  27. app->view_dispatcher, uart_terminal_app_custom_event_callback);
  28. view_dispatcher_set_navigation_event_callback(
  29. app->view_dispatcher, uart_terminal_app_back_event_callback);
  30. view_dispatcher_set_tick_event_callback(
  31. app->view_dispatcher, uart_terminal_app_tick_event_callback, 100);
  32. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  33. app->var_item_list = variable_item_list_alloc();
  34. view_dispatcher_add_view(
  35. app->view_dispatcher,
  36. UART_TerminalAppViewVarItemList,
  37. variable_item_list_get_view(app->var_item_list));
  38. for(int i = 0; i < START_MENU_ITEMS; ++i) {
  39. app->selected_option_index[i] = 0;
  40. }
  41. for(int i = 0; i < SETUP_MENU_ITEMS; ++i) {
  42. app->setup_selected_option_index[i] = 0;
  43. }
  44. app->widget = widget_alloc();
  45. view_dispatcher_add_view(
  46. app->view_dispatcher, UART_TerminalAppViewHelp, widget_get_view(app->widget));
  47. app->text_box = text_box_alloc();
  48. view_dispatcher_add_view(
  49. app->view_dispatcher, UART_TerminalAppViewConsoleOutput, text_box_get_view(app->text_box));
  50. app->text_box_store = furi_string_alloc();
  51. furi_string_reserve(app->text_box_store, UART_TERMINAL_TEXT_BOX_STORE_SIZE);
  52. app->text_input = text_input_alloc();
  53. view_dispatcher_add_view(
  54. app->view_dispatcher, UART_TerminalAppViewTextInput, text_input_get_view(app->text_input));
  55. app->hex_input = uart_hex_input_alloc();
  56. view_dispatcher_add_view(
  57. app->view_dispatcher,
  58. UART_TerminalAppViewHexInput,
  59. uart_hex_input_get_view(app->hex_input));
  60. app->setup_selected_option_index[BAUDRATE_ITEM_IDX] = DEFAULT_BAUDRATE_OPT_IDX;
  61. app->old_term_mode = 0;
  62. app->TERMINAL_MODE = 0;
  63. app->atmode_was_set = false;
  64. scene_manager_next_scene(app->scene_manager, UART_TerminalSceneStart);
  65. return app;
  66. }
  67. void uart_terminal_app_free(UART_TerminalApp* app) {
  68. furi_assert(app);
  69. // Views
  70. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewVarItemList);
  71. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewHelp);
  72. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewConsoleOutput);
  73. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewTextInput);
  74. view_dispatcher_remove_view(app->view_dispatcher, UART_TerminalAppViewHexInput);
  75. variable_item_list_free(app->var_item_list);
  76. widget_free(app->widget);
  77. text_box_free(app->text_box);
  78. furi_string_free(app->text_box_store);
  79. text_input_free(app->text_input);
  80. uart_hex_input_free(app->hex_input);
  81. // View dispatcher
  82. view_dispatcher_free(app->view_dispatcher);
  83. scene_manager_free(app->scene_manager);
  84. uart_terminal_uart_free(app->uart);
  85. // Close records
  86. furi_record_close(RECORD_GUI);
  87. free(app);
  88. }
  89. int32_t uart_terminal_app(void* p) {
  90. UNUSED(p);
  91. // Disable expansion protocol to avoid interference with UART Handle
  92. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  93. expansion_disable(expansion);
  94. UART_TerminalApp* uart_terminal_app = uart_terminal_app_alloc();
  95. uart_terminal_app->uart = uart_terminal_uart_init(uart_terminal_app);
  96. view_dispatcher_run(uart_terminal_app->view_dispatcher);
  97. uart_terminal_app_free(uart_terminal_app);
  98. // Return previous state of expansion
  99. expansion_enable(expansion);
  100. furi_record_close(RECORD_EXPANSION);
  101. return 0;
  102. }