mag_scene_read.c 6.4 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, "Flipper Elite, swipe a card!\n");
  64. mag->uart_text_box_store = message;
  65. view_dispatcher_switch_to_view(mag->view_dispatcher, MagViewWidget);
  66. update_widgets(mag);
  67. // Initialize UART
  68. // furi_hal_console_disable();
  69. furi_hal_uart_deinit(FuriHalUartIdUSART1);
  70. furi_hal_uart_init(FuriHalUartIdUSART1, 9600);
  71. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, uart_callback, mag);
  72. FURI_LOG_D(TAG, "UART initialized");
  73. mag->uart_rx_thread = furi_thread_alloc();
  74. furi_thread_set_name(mag->uart_rx_thread, "UartRx");
  75. furi_thread_set_stack_size(mag->uart_rx_thread, 1024);
  76. furi_thread_set_context(mag->uart_rx_thread, mag);
  77. furi_thread_set_callback(mag->uart_rx_thread, uart_worker);
  78. furi_thread_start(mag->uart_rx_thread);
  79. FURI_LOG_D(TAG, "UART worker started");
  80. }
  81. bool mag_scene_read_on_event(void* context, SceneManagerEvent event) {
  82. Mag* mag = context;
  83. bool consumed = false;
  84. if(event.type == SceneManagerEventTypeCustom) {
  85. FURI_LOG_D(TAG, "Custom event: %ld", event.event);
  86. switch(event.event) {
  87. case GuiButtonTypeLeft: // Clear
  88. consumed = true;
  89. // Clear text box store
  90. furi_string_reset(mag->uart_text_box_store);
  91. mag->uart_text_box_store_strlen = 0;
  92. break;
  93. case GuiButtonTypeRight: // Parse
  94. consumed = true;
  95. FURI_LOG_D(TAG, "Trying to parse");
  96. MagDevice* mag_dev = mag->mag_dev;
  97. bool res = mag_device_parse_card_string(mag_dev, mag->uart_text_box_store);
  98. furi_string_reset(mag->uart_text_box_store);
  99. if(res) {
  100. notification_message(mag->notifications, &sequence_success);
  101. furi_string_printf(mag->uart_text_box_store, "Track 1: %.*s\nTrack 2: %.*s\nTrack 3: %.*s",
  102. mag_dev->dev_data.track[0].len, furi_string_get_cstr(mag_dev->dev_data.track[0].str),
  103. mag_dev->dev_data.track[1].len, furi_string_get_cstr(mag_dev->dev_data.track[1].str),
  104. mag_dev->dev_data.track[2].len, furi_string_get_cstr(mag_dev->dev_data.track[2].str));
  105. // Switch to saved menu scene
  106. scene_manager_next_scene(mag->scene_manager, MagSceneSavedMenu);
  107. } else {
  108. furi_string_printf(mag->uart_text_box_store, "Failed to parse! Try again\n");
  109. notification_message(mag->notifications, &sequence_error);
  110. }
  111. break;
  112. }
  113. update_widgets(mag);
  114. }
  115. return consumed;
  116. }
  117. void mag_scene_read_on_exit(void* context) {
  118. Mag* mag = context;
  119. // notification_message(mag->notifications, &sequence_blink_stop);
  120. widget_reset(mag->widget);
  121. // view_dispatcher_remove_view(mag->view_dispatcher, MagViewWidget);
  122. // Stop UART worker
  123. FURI_LOG_D(TAG, "Stopping UART worker");
  124. furi_thread_flags_set(furi_thread_get_id(mag->uart_rx_thread), WorkerEvtStop);
  125. furi_thread_join(mag->uart_rx_thread);
  126. furi_thread_free(mag->uart_rx_thread);
  127. FURI_LOG_D(TAG, "UART worker stopped");
  128. furi_string_free(mag->uart_text_box_store);
  129. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
  130. furi_hal_uart_deinit(FuriHalUartIdUSART1);
  131. // furi_hal_console_enable();
  132. notification_message(mag->notifications, &sequence_blink_stop);
  133. }