subghz_scene_read_raw.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "../subghz_i.h"
  2. #include "../views/subghz_read_raw.h"
  3. #include <lib/subghz/protocols/subghz_protocol_raw.h>
  4. #include <lib/subghz/subghz_parser.h>
  5. static void subghz_scene_read_raw_update_statusbar(void* context) {
  6. furi_assert(context);
  7. SubGhz* subghz = context;
  8. string_t frequency_str;
  9. string_t modulation_str;
  10. string_init(frequency_str);
  11. string_init(modulation_str);
  12. subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
  13. subghz_read_raw_add_data_statusbar(
  14. subghz->subghz_read_raw, string_get_cstr(frequency_str), string_get_cstr(modulation_str));
  15. string_clear(frequency_str);
  16. string_clear(modulation_str);
  17. }
  18. void subghz_scene_read_raw_callback(SubghzCustomEvent event, void* context) {
  19. furi_assert(context);
  20. SubGhz* subghz = context;
  21. view_dispatcher_send_custom_event(subghz->view_dispatcher, event);
  22. }
  23. void subghz_scene_read_raw_on_enter(void* context) {
  24. SubGhz* subghz = context;
  25. if(subghz->txrx->rx_key_state == SubGhzRxKeyStateNeedSave) {
  26. view_dispatcher_send_custom_event(
  27. subghz->view_dispatcher, SubghzCustomEventViewReadRAWMore);
  28. } else {
  29. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  30. }
  31. subghz_scene_read_raw_update_statusbar(subghz);
  32. subghz_read_raw_set_callback(subghz->subghz_read_raw, subghz_scene_read_raw_callback, subghz);
  33. subghz->txrx->protocol_result = subghz_parser_get_by_name(subghz->txrx->parser, "RAW");
  34. furi_assert(subghz->txrx->protocol_result);
  35. subghz_worker_set_pair_callback(
  36. subghz->txrx->worker, (SubGhzWorkerPairCallback)subghz_parser_raw_parse);
  37. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewReadRAW);
  38. }
  39. bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
  40. SubGhz* subghz = context;
  41. if(event.type == SceneManagerEventTypeCustom) {
  42. switch(event.event) {
  43. case SubghzCustomEventViewReadRAWBack:
  44. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  45. subghz_rx_end(subghz);
  46. subghz_sleep(subghz);
  47. };
  48. subghz->txrx->frequency = subghz_frequencies[subghz_frequencies_433_92];
  49. subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
  50. subghz_protocol_raw_save_to_file_stop(
  51. (SubGhzProtocolRAW*)subghz->txrx->protocol_result);
  52. subghz->state_notifications = SubGhzNotificationStateIDLE;
  53. if(subghz->txrx->rx_key_state == SubGhzRxKeyStateAddKey) {
  54. subghz->txrx->rx_key_state = SubGhzRxKeyStateExit;
  55. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  56. } else {
  57. scene_manager_search_and_switch_to_previous_scene(
  58. subghz->scene_manager, SubGhzSceneStart);
  59. }
  60. return true;
  61. break;
  62. case SubghzCustomEventViewReadRAWConfig:
  63. scene_manager_set_scene_state(
  64. subghz->scene_manager, SubGhzSceneReadRAW, SubghzCustomEventManagerSet);
  65. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverConfig);
  66. return true;
  67. break;
  68. case SubghzCustomEventViewReadRAWIDLE:
  69. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  70. subghz_rx_end(subghz);
  71. subghz_sleep(subghz);
  72. };
  73. subghz_protocol_raw_save_to_file_stop(
  74. (SubGhzProtocolRAW*)subghz->txrx->protocol_result);
  75. subghz->state_notifications = SubGhzNotificationStateIDLE;
  76. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  77. return true;
  78. break;
  79. case SubghzCustomEventViewReadRAWREC:
  80. if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
  81. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  82. } else {
  83. subghz_get_preset_name(subghz, subghz->error_str);
  84. if(subghz_protocol_raw_save_to_file_init(
  85. (SubGhzProtocolRAW*)subghz->txrx->protocol_result,
  86. "Raw_temp",
  87. subghz->txrx->frequency,
  88. string_get_cstr(subghz->error_str))) {
  89. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  90. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  91. subghz_begin(subghz, subghz->txrx->preset);
  92. subghz_rx(subghz, subghz->txrx->frequency);
  93. }
  94. subghz->state_notifications = SubGhzNotificationStateRX;
  95. } else {
  96. string_set(subghz->error_str, "No SD card");
  97. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  98. }
  99. }
  100. return true;
  101. break;
  102. case SubghzCustomEventViewReadRAWMore:
  103. if(strcmp(
  104. subghz_protocol_raw_get_last_file_name(
  105. (SubGhzProtocolRAW*)subghz->txrx->protocol_result),
  106. "")) {
  107. strlcpy(
  108. subghz->file_name,
  109. subghz_protocol_raw_get_last_file_name(
  110. (SubGhzProtocolRAW*)subghz->txrx->protocol_result),
  111. strlen(subghz_protocol_raw_get_last_file_name(
  112. (SubGhzProtocolRAW*)subghz->txrx->protocol_result)) +
  113. 1);
  114. //set the path to read the file
  115. string_t temp_str;
  116. string_init_printf(
  117. temp_str,
  118. "%s/%s%s",
  119. SUBGHZ_APP_PATH_FOLDER,
  120. subghz->file_name,
  121. SUBGHZ_APP_EXTENSION);
  122. subghz_protocol_raw_set_last_file_name(
  123. (SubGhzProtocolRAW*)subghz->txrx->protocol_result, string_get_cstr(temp_str));
  124. string_clear(temp_str);
  125. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReadRAWMenu);
  126. }
  127. return true;
  128. break;
  129. default:
  130. break;
  131. }
  132. } else if(event.type == SceneManagerEventTypeTick) {
  133. switch(subghz->state_notifications) {
  134. case SubGhzNotificationStateRX:
  135. notification_message(subghz->notifications, &sequence_blink_blue_10);
  136. subghz_read_raw_update_sample_write(
  137. subghz->subghz_read_raw,
  138. subghz_protocol_raw_get_sample_write(
  139. (SubGhzProtocolRAW*)subghz->txrx->protocol_result));
  140. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, furi_hal_subghz_get_rssi());
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. return false;
  147. }
  148. void subghz_scene_read_raw_on_exit(void* context) {
  149. SubGhz* subghz = context;
  150. //Stop CC1101
  151. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  152. subghz_rx_end(subghz);
  153. subghz_sleep(subghz);
  154. };
  155. subghz->state_notifications = SubGhzNotificationStateIDLE;
  156. //Сallback restoration
  157. subghz_worker_set_pair_callback(
  158. subghz->txrx->worker, (SubGhzWorkerPairCallback)subghz_parser_parse);
  159. }