mag_scene_read.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Creator: Hummus@FlipperGang
  2. #include "../mag_i.h"
  3. #include "../helpers/mag_helpers.h"
  4. #include "mag_scene_read.h"
  5. #define TAG "MagSceneRead"
  6. void uart_callback(UartIrqEvent event, uint8_t data, void *context) {
  7. Mag* mag = context;
  8. if(event == UartIrqEventRXNE) {
  9. furi_stream_buffer_send(mag->uart_rx_stream, &data, 1, 0);
  10. furi_thread_flags_set(furi_thread_get_id(mag->uart_rx_thread), WorkerEvtRxDone);
  11. }
  12. }
  13. static int32_t uart_worker(void* context) {
  14. Mag* mag = context;
  15. mag->uart_rx_stream = furi_stream_buffer_alloc(UART_RX_BUF_SIZE, 1);
  16. mag->uart_text_box_store_strlen = 0;
  17. while (1) {
  18. uint32_t events = furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  19. // furi_check((events & FuriFlagError) == 0);
  20. if(events & WorkerEvtStop) break;
  21. if(events & WorkerEvtRxDone) {
  22. FURI_LOG_D(TAG, "WorkerEvtRxDone");
  23. // notification_message(mag->notifications, &sequence_success);
  24. size_t len = furi_stream_buffer_receive(mag->uart_rx_stream, mag->uart_rx_buf, UART_RX_BUF_SIZE, 200);
  25. FURI_LOG_D(TAG, "UART RX len: %d", len);
  26. if (len > 0) {
  27. // If text box store gets too big, then truncate it
  28. mag->uart_text_box_store_strlen += len;
  29. if(mag->uart_text_box_store_strlen >= UART_TERMINAL_TEXT_BOX_STORE_SIZE - 1) {
  30. furi_string_right(mag->uart_text_box_store, mag->uart_text_box_store_strlen / 2);
  31. mag->uart_text_box_store_strlen = furi_string_size(mag->uart_text_box_store) + len;
  32. }
  33. // Add '\0' to the end of the string, and then add the new data
  34. mag->uart_rx_buf[len] = '\0';
  35. furi_string_cat_printf(mag->uart_text_box_store, "%s", mag->uart_rx_buf);
  36. FURI_LOG_D(TAG, "UART RX buf: %*.s", len, mag->uart_rx_buf);
  37. FURI_LOG_D(TAG, "UART RX store: %s", furi_string_get_cstr(mag->uart_text_box_store));
  38. }
  39. FURI_LOG_D(TAG, "UARTEventRxData");
  40. view_dispatcher_send_custom_event(
  41. mag->view_dispatcher, UARTEventRxData);
  42. }
  43. }
  44. furi_stream_buffer_free(mag->uart_rx_stream);
  45. return 0;
  46. }
  47. void update_widgets(Mag* mag) {
  48. // Clear widget from all elements
  49. widget_reset(mag->widget);
  50. // Titlebar
  51. widget_add_icon_element(mag->widget, 38, -1, &I_mag_file_10px);
  52. widget_add_string_element(mag->widget, 64, 0, AlignCenter, AlignTop, FontPrimary, "READ");
  53. widget_add_icon_element(mag->widget, 81, -1, &I_mag_file_10px);
  54. // Text box
  55. widget_add_text_scroll_element(mag->widget, 0, 10, 128, 40, furi_string_get_cstr(mag->uart_text_box_store));
  56. // Buttons
  57. widget_add_button_element(mag->widget, GuiButtonTypeLeft, "Clear", mag_widget_callback, mag);
  58. widget_add_button_element(mag->widget, GuiButtonTypeRight, "Parse", mag_widget_callback, mag);
  59. }
  60. void mag_scene_read_on_enter(void* context) {
  61. Mag* mag = context;
  62. FuriString* message = furi_string_alloc();
  63. furi_string_printf(message, "Please swipe a card!\n");
  64. mag->uart_text_box_store = message;
  65. furi_string_free(message);
  66. view_dispatcher_switch_to_view(mag->view_dispatcher, MagViewWidget);
  67. update_widgets(mag);
  68. // Initialize UART
  69. // furi_hal_console_disable();
  70. furi_hal_uart_deinit(FuriHalUartIdUSART1);
  71. furi_hal_uart_init(FuriHalUartIdUSART1, 9600);
  72. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, uart_callback, mag);
  73. FURI_LOG_D(TAG, "UART initialized");
  74. mag->uart_rx_thread = furi_thread_alloc();
  75. furi_thread_set_name(mag->uart_rx_thread, "UartRx");
  76. furi_thread_set_stack_size(mag->uart_rx_thread, 1024);
  77. furi_thread_set_context(mag->uart_rx_thread, mag);
  78. furi_thread_set_callback(mag->uart_rx_thread, uart_worker);
  79. furi_thread_start(mag->uart_rx_thread);
  80. FURI_LOG_D(TAG, "UART worker started");
  81. }
  82. bool mag_scene_read_on_event(void* context, SceneManagerEvent event) {
  83. Mag* mag = context;
  84. bool consumed = false;
  85. if(event.type == SceneManagerEventTypeCustom) {
  86. FURI_LOG_D(TAG, "Custom event: %ld", event.event);
  87. switch(event.event) {
  88. case GuiButtonTypeLeft: // Clear
  89. consumed = true;
  90. // Clear text box store
  91. furi_string_reset(mag->uart_text_box_store);
  92. mag->uart_text_box_store_strlen = 0;
  93. break;
  94. case GuiButtonTypeRight: // Parse
  95. consumed = true;
  96. FURI_LOG_D(TAG, "Trying to parse");
  97. MagDevice* mag_dev = mag->mag_dev;
  98. bool res = mag_device_parse_card_string(mag_dev, mag->uart_text_box_store);
  99. furi_string_reset(mag->uart_text_box_store);
  100. if(res) {
  101. notification_message(mag->notifications, &sequence_success);
  102. furi_string_printf(mag->uart_text_box_store, "Track 1: %.*s\nTrack 2: %.*s\nTrack 3: %.*s",
  103. mag_dev->dev_data.track[0].len, furi_string_get_cstr(mag_dev->dev_data.track[0].str),
  104. mag_dev->dev_data.track[1].len, furi_string_get_cstr(mag_dev->dev_data.track[1].str),
  105. mag_dev->dev_data.track[2].len, furi_string_get_cstr(mag_dev->dev_data.track[2].str));
  106. // Switch to saved menu scene
  107. scene_manager_next_scene(mag->scene_manager, MagSceneSavedMenu);
  108. } else {
  109. furi_string_printf(mag->uart_text_box_store, "Failed to parse! Try again\n");
  110. notification_message(mag->notifications, &sequence_error);
  111. }
  112. break;
  113. }
  114. update_widgets(mag);
  115. }
  116. return consumed;
  117. }
  118. void mag_scene_read_on_exit(void* context) {
  119. Mag* mag = context;
  120. // notification_message(mag->notifications, &sequence_blink_stop);
  121. widget_reset(mag->widget);
  122. // view_dispatcher_remove_view(mag->view_dispatcher, MagViewWidget);
  123. // Stop UART worker
  124. FURI_LOG_D(TAG, "Stopping UART worker");
  125. furi_thread_flags_set(furi_thread_get_id(mag->uart_rx_thread), WorkerEvtStop);
  126. furi_thread_join(mag->uart_rx_thread);
  127. furi_thread_free(mag->uart_rx_thread);
  128. FURI_LOG_D(TAG, "UART worker stopped");
  129. furi_string_free(mag->uart_text_box_store);
  130. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
  131. furi_hal_uart_deinit(FuriHalUartIdUSART1);
  132. // furi_hal_console_enable();
  133. notification_message(mag->notifications, &sequence_blink_stop);
  134. }