tpms_app.c 5.9 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_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, tpms_app_custom_event_callback);
  31. view_dispatcher_set_navigation_event_callback(
  32. app->view_dispatcher, tpms_app_back_event_callback);
  33. view_dispatcher_set_tick_event_callback(
  34. app->view_dispatcher, tpms_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. TPMSViewVariableItemList,
  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, TPMSViewSubmenu, submenu_get_view(app->submenu));
  48. // Widget
  49. app->widget = widget_alloc();
  50. view_dispatcher_add_view(app->view_dispatcher, TPMSViewWidget, widget_get_view(app->widget));
  51. // Receiver
  52. app->tpms_receiver = tpms_view_receiver_alloc();
  53. view_dispatcher_add_view(
  54. app->view_dispatcher, TPMSViewReceiver, tpms_view_receiver_get_view(app->tpms_receiver));
  55. // Receiver Info
  56. app->tpms_receiver_info = tpms_view_receiver_info_alloc();
  57. view_dispatcher_add_view(
  58. app->view_dispatcher,
  59. TPMSViewReceiverInfo,
  60. tpms_view_receiver_info_get_view(app->tpms_receiver_info));
  61. //init setting
  62. app->setting = subghz_setting_alloc();
  63. //ToDo FIX file name setting
  64. subghz_setting_load(app->setting, EXT_PATH("subghz/assets/setting_user"));
  65. //init Worker & Protocol & History
  66. app->lock = TPMSLockOff;
  67. app->txrx = malloc(sizeof(TPMSTxRx));
  68. app->txrx->preset = malloc(sizeof(SubGhzRadioPreset));
  69. app->txrx->preset->name = furi_string_alloc();
  70. tpms_preset_init(app, "AM650", subghz_setting_get_default_frequency(app->setting), NULL, 0);
  71. app->txrx->hopper_state = TPMSHopperStateOFF;
  72. app->txrx->history = tpms_history_alloc();
  73. app->txrx->worker = subghz_worker_alloc();
  74. app->txrx->environment = subghz_environment_alloc();
  75. subghz_environment_set_protocol_registry(
  76. app->txrx->environment, (void*)&tpms_protocol_registry);
  77. app->txrx->receiver = subghz_receiver_alloc_init(app->txrx->environment);
  78. subghz_devices_init();
  79. app->txrx->radio_device =
  80. radio_device_loader_set(app->txrx->radio_device, SubGhzRadioDeviceTypeExternalCC1101);
  81. subghz_devices_reset(app->txrx->radio_device);
  82. subghz_devices_idle(app->txrx->radio_device);
  83. subghz_receiver_set_filter(app->txrx->receiver, SubGhzProtocolFlag_Decodable);
  84. subghz_worker_set_overrun_callback(
  85. app->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  86. subghz_worker_set_pair_callback(
  87. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  88. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  89. furi_hal_power_suppress_charge_enter();
  90. scene_manager_next_scene(app->scene_manager, TPMSSceneReceiver);
  91. return app;
  92. }
  93. void tpms_app_free(TPMSApp* app) {
  94. furi_assert(app);
  95. subghz_devices_sleep(app->txrx->radio_device);
  96. radio_device_loader_end(app->txrx->radio_device);
  97. subghz_devices_deinit();
  98. // Submenu
  99. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewSubmenu);
  100. submenu_free(app->submenu);
  101. // Variable Item List
  102. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewVariableItemList);
  103. variable_item_list_free(app->variable_item_list);
  104. // Widget
  105. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewWidget);
  106. widget_free(app->widget);
  107. // Receiver
  108. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewReceiver);
  109. tpms_view_receiver_free(app->tpms_receiver);
  110. // Receiver Info
  111. view_dispatcher_remove_view(app->view_dispatcher, TPMSViewReceiverInfo);
  112. tpms_view_receiver_info_free(app->tpms_receiver_info);
  113. //setting
  114. subghz_setting_free(app->setting);
  115. //Worker & Protocol & History
  116. subghz_receiver_free(app->txrx->receiver);
  117. subghz_environment_free(app->txrx->environment);
  118. tpms_history_free(app->txrx->history);
  119. subghz_worker_free(app->txrx->worker);
  120. furi_string_free(app->txrx->preset->name);
  121. free(app->txrx->preset);
  122. free(app->txrx);
  123. // View dispatcher
  124. view_dispatcher_free(app->view_dispatcher);
  125. scene_manager_free(app->scene_manager);
  126. // Notifications
  127. furi_record_close(RECORD_NOTIFICATION);
  128. app->notifications = NULL;
  129. // Close records
  130. furi_record_close(RECORD_GUI);
  131. furi_hal_power_suppress_charge_exit();
  132. free(app);
  133. }
  134. int32_t tpms_app(void* p) {
  135. UNUSED(p);
  136. TPMSApp* tpms_app = tpms_app_alloc();
  137. view_dispatcher_run(tpms_app->view_dispatcher);
  138. tpms_app_free(tpms_app);
  139. return 0;
  140. }