uart_echo.c 7.3 KB

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