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(
  111. app->view, UartDumpModel * model, { UNUSED(model); }, true);
  112. }
  113. }
  114. return 0;
  115. }
  116. static UartEchoApp* uart_echo_app_alloc() {
  117. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  118. app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  119. // Gui
  120. app->gui = furi_record_open(RECORD_GUI);
  121. app->notification = furi_record_open(RECORD_NOTIFICATION);
  122. // View dispatcher
  123. app->view_dispatcher = view_dispatcher_alloc();
  124. view_dispatcher_enable_queue(app->view_dispatcher);
  125. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  126. // Views
  127. app->view = view_alloc();
  128. view_set_draw_callback(app->view, uart_echo_view_draw_callback);
  129. view_set_input_callback(app->view, uart_echo_view_input_callback);
  130. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  131. with_view_model(
  132. app->view,
  133. UartDumpModel * model,
  134. {
  135. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  136. model->line = 0;
  137. model->escape = false;
  138. model->list[i] = malloc(sizeof(ListElement));
  139. model->list[i]->text = furi_string_alloc();
  140. }
  141. },
  142. true);
  143. view_set_previous_callback(app->view, uart_echo_exit);
  144. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  145. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  146. app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 1024, uart_echo_worker, app);
  147. furi_thread_start(app->worker_thread);
  148. // Enable uart listener
  149. app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  150. furi_check(app->serial_handle);
  151. furi_hal_serial_init(app->serial_handle, 230400);
  152. furi_hal_serial_async_rx_start(app->serial_handle, uart_echo_on_irq_cb, app, false);
  153. furi_hal_power_disable_external_3_3v();
  154. furi_hal_power_disable_otg();
  155. furi_delay_ms(200);
  156. furi_hal_power_enable_external_3_3v();
  157. furi_hal_power_enable_otg();
  158. for(int i = 0; i < 2; i++) {
  159. furi_delay_ms(500);
  160. furi_hal_serial_tx(app->serial_handle, (uint8_t[1]){'q'}, 1);
  161. }
  162. furi_delay_ms(1);
  163. app->initialized = true;
  164. return app;
  165. }
  166. static void uart_echo_app_free(UartEchoApp* app) {
  167. furi_assert(app);
  168. furi_hal_serial_deinit(app->serial_handle);
  169. furi_hal_serial_control_release(app->serial_handle);
  170. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  171. furi_thread_join(app->worker_thread);
  172. furi_thread_free(app->worker_thread);
  173. // Free views
  174. view_dispatcher_remove_view(app->view_dispatcher, 0);
  175. with_view_model(
  176. app->view,
  177. UartDumpModel * model,
  178. {
  179. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  180. furi_string_free(model->list[i]->text);
  181. free(model->list[i]);
  182. }
  183. },
  184. true);
  185. view_free(app->view);
  186. view_dispatcher_free(app->view_dispatcher);
  187. // Close gui record
  188. furi_record_close(RECORD_GUI);
  189. furi_record_close(RECORD_NOTIFICATION);
  190. app->gui = NULL;
  191. furi_stream_buffer_free(app->rx_stream);
  192. // Free rest
  193. free(app);
  194. }
  195. int32_t uart_echo_app(void* p) {
  196. UNUSED(p);
  197. // Disable expansion protocol to avoid interference with UART Handle
  198. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  199. expansion_disable(expansion);
  200. UartEchoApp* app = uart_echo_app_alloc();
  201. view_dispatcher_run(app->view_dispatcher);
  202. uart_echo_app_free(app);
  203. furi_hal_power_disable_otg();
  204. // Return previous state of expansion
  205. expansion_enable(expansion);
  206. furi_record_close(RECORD_EXPANSION);
  207. return 0;
  208. }