uart_echo.c 8.1 KB

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