subghz_scene_receiver_info.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. //todo we are trying to deserialize without checking for errors, since it is assumed that we just received this chignal
  24. subghz_protocol_decoder_base_deserialize(
  25. subghz->txrx->decoder_result,
  26. subghz_history_get_raw_data(subghz->txrx->history, subghz->txrx->idx_menu_chosen));
  27. SubGhzRadioPreset* preset =
  28. subghz_history_get_radio_preset(subghz->txrx->history, subghz->txrx->idx_menu_chosen);
  29. subghz_preset_init(
  30. subghz,
  31. furi_string_get_cstr(preset->name),
  32. preset->frequency,
  33. preset->data,
  34. preset->data_size);
  35. return true;
  36. }
  37. return false;
  38. }
  39. void subghz_scene_receiver_info_on_enter(void* context) {
  40. SubGhz* subghz = context;
  41. if(subghz_scene_receiver_info_update_parser(subghz)) {
  42. FuriString* frequency_str;
  43. FuriString* modulation_str;
  44. FuriString* text;
  45. frequency_str = furi_string_alloc();
  46. modulation_str = furi_string_alloc();
  47. text = furi_string_alloc();
  48. subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
  49. widget_add_string_element(
  50. subghz->widget,
  51. 78,
  52. 0,
  53. AlignLeft,
  54. AlignTop,
  55. FontSecondary,
  56. furi_string_get_cstr(frequency_str));
  57. widget_add_string_element(
  58. subghz->widget,
  59. 113,
  60. 0,
  61. AlignLeft,
  62. AlignTop,
  63. FontSecondary,
  64. furi_string_get_cstr(modulation_str));
  65. subghz_protocol_decoder_base_get_string(subghz->txrx->decoder_result, text);
  66. widget_add_string_multiline_element(
  67. subghz->widget, 0, 0, AlignLeft, AlignTop, FontSecondary, furi_string_get_cstr(text));
  68. furi_string_free(frequency_str);
  69. furi_string_free(modulation_str);
  70. furi_string_free(text);
  71. if((subghz->txrx->decoder_result->protocol->flag & SubGhzProtocolFlag_Save) ==
  72. SubGhzProtocolFlag_Save) {
  73. widget_add_button_element(
  74. subghz->widget,
  75. GuiButtonTypeRight,
  76. "Save",
  77. subghz_scene_receiver_info_callback,
  78. subghz);
  79. }
  80. if(((subghz->txrx->decoder_result->protocol->flag & SubGhzProtocolFlag_Send) ==
  81. SubGhzProtocolFlag_Send) &&
  82. subghz->txrx->decoder_result->protocol->encoder->deserialize &&
  83. subghz->txrx->decoder_result->protocol->type == SubGhzProtocolTypeStatic) {
  84. widget_add_button_element(
  85. subghz->widget,
  86. GuiButtonTypeCenter,
  87. "Send",
  88. subghz_scene_receiver_info_callback,
  89. subghz);
  90. }
  91. } else {
  92. widget_add_icon_element(subghz->widget, 37, 15, &I_DolphinCommon_56x48);
  93. widget_add_string_element(
  94. subghz->widget, 13, 8, AlignLeft, AlignBottom, FontSecondary, "Error history parse.");
  95. }
  96. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdWidget);
  97. }
  98. bool subghz_scene_receiver_info_on_event(void* context, SceneManagerEvent event) {
  99. SubGhz* subghz = context;
  100. if(event.type == SceneManagerEventTypeCustom) {
  101. if(event.event == SubGhzCustomEventSceneReceiverInfoTxStart) {
  102. //CC1101 Stop RX -> Start TX
  103. if(subghz->txrx->hopper_state != SubGhzHopperStateOFF) {
  104. subghz->txrx->hopper_state = SubGhzHopperStatePause;
  105. }
  106. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  107. subghz_rx_end(subghz);
  108. }
  109. if(!subghz_scene_receiver_info_update_parser(subghz)) {
  110. return false;
  111. }
  112. if(subghz->txrx->txrx_state == SubGhzTxRxStateIDLE ||
  113. subghz->txrx->txrx_state == SubGhzTxRxStateSleep) {
  114. if(!subghz_tx_start(
  115. subghz,
  116. subghz_history_get_raw_data(
  117. subghz->txrx->history, subghz->txrx->idx_menu_chosen))) {
  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. }