xremote.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "xremote.h"
  2. #include <infrared/infrared_settings.h>
  3. bool xremote_custom_event_callback(void* context, uint32_t event) {
  4. furi_assert(context);
  5. XRemote* app = context;
  6. return scene_manager_handle_custom_event(app->scene_manager, event);
  7. }
  8. void xremote_tick_event_callback(void* context) {
  9. furi_assert(context);
  10. XRemote* app = context;
  11. scene_manager_handle_tick_event(app->scene_manager);
  12. }
  13. //leave app if back button pressed
  14. bool xremote_navigation_event_callback(void* context) {
  15. furi_assert(context);
  16. XRemote* app = context;
  17. return scene_manager_handle_back_event(app->scene_manager);
  18. }
  19. XRemote* xremote_app_alloc() {
  20. XRemote* app = malloc(sizeof(XRemote));
  21. app->gui = furi_record_open(RECORD_GUI);
  22. app->notification = furi_record_open(RECORD_NOTIFICATION);
  23. //Turn backlight on, believe me this makes testing your app easier
  24. notification_message(app->notification, &sequence_display_backlight_on);
  25. //Scene additions
  26. app->view_dispatcher = view_dispatcher_alloc();
  27. app->scene_manager = scene_manager_alloc(&xremote_scene_handlers, app);
  28. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  29. view_dispatcher_set_navigation_event_callback(
  30. app->view_dispatcher, xremote_navigation_event_callback);
  31. view_dispatcher_set_tick_event_callback(
  32. app->view_dispatcher, xremote_tick_event_callback, 100);
  33. view_dispatcher_set_custom_event_callback(app->view_dispatcher, xremote_custom_event_callback);
  34. app->submenu = submenu_alloc();
  35. app->editmenu = submenu_alloc();
  36. // Set defaults, in case no config loaded
  37. app->haptic = 1;
  38. app->speaker = 1;
  39. app->led = 1;
  40. app->save_settings = 1;
  41. app->transmitting = 0;
  42. app->ir_timing = 1000;
  43. app->ir_timing_char = "1000";
  44. app->sg_timing = 500;
  45. app->sg_timing_char = "500";
  46. app->stop_transmit = false;
  47. app->loop_transmit = 0;
  48. app->transmit_item = 0;
  49. // Load configs
  50. xremote_read_settings(app);
  51. app->dialogs = furi_record_open(RECORD_DIALOGS);
  52. app->file_path = furi_string_alloc();
  53. app->ir_remote_buffer = xremote_ir_remote_alloc();
  54. app->ir_worker = infrared_worker_alloc();
  55. app->cross_remote = xremote_cross_remote_alloc();
  56. app->sg_remote_buffer = xremote_sg_remote_alloc();
  57. app->loading = loading_alloc();
  58. app->subghz = subghz_alloc();
  59. app->text_input = text_input_alloc();
  60. app->number_input = number_input_alloc();
  61. view_dispatcher_add_view(
  62. app->view_dispatcher, XRemoteViewIdNumberInput, number_input_get_view(app->number_input));
  63. view_dispatcher_add_view(
  64. app->view_dispatcher, XRemoteViewIdTextInput, text_input_get_view(app->text_input));
  65. view_dispatcher_add_view(
  66. app->view_dispatcher, XRemoteViewIdMenu, submenu_get_view(app->submenu));
  67. view_dispatcher_add_view(
  68. app->view_dispatcher, XRemoteViewIdEditItem, submenu_get_view(app->editmenu));
  69. app->xremote_infoscreen = xremote_infoscreen_alloc();
  70. view_dispatcher_add_view(
  71. app->view_dispatcher,
  72. XRemoteViewIdInfoscreen,
  73. xremote_infoscreen_get_view(app->xremote_infoscreen));
  74. app->xremote_transmit = xremote_transmit_alloc();
  75. view_dispatcher_add_view(
  76. app->view_dispatcher,
  77. XRemoteViewIdTransmit,
  78. xremote_transmit_get_view(app->xremote_transmit));
  79. app->xremote_pause_set = xremote_pause_set_alloc();
  80. view_dispatcher_add_view(
  81. app->view_dispatcher,
  82. XRemoteViewIdPauseSet,
  83. xremote_pause_set_get_view(app->xremote_pause_set));
  84. app->button_menu_create = button_menu_alloc();
  85. view_dispatcher_add_view(
  86. app->view_dispatcher, XRemoteViewIdCreate, button_menu_get_view(app->button_menu_create));
  87. app->button_menu_create_add = button_menu_alloc();
  88. view_dispatcher_add_view(
  89. app->view_dispatcher,
  90. XRemoteViewIdCreateAdd,
  91. button_menu_get_view(app->button_menu_create_add));
  92. app->button_menu_ir = button_menu_alloc();
  93. view_dispatcher_add_view(
  94. app->view_dispatcher, XRemoteViewIdIrRemote, button_menu_get_view(app->button_menu_ir));
  95. app->variable_item_list = variable_item_list_alloc();
  96. view_dispatcher_add_view(
  97. app->view_dispatcher,
  98. XRemoteViewIdSettings,
  99. variable_item_list_get_view(app->variable_item_list));
  100. app->popup = popup_alloc();
  101. view_dispatcher_add_view(app->view_dispatcher, XRemoteViewIdWip, popup_get_view(app->popup));
  102. app->view_stack = view_stack_alloc();
  103. view_dispatcher_add_view(
  104. app->view_dispatcher, XRemoteViewIdStack, view_stack_get_view(app->view_stack));
  105. //End Scene Additions
  106. return app;
  107. }
  108. void xremote_app_free(XRemote* app) {
  109. furi_assert(app);
  110. // Scene manager
  111. scene_manager_free(app->scene_manager);
  112. infrared_worker_free(app->ir_worker);
  113. xremote_cross_remote_free(app->cross_remote);
  114. subghz_free(app->subghz);
  115. // View Dispatcher
  116. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdMenu);
  117. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdEditItem);
  118. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdInfoscreen);
  119. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdCreate);
  120. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdCreateAdd);
  121. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdSettings);
  122. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdWip);
  123. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdStack);
  124. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdTextInput);
  125. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdNumberInput);
  126. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdTransmit);
  127. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdPauseSet);
  128. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdIrRemote);
  129. text_input_free(app->text_input);
  130. number_input_free(app->number_input);
  131. button_menu_free(app->button_menu_create);
  132. button_menu_free(app->button_menu_create_add);
  133. button_menu_free(app->button_menu_ir);
  134. view_stack_free(app->view_stack);
  135. popup_free(app->popup);
  136. submenu_free(app->submenu);
  137. xremote_infoscreen_free(app->xremote_infoscreen);
  138. xremote_pause_set_free(app->xremote_pause_set);
  139. xremote_transmit_free(app->xremote_transmit);
  140. view_dispatcher_free(app->view_dispatcher);
  141. furi_record_close(RECORD_GUI);
  142. furi_record_close(RECORD_DIALOGS);
  143. furi_string_free(app->file_path);
  144. loading_free(app->loading);
  145. app->gui = NULL;
  146. app->notification = NULL;
  147. //Remove whatever is left
  148. free(app);
  149. }
  150. void xremote_popup_closed_callback(void* context) {
  151. furi_assert(context);
  152. XRemote* app = context;
  153. view_dispatcher_send_custom_event(app->view_dispatcher, XRemoteCustomEventTypePopupClosed);
  154. }
  155. void xremote_text_input_callback(void* context) {
  156. furi_assert(context);
  157. XRemote* app = context;
  158. view_dispatcher_send_custom_event(app->view_dispatcher, XRemoteCustomEventTextInput);
  159. }
  160. int32_t xremote_app(void* p) {
  161. UNUSED(p);
  162. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  163. XRemote* app = xremote_app_alloc();
  164. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  165. //scene_manager_next_scene(app->scene_manager, XRemoteSceneInfoscreen); //Start with start screen
  166. scene_manager_next_scene(
  167. app->scene_manager, XRemoteSceneMenu); //if you want to directly start with Menu
  168. furi_hal_power_suppress_charge_enter();
  169. Storage* storage = furi_record_open(RECORD_STORAGE);
  170. storage_common_mkdir(storage, XREMOTE_APP_FOLDER);
  171. furi_record_close(RECORD_STORAGE);
  172. if(strcmp(subghz_txrx_radio_device_get_name(app->subghz->txrx), "cc1101_ext") != 0) {
  173. InfraredSettings settings = {0};
  174. infrared_settings_load(&settings);
  175. if(settings.tx_pin < FuriHalInfraredTxPinMax) {
  176. furi_hal_infrared_set_tx_output(settings.tx_pin);
  177. if(settings.otg_enabled != otg_was_enabled) {
  178. if(settings.otg_enabled) {
  179. furi_hal_power_enable_otg();
  180. } else {
  181. furi_hal_power_disable_otg();
  182. }
  183. }
  184. } else {
  185. FuriHalInfraredTxPin tx_pin_detected = furi_hal_infrared_detect_tx_output();
  186. furi_hal_infrared_set_tx_output(tx_pin_detected);
  187. if(tx_pin_detected != FuriHalInfraredTxPinInternal) {
  188. furi_hal_power_enable_otg();
  189. }
  190. }
  191. }
  192. view_dispatcher_run(app->view_dispatcher);
  193. furi_hal_infrared_set_tx_output(FuriHalInfraredTxPinInternal);
  194. if(furi_hal_power_is_otg_enabled() != otg_was_enabled) {
  195. if(otg_was_enabled) {
  196. furi_hal_power_enable_otg();
  197. } else {
  198. furi_hal_power_disable_otg();
  199. }
  200. }
  201. xremote_save_settings(app);
  202. furi_hal_power_suppress_charge_exit();
  203. xremote_app_free(app);
  204. return 0;
  205. }