uart_echo.c 8.0 KB

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