weather_station_app.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "weather_station_app_i.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include "protocols/protocol_items.h"
  5. static bool weather_station_app_custom_event_callback(void* context, uint32_t event) {
  6. furi_assert(context);
  7. WeatherStationApp* app = context;
  8. return scene_manager_handle_custom_event(app->scene_manager, event);
  9. }
  10. static bool weather_station_app_back_event_callback(void* context) {
  11. furi_assert(context);
  12. WeatherStationApp* app = context;
  13. return scene_manager_handle_back_event(app->scene_manager);
  14. }
  15. static void weather_station_app_tick_event_callback(void* context) {
  16. furi_assert(context);
  17. WeatherStationApp* app = context;
  18. scene_manager_handle_tick_event(app->scene_manager);
  19. }
  20. WeatherStationApp* weather_station_app_alloc() {
  21. WeatherStationApp* app = malloc(sizeof(WeatherStationApp));
  22. // GUI
  23. app->gui = furi_record_open(RECORD_GUI);
  24. // View Dispatcher
  25. app->view_dispatcher = view_dispatcher_alloc();
  26. app->scene_manager = scene_manager_alloc(&weather_station_scene_handlers, app);
  27. view_dispatcher_enable_queue(app->view_dispatcher);
  28. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  29. view_dispatcher_set_custom_event_callback(
  30. app->view_dispatcher, weather_station_app_custom_event_callback);
  31. view_dispatcher_set_navigation_event_callback(
  32. app->view_dispatcher, weather_station_app_back_event_callback);
  33. view_dispatcher_set_tick_event_callback(
  34. app->view_dispatcher, weather_station_app_tick_event_callback, 100);
  35. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  36. // Open Notification record
  37. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  38. // Variable Item List
  39. app->variable_item_list = variable_item_list_alloc();
  40. view_dispatcher_add_view(
  41. app->view_dispatcher,
  42. WeatherStationViewVariableItemList,
  43. variable_item_list_get_view(app->variable_item_list));
  44. // SubMenu
  45. app->submenu = submenu_alloc();
  46. view_dispatcher_add_view(
  47. app->view_dispatcher, WeatherStationViewSubmenu, submenu_get_view(app->submenu));
  48. // Widget
  49. app->widget = widget_alloc();
  50. view_dispatcher_add_view(
  51. app->view_dispatcher, WeatherStationViewWidget, widget_get_view(app->widget));
  52. // Receiver
  53. app->ws_receiver = ws_view_receiver_alloc();
  54. view_dispatcher_add_view(
  55. app->view_dispatcher,
  56. WeatherStationViewReceiver,
  57. ws_view_receiver_get_view(app->ws_receiver));
  58. // Receiver Info
  59. app->ws_receiver_info = ws_view_receiver_info_alloc();
  60. view_dispatcher_add_view(
  61. app->view_dispatcher,
  62. WeatherStationViewReceiverInfo,
  63. ws_view_receiver_info_get_view(app->ws_receiver_info));
  64. //init setting
  65. app->setting = subghz_setting_alloc();
  66. //ToDo FIX file name setting
  67. subghz_setting_load(app->setting, EXT_PATH("subghz/assets/setting_user"));
  68. //init Worker & Protocol & History
  69. app->lock = WSLockOff;
  70. app->txrx = malloc(sizeof(WeatherStationTxRx));
  71. app->txrx->preset = malloc(sizeof(SubGhzRadioPreset));
  72. app->txrx->preset->name = furi_string_alloc();
  73. ws_preset_init(app, "AM650", subghz_setting_get_default_frequency(app->setting), NULL, 0);
  74. app->txrx->hopper_state = WSHopperStateOFF;
  75. app->txrx->history = ws_history_alloc();
  76. app->txrx->worker = subghz_worker_alloc();
  77. app->txrx->environment = subghz_environment_alloc();
  78. subghz_environment_set_protocol_registry(
  79. app->txrx->environment, (void*)&weather_station_protocol_registry);
  80. app->txrx->receiver = subghz_receiver_alloc_init(app->txrx->environment);
  81. subghz_devices_init();
  82. app->txrx->radio_device =
  83. radio_device_loader_set(app->txrx->radio_device, SubGhzRadioDeviceTypeExternalCC1101);
  84. subghz_devices_reset(app->txrx->radio_device);
  85. subghz_devices_idle(app->txrx->radio_device);
  86. subghz_receiver_set_filter(app->txrx->receiver, SubGhzProtocolFlag_Decodable);
  87. subghz_worker_set_overrun_callback(
  88. app->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  89. subghz_worker_set_pair_callback(
  90. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  91. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  92. furi_hal_power_suppress_charge_enter();
  93. scene_manager_next_scene(app->scene_manager, WeatherStationSceneStart);
  94. return app;
  95. }
  96. void weather_station_app_free(WeatherStationApp* app) {
  97. furi_assert(app);
  98. subghz_devices_sleep(app->txrx->radio_device);
  99. radio_device_loader_end(app->txrx->radio_device);
  100. subghz_devices_deinit();
  101. // Submenu
  102. view_dispatcher_remove_view(app->view_dispatcher, WeatherStationViewSubmenu);
  103. submenu_free(app->submenu);
  104. // Variable Item List
  105. view_dispatcher_remove_view(app->view_dispatcher, WeatherStationViewVariableItemList);
  106. variable_item_list_free(app->variable_item_list);
  107. // Widget
  108. view_dispatcher_remove_view(app->view_dispatcher, WeatherStationViewWidget);
  109. widget_free(app->widget);
  110. // Receiver
  111. view_dispatcher_remove_view(app->view_dispatcher, WeatherStationViewReceiver);
  112. ws_view_receiver_free(app->ws_receiver);
  113. // Receiver Info
  114. view_dispatcher_remove_view(app->view_dispatcher, WeatherStationViewReceiverInfo);
  115. ws_view_receiver_info_free(app->ws_receiver_info);
  116. //setting
  117. subghz_setting_free(app->setting);
  118. //Worker & Protocol & History
  119. subghz_receiver_free(app->txrx->receiver);
  120. subghz_environment_free(app->txrx->environment);
  121. ws_history_free(app->txrx->history);
  122. subghz_worker_free(app->txrx->worker);
  123. furi_string_free(app->txrx->preset->name);
  124. free(app->txrx->preset);
  125. free(app->txrx);
  126. // View dispatcher
  127. view_dispatcher_free(app->view_dispatcher);
  128. scene_manager_free(app->scene_manager);
  129. // Notifications
  130. furi_record_close(RECORD_NOTIFICATION);
  131. app->notifications = NULL;
  132. // Close records
  133. furi_record_close(RECORD_GUI);
  134. furi_hal_power_suppress_charge_exit();
  135. free(app);
  136. }
  137. int32_t weather_station_app(void* p) {
  138. UNUSED(p);
  139. WeatherStationApp* weather_station_app = weather_station_app_alloc();
  140. view_dispatcher_run(weather_station_app->view_dispatcher);
  141. weather_station_app_free(weather_station_app);
  142. return 0;
  143. }