uart_echo.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "uart_echo.h"
  2. #include <expansion/expansion.h>
  3. static void uart_echo_view_draw_callback(Canvas* canvas, void* _model) {
  4. UartDumpModel* model = _model;
  5. // Prepare canvas
  6. canvas_clear(canvas);
  7. canvas_set_color(canvas, ColorBlack);
  8. canvas_set_font(canvas, FontKeyboard);
  9. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  10. canvas_draw_str(
  11. canvas,
  12. 0,
  13. (i + 1) * (canvas_current_font_height(canvas) - 1),
  14. furi_string_get_cstr(model->list[i]->text));
  15. if(i == model->line) {
  16. uint8_t width =
  17. canvas_string_width(canvas, furi_string_get_cstr(model->list[i]->text));
  18. canvas_draw_box(
  19. canvas,
  20. width,
  21. (i) * (canvas_current_font_height(canvas) - 1) + 2,
  22. 2,
  23. canvas_current_font_height(canvas) - 2);
  24. }
  25. }
  26. }
  27. static bool uart_echo_view_input_callback(InputEvent* event, void* context) {
  28. UNUSED(event);
  29. UNUSED(context);
  30. return false;
  31. }
  32. static uint32_t uart_echo_exit(void* context) {
  33. UNUSED(context);
  34. return VIEW_NONE;
  35. }
  36. static void
  37. uart_echo_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) {
  38. furi_assert(context);
  39. UartEchoApp* app = context;
  40. if(event == FuriHalSerialRxEventData) {
  41. uint8_t data = furi_hal_serial_async_rx(handle);
  42. furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
  43. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
  44. }
  45. }
  46. static void uart_echo_push_to_list(UartDumpModel* model, const char data) {
  47. if(model->escape) {
  48. // escape code end with letter
  49. if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z')) {
  50. model->escape = false;
  51. }
  52. } else if(data == '[' && model->last_char == '\e') {
  53. // "Esc[" is a escape code
  54. model->escape = true;
  55. } else if((data >= ' ' && data <= '~') || (data == '\n' || data == '\r')) {
  56. bool new_string_needed = false;
  57. if(furi_string_size(model->list[model->line]->text) >= COLUMNS_ON_SCREEN) {
  58. new_string_needed = true;
  59. } else if((data == '\n' || data == '\r')) {
  60. // pack line breaks
  61. if(model->last_char != '\n' && model->last_char != '\r') {
  62. new_string_needed = true;
  63. }
  64. }
  65. if(new_string_needed) {
  66. if((model->line + 1) < LINES_ON_SCREEN) {
  67. model->line += 1;
  68. } else {
  69. ListElement* first = model->list[0];
  70. for(size_t i = 1; i < LINES_ON_SCREEN; i++) {
  71. model->list[i - 1] = model->list[i];
  72. }
  73. furi_string_reset(first->text);
  74. model->list[model->line] = first;
  75. }
  76. }
  77. if(data != '\n' && data != '\r') {
  78. furi_string_push_back(model->list[model->line]->text, data);
  79. }
  80. }
  81. model->last_char = data;
  82. }
  83. static int32_t uart_echo_worker(void* context) {
  84. furi_assert(context);
  85. UartEchoApp* app = context;
  86. while(1) {
  87. uint32_t events =
  88. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  89. furi_check((events & FuriFlagError) == 0);
  90. if(events & WorkerEventStop) break;
  91. if(events & WorkerEventRx) {
  92. size_t length = 0;
  93. do {
  94. uint8_t data[64];
  95. length = furi_stream_buffer_receive(app->rx_stream, data, 64, 0);
  96. if(length > 0 && app->initialized) {
  97. furi_hal_serial_tx(app->serial_handle, data, length);
  98. with_view_model(
  99. app->view,
  100. UartDumpModel * model,
  101. {
  102. for(size_t i = 0; i < length; i++) {
  103. uart_echo_push_to_list(model, data[i]);
  104. }
  105. },
  106. false);
  107. }
  108. } while(length > 0);
  109. notification_message(app->notification, &sequence_notification);
  110. with_view_model(app->view, UartDumpModel * model, { UNUSED(model); }, true);
  111. }
  112. }
  113. return 0;
  114. }
  115. static UartEchoApp* uart_echo_app_alloc() {
  116. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  117. app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  118. // Gui
  119. app->gui = furi_record_open(RECORD_GUI);
  120. app->notification = furi_record_open(RECORD_NOTIFICATION);
  121. // View dispatcher
  122. app->view_dispatcher = view_dispatcher_alloc();
  123. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  124. // Views
  125. app->view = view_alloc();
  126. view_set_draw_callback(app->view, uart_echo_view_draw_callback);
  127. view_set_input_callback(app->view, uart_echo_view_input_callback);
  128. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  129. with_view_model(
  130. app->view,
  131. UartDumpModel * model,
  132. {
  133. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  134. model->line = 0;
  135. model->escape = false;
  136. model->list[i] = malloc(sizeof(ListElement));
  137. model->list[i]->text = furi_string_alloc();
  138. }
  139. },
  140. true);
  141. view_set_previous_callback(app->view, uart_echo_exit);
  142. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  143. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  144. app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 1024, uart_echo_worker, app);
  145. furi_thread_start(app->worker_thread);
  146. // Enable uart listener
  147. app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  148. furi_check(app->serial_handle);
  149. furi_hal_serial_init(app->serial_handle, 230400);
  150. furi_hal_serial_async_rx_start(app->serial_handle, uart_echo_on_irq_cb, app, false);
  151. furi_hal_power_disable_external_3_3v();
  152. furi_hal_power_disable_otg();
  153. furi_delay_ms(200);
  154. furi_hal_power_enable_external_3_3v();
  155. furi_hal_power_enable_otg();
  156. for(int i = 0; i < 2; i++) {
  157. furi_delay_ms(500);
  158. furi_hal_serial_tx(app->serial_handle, (uint8_t[1]){'n'}, 1);
  159. }
  160. furi_delay_ms(1);
  161. app->initialized = true;
  162. return app;
  163. }
  164. static void uart_echo_app_free(UartEchoApp* app) {
  165. furi_assert(app);
  166. furi_hal_serial_async_rx_stop(app->serial_handle);
  167. furi_hal_serial_deinit(app->serial_handle);
  168. furi_hal_serial_control_release(app->serial_handle);
  169. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  170. furi_thread_join(app->worker_thread);
  171. furi_thread_free(app->worker_thread);
  172. // Free views
  173. view_dispatcher_remove_view(app->view_dispatcher, 0);
  174. with_view_model(
  175. app->view,
  176. UartDumpModel * model,
  177. {
  178. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  179. furi_string_free(model->list[i]->text);
  180. free(model->list[i]);
  181. }
  182. },
  183. true);
  184. view_free(app->view);
  185. view_dispatcher_free(app->view_dispatcher);
  186. // Close gui record
  187. furi_record_close(RECORD_GUI);
  188. furi_record_close(RECORD_NOTIFICATION);
  189. app->gui = NULL;
  190. furi_stream_buffer_free(app->rx_stream);
  191. // Free rest
  192. free(app);
  193. }
  194. int32_t uart_echo_app(void* p) {
  195. UNUSED(p);
  196. // Disable expansion protocol to avoid interference with UART Handle
  197. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  198. expansion_disable(expansion);
  199. UartEchoApp* app = uart_echo_app_alloc();
  200. view_dispatcher_run(app->view_dispatcher);
  201. uart_echo_app_free(app);
  202. furi_hal_power_disable_otg();
  203. // Return previous state of expansion
  204. expansion_enable(expansion);
  205. furi_record_close(RECORD_EXPANSION);
  206. return 0;
  207. }