xremote.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*!
  2. * @file flipper-xremote/xremote.c
  3. @license This project is released under the GNU GPLv3 License
  4. * @copyright (c) 2023 Sandro Kalatozishvili (s.kalatoz@gmail.com)
  5. *
  6. * @brief Entrypoint and factory of the XRemote main app.
  7. */
  8. #include "xremote.h"
  9. #include "xremote_learn.h"
  10. #include "xremote_control.h"
  11. #include "xremote_settings.h"
  12. #include "xremote_analyzer.h"
  13. #include "views/xremote_about_view.h"
  14. #include "views/xremote_learn_view.h"
  15. #include "views/xremote_signal_view.h"
  16. #include <infrared/infrared_settings.h>
  17. #include <toolbox/saved_struct.h>
  18. #define TAG "XRemote"
  19. void xremote_get_version(char* version, size_t length) {
  20. snprintf(
  21. version,
  22. length,
  23. "%d.%d.%d",
  24. XREMOTE_VERSION_MAJ,
  25. XREMOTE_VERSION_MIN,
  26. XREMOTE_BUILD_NUMBER);
  27. }
  28. static uint32_t xremote_view_exit_callback(void* context) {
  29. UNUSED(context);
  30. return XRemoteViewSubmenu;
  31. }
  32. static uint32_t xremote_exit_callback(void* context) {
  33. UNUSED(context);
  34. return VIEW_NONE;
  35. }
  36. static void xremote_child_clear_callback(void* context) {
  37. xremote_app_assert_void(context);
  38. xremote_app_free((XRemoteApp*)context);
  39. }
  40. static XRemoteApp* xremote_about_alloc(XRemoteAppContext* app_ctx) {
  41. XRemoteApp* app = xremote_app_alloc(app_ctx);
  42. xremote_app_view_alloc(app, XRemoteViewAbout, xremote_about_view_alloc);
  43. xremote_app_view_set_previous_callback(app, xremote_view_exit_callback);
  44. return app;
  45. }
  46. void xremote_submenu_callback(void* context, uint32_t index) {
  47. furi_assert(context);
  48. XRemoteApp* app = (XRemoteApp*)context;
  49. /* Cleanup previous app first */
  50. xremote_app_user_context_free(app);
  51. XRemoteApp* child = NULL;
  52. /* Allocate child app and view based on submenu selection */
  53. if(index == XRemoteViewLearn)
  54. child = xremote_learn_alloc(app->app_ctx);
  55. else if(index == XRemoteViewIRSubmenu)
  56. child = xremote_control_alloc(app->app_ctx);
  57. else if(index == XRemoteViewAnalyzer)
  58. child = xremote_analyzer_alloc(app->app_ctx);
  59. else if(index == XRemoteViewSettings)
  60. child = xremote_settings_alloc(app->app_ctx);
  61. else if(index == XRemoteViewAbout)
  62. child = xremote_about_alloc(app->app_ctx);
  63. if(child != NULL) {
  64. /* Switch to the view of newely allocated app */
  65. xremote_app_set_user_context(app, child, xremote_child_clear_callback);
  66. xremote_app_switch_to_view(child, index);
  67. }
  68. }
  69. int32_t xremote_main(void* p) {
  70. /* Allocate context and main application */
  71. XRemoteAppContext* context = xremote_app_context_alloc(p);
  72. XRemoteApp* app = xremote_app_alloc(context);
  73. /* Allocate and build the menu */
  74. xremote_app_submenu_alloc(app, XRemoteViewSubmenu, xremote_exit_callback);
  75. xremote_app_submenu_add(app, "Learn", XRemoteViewLearn, xremote_submenu_callback);
  76. xremote_app_submenu_add(app, "Saved", XRemoteViewIRSubmenu, xremote_submenu_callback);
  77. xremote_app_submenu_add(app, "Analyzer", XRemoteViewAnalyzer, xremote_submenu_callback);
  78. xremote_app_submenu_add(app, "Settings", XRemoteViewSettings, xremote_submenu_callback);
  79. xremote_app_submenu_add(app, "About", XRemoteViewAbout, xremote_submenu_callback);
  80. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  81. InfraredSettings settings = {0};
  82. infrared_settings_load(&settings);
  83. if(settings.tx_pin < FuriHalInfraredTxPinMax) {
  84. furi_hal_infrared_set_tx_output(settings.tx_pin);
  85. if(settings.otg_enabled != otg_was_enabled) {
  86. if(settings.otg_enabled) {
  87. furi_hal_power_enable_otg();
  88. } else {
  89. furi_hal_power_disable_otg();
  90. }
  91. }
  92. } else {
  93. FuriHalInfraredTxPin tx_pin_detected = furi_hal_infrared_detect_tx_output();
  94. furi_hal_infrared_set_tx_output(tx_pin_detected);
  95. if(tx_pin_detected != FuriHalInfraredTxPinInternal) {
  96. furi_hal_power_enable_otg();
  97. }
  98. }
  99. /* Switch to main menu by default and run disparcher*/
  100. xremote_app_switch_to_view(app, XRemoteViewSubmenu);
  101. view_dispatcher_run(app->app_ctx->view_dispatcher);
  102. furi_hal_infrared_set_tx_output(FuriHalInfraredTxPinInternal);
  103. if(furi_hal_power_is_otg_enabled() != otg_was_enabled) {
  104. if(otg_was_enabled) {
  105. furi_hal_power_enable_otg();
  106. } else {
  107. furi_hal_power_disable_otg();
  108. }
  109. }
  110. /* Cleanup and exit */
  111. xremote_app_free(app);
  112. xremote_app_context_free(context);
  113. return 0;
  114. }