dap_scene_main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "../dap_gui_i.h"
  2. #include "../../dap_link.h"
  3. typedef struct {
  4. DapState dap_state;
  5. bool dap_active;
  6. bool tx_active;
  7. bool rx_active;
  8. } DapSceneMainState;
  9. static bool process_dap_state(DapGuiApp* app) {
  10. DapSceneMainState* state =
  11. (DapSceneMainState*)scene_manager_get_scene_state(app->scene_manager, DapSceneMain);
  12. if(state == NULL) return true;
  13. DapState* prev_state = &state->dap_state;
  14. DapState next_state;
  15. dap_app_get_state(app->dap_app, &next_state);
  16. bool need_to_update = false;
  17. if(prev_state->dap_mode != next_state.dap_mode) {
  18. switch(next_state.dap_mode) {
  19. case DapModeDisconnected:
  20. dap_main_view_set_mode(app->main_view, DapMainViewModeDisconnected);
  21. notification_message(app->notifications, &sequence_blink_stop);
  22. break;
  23. case DapModeSWD:
  24. dap_main_view_set_mode(app->main_view, DapMainViewModeSWD);
  25. notification_message(app->notifications, &sequence_blink_start_blue);
  26. break;
  27. case DapModeJTAG:
  28. dap_main_view_set_mode(app->main_view, DapMainViewModeJTAG);
  29. notification_message(app->notifications, &sequence_blink_start_magenta);
  30. break;
  31. }
  32. need_to_update = true;
  33. }
  34. if(prev_state->dap_version != next_state.dap_version) {
  35. switch(next_state.dap_version) {
  36. case DapVersionUnknown:
  37. dap_main_view_set_version(app->main_view, DapMainViewVersionUnknown);
  38. break;
  39. case DapVersionV1:
  40. dap_main_view_set_version(app->main_view, DapMainViewVersionV1);
  41. break;
  42. case DapVersionV2:
  43. dap_main_view_set_version(app->main_view, DapMainViewVersionV2);
  44. break;
  45. }
  46. need_to_update = true;
  47. }
  48. if(prev_state->usb_connected != next_state.usb_connected) {
  49. dap_main_view_set_usb_connected(app->main_view, next_state.usb_connected);
  50. need_to_update = true;
  51. }
  52. if(prev_state->dap_counter != next_state.dap_counter) {
  53. if(!state->dap_active) {
  54. state->dap_active = true;
  55. dap_main_view_set_dap(app->main_view, state->dap_active);
  56. need_to_update = true;
  57. }
  58. } else {
  59. if(state->dap_active) {
  60. state->dap_active = false;
  61. dap_main_view_set_dap(app->main_view, state->dap_active);
  62. need_to_update = true;
  63. }
  64. }
  65. if(prev_state->cdc_baudrate != next_state.cdc_baudrate) {
  66. dap_main_view_set_baudrate(app->main_view, next_state.cdc_baudrate);
  67. need_to_update = true;
  68. }
  69. if(prev_state->cdc_tx_counter != next_state.cdc_tx_counter) {
  70. if(!state->tx_active) {
  71. state->tx_active = true;
  72. dap_main_view_set_tx(app->main_view, state->tx_active);
  73. need_to_update = true;
  74. notification_message(app->notifications, &sequence_blink_start_red);
  75. }
  76. } else {
  77. if(state->tx_active) {
  78. state->tx_active = false;
  79. dap_main_view_set_tx(app->main_view, state->tx_active);
  80. need_to_update = true;
  81. notification_message(app->notifications, &sequence_blink_stop);
  82. }
  83. }
  84. if(prev_state->cdc_rx_counter != next_state.cdc_rx_counter) {
  85. if(!state->rx_active) {
  86. state->rx_active = true;
  87. dap_main_view_set_rx(app->main_view, state->rx_active);
  88. need_to_update = true;
  89. notification_message(app->notifications, &sequence_blink_start_green);
  90. }
  91. } else {
  92. if(state->rx_active) {
  93. state->rx_active = false;
  94. dap_main_view_set_rx(app->main_view, state->rx_active);
  95. need_to_update = true;
  96. notification_message(app->notifications, &sequence_blink_stop);
  97. }
  98. }
  99. if(need_to_update) {
  100. dap_main_view_update(app->main_view);
  101. }
  102. *prev_state = next_state;
  103. return true;
  104. }
  105. static void dap_scene_main_on_left(void* context) {
  106. DapGuiApp* app = (DapGuiApp*)context;
  107. view_dispatcher_send_custom_event(app->view_dispatcher, DapAppCustomEventConfig);
  108. }
  109. void dap_scene_main_on_enter(void* context) {
  110. DapGuiApp* app = context;
  111. DapSceneMainState* state = malloc(sizeof(DapSceneMainState));
  112. dap_main_view_set_left_callback(app->main_view, dap_scene_main_on_left, app);
  113. view_dispatcher_switch_to_view(app->view_dispatcher, DapGuiAppViewMainView);
  114. scene_manager_set_scene_state(app->scene_manager, DapSceneMain, (uint32_t)state);
  115. }
  116. bool dap_scene_main_on_event(void* context, SceneManagerEvent event) {
  117. DapGuiApp* app = context;
  118. if(event.type == SceneManagerEventTypeCustom) {
  119. if(event.event == DapAppCustomEventConfig) {
  120. scene_manager_next_scene(app->scene_manager, DapSceneConfig);
  121. return true;
  122. }
  123. } else if(event.type == SceneManagerEventTypeTick) {
  124. return process_dap_state(app);
  125. }
  126. return false;
  127. }
  128. void dap_scene_main_on_exit(void* context) {
  129. DapGuiApp* app = context;
  130. DapSceneMainState* state =
  131. (DapSceneMainState*)scene_manager_get_scene_state(app->scene_manager, DapSceneMain);
  132. scene_manager_set_scene_state(app->scene_manager, DapSceneMain, (uint32_t)NULL);
  133. FURI_SW_MEMBARRIER();
  134. free(state);
  135. notification_message(app->notifications, &sequence_blink_stop);
  136. }