uart_echo.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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
  36. uart_echo_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) {
  37. furi_assert(context);
  38. UartEchoApp* app = context;
  39. if(event == FuriHalSerialRxEventData) {
  40. uint8_t data = furi_hal_serial_async_rx(handle);
  41. furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
  42. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
  43. }
  44. }
  45. static void uart_echo_push_to_list(UartDumpModel* model, const char data) {
  46. if(model->escape) {
  47. // escape code end with letter
  48. if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z')) {
  49. model->escape = false;
  50. }
  51. } else if(data == '[' && model->last_char == '\e') {
  52. // "Esc[" is a escape code
  53. model->escape = true;
  54. } else if((data >= ' ' && data <= '~') || (data == '\n' || data == '\r')) {
  55. bool new_string_needed = false;
  56. if(furi_string_size(model->list[model->line]->text) >= COLUMNS_ON_SCREEN) {
  57. new_string_needed = true;
  58. } else if((data == '\n' || data == '\r')) {
  59. // pack line breaks
  60. if(model->last_char != '\n' && model->last_char != '\r') {
  61. new_string_needed = true;
  62. }
  63. }
  64. if(new_string_needed) {
  65. if((model->line + 1) < LINES_ON_SCREEN) {
  66. model->line += 1;
  67. } else {
  68. ListElement* first = model->list[0];
  69. for(size_t i = 1; i < LINES_ON_SCREEN; i++) {
  70. model->list[i - 1] = model->list[i];
  71. }
  72. furi_string_reset(first->text);
  73. model->list[model->line] = first;
  74. }
  75. }
  76. if(data != '\n' && data != '\r') {
  77. furi_string_push_back(model->list[model->line]->text, data);
  78. }
  79. }
  80. model->last_char = data;
  81. }
  82. static int32_t uart_echo_worker(void* context) {
  83. furi_assert(context);
  84. UartEchoApp* app = context;
  85. while(1) {
  86. uint32_t events =
  87. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  88. furi_check((events & FuriFlagError) == 0);
  89. if(events & WorkerEventStop) break;
  90. if(events & WorkerEventRx) {
  91. size_t length = 0;
  92. do {
  93. uint8_t data[64];
  94. length = furi_stream_buffer_receive(app->rx_stream, data, 64, 0);
  95. if(length > 0 && app->initialized) {
  96. furi_hal_serial_tx(app->serial_handle, data, length);
  97. with_view_model(
  98. app->view,
  99. UartDumpModel * model,
  100. {
  101. for(size_t i = 0; i < length; i++) {
  102. uart_echo_push_to_list(model, data[i]);
  103. }
  104. },
  105. false);
  106. }
  107. } while(length > 0);
  108. notification_message(app->notification, &sequence_notification);
  109. with_view_model(
  110. 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_enable_queue(app->view_dispatcher);
  124. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  125. // Views
  126. app->view = view_alloc();
  127. view_set_draw_callback(app->view, uart_echo_view_draw_callback);
  128. view_set_input_callback(app->view, uart_echo_view_input_callback);
  129. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  130. with_view_model(
  131. app->view,
  132. UartDumpModel * model,
  133. {
  134. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  135. model->line = 0;
  136. model->escape = false;
  137. model->list[i] = malloc(sizeof(ListElement));
  138. model->list[i]->text = furi_string_alloc();
  139. }
  140. },
  141. true);
  142. view_set_previous_callback(app->view, uart_echo_exit);
  143. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  144. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  145. app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 1024, uart_echo_worker, app);
  146. furi_thread_start(app->worker_thread);
  147. // Enable uart listener
  148. app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  149. furi_check(app->serial_handle);
  150. furi_hal_serial_init(app->serial_handle, 230400);
  151. furi_hal_serial_async_rx_start(app->serial_handle, uart_echo_on_irq_cb, app, false);
  152. furi_hal_power_disable_external_3_3v();
  153. furi_hal_power_disable_otg();
  154. furi_delay_ms(200);
  155. furi_hal_power_enable_external_3_3v();
  156. furi_hal_power_enable_otg();
  157. for(int i = 0; i < 2; i++) {
  158. furi_delay_ms(500);
  159. furi_hal_serial_tx(app->serial_handle, (uint8_t[1]){'q'}, 1);
  160. }
  161. furi_delay_ms(1);
  162. app->initialized = true;
  163. return app;
  164. }
  165. static void uart_echo_app_free(UartEchoApp* app) {
  166. furi_assert(app);
  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. UartEchoApp* app = uart_echo_app_alloc();
  197. view_dispatcher_run(app->view_dispatcher);
  198. uart_echo_app_free(app);
  199. furi_hal_power_disable_otg();
  200. return 0;
  201. }