subghz_scene_receiver_info.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. subghz->txrx->decoder_result = subghz_receiver_search_decoder_base_by_name(
  20. subghz->txrx->receiver,
  21. subghz_history_get_protocol_name(subghz->txrx->history, subghz->txrx->idx_menu_chosen));
  22. if(subghz->txrx->decoder_result) {
  23. subghz_protocol_decoder_base_deserialize(
  24. subghz->txrx->decoder_result,
  25. subghz_history_get_raw_data(subghz->txrx->history, subghz->txrx->idx_menu_chosen));
  26. SubGhzRadioPreset* preset =
  27. subghz_history_get_radio_preset(subghz->txrx->history, subghz->txrx->idx_menu_chosen);
  28. subghz_preset_init(
  29. subghz,
  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;
  42. FuriString* modulation_str;
  43. FuriString* text;
  44. frequency_str = furi_string_alloc();
  45. modulation_str = furi_string_alloc();
  46. text = furi_string_alloc();
  47. subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
  48. widget_add_string_element(
  49. subghz->widget,
  50. 78,
  51. 0,
  52. AlignLeft,
  53. AlignTop,
  54. FontSecondary,
  55. furi_string_get_cstr(frequency_str));
  56. widget_add_string_element(
  57. subghz->widget,
  58. 113,
  59. 0,
  60. AlignLeft,
  61. AlignTop,
  62. FontSecondary,
  63. furi_string_get_cstr(modulation_str));
  64. subghz_protocol_decoder_base_get_string(subghz->txrx->decoder_result, text);
  65. widget_add_string_multiline_element(
  66. subghz->widget, 0, 0, AlignLeft, AlignTop, FontSecondary, furi_string_get_cstr(text));
  67. furi_string_free(frequency_str);
  68. furi_string_free(modulation_str);
  69. furi_string_free(text);
  70. if((subghz->txrx->decoder_result->protocol->flag & SubGhzProtocolFlag_Save) ==
  71. SubGhzProtocolFlag_Save) {
  72. widget_add_button_element(
  73. subghz->widget,
  74. GuiButtonTypeRight,
  75. "Save",
  76. subghz_scene_receiver_info_callback,
  77. subghz);
  78. }
  79. if(((subghz->txrx->decoder_result->protocol->flag & SubGhzProtocolFlag_Send) ==
  80. SubGhzProtocolFlag_Send) &&
  81. subghz->txrx->decoder_result->protocol->encoder->deserialize &&
  82. subghz->txrx->decoder_result->protocol->type == SubGhzProtocolTypeStatic) {
  83. widget_add_button_element(
  84. subghz->widget,
  85. GuiButtonTypeCenter,
  86. "Send",
  87. subghz_scene_receiver_info_callback,
  88. subghz);
  89. }
  90. } else {
  91. widget_add_icon_element(subghz->widget, 37, 15, &I_DolphinCommon_56x48);
  92. widget_add_string_element(
  93. subghz->widget, 13, 8, AlignLeft, AlignBottom, FontSecondary, "Error history parse.");
  94. }
  95. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdWidget);
  96. }
  97. bool subghz_scene_receiver_info_on_event(void* context, SceneManagerEvent event) {
  98. SubGhz* subghz = context;
  99. if(event.type == SceneManagerEventTypeCustom) {
  100. if(event.event == SubGhzCustomEventSceneReceiverInfoTxStart) {
  101. //CC1101 Stop RX -> Start TX
  102. if(subghz->txrx->hopper_state != SubGhzHopperStateOFF) {
  103. subghz->txrx->hopper_state = SubGhzHopperStatePause;
  104. }
  105. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  106. subghz_rx_end(subghz);
  107. }
  108. if(!subghz_scene_receiver_info_update_parser(subghz)) {
  109. return false;
  110. }
  111. if(subghz->txrx->txrx_state == SubGhzTxRxStateIDLE ||
  112. subghz->txrx->txrx_state == SubGhzTxRxStateSleep) {
  113. if(!subghz_tx_start(
  114. subghz,
  115. subghz_history_get_raw_data(
  116. subghz->txrx->history, subghz->txrx->idx_menu_chosen))) {
  117. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowOnlyRx);
  118. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  119. subghz_tx_stop(subghz);
  120. }
  121. if(subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) {
  122. subghz_begin(
  123. subghz,
  124. subghz_setting_get_preset_data_by_name(
  125. subghz->setting,
  126. furi_string_get_cstr(subghz->txrx->preset->name)));
  127. subghz_rx(subghz, subghz->txrx->preset->frequency);
  128. }
  129. if(subghz->txrx->hopper_state == SubGhzHopperStatePause) {
  130. subghz->txrx->hopper_state = SubGhzHopperStateRunnig;
  131. }
  132. subghz->state_notifications = SubGhzNotificationStateRx;
  133. } else {
  134. subghz->state_notifications = SubGhzNotificationStateTx;
  135. }
  136. }
  137. return true;
  138. } else if(event.event == SubGhzCustomEventSceneReceiverInfoTxStop) {
  139. //CC1101 Stop Tx -> Start RX
  140. subghz->state_notifications = SubGhzNotificationStateIDLE;
  141. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  142. subghz_tx_stop(subghz);
  143. }
  144. if(subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) {
  145. subghz_begin(
  146. subghz,
  147. subghz_setting_get_preset_data_by_name(
  148. subghz->setting, furi_string_get_cstr(subghz->txrx->preset->name)));
  149. subghz_rx(subghz, subghz->txrx->preset->frequency);
  150. }
  151. if(subghz->txrx->hopper_state == SubGhzHopperStatePause) {
  152. subghz->txrx->hopper_state = SubGhzHopperStateRunnig;
  153. }
  154. subghz->state_notifications = SubGhzNotificationStateRx;
  155. return true;
  156. } else if(event.event == SubGhzCustomEventSceneReceiverInfoSave) {
  157. //CC1101 Stop RX -> Save
  158. subghz->state_notifications = SubGhzNotificationStateIDLE;
  159. subghz->txrx->hopper_state = SubGhzHopperStateOFF;
  160. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  161. subghz_rx_end(subghz);
  162. subghz_sleep(subghz);
  163. }
  164. if(!subghz_scene_receiver_info_update_parser(subghz)) {
  165. return false;
  166. }
  167. if((subghz->txrx->decoder_result->protocol->flag & SubGhzProtocolFlag_Save) ==
  168. SubGhzProtocolFlag_Save) {
  169. subghz_file_name_clear(subghz);
  170. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  171. }
  172. return true;
  173. }
  174. } else if(event.type == SceneManagerEventTypeTick) {
  175. if(subghz->txrx->hopper_state != SubGhzHopperStateOFF) {
  176. subghz_hopper_update(subghz);
  177. }
  178. switch(subghz->state_notifications) {
  179. case SubGhzNotificationStateTx:
  180. notification_message(subghz->notifications, &sequence_blink_magenta_10);
  181. break;
  182. case SubGhzNotificationStateRx:
  183. notification_message(subghz->notifications, &sequence_blink_cyan_10);
  184. break;
  185. case SubGhzNotificationStateRxDone:
  186. notification_message(subghz->notifications, &sequence_blink_green_100);
  187. subghz->state_notifications = SubGhzNotificationStateRx;
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. return false;
  194. }
  195. void subghz_scene_receiver_info_on_exit(void* context) {
  196. SubGhz* subghz = context;
  197. widget_reset(subghz->widget);
  198. }