uart_terminal_scene_console_output.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "../uart_terminal_app_i.h"
  2. void uart_terminal_console_output_handle_rx_data_cb(uint8_t* buf, size_t len, void* context) {
  3. furi_assert(context);
  4. UART_TerminalApp* app = context;
  5. // If text box store gets too big, then truncate it
  6. app->text_box_store_strlen += len;
  7. if(app->text_box_store_strlen >= UART_TERMINAL_TEXT_BOX_STORE_SIZE - 1) {
  8. furi_string_right(app->text_box_store, app->text_box_store_strlen / 2);
  9. app->text_box_store_strlen = furi_string_size(app->text_box_store) + len;
  10. }
  11. // Null-terminate buf and append to text box store
  12. buf[len] = '\0';
  13. furi_string_cat_printf(app->text_box_store, "%s", buf);
  14. view_dispatcher_send_custom_event(
  15. app->view_dispatcher, UART_TerminalEventRefreshConsoleOutput);
  16. }
  17. void uart_terminal_scene_console_output_on_enter(void* context) {
  18. UART_TerminalApp* app = context;
  19. TextBox* text_box = app->text_box;
  20. text_box_reset(app->text_box);
  21. text_box_set_font(text_box, TextBoxFontText);
  22. if(app->focus_console_start) {
  23. text_box_set_focus(text_box, TextBoxFocusStart);
  24. } else {
  25. text_box_set_focus(text_box, TextBoxFocusEnd);
  26. }
  27. //Change baudrate ///////////////////////////////////////////////////////////////////////////
  28. if(0 == strncmp("2400", app->selected_tx_string, strlen("2400")) && app->BAUDRATE != 2400) {
  29. uart_terminal_uart_free(app->uart);
  30. app->BAUDRATE = 2400;
  31. app->uart = uart_terminal_uart_init(app);
  32. }
  33. if(0 == strncmp("9600", app->selected_tx_string, strlen("9600")) && app->BAUDRATE != 9600) {
  34. uart_terminal_uart_free(app->uart);
  35. app->BAUDRATE = 9600;
  36. app->uart = uart_terminal_uart_init(app);
  37. }
  38. if(0 == strncmp("19200", app->selected_tx_string, strlen("19200")) && app->BAUDRATE != 19200) {
  39. uart_terminal_uart_free(app->uart);
  40. app->BAUDRATE = 19200;
  41. app->uart = uart_terminal_uart_init(app);
  42. }
  43. if(0 == strncmp("38400", app->selected_tx_string, strlen("38400")) && app->BAUDRATE != 38400) {
  44. uart_terminal_uart_free(app->uart);
  45. app->BAUDRATE = 38400;
  46. app->uart = uart_terminal_uart_init(app);
  47. }
  48. if(0 == strncmp("57600", app->selected_tx_string, strlen("57600")) && app->BAUDRATE != 57600) {
  49. uart_terminal_uart_free(app->uart);
  50. app->BAUDRATE = 57600;
  51. app->uart = uart_terminal_uart_init(app);
  52. }
  53. if(0 == strncmp("115200", app->selected_tx_string, strlen("115200")) &&
  54. app->BAUDRATE != 115200) {
  55. uart_terminal_uart_free(app->uart);
  56. app->BAUDRATE = 115200;
  57. app->uart = uart_terminal_uart_init(app);
  58. }
  59. if(0 == strncmp("230400", app->selected_tx_string, strlen("230400")) &&
  60. app->BAUDRATE != 230400) {
  61. uart_terminal_uart_free(app->uart);
  62. app->BAUDRATE = 230400;
  63. app->uart = uart_terminal_uart_init(app);
  64. }
  65. if(0 == strncmp("460800", app->selected_tx_string, strlen("460800")) &&
  66. app->BAUDRATE != 460800) {
  67. uart_terminal_uart_free(app->uart);
  68. app->BAUDRATE = 460800;
  69. app->uart = uart_terminal_uart_init(app);
  70. }
  71. if(0 == strncmp("921600", app->selected_tx_string, strlen("921600")) &&
  72. app->BAUDRATE != 921600) {
  73. uart_terminal_uart_free(app->uart);
  74. app->BAUDRATE = 921600;
  75. app->uart = uart_terminal_uart_init(app);
  76. }
  77. /////////////////////////////////////////////////////////////////////////////////////////////////////
  78. if(app->is_command) {
  79. furi_string_reset(app->text_box_store);
  80. app->text_box_store_strlen = 0;
  81. if(0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  82. const char* help_msg =
  83. "UART terminal for Flipper\n\nI'm in github: cool4uma\n\nThis app is a modified\nWiFi Marauder companion,\nThanks 0xchocolate(github)\nfor great code and app.\n\n";
  84. furi_string_cat_str(app->text_box_store, help_msg);
  85. app->text_box_store_strlen += strlen(help_msg);
  86. }
  87. if(app->show_stopscan_tip) {
  88. const char* help_msg = "Press BACK to return\n";
  89. furi_string_cat_str(app->text_box_store, help_msg);
  90. app->text_box_store_strlen += strlen(help_msg);
  91. }
  92. }
  93. // Set starting text - for "View Log", this will just be what was already in the text box store
  94. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  95. scene_manager_set_scene_state(app->scene_manager, UART_TerminalSceneConsoleOutput, 0);
  96. view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewConsoleOutput);
  97. // Register callback to receive data
  98. uart_terminal_uart_set_handle_rx_data_cb(
  99. app->uart, uart_terminal_console_output_handle_rx_data_cb); // setup callback for rx thread
  100. // Send command with CR+LF or newline '\n'
  101. if(app->is_command && app->selected_tx_string) {
  102. if(app->TERMINAL_MODE == 1){
  103. // char buffer[240];
  104. // snprintf(buffer, 240, "%s\r\n", (app->selected_tx_string));
  105. // uart_terminal_uart_tx((unsigned char *)buffer, strlen(buffer));
  106. uart_terminal_uart_tx(
  107. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  108. uart_terminal_uart_tx((uint8_t*)("\r"), 1);
  109. uart_terminal_uart_tx((uint8_t*)("\n"), 1);
  110. } else {
  111. uart_terminal_uart_tx(
  112. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  113. uart_terminal_uart_tx((uint8_t*)("\n"), 1);
  114. }
  115. }
  116. }
  117. bool uart_terminal_scene_console_output_on_event(void* context, SceneManagerEvent event) {
  118. UART_TerminalApp* app = context;
  119. bool consumed = false;
  120. if(event.type == SceneManagerEventTypeCustom) {
  121. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  122. consumed = true;
  123. } else if(event.type == SceneManagerEventTypeTick) {
  124. consumed = true;
  125. }
  126. return consumed;
  127. }
  128. void uart_terminal_scene_console_output_on_exit(void* context) {
  129. UART_TerminalApp* app = context;
  130. // Unregister rx callback
  131. uart_terminal_uart_set_handle_rx_data_cb(app->uart, NULL);
  132. // Automatically logut when exiting view
  133. //if(app->is_command) {
  134. // uart_terminal_uart_tx((uint8_t*)("exit\n"), strlen("exit\n"));
  135. //}
  136. }