infrared_scene_rpc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "../infrared_i.h"
  2. #include <gui/canvas.h>
  3. typedef enum {
  4. InfraredRpcStateIdle,
  5. InfraredRpcStateLoaded,
  6. InfraredRpcStateSending,
  7. } InfraredRpcState;
  8. void infrared_scene_rpc_on_enter(void* context) {
  9. Infrared* infrared = context;
  10. Popup* popup = infrared->popup;
  11. popup_set_header(popup, "Infrared", 89, 42, AlignCenter, AlignBottom);
  12. popup_set_text(popup, "RPC mode", 89, 44, AlignCenter, AlignTop);
  13. popup_set_icon(popup, 0, 12, &I_RFIDDolphinSend_97x61);
  14. popup_set_context(popup, context);
  15. popup_set_callback(popup, infrared_popup_closed_callback);
  16. view_dispatcher_switch_to_view(infrared->view_dispatcher, InfraredViewPopup);
  17. scene_manager_set_scene_state(infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateIdle);
  18. notification_message(infrared->notifications, &sequence_display_backlight_on);
  19. }
  20. bool infrared_scene_rpc_on_event(void* context, SceneManagerEvent event) {
  21. Infrared* infrared = context;
  22. bool consumed = false;
  23. if(event.type == SceneManagerEventTypeCustom) {
  24. consumed = true;
  25. InfraredRpcState state =
  26. scene_manager_get_scene_state(infrared->scene_manager, InfraredSceneRpc);
  27. if(event.event == InfraredCustomEventTypeBackPressed) {
  28. view_dispatcher_stop(infrared->view_dispatcher);
  29. } else if(event.event == InfraredCustomEventTypePopupClosed) {
  30. view_dispatcher_stop(infrared->view_dispatcher);
  31. } else if(event.event == InfraredCustomEventTypeRpcLoad) {
  32. bool result = false;
  33. const char* arg = rpc_system_app_get_data(infrared->rpc_ctx);
  34. if(arg && (state == InfraredRpcStateIdle)) {
  35. furi_string_set(infrared->file_path, arg);
  36. result = infrared_remote_load(infrared->remote, infrared->file_path);
  37. if(result) {
  38. scene_manager_set_scene_state(
  39. infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateLoaded);
  40. }
  41. }
  42. const char* remote_name = infrared_remote_get_name(infrared->remote);
  43. infrared_text_store_set(infrared, 0, "loaded\n%s", remote_name);
  44. popup_set_text(
  45. infrared->popup, infrared->text_store[0], 89, 44, AlignCenter, AlignTop);
  46. rpc_system_app_confirm(infrared->rpc_ctx, RpcAppEventLoadFile, result);
  47. } else if(event.event == InfraredCustomEventTypeRpcButtonPress) {
  48. bool result = false;
  49. const char* arg = rpc_system_app_get_data(infrared->rpc_ctx);
  50. if(arg && (state == InfraredRpcStateLoaded)) {
  51. size_t button_index = 0;
  52. if(infrared_remote_find_button_by_name(infrared->remote, arg, &button_index)) {
  53. infrared_tx_start_button_index(infrared, button_index);
  54. result = true;
  55. scene_manager_set_scene_state(
  56. infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateSending);
  57. }
  58. }
  59. rpc_system_app_confirm(infrared->rpc_ctx, RpcAppEventButtonRelease, result);
  60. } else if(event.event == InfraredCustomEventTypeRpcButtonRelease) {
  61. bool result = false;
  62. if(state == InfraredRpcStateSending) {
  63. infrared_tx_stop(infrared);
  64. result = true;
  65. scene_manager_set_scene_state(
  66. infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateLoaded);
  67. }
  68. rpc_system_app_confirm(infrared->rpc_ctx, RpcAppEventButtonRelease, result);
  69. } else if(event.event == InfraredCustomEventTypeRpcExit) {
  70. scene_manager_stop(infrared->scene_manager);
  71. view_dispatcher_stop(infrared->view_dispatcher);
  72. rpc_system_app_confirm(infrared->rpc_ctx, RpcAppEventAppExit, true);
  73. } else if(event.event == InfraredCustomEventTypeRpcSessionClose) {
  74. scene_manager_stop(infrared->scene_manager);
  75. view_dispatcher_stop(infrared->view_dispatcher);
  76. }
  77. }
  78. return consumed;
  79. }
  80. void infrared_scene_rpc_on_exit(void* context) {
  81. Infrared* infrared = context;
  82. if(scene_manager_get_scene_state(infrared->scene_manager, InfraredSceneRpc) ==
  83. InfraredRpcStateSending) {
  84. infrared_tx_stop(infrared);
  85. }
  86. popup_reset(infrared->popup);
  87. }