weather_station_receiver.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "../weather_station_app_i.h"
  2. #include "../views/weather_station_receiver.h"
  3. static const NotificationSequence subghs_sequence_rx = {
  4. &message_green_255,
  5. &message_vibro_on,
  6. &message_note_c6,
  7. &message_delay_50,
  8. &message_sound_off,
  9. &message_vibro_off,
  10. &message_delay_50,
  11. NULL,
  12. };
  13. static const NotificationSequence subghs_sequence_rx_locked = {
  14. &message_green_255,
  15. &message_display_backlight_on,
  16. &message_vibro_on,
  17. &message_note_c6,
  18. &message_delay_50,
  19. &message_sound_off,
  20. &message_vibro_off,
  21. &message_delay_500,
  22. &message_display_backlight_off,
  23. NULL,
  24. };
  25. static void weather_station_scene_receiver_update_statusbar(void* context) {
  26. WeatherStationApp* app = context;
  27. FuriString* history_stat_str;
  28. history_stat_str = furi_string_alloc();
  29. if(!ws_history_get_text_space_left(app->txrx->history, history_stat_str)) {
  30. FuriString* frequency_str;
  31. FuriString* modulation_str;
  32. frequency_str = furi_string_alloc();
  33. modulation_str = furi_string_alloc();
  34. ws_get_frequency_modulation(app, frequency_str, modulation_str);
  35. ws_view_receiver_add_data_statusbar(
  36. app->ws_receiver,
  37. furi_string_get_cstr(frequency_str),
  38. furi_string_get_cstr(modulation_str),
  39. furi_string_get_cstr(history_stat_str),
  40. radio_device_loader_is_external(app->txrx->radio_device));
  41. furi_string_free(frequency_str);
  42. furi_string_free(modulation_str);
  43. } else {
  44. ws_view_receiver_add_data_statusbar(
  45. app->ws_receiver,
  46. furi_string_get_cstr(history_stat_str),
  47. "",
  48. "",
  49. radio_device_loader_is_external(app->txrx->radio_device));
  50. }
  51. furi_string_free(history_stat_str);
  52. }
  53. void weather_station_scene_receiver_callback(WSCustomEvent event, void* context) {
  54. furi_assert(context);
  55. WeatherStationApp* app = context;
  56. view_dispatcher_send_custom_event(app->view_dispatcher, event);
  57. }
  58. static void weather_station_scene_receiver_add_to_history_callback(
  59. SubGhzReceiver* receiver,
  60. SubGhzProtocolDecoderBase* decoder_base,
  61. void* context) {
  62. furi_assert(context);
  63. WeatherStationApp* app = context;
  64. FuriString* str_buff;
  65. str_buff = furi_string_alloc();
  66. if(ws_history_add_to_history(app->txrx->history, decoder_base, app->txrx->preset) ==
  67. WSHistoryStateAddKeyNewDada) {
  68. furi_string_reset(str_buff);
  69. ws_history_get_text_item_menu(
  70. app->txrx->history, str_buff, ws_history_get_item(app->txrx->history) - 1);
  71. ws_view_receiver_add_item_to_menu(
  72. app->ws_receiver,
  73. furi_string_get_cstr(str_buff),
  74. ws_history_get_type_protocol(
  75. app->txrx->history, ws_history_get_item(app->txrx->history) - 1));
  76. weather_station_scene_receiver_update_statusbar(app);
  77. notification_message(app->notifications, &sequence_blink_green_10);
  78. if(app->lock != WSLockOn) {
  79. notification_message(app->notifications, &subghs_sequence_rx);
  80. } else {
  81. notification_message(app->notifications, &subghs_sequence_rx_locked);
  82. }
  83. }
  84. subghz_receiver_reset(receiver);
  85. furi_string_free(str_buff);
  86. app->txrx->rx_key_state = WSRxKeyStateAddKey;
  87. }
  88. void weather_station_scene_receiver_on_enter(void* context) {
  89. WeatherStationApp* app = context;
  90. FuriString* str_buff;
  91. str_buff = furi_string_alloc();
  92. if(app->txrx->rx_key_state == WSRxKeyStateIDLE) {
  93. ws_preset_init(app, "AM650", subghz_setting_get_default_frequency(app->setting), NULL, 0);
  94. ws_history_reset(app->txrx->history);
  95. app->txrx->rx_key_state = WSRxKeyStateStart;
  96. }
  97. ws_view_receiver_set_lock(app->ws_receiver, app->lock);
  98. //Load history to receiver
  99. ws_view_receiver_exit(app->ws_receiver);
  100. for(uint8_t i = 0; i < ws_history_get_item(app->txrx->history); i++) {
  101. furi_string_reset(str_buff);
  102. ws_history_get_text_item_menu(app->txrx->history, str_buff, i);
  103. ws_view_receiver_add_item_to_menu(
  104. app->ws_receiver,
  105. furi_string_get_cstr(str_buff),
  106. ws_history_get_type_protocol(app->txrx->history, i));
  107. app->txrx->rx_key_state = WSRxKeyStateAddKey;
  108. }
  109. furi_string_free(str_buff);
  110. weather_station_scene_receiver_update_statusbar(app);
  111. ws_view_receiver_set_callback(app->ws_receiver, weather_station_scene_receiver_callback, app);
  112. subghz_receiver_set_rx_callback(
  113. app->txrx->receiver, weather_station_scene_receiver_add_to_history_callback, app);
  114. if(app->txrx->txrx_state == WSTxRxStateRx) {
  115. ws_rx_end(app);
  116. };
  117. if((app->txrx->txrx_state == WSTxRxStateIDLE) || (app->txrx->txrx_state == WSTxRxStateSleep)) {
  118. ws_begin(
  119. app,
  120. subghz_setting_get_preset_data_by_name(
  121. app->setting, furi_string_get_cstr(app->txrx->preset->name)));
  122. ws_rx(app, app->txrx->preset->frequency);
  123. }
  124. ws_view_receiver_set_idx_menu(app->ws_receiver, app->txrx->idx_menu_chosen);
  125. view_dispatcher_switch_to_view(app->view_dispatcher, WeatherStationViewReceiver);
  126. }
  127. bool weather_station_scene_receiver_on_event(void* context, SceneManagerEvent event) {
  128. WeatherStationApp* app = context;
  129. bool consumed = false;
  130. if(event.type == SceneManagerEventTypeCustom) {
  131. switch(event.event) {
  132. case WSCustomEventViewReceiverBack:
  133. // Stop CC1101 Rx
  134. if(app->txrx->txrx_state == WSTxRxStateRx) {
  135. ws_rx_end(app);
  136. ws_sleep(app);
  137. };
  138. app->txrx->hopper_state = WSHopperStateOFF;
  139. app->txrx->idx_menu_chosen = 0;
  140. subghz_receiver_set_rx_callback(app->txrx->receiver, NULL, app);
  141. app->txrx->rx_key_state = WSRxKeyStateIDLE;
  142. ws_preset_init(
  143. app, "AM650", subghz_setting_get_default_frequency(app->setting), NULL, 0);
  144. scene_manager_search_and_switch_to_previous_scene(
  145. app->scene_manager, WeatherStationSceneStart);
  146. consumed = true;
  147. break;
  148. case WSCustomEventViewReceiverOK:
  149. app->txrx->idx_menu_chosen = ws_view_receiver_get_idx_menu(app->ws_receiver);
  150. scene_manager_next_scene(app->scene_manager, WeatherStationSceneReceiverInfo);
  151. consumed = true;
  152. break;
  153. case WSCustomEventViewReceiverConfig:
  154. app->txrx->idx_menu_chosen = ws_view_receiver_get_idx_menu(app->ws_receiver);
  155. scene_manager_next_scene(app->scene_manager, WeatherStationSceneReceiverConfig);
  156. consumed = true;
  157. break;
  158. case WSCustomEventViewReceiverOffDisplay:
  159. notification_message(app->notifications, &sequence_display_backlight_off);
  160. consumed = true;
  161. break;
  162. case WSCustomEventViewReceiverUnlock:
  163. app->lock = WSLockOff;
  164. consumed = true;
  165. break;
  166. default:
  167. break;
  168. }
  169. } else if(event.type == SceneManagerEventTypeTick) {
  170. if(app->txrx->hopper_state != WSHopperStateOFF) {
  171. ws_hopper_update(app);
  172. weather_station_scene_receiver_update_statusbar(app);
  173. }
  174. // Get current RSSI
  175. float rssi = subghz_devices_get_rssi(app->txrx->radio_device);
  176. ws_view_receiver_set_rssi(app->ws_receiver, rssi);
  177. if(app->txrx->txrx_state == WSTxRxStateRx) {
  178. notification_message(app->notifications, &sequence_blink_cyan_10);
  179. }
  180. }
  181. return consumed;
  182. }
  183. void weather_station_scene_receiver_on_exit(void* context) {
  184. UNUSED(context);
  185. }