uart_terminal_scene_console_output.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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("75", app->selected_tx_string, strlen("75")) && app->BAUDRATE != 75) {
  29. uart_terminal_uart_free(app->uart);
  30. app->BAUDRATE = 75;
  31. app->uart = uart_terminal_uart_init(app);
  32. }
  33. if(0 == strncmp("110", app->selected_tx_string, strlen("110")) && app->BAUDRATE != 110) {
  34. uart_terminal_uart_free(app->uart);
  35. app->BAUDRATE = 110;
  36. app->uart = uart_terminal_uart_init(app);
  37. }
  38. if(0 == strncmp("150", app->selected_tx_string, strlen("150")) && app->BAUDRATE != 150) {
  39. uart_terminal_uart_free(app->uart);
  40. app->BAUDRATE = 150;
  41. app->uart = uart_terminal_uart_init(app);
  42. }
  43. if(0 == strncmp("300", app->selected_tx_string, strlen("300")) && app->BAUDRATE != 300) {
  44. uart_terminal_uart_free(app->uart);
  45. app->BAUDRATE = 300;
  46. app->uart = uart_terminal_uart_init(app);
  47. }
  48. if(0 == strncmp("600", app->selected_tx_string, strlen("600")) && app->BAUDRATE != 600) {
  49. uart_terminal_uart_free(app->uart);
  50. app->BAUDRATE = 600;
  51. app->uart = uart_terminal_uart_init(app);
  52. }
  53. if(0 == strncmp("1200", app->selected_tx_string, strlen("1200")) && app->BAUDRATE != 1200) {
  54. uart_terminal_uart_free(app->uart);
  55. app->BAUDRATE = 1200;
  56. app->uart = uart_terminal_uart_init(app);
  57. }
  58. if(0 == strncmp("1800", app->selected_tx_string, strlen("1800")) && app->BAUDRATE != 1800) {
  59. uart_terminal_uart_free(app->uart);
  60. app->BAUDRATE = 1800;
  61. app->uart = uart_terminal_uart_init(app);
  62. }
  63. if(0 == strncmp("2400", app->selected_tx_string, strlen("2400")) && app->BAUDRATE != 2400) {
  64. uart_terminal_uart_free(app->uart);
  65. app->BAUDRATE = 2400;
  66. app->uart = uart_terminal_uart_init(app);
  67. }
  68. if(0 == strncmp("4800", app->selected_tx_string, strlen("4800")) && app->BAUDRATE != 4800) {
  69. uart_terminal_uart_free(app->uart);
  70. app->BAUDRATE = 4800;
  71. app->uart = uart_terminal_uart_init(app);
  72. }
  73. if(0 == strncmp("7200", app->selected_tx_string, strlen("7200")) && app->BAUDRATE != 7200) {
  74. uart_terminal_uart_free(app->uart);
  75. app->BAUDRATE = 7200;
  76. app->uart = uart_terminal_uart_init(app);
  77. }
  78. if(0 == strncmp("9600", app->selected_tx_string, strlen("9600")) && app->BAUDRATE != 9600) {
  79. uart_terminal_uart_free(app->uart);
  80. app->BAUDRATE = 9600;
  81. app->uart = uart_terminal_uart_init(app);
  82. }
  83. if(0 == strncmp("14400", app->selected_tx_string, strlen("14400")) && app->BAUDRATE != 14400) {
  84. uart_terminal_uart_free(app->uart);
  85. app->BAUDRATE = 14400;
  86. app->uart = uart_terminal_uart_init(app);
  87. }
  88. if(0 == strncmp("19200", app->selected_tx_string, strlen("19200")) && app->BAUDRATE != 19200) {
  89. uart_terminal_uart_free(app->uart);
  90. app->BAUDRATE = 19200;
  91. app->uart = uart_terminal_uart_init(app);
  92. }
  93. if(0 == strncmp("38400", app->selected_tx_string, strlen("38400")) && app->BAUDRATE != 38400) {
  94. uart_terminal_uart_free(app->uart);
  95. app->BAUDRATE = 38400;
  96. app->uart = uart_terminal_uart_init(app);
  97. }
  98. if(0 == strncmp("56000", app->selected_tx_string, strlen("56000")) && app->BAUDRATE != 56000) {
  99. uart_terminal_uart_free(app->uart);
  100. app->BAUDRATE = 56000;
  101. app->uart = uart_terminal_uart_init(app);
  102. }
  103. if(0 == strncmp("57600", app->selected_tx_string, strlen("57600")) && app->BAUDRATE != 57600) {
  104. uart_terminal_uart_free(app->uart);
  105. app->BAUDRATE = 57600;
  106. app->uart = uart_terminal_uart_init(app);
  107. }
  108. if(0 == strncmp("76800", app->selected_tx_string, strlen("76800")) && app->BAUDRATE != 76800) {
  109. uart_terminal_uart_free(app->uart);
  110. app->BAUDRATE = 76800;
  111. app->uart = uart_terminal_uart_init(app);
  112. }
  113. if(0 == strncmp("115200", app->selected_tx_string, strlen("115200")) &&
  114. app->BAUDRATE != 115200) {
  115. uart_terminal_uart_free(app->uart);
  116. app->BAUDRATE = 115200;
  117. app->uart = uart_terminal_uart_init(app);
  118. }
  119. if(0 == strncmp("128000", app->selected_tx_string, strlen("128000")) &&
  120. app->BAUDRATE != 128000) {
  121. uart_terminal_uart_free(app->uart);
  122. app->BAUDRATE = 128000;
  123. app->uart = uart_terminal_uart_init(app);
  124. }
  125. if(0 == strncmp("230400", app->selected_tx_string, strlen("230400")) &&
  126. app->BAUDRATE != 230400) {
  127. uart_terminal_uart_free(app->uart);
  128. app->BAUDRATE = 230400;
  129. app->uart = uart_terminal_uart_init(app);
  130. }
  131. if(0 == strncmp("250000", app->selected_tx_string, strlen("250000")) &&
  132. app->BAUDRATE != 250000) {
  133. uart_terminal_uart_free(app->uart);
  134. app->BAUDRATE = 250000;
  135. app->uart = uart_terminal_uart_init(app);
  136. }
  137. if(0 == strncmp("256000", app->selected_tx_string, strlen("256000")) &&
  138. app->BAUDRATE != 256000) {
  139. uart_terminal_uart_free(app->uart);
  140. app->BAUDRATE = 256000;
  141. app->uart = uart_terminal_uart_init(app);
  142. }
  143. if(0 == strncmp("460800", app->selected_tx_string, strlen("460800")) &&
  144. app->BAUDRATE != 460800) {
  145. uart_terminal_uart_free(app->uart);
  146. app->BAUDRATE = 460800;
  147. app->uart = uart_terminal_uart_init(app);
  148. }
  149. if(0 == strncmp("921600", app->selected_tx_string, strlen("921600")) &&
  150. app->BAUDRATE != 921600) {
  151. uart_terminal_uart_free(app->uart);
  152. app->BAUDRATE = 921600;
  153. app->uart = uart_terminal_uart_init(app);
  154. }
  155. /////////////////////////////////////////////////////////////////////////////////////////////////////
  156. if(app->is_command) {
  157. furi_string_reset(app->text_box_store);
  158. app->text_box_store_strlen = 0;
  159. if(0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  160. const char* help_msg =
  161. "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";
  162. furi_string_cat_str(app->text_box_store, help_msg);
  163. app->text_box_store_strlen += strlen(help_msg);
  164. }
  165. if(app->show_stopscan_tip) {
  166. const char* help_msg = "Press BACK to return\n";
  167. furi_string_cat_str(app->text_box_store, help_msg);
  168. app->text_box_store_strlen += strlen(help_msg);
  169. }
  170. }
  171. // Set starting text - for "View Log", this will just be what was already in the text box store
  172. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  173. scene_manager_set_scene_state(app->scene_manager, UART_TerminalSceneConsoleOutput, 0);
  174. view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewConsoleOutput);
  175. // Register callback to receive data
  176. uart_terminal_uart_set_handle_rx_data_cb(
  177. app->uart, uart_terminal_console_output_handle_rx_data_cb); // setup callback for rx thread
  178. // Send command with CR+LF or newline '\n'
  179. if(app->is_command && app->selected_tx_string) {
  180. if(app->TERMINAL_MODE == 1) {
  181. uart_terminal_uart_tx(
  182. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  183. uart_terminal_uart_tx((uint8_t*)("\r\n"), 2);
  184. } else {
  185. uart_terminal_uart_tx(
  186. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  187. uart_terminal_uart_tx((uint8_t*)("\n"), 1);
  188. }
  189. }
  190. }
  191. bool uart_terminal_scene_console_output_on_event(void* context, SceneManagerEvent event) {
  192. UART_TerminalApp* app = context;
  193. bool consumed = false;
  194. if(event.type == SceneManagerEventTypeCustom) {
  195. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  196. consumed = true;
  197. } else if(event.type == SceneManagerEventTypeTick) {
  198. consumed = true;
  199. }
  200. return consumed;
  201. }
  202. void uart_terminal_scene_console_output_on_exit(void* context) {
  203. UART_TerminalApp* app = context;
  204. // Unregister rx callback
  205. uart_terminal_uart_set_handle_rx_data_cb(app->uart, NULL);
  206. }