uart_echo.c 7.4 KB

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