subghz_scene_receiver_info.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "../subghz_i.h"
  2. #include "../helpers/subghz_custom_event.h"
  3. void subghz_scene_receiver_info_callback(GuiButtonType result, InputType type, void* context) {
  4. furi_assert(context);
  5. SubGhz* subghz = context;
  6. if((result == GuiButtonTypeCenter) && (type == InputTypePress)) {
  7. view_dispatcher_send_custom_event(
  8. subghz->view_dispatcher, SubGhzCustomEventSceneReceiverInfoTxStart);
  9. } else if((result == GuiButtonTypeCenter) && (type == InputTypeRelease)) {
  10. view_dispatcher_send_custom_event(
  11. subghz->view_dispatcher, SubGhzCustomEventSceneReceiverInfoTxStop);
  12. } else if((result == GuiButtonTypeRight) && (type == InputTypeShort)) {
  13. view_dispatcher_send_custom_event(
  14. subghz->view_dispatcher, SubGhzCustomEventSceneReceiverInfoSave);
  15. }
  16. }
  17. static bool subghz_scene_receiver_info_update_parser(void* context) {
  18. SubGhz* subghz = context;
  19. if(subghz_txrx_load_decoder_by_name_protocol(
  20. subghz->txrx,
  21. subghz_history_get_protocol_name(subghz->history, subghz->idx_menu_chosen))) {
  22. //todo we are trying to deserialize without checking for errors, since it is assumed that we just received this chignal
  23. subghz_protocol_decoder_base_deserialize(
  24. subghz_txrx_get_decoder(subghz->txrx),
  25. subghz_history_get_raw_data(subghz->history, subghz->idx_menu_chosen));
  26. SubGhzRadioPreset* preset =
  27. subghz_history_get_radio_preset(subghz->history, subghz->idx_menu_chosen);
  28. subghz_txrx_set_preset(
  29. subghz->txrx,
  30. furi_string_get_cstr(preset->name),
  31. preset->frequency,
  32. preset->data,
  33. preset->data_size);
  34. return true;
  35. }
  36. return false;
  37. }
  38. void subghz_scene_receiver_info_on_enter(void* context) {
  39. SubGhz* subghz = context;
  40. if(subghz_scene_receiver_info_update_parser(subghz)) {
  41. FuriString* frequency_str = furi_string_alloc();
  42. FuriString* modulation_str = furi_string_alloc();
  43. FuriString* text = furi_string_alloc();
  44. subghz_txrx_get_frequency_and_modulation(subghz->txrx, frequency_str, modulation_str);
  45. widget_add_string_element(
  46. subghz->widget,
  47. 78,
  48. 0,
  49. AlignLeft,
  50. AlignTop,
  51. FontSecondary,
  52. furi_string_get_cstr(frequency_str));
  53. widget_add_string_element(
  54. subghz->widget,
  55. 113,
  56. 0,
  57. AlignLeft,
  58. AlignTop,
  59. FontSecondary,
  60. furi_string_get_cstr(modulation_str));
  61. subghz_protocol_decoder_base_get_string(subghz_txrx_get_decoder(subghz->txrx), text);
  62. widget_add_string_multiline_element(
  63. subghz->widget, 0, 0, AlignLeft, AlignTop, FontSecondary, furi_string_get_cstr(text));
  64. furi_string_free(frequency_str);
  65. furi_string_free(modulation_str);
  66. furi_string_free(text);
  67. if(subghz_txrx_protocol_is_serializable(subghz->txrx)) {
  68. widget_add_button_element(
  69. subghz->widget,
  70. GuiButtonTypeRight,
  71. "Save",
  72. subghz_scene_receiver_info_callback,
  73. subghz);
  74. }
  75. if(subghz_txrx_protocol_is_transmittable(subghz->txrx, true)) {
  76. widget_add_button_element(
  77. subghz->widget,
  78. GuiButtonTypeCenter,
  79. "Send",
  80. subghz_scene_receiver_info_callback,
  81. subghz);
  82. }
  83. } else {
  84. widget_add_icon_element(subghz->widget, 37, 15, &I_DolphinCommon_56x48);
  85. widget_add_string_element(
  86. subghz->widget, 13, 8, AlignLeft, AlignBottom, FontSecondary, "Error history parse.");
  87. }
  88. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdWidget);
  89. }
  90. bool subghz_scene_receiver_info_on_event(void* context, SceneManagerEvent event) {
  91. SubGhz* subghz = context;
  92. if(event.type == SceneManagerEventTypeCustom) {
  93. if(event.event == SubGhzCustomEventSceneReceiverInfoTxStart) {
  94. if(!subghz_scene_receiver_info_update_parser(subghz)) {
  95. return false;
  96. }
  97. //CC1101 Stop RX -> Start TX
  98. subghz_txrx_hopper_pause(subghz->txrx);
  99. if(!subghz_tx_start(
  100. subghz,
  101. subghz_history_get_raw_data(subghz->history, subghz->idx_menu_chosen))) {
  102. subghz_txrx_rx_start(subghz->txrx);
  103. subghz_txrx_hopper_unpause(subghz->txrx);
  104. subghz->state_notifications = SubGhzNotificationStateRx;
  105. } else {
  106. subghz->state_notifications = SubGhzNotificationStateTx;
  107. }
  108. return true;
  109. } else if(event.event == SubGhzCustomEventSceneReceiverInfoTxStop) {
  110. //CC1101 Stop Tx -> Start RX
  111. subghz->state_notifications = SubGhzNotificationStateIDLE;
  112. subghz_txrx_rx_start(subghz->txrx);
  113. subghz_txrx_hopper_unpause(subghz->txrx);
  114. subghz->state_notifications = SubGhzNotificationStateRx;
  115. return true;
  116. } else if(event.event == SubGhzCustomEventSceneReceiverInfoSave) {
  117. //CC1101 Stop RX -> Save
  118. subghz->state_notifications = SubGhzNotificationStateIDLE;
  119. subghz_txrx_hopper_set_state(subghz->txrx, SubGhzHopperStateOFF);
  120. subghz_txrx_stop(subghz->txrx);
  121. if(!subghz_scene_receiver_info_update_parser(subghz)) {
  122. return false;
  123. }
  124. if(subghz_txrx_protocol_is_serializable(subghz->txrx)) {
  125. subghz_file_name_clear(subghz);
  126. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  127. }
  128. return true;
  129. }
  130. } else if(event.type == SceneManagerEventTypeTick) {
  131. if(subghz_txrx_hopper_get_state(subghz->txrx) != SubGhzHopperStateOFF) {
  132. subghz_txrx_hopper_update(subghz->txrx);
  133. }
  134. switch(subghz->state_notifications) {
  135. case SubGhzNotificationStateTx:
  136. notification_message(subghz->notifications, &sequence_blink_magenta_10);
  137. break;
  138. case SubGhzNotificationStateRx:
  139. notification_message(subghz->notifications, &sequence_blink_cyan_10);
  140. break;
  141. case SubGhzNotificationStateRxDone:
  142. notification_message(subghz->notifications, &sequence_blink_green_100);
  143. subghz->state_notifications = SubGhzNotificationStateRx;
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. return false;
  150. }
  151. void subghz_scene_receiver_info_on_exit(void* context) {
  152. SubGhz* subghz = context;
  153. widget_reset(subghz->widget);
  154. }