uart_echo.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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, void* context, const char data) {
  46. // Alarm sound
  47. if(data == '!') {
  48. UartEchoApp* app = context;
  49. notification_message(app->notification, &sequence_alarm);
  50. }
  51. if(model->escape) {
  52. // escape code end with letter
  53. if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z')) {
  54. model->escape = false;
  55. }
  56. } else if(data == '[' && model->last_char == '\e') {
  57. // "Esc[" is a escape code
  58. model->escape = true;
  59. } else if((data >= ' ' && data <= '~') || (data == '\n' || data == '\r')) {
  60. bool new_string_needed = false;
  61. if(furi_string_size(model->list[model->line]->text) >= COLUMNS_ON_SCREEN) {
  62. new_string_needed = true;
  63. } else if((data == '\n' || data == '\r')) {
  64. // pack line breaks
  65. if(model->last_char != '\n' && model->last_char != '\r') {
  66. new_string_needed = true;
  67. }
  68. }
  69. if(new_string_needed) {
  70. if((model->line + 1) < LINES_ON_SCREEN) {
  71. model->line += 1;
  72. } else {
  73. ListElement* first = model->list[0];
  74. for(size_t i = 1; i < LINES_ON_SCREEN; i++) {
  75. model->list[i - 1] = model->list[i];
  76. }
  77. furi_string_reset(first->text);
  78. model->list[model->line] = first;
  79. }
  80. }
  81. if(data != '\n' && data != '\r') {
  82. furi_string_push_back(model->list[model->line]->text, data);
  83. }
  84. }
  85. model->last_char = data;
  86. }
  87. static int32_t uart_echo_worker(void* context) {
  88. furi_assert(context);
  89. UartEchoApp* app = context;
  90. while(1) {
  91. uint32_t events =
  92. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  93. furi_check((events & FuriFlagError) == 0);
  94. if(events & WorkerEventStop) break;
  95. if(events & WorkerEventRx) {
  96. size_t length = 0;
  97. do {
  98. uint8_t data[64];
  99. length = furi_stream_buffer_receive(app->rx_stream, data, 64, 0);
  100. if(length > 0 && app->initialized) {
  101. furi_hal_serial_tx(app->serial_handle, data, length);
  102. with_view_model(
  103. app->view,
  104. UartDumpModel * model,
  105. {
  106. for(size_t i = 0; i < length; i++) {
  107. uart_echo_push_to_list(model, app, data[i]);
  108. }
  109. },
  110. false);
  111. }
  112. } while(length > 0);
  113. //notification_message(app->notification, &sequence_notification);
  114. with_view_model(
  115. app->view, UartDumpModel * model, { UNUSED(model); }, true);
  116. }
  117. }
  118. return 0;
  119. }
  120. static UartEchoApp* uart_echo_app_alloc() {
  121. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  122. app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  123. // Gui
  124. app->gui = furi_record_open(RECORD_GUI);
  125. app->notification = furi_record_open(RECORD_NOTIFICATION);
  126. // View dispatcher
  127. app->view_dispatcher = view_dispatcher_alloc();
  128. view_dispatcher_enable_queue(app->view_dispatcher);
  129. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  130. // Views
  131. app->view = view_alloc();
  132. view_set_draw_callback(app->view, uart_echo_view_draw_callback);
  133. view_set_input_callback(app->view, uart_echo_view_input_callback);
  134. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  135. with_view_model(
  136. app->view,
  137. UartDumpModel * model,
  138. {
  139. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  140. model->line = 0;
  141. model->escape = false;
  142. model->list[i] = malloc(sizeof(ListElement));
  143. model->list[i]->text = furi_string_alloc();
  144. }
  145. },
  146. true);
  147. view_set_previous_callback(app->view, uart_echo_exit);
  148. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  149. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  150. app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 1024, uart_echo_worker, app);
  151. furi_thread_start(app->worker_thread);
  152. // Enable uart listener
  153. app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  154. furi_check(app->serial_handle);
  155. furi_hal_serial_init(app->serial_handle, 230400);
  156. furi_hal_serial_async_rx_start(app->serial_handle, uart_echo_on_irq_cb, app, false);
  157. furi_hal_power_disable_external_3_3v();
  158. furi_hal_power_disable_otg();
  159. furi_delay_ms(200);
  160. furi_hal_power_enable_external_3_3v();
  161. furi_hal_power_enable_otg();
  162. for(int i = 0; i < 2; i++) {
  163. furi_delay_ms(500);
  164. furi_hal_serial_tx(app->serial_handle, (uint8_t[1]){'m'}, 1);
  165. }
  166. furi_delay_ms(1);
  167. app->initialized = true;
  168. return app;
  169. }
  170. static void uart_echo_app_free(UartEchoApp* app) {
  171. furi_assert(app);
  172. furi_hal_serial_deinit(app->serial_handle);
  173. furi_hal_serial_control_release(app->serial_handle);
  174. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  175. furi_thread_join(app->worker_thread);
  176. furi_thread_free(app->worker_thread);
  177. // Free views
  178. view_dispatcher_remove_view(app->view_dispatcher, 0);
  179. with_view_model(
  180. app->view,
  181. UartDumpModel * model,
  182. {
  183. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  184. furi_string_free(model->list[i]->text);
  185. free(model->list[i]);
  186. }
  187. },
  188. true);
  189. view_free(app->view);
  190. view_dispatcher_free(app->view_dispatcher);
  191. // Close gui record
  192. furi_record_close(RECORD_GUI);
  193. furi_record_close(RECORD_NOTIFICATION);
  194. app->gui = NULL;
  195. furi_stream_buffer_free(app->rx_stream);
  196. // Free rest
  197. free(app);
  198. }
  199. int32_t uart_echo_app(void* p) {
  200. UNUSED(p);
  201. UartEchoApp* app = uart_echo_app_alloc();
  202. view_dispatcher_run(app->view_dispatcher);
  203. uart_echo_app_free(app);
  204. furi_hal_power_disable_otg();
  205. return 0;
  206. }