tpms_app.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "tpms_app_i.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include "protocols/protocol_items.h"
  5. static bool tpms_app_custom_event_callback(void* context, uint32_t event) {
  6. furi_assert(context);
  7. TPMSApp* app = context;
  8. return scene_manager_handle_custom_event(app->scene_manager, event);
  9. }
  10. static bool tpms_app_back_event_callback(void* context) {
  11. furi_assert(context);
  12. TPMSApp* app = context;
  13. return scene_manager_handle_back_event(app->scene_manager);
  14. }
  15. static void tpms_app_tick_event_callback(void* context) {
  16. furi_assert(context);
  17. TPMSApp* app = context;
  18. scene_manager_handle_tick_event(app->scene_manager);
  19. }
  20. TPMSApp* tpms_app_alloc() {
  21. TPMSApp* app = malloc(sizeof(TPMSApp));
  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(&tpms_scene_handlers, app);
  27. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  28. view_dispatcher_set_custom_event_callback(
  29. app->view_dispatcher, tpms_app_custom_event_callback);
  30. view_dispatcher_set_navigation_event_callback(
  31. app->view_dispatcher, tpms_app_back_event_callback);
  32. view_dispatcher_set_tick_event_callback(
  33. app->view_dispatcher, tpms_app_tick_event_callback, 100);
  34. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  35. // Open Notification record
  36. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  37. // Variable Item List
  38. app->variable_item_list = variable_item_list_alloc();
  39. view_dispatcher_add_view(
  40. app->view_dispatcher,
  41. TPMSViewVariableItemList,
  42. variable_item_list_get_view(app->variable_item_list));
  43. // SubMenu
  44. app->submenu = submenu_alloc();
  45. view_dispatcher_add_view(
  46. app->view_dispatcher, TPMSViewSubmenu, submenu_get_view(app->submenu));
  47. // Widget
  48. app->widget = widget_alloc();
  49. view_dispatcher_add_view(app->view_dispatcher, TPMSViewWidget, widget_get_view(app->widget));
  50. // Receiver
  51. app->tpms_receiver = tpms_view_receiver_alloc();
  52. view_dispatcher_add_view(
  53. app->view_dispatcher, TPMSViewReceiver, tpms_view_receiver_get_view(app->tpms_receiver));
  54. // Receiver Info
  55. app->tpms_receiver_info = tpms_view_receiver_info_alloc();
  56. view_dispatcher_add_view(
  57. app->view_dispatcher,
  58. TPMSViewReceiverInfo,
  59. tpms_view_receiver_info_get_view(app->tpms_receiver_info));
  60. //init setting
  61. app->setting = subghz_setting_alloc();
  62. //ToDo FIX file name setting
  63. subghz_setting_load(app->setting, EXT_PATH("subghz/assets/setting_user"));
  64. //init Worker & Protocol & History
  65. app->lock = TPMSLockOff;
  66. app->txrx = malloc(sizeof(TPMSTxRx));
  67. app->txrx->preset = malloc(sizeof(SubGhzRadioPreset));
  68. app->txrx->preset->name = furi_string_alloc();
  69. tpms_preset_init(app, "AM650", subghz_setting_get_default_frequency(app->setting), NULL, 0);
  70. app->txrx->hopper_state = TPMSHopperStateOFF;
  71. app->txrx->history = tpms_history_alloc();
  72. app->txrx->worker = subghz_worker_alloc();
  73. app->txrx->environment = subghz_environment_alloc();
  74. subghz_environment_set_protocol_registry(
  75. app->txrx->environment, (void*)&tpms_protocol_registry);
  76. app->txrx->receiver = subghz_receiver_alloc_init(app->txrx->environment);
  77. subghz_devices_init();
  78. app->txrx->radio_device =
  79. radio_device_loader_set(app->txrx->radio_device, SubGhzRadioDeviceTypeExternalCC1101);
  80. subghz_devices_reset(app->txrx->radio_device);
  81. subghz_devices_idle(app->txrx->radio_device);
  82. subghz_receiver_set_filter(app->txrx->receiver, SubGhzProtocolFlag_Decodable);
  83. subghz_worker_set_overrun_callback(
  84. app->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  85. subghz_worker_set_pair_callback(
  86. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  87. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  88. furi_hal_power_suppress_charge_enter();
  89. scene_manager_next_scene(app->scene_manager, TPMSSceneReceiver);
  90. return app;
  91. }
  92. void tpms_app_free(TPMSApp* app) {
  93. furi_assert(app);
  94. subghz_devices_sleep(app->txrx->radio_device);
  95. radio_device_loader_end(app->txrx->radio_device);
  96. subghz_devices_deinit();
  97. // Submenu
  98. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewSubmenu);
  99. submenu_free(app->submenu);
  100. // Variable Item List
  101. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewVariableItemList);
  102. variable_item_list_free(app->variable_item_list);
  103. // Widget
  104. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewWidget);
  105. widget_free(app->widget);
  106. // Receiver
  107. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewReceiver);
  108. tpms_view_receiver_free(app->tpms_receiver);
  109. // Receiver Info
  110. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewReceiverInfo);
  111. tpms_view_receiver_info_free(app->tpms_receiver_info);
  112. //setting
  113. subghz_setting_free(app->setting);
  114. //Worker & Protocol & History
  115. subghz_receiver_free(app->txrx->receiver);
  116. subghz_environment_free(app->txrx->environment);
  117. tpms_history_free(app->txrx->history);
  118. subghz_worker_free(app->txrx->worker);
  119. furi_string_free(app->txrx->preset->name);
  120. free(app->txrx->preset);
  121. free(app->txrx);
  122. // View dispatcher
  123. view_dispatcher_free(app->view_dispatcher);
  124. scene_manager_free(app->scene_manager);
  125. // Notifications
  126. furi_record_close(RECORD_NOTIFICATION);
  127. app->notifications = NULL;
  128. // Close records
  129. furi_record_close(RECORD_GUI);
  130. furi_hal_power_suppress_charge_exit();
  131. free(app);
  132. }
  133. int32_t tpms_app(void* p) {
  134. UNUSED(p);
  135. TPMSApp* tpms_app = tpms_app_alloc();
  136. view_dispatcher_run(tpms_app->view_dispatcher);
  137. tpms_app_free(tpms_app);
  138. return 0;
  139. }