uart_terminal_scene_console_output.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. FuriString* new_str = furi_string_alloc();
  6. if(app->hex_mode) {
  7. while(len--) {
  8. uint8_t byte = *(buf++);
  9. if(byte == '\0') break;
  10. furi_string_cat_printf(new_str, "%02X ", byte);
  11. }
  12. } else {
  13. buf[len] = '\0';
  14. furi_string_cat_printf(new_str, "%s", buf);
  15. }
  16. // If text box store gets too big, then truncate it
  17. app->text_box_store_strlen += furi_string_size(new_str);
  18. ;
  19. while(app->text_box_store_strlen >= UART_TERMINAL_TEXT_BOX_STORE_SIZE - 1) {
  20. furi_string_right(app->text_box_store, app->text_box_store_strlen / 2);
  21. app->text_box_store_strlen = furi_string_size(app->text_box_store) + len;
  22. }
  23. furi_string_cat(app->text_box_store, new_str);
  24. furi_string_free(new_str);
  25. view_dispatcher_send_custom_event(
  26. app->view_dispatcher, UART_TerminalEventRefreshConsoleOutput);
  27. }
  28. static uint8_t hex_char_to_byte(const char c) {
  29. if(c >= '0' && c <= '9') {
  30. return c - '0';
  31. }
  32. if(c >= 'A' && c <= 'F') {
  33. return c - 'A' + 10;
  34. }
  35. if(c >= 'a' && c <= 'f') {
  36. return c - 'a' + 10;
  37. }
  38. return 0;
  39. }
  40. void uart_terminal_scene_console_output_on_enter(void* context) {
  41. UART_TerminalApp* app = context;
  42. TextBox* text_box = app->text_box;
  43. text_box_reset(app->text_box);
  44. text_box_set_font(text_box, TextBoxFontText);
  45. text_box_set_focus(text_box, TextBoxFocusEnd);
  46. bool need_reinit = false;
  47. //Change baudrate
  48. if(app->BAUDRATE != app->NEW_BAUDRATE && app->NEW_BAUDRATE) {
  49. need_reinit = true;
  50. }
  51. //Change UART port
  52. if(app->uart_ch != app->new_uart_ch) {
  53. need_reinit = true;
  54. }
  55. if(need_reinit) {
  56. uart_terminal_uart_free(app->uart);
  57. app->BAUDRATE = app->NEW_BAUDRATE;
  58. app->uart_ch = app->new_uart_ch;
  59. app->uart = uart_terminal_uart_init(app);
  60. }
  61. if(app->is_command) {
  62. furi_string_reset(app->text_box_store);
  63. app->text_box_store_strlen = 0;
  64. }
  65. // Set starting text - for "View Log", this will just be what was already in the text box store
  66. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  67. scene_manager_set_scene_state(app->scene_manager, UART_TerminalSceneConsoleOutput, 0);
  68. view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewConsoleOutput);
  69. // Register callback to receive data
  70. uart_terminal_uart_set_handle_rx_data_cb(
  71. app->uart, uart_terminal_console_output_handle_rx_data_cb); // setup callback for rx thread
  72. if(app->hex_mode) {
  73. // Send binary packet
  74. if(app->selected_tx_string) {
  75. const char* str = app->selected_tx_string;
  76. uint8_t digit_num = 0;
  77. uint8_t byte = 0;
  78. while(*str) {
  79. byte |= (hex_char_to_byte(*str) << ((1 - digit_num) * 4));
  80. if(++digit_num == 2) {
  81. uart_terminal_uart_tx(app->uart, &byte, 1);
  82. digit_num = 0;
  83. byte = 0;
  84. }
  85. str++;
  86. }
  87. if(digit_num) {
  88. uart_terminal_uart_tx(app->uart, &byte, 1);
  89. }
  90. }
  91. } else {
  92. // Send command with CR+LF or newline '\n'
  93. if(app->is_command && app->selected_tx_string) {
  94. if(app->TERMINAL_MODE == 1) {
  95. uart_terminal_uart_tx(
  96. app->uart,
  97. (uint8_t*)(app->selected_tx_string),
  98. strlen(app->selected_tx_string));
  99. uart_terminal_uart_tx(app->uart, (uint8_t*)("\r\n"), 2);
  100. } else {
  101. uart_terminal_uart_tx(
  102. app->uart,
  103. (uint8_t*)(app->selected_tx_string),
  104. strlen(app->selected_tx_string));
  105. uart_terminal_uart_tx(app->uart, (uint8_t*)("\n"), 1);
  106. }
  107. }
  108. }
  109. }
  110. bool uart_terminal_scene_console_output_on_event(void* context, SceneManagerEvent event) {
  111. UART_TerminalApp* app = context;
  112. bool consumed = false;
  113. if(event.type == SceneManagerEventTypeCustom) {
  114. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  115. consumed = true;
  116. } else if(event.type == SceneManagerEventTypeTick) {
  117. consumed = true;
  118. }
  119. return consumed;
  120. }
  121. void uart_terminal_scene_console_output_on_exit(void* context) {
  122. UART_TerminalApp* app = context;
  123. // Unregister rx callback
  124. uart_terminal_uart_set_handle_rx_data_cb(app->uart, NULL);
  125. }