uart_echo.c 7.9 KB

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