scene_action_ir_list.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <furi.h>
  2. #include <gui/view_dispatcher.h>
  3. #include <gui/scene_manager.h>
  4. #include <gui/modules/submenu.h>
  5. #include <lib/toolbox/path.h>
  6. #include "quac.h"
  7. #include "scenes.h"
  8. #include "scene_action_ir_list.h"
  9. #include "../actions/action.h"
  10. #include "../actions/action_ir_utils.h"
  11. #include <flipper_format/flipper_format.h>
  12. void scene_action_ir_list_callback(void* context, uint32_t index) {
  13. App* app = context;
  14. view_dispatcher_send_custom_event(app->view_dispatcher, index);
  15. }
  16. void scene_action_ir_list_on_enter(void* context) {
  17. App* app = context;
  18. Submenu* menu = app->sub_menu;
  19. submenu_reset(menu);
  20. // Our selected IR File is app->temp_str
  21. submenu_set_header(menu, "Select IR Command");
  22. // read the IR file and load the names of all of the commands
  23. FuriString* name = furi_string_alloc();
  24. uint32_t index = 0;
  25. FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
  26. if(flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(app->temp_str))) {
  27. while(flipper_format_read_string(fff_data_file, "name", name)) {
  28. submenu_add_item(
  29. menu, furi_string_get_cstr(name), index, scene_action_ir_list_callback, app);
  30. index++;
  31. }
  32. }
  33. if(index == 0) {
  34. FURI_LOG_E(TAG, "Failed to get commands from %s", furi_string_get_cstr(app->temp_str));
  35. }
  36. flipper_format_file_close(fff_data_file);
  37. flipper_format_free(fff_data_file);
  38. furi_string_free(name);
  39. view_dispatcher_switch_to_view(app->view_dispatcher, QView_SubMenu);
  40. }
  41. bool scene_action_ir_list_on_event(void* context, SceneManagerEvent event) {
  42. App* app = context;
  43. bool consumed = false;
  44. if(event.type == SceneManagerEventTypeCustom) {
  45. consumed = true;
  46. uint32_t index = event.event;
  47. InfraredSignal* signal = infrared_utils_signal_alloc();
  48. // extract that item as it's own file and place it "here", as defined by
  49. // the currently selected_item
  50. FuriString* name = furi_string_alloc(); // IR command name
  51. FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
  52. FuriString* file_name = furi_string_alloc(); // new IR file name
  53. do {
  54. if(!flipper_format_file_open_existing(
  55. fff_data_file, furi_string_get_cstr(app->temp_str))) {
  56. FURI_LOG_E(TAG, "Failed to open %s", furi_string_get_cstr(app->temp_str));
  57. break;
  58. }
  59. if(!infrared_utils_read_signal_at_index(fff_data_file, index, signal, name)) {
  60. FURI_LOG_E(TAG, "Failed to read signal at %lu", index);
  61. break;
  62. }
  63. FURI_LOG_I(TAG, "Read IR signal: %s", furi_string_get_cstr(name));
  64. flipper_format_file_close(fff_data_file);
  65. // generate the new path, based on current item's dir and new command name
  66. if(app->selected_item != EMPTY_ACTION_INDEX) {
  67. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  68. path_extract_dirname(furi_string_get_cstr(item->path), file_name);
  69. } else {
  70. furi_string_set(file_name, app->items_view->path);
  71. }
  72. furi_string_cat_printf(file_name, "/%s.ir", furi_string_get_cstr(name));
  73. FURI_LOG_I(TAG, "Writing new IR file: %s", furi_string_get_cstr(file_name));
  74. if(!flipper_format_file_open_new(fff_data_file, furi_string_get_cstr(file_name))) {
  75. FURI_LOG_E(TAG, "Error creating new file: %s", furi_string_get_cstr(file_name));
  76. break;
  77. }
  78. if(!infrared_utils_write_signal(fff_data_file, signal, name)) {
  79. FURI_LOG_E(TAG, "Failed to write signal!");
  80. break;
  81. }
  82. // Import successful!
  83. // Leave the user on this scene, in case they want to import
  84. // more commands from this IR file
  85. notification_message(app->notifications, &sequence_success);
  86. } while(false);
  87. // cleanup
  88. flipper_format_file_close(fff_data_file);
  89. flipper_format_free(fff_data_file);
  90. furi_string_free(name);
  91. furi_string_free(file_name);
  92. infrared_utils_signal_free(signal);
  93. }
  94. return consumed;
  95. }
  96. void scene_action_ir_list_on_exit(void* context) {
  97. App* app = context;
  98. submenu_reset(app->sub_menu);
  99. }