weather_station_receiver.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. furi_string_free(frequency_str);
  41. furi_string_free(modulation_str);
  42. } else {
  43. ws_view_receiver_add_data_statusbar(
  44. app->ws_receiver, furi_string_get_cstr(history_stat_str), "", "");
  45. }
  46. furi_string_free(history_stat_str);
  47. }
  48. void weather_station_scene_receiver_callback(WSCustomEvent event, void* context) {
  49. furi_assert(context);
  50. WeatherStationApp* app = context;
  51. view_dispatcher_send_custom_event(app->view_dispatcher, event);
  52. }
  53. static void weather_station_scene_receiver_add_to_history_callback(
  54. SubGhzReceiver* receiver,
  55. SubGhzProtocolDecoderBase* decoder_base,
  56. void* context) {
  57. furi_assert(context);
  58. WeatherStationApp* app = context;
  59. FuriString* str_buff;
  60. str_buff = furi_string_alloc();
  61. if(ws_history_add_to_history(app->txrx->history, decoder_base, app->txrx->preset) ==
  62. WSHistoryStateAddKeyNewDada) {
  63. furi_string_reset(str_buff);
  64. ws_history_get_text_item_menu(
  65. app->txrx->history, str_buff, ws_history_get_item(app->txrx->history) - 1);
  66. ws_view_receiver_add_item_to_menu(
  67. app->ws_receiver,
  68. furi_string_get_cstr(str_buff),
  69. ws_history_get_type_protocol(
  70. app->txrx->history, ws_history_get_item(app->txrx->history) - 1));
  71. weather_station_scene_receiver_update_statusbar(app);
  72. notification_message(app->notifications, &sequence_blink_green_10);
  73. if(app->lock != WSLockOn) {
  74. notification_message(app->notifications, &subghs_sequence_rx);
  75. } else {
  76. notification_message(app->notifications, &subghs_sequence_rx_locked);
  77. }
  78. }
  79. subghz_receiver_reset(receiver);
  80. furi_string_free(str_buff);
  81. app->txrx->rx_key_state = WSRxKeyStateAddKey;
  82. }
  83. void weather_station_scene_receiver_on_enter(void* context) {
  84. WeatherStationApp* app = context;
  85. FuriString* str_buff;
  86. str_buff = furi_string_alloc();
  87. if(app->txrx->rx_key_state == WSRxKeyStateIDLE) {
  88. ws_preset_init(app, "AM650", subghz_setting_get_default_frequency(app->setting), NULL, 0);
  89. ws_history_reset(app->txrx->history);
  90. app->txrx->rx_key_state = WSRxKeyStateStart;
  91. }
  92. ws_view_receiver_set_lock(app->ws_receiver, app->lock);
  93. //Load history to receiver
  94. ws_view_receiver_exit(app->ws_receiver);
  95. for(uint8_t i = 0; i < ws_history_get_item(app->txrx->history); i++) {
  96. furi_string_reset(str_buff);
  97. ws_history_get_text_item_menu(app->txrx->history, str_buff, i);
  98. ws_view_receiver_add_item_to_menu(
  99. app->ws_receiver,
  100. furi_string_get_cstr(str_buff),
  101. ws_history_get_type_protocol(app->txrx->history, i));
  102. app->txrx->rx_key_state = WSRxKeyStateAddKey;
  103. }
  104. furi_string_free(str_buff);
  105. weather_station_scene_receiver_update_statusbar(app);
  106. ws_view_receiver_set_callback(app->ws_receiver, weather_station_scene_receiver_callback, app);
  107. subghz_receiver_set_rx_callback(
  108. app->txrx->receiver, weather_station_scene_receiver_add_to_history_callback, app);
  109. if(app->txrx->txrx_state == WSTxRxStateRx) {
  110. ws_rx_end(app);
  111. };
  112. if((app->txrx->txrx_state == WSTxRxStateIDLE) || (app->txrx->txrx_state == WSTxRxStateSleep)) {
  113. ws_begin(
  114. app,
  115. subghz_setting_get_preset_data_by_name(
  116. app->setting, furi_string_get_cstr(app->txrx->preset->name)));
  117. ws_rx(app, app->txrx->preset->frequency);
  118. }
  119. ws_view_receiver_set_idx_menu(app->ws_receiver, app->txrx->idx_menu_chosen);
  120. view_dispatcher_switch_to_view(app->view_dispatcher, WeatherStationViewReceiver);
  121. }
  122. bool weather_station_scene_receiver_on_event(void* context, SceneManagerEvent event) {
  123. WeatherStationApp* app = context;
  124. bool consumed = false;
  125. if(event.type == SceneManagerEventTypeCustom) {
  126. switch(event.event) {
  127. case WSCustomEventViewReceiverBack:
  128. // Stop CC1101 Rx
  129. if(app->txrx->txrx_state == WSTxRxStateRx) {
  130. ws_rx_end(app);
  131. ws_sleep(app);
  132. };
  133. app->txrx->hopper_state = WSHopperStateOFF;
  134. app->txrx->idx_menu_chosen = 0;
  135. subghz_receiver_set_rx_callback(app->txrx->receiver, NULL, app);
  136. app->txrx->rx_key_state = WSRxKeyStateIDLE;
  137. ws_preset_init(
  138. app, "AM650", subghz_setting_get_default_frequency(app->setting), NULL, 0);
  139. scene_manager_search_and_switch_to_previous_scene(
  140. app->scene_manager, WeatherStationSceneStart);
  141. consumed = true;
  142. break;
  143. case WSCustomEventViewReceiverOK:
  144. app->txrx->idx_menu_chosen = ws_view_receiver_get_idx_menu(app->ws_receiver);
  145. scene_manager_next_scene(app->scene_manager, WeatherStationSceneReceiverInfo);
  146. consumed = true;
  147. break;
  148. case WSCustomEventViewReceiverConfig:
  149. app->txrx->idx_menu_chosen = ws_view_receiver_get_idx_menu(app->ws_receiver);
  150. scene_manager_next_scene(app->scene_manager, WeatherStationSceneReceiverConfig);
  151. consumed = true;
  152. break;
  153. case WSCustomEventViewReceiverOffDisplay:
  154. notification_message(app->notifications, &sequence_display_backlight_off);
  155. consumed = true;
  156. break;
  157. case WSCustomEventViewReceiverUnlock:
  158. app->lock = WSLockOff;
  159. consumed = true;
  160. break;
  161. default:
  162. break;
  163. }
  164. } else if(event.type == SceneManagerEventTypeTick) {
  165. if(app->txrx->hopper_state != WSHopperStateOFF) {
  166. ws_hopper_update(app);
  167. weather_station_scene_receiver_update_statusbar(app);
  168. }
  169. if(app->txrx->txrx_state == WSTxRxStateRx) {
  170. notification_message(app->notifications, &sequence_blink_cyan_10);
  171. }
  172. }
  173. return consumed;
  174. }
  175. void weather_station_scene_receiver_on_exit(void* context) {
  176. UNUSED(context);
  177. }