uart_terminal_scene_console_output.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. if(0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  31. const char* help_msg =
  32. "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";
  33. furi_string_cat_str(app->text_box_store, help_msg);
  34. app->text_box_store_strlen += strlen(help_msg);
  35. }
  36. if(app->show_stopscan_tip) {
  37. const char* help_msg = "Press BACK to return\n";
  38. furi_string_cat_str(app->text_box_store, help_msg);
  39. app->text_box_store_strlen += strlen(help_msg);
  40. }
  41. }
  42. // Set starting text - for "View Log", this will just be what was already in the text box store
  43. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  44. scene_manager_set_scene_state(app->scene_manager, UART_TerminalSceneConsoleOutput, 0);
  45. view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewConsoleOutput);
  46. // Register callback to receive data
  47. uart_terminal_uart_set_handle_rx_data_cb(
  48. app->uart, uart_terminal_console_output_handle_rx_data_cb); // setup callback for rx thread
  49. // Send command with newline '\n'
  50. if(app->is_command && app->selected_tx_string) {
  51. uart_terminal_uart_tx(
  52. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  53. uart_terminal_uart_tx((uint8_t*)("\n"), 1);
  54. }
  55. }
  56. bool uart_terminal_scene_console_output_on_event(void* context, SceneManagerEvent event) {
  57. UART_TerminalApp* app = context;
  58. bool consumed = false;
  59. if(event.type == SceneManagerEventTypeCustom) {
  60. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  61. consumed = true;
  62. } else if(event.type == SceneManagerEventTypeTick) {
  63. consumed = true;
  64. }
  65. return consumed;
  66. }
  67. void uart_terminal_scene_console_output_on_exit(void* context) {
  68. UART_TerminalApp* app = context;
  69. // Unregister rx callback
  70. uart_terminal_uart_set_handle_rx_data_cb(app->uart, NULL);
  71. // Automatically stop the scan when exiting view
  72. if(app->is_command) {
  73. uart_terminal_uart_tx((uint8_t*)("stopscan\n"), strlen("stopscan\n"));
  74. }
  75. }