uart_terminal_scene_console_output.c 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if(app->is_command) {
  28. furi_string_reset(app->text_box_store);
  29. app->text_box_store_strlen = 0;
  30. // app->show_stopscan_tip in the if is just a hack to get the help displayed since there is no commands in this app
  31. if(app->show_stopscan_tip ||
  32. 0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  33. const char* help_msg =
  34. "Morse Flasher for\nMayhem Fin\n\nBased on UART terminal by\ncool4uma, which is a\nmodified WiFi Marauder\ncompanion by 0xchocolate\n\n";
  35. furi_string_cat_str(app->text_box_store, help_msg);
  36. app->text_box_store_strlen += strlen(help_msg);
  37. }
  38. if(app->show_stopscan_tip) {
  39. const char* help_msg = "Press BACK to return\n";
  40. furi_string_cat_str(app->text_box_store, help_msg);
  41. app->text_box_store_strlen += strlen(help_msg);
  42. }
  43. }
  44. // Set starting text - for "View Log", this will just be what was already in the text box store
  45. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  46. scene_manager_set_scene_state(app->scene_manager, UART_TerminalSceneConsoleOutput, 0);
  47. view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewConsoleOutput);
  48. // Register callback to receive data
  49. uart_terminal_uart_set_handle_rx_data_cb(
  50. app->uart, uart_terminal_console_output_handle_rx_data_cb); // setup callback for rx thread
  51. // Send command with newline '\n'
  52. /*if(!app->is_command && app->selected_tx_string)*/ {
  53. uart_terminal_uart_tx(
  54. app->uart, (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  55. uart_terminal_uart_tx(app->uart, (uint8_t*)("\n"), 1);
  56. }
  57. }
  58. bool uart_terminal_scene_console_output_on_event(void* context, SceneManagerEvent event) {
  59. UART_TerminalApp* app = context;
  60. bool consumed = false;
  61. if(event.type == SceneManagerEventTypeCustom) {
  62. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  63. consumed = true;
  64. } else if(event.type == SceneManagerEventTypeTick) {
  65. consumed = true;
  66. }
  67. return consumed;
  68. }
  69. void uart_terminal_scene_console_output_on_exit(void* context) {
  70. UART_TerminalApp* app = context;
  71. // Unregister rx callback
  72. uart_terminal_uart_set_handle_rx_data_cb(app->uart, NULL);
  73. // Automatically logut when exiting view
  74. //if(app->is_command) {
  75. // uart_terminal_uart_tx(app->uart,(uint8_t*)("exit\n"), strlen("exit\n"));
  76. //}
  77. }