infrared_scene_remote.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "../infrared_i.h"
  2. typedef enum {
  3. ButtonIndexPlus = -2,
  4. ButtonIndexEdit = -1,
  5. ButtonIndexNA = 0,
  6. } ButtonIndex;
  7. static void
  8. infrared_scene_remote_button_menu_callback(void* context, int32_t index, InputType type) {
  9. Infrared* infrared = context;
  10. uint16_t custom_type;
  11. if(type == InputTypePress) {
  12. custom_type = InfraredCustomEventTypeTransmitStarted;
  13. } else if(type == InputTypeRelease) {
  14. custom_type = InfraredCustomEventTypeTransmitStopped;
  15. } else if(type == InputTypeShort) {
  16. custom_type = InfraredCustomEventTypeMenuSelected;
  17. } else {
  18. furi_crash("Unexpected input type");
  19. }
  20. view_dispatcher_send_custom_event(
  21. infrared->view_dispatcher, infrared_custom_event_pack(custom_type, index));
  22. }
  23. void infrared_scene_remote_on_enter(void* context) {
  24. Infrared* infrared = context;
  25. InfraredRemote* remote = infrared->remote;
  26. ButtonMenu* button_menu = infrared->button_menu;
  27. SceneManager* scene_manager = infrared->scene_manager;
  28. infrared_worker_tx_set_get_signal_callback(
  29. infrared->worker, infrared_worker_tx_get_signal_steady_callback, infrared);
  30. infrared_worker_tx_set_signal_sent_callback(
  31. infrared->worker, infrared_signal_sent_callback, infrared);
  32. size_t button_count = infrared_remote_get_button_count(remote);
  33. for(size_t i = 0; i < button_count; ++i) {
  34. InfraredRemoteButton* button = infrared_remote_get_button(remote, i);
  35. button_menu_add_item(
  36. button_menu,
  37. infrared_remote_button_get_name(button),
  38. i,
  39. infrared_scene_remote_button_menu_callback,
  40. ButtonMenuItemTypeCommon,
  41. context);
  42. }
  43. button_menu_add_item(
  44. button_menu,
  45. "+",
  46. ButtonIndexPlus,
  47. infrared_scene_remote_button_menu_callback,
  48. ButtonMenuItemTypeControl,
  49. context);
  50. button_menu_add_item(
  51. button_menu,
  52. "Edit",
  53. ButtonIndexEdit,
  54. infrared_scene_remote_button_menu_callback,
  55. ButtonMenuItemTypeControl,
  56. context);
  57. button_menu_set_header(button_menu, infrared_remote_get_name(remote));
  58. const int16_t button_index =
  59. (signed)scene_manager_get_scene_state(scene_manager, InfraredSceneRemote);
  60. button_menu_set_selected_item(button_menu, button_index);
  61. scene_manager_set_scene_state(scene_manager, InfraredSceneRemote, ButtonIndexNA);
  62. view_dispatcher_switch_to_view(infrared->view_dispatcher, InfraredViewButtonMenu);
  63. }
  64. bool infrared_scene_remote_on_event(void* context, SceneManagerEvent event) {
  65. Infrared* infrared = context;
  66. SceneManager* scene_manager = infrared->scene_manager;
  67. bool consumed = false;
  68. if(event.type == SceneManagerEventTypeBack) {
  69. const uint32_t possible_scenes[] = {InfraredSceneRemoteList, InfraredSceneStart};
  70. consumed = scene_manager_search_and_switch_to_previous_scene_one_of(
  71. scene_manager, possible_scenes, sizeof(possible_scenes) / sizeof(uint32_t));
  72. } else if(event.type == SceneManagerEventTypeCustom) {
  73. const uint16_t custom_type = infrared_custom_event_get_type(event.event);
  74. const int16_t button_index = infrared_custom_event_get_value(event.event);
  75. if(custom_type == InfraredCustomEventTypeTransmitStarted) {
  76. furi_assert(button_index >= 0);
  77. infrared_tx_start_button_index(infrared, button_index);
  78. consumed = true;
  79. } else if(custom_type == InfraredCustomEventTypeTransmitStopped) {
  80. infrared_tx_stop(infrared);
  81. consumed = true;
  82. } else if(custom_type == InfraredCustomEventTypeMenuSelected) {
  83. furi_assert(button_index < 0);
  84. scene_manager_set_scene_state(
  85. scene_manager, InfraredSceneRemote, (unsigned)button_index);
  86. if(button_index == ButtonIndexPlus) {
  87. infrared->app_state.is_learning_new_remote = false;
  88. scene_manager_next_scene(scene_manager, InfraredSceneLearn);
  89. consumed = true;
  90. } else if(button_index == ButtonIndexEdit) {
  91. scene_manager_next_scene(scene_manager, InfraredSceneEdit);
  92. consumed = true;
  93. }
  94. }
  95. }
  96. return consumed;
  97. }
  98. void infrared_scene_remote_on_exit(void* context) {
  99. Infrared* infrared = context;
  100. infrared_worker_tx_set_get_signal_callback(infrared->worker, NULL, NULL);
  101. infrared_worker_tx_set_signal_sent_callback(infrared->worker, NULL, NULL);
  102. button_menu_reset(infrared->button_menu);
  103. }