scene_action_ir_list.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. uint32_t index = 0;
  23. // Add an entry for IMPORT ALL
  24. submenu_add_item(menu, "* IMPORT ALL *", index++, scene_action_ir_list_callback, app);
  25. // read the IR file and load the names of all of the commands
  26. FuriString* name = furi_string_alloc();
  27. FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
  28. if(flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(app->temp_str))) {
  29. while(flipper_format_read_string(fff_data_file, "name", name)) {
  30. submenu_add_item(
  31. menu, furi_string_get_cstr(name), index, scene_action_ir_list_callback, app);
  32. index++;
  33. }
  34. }
  35. // Number of IR Commands in file
  36. app->temp_u32 = index - 1;
  37. if(app->temp_u32 == 0) {
  38. FURI_LOG_E(TAG, "Failed to get ANY commands from %s", furi_string_get_cstr(app->temp_str));
  39. submenu_change_item_label(menu, 0, "No IR cmds!");
  40. }
  41. flipper_format_file_close(fff_data_file);
  42. flipper_format_free(fff_data_file);
  43. furi_string_free(name);
  44. view_dispatcher_switch_to_view(app->view_dispatcher, QView_SubMenu);
  45. }
  46. bool scene_action_ir_list_on_event(void* context, SceneManagerEvent event) {
  47. App* app = context;
  48. bool consumed = false;
  49. if(event.type == SceneManagerEventTypeCustom) {
  50. consumed = true;
  51. uint32_t index = event.event;
  52. InfraredSignal* signal = infrared_utils_signal_alloc();
  53. // extract that item as it's own file and place it "here", as defined by
  54. // the currently selected_item
  55. FuriString* name = furi_string_alloc(); // IR command name
  56. FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
  57. FuriString* file_name = furi_string_alloc(); // new IR file name
  58. do {
  59. uint32_t num_imported = 0;
  60. uint32_t start = index - 1;
  61. uint32_t end = index;
  62. if(index == 0) {
  63. start = 0;
  64. end = app->temp_u32; // Number of IR Commands in file
  65. }
  66. for(uint32_t ir_index = start; ir_index < end; ir_index++) {
  67. if(!flipper_format_file_open_existing(
  68. fff_data_file, furi_string_get_cstr(app->temp_str))) {
  69. FURI_LOG_E(TAG, "Failed to open %s", furi_string_get_cstr(app->temp_str));
  70. break;
  71. }
  72. if(!infrared_utils_read_signal_at_index(fff_data_file, ir_index, signal, name)) {
  73. FURI_LOG_E(TAG, "Failed to read signal at %lu", index);
  74. break;
  75. }
  76. FURI_LOG_I(TAG, "Read IR signal: %s", furi_string_get_cstr(name));
  77. flipper_format_file_close(fff_data_file);
  78. // generate the new path, based on current item's dir and new command name
  79. if(app->selected_item != EMPTY_ACTION_INDEX) {
  80. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  81. path_extract_dirname(furi_string_get_cstr(item->path), file_name);
  82. } else {
  83. furi_string_set(file_name, app->items_view->path);
  84. }
  85. furi_string_cat_printf(file_name, "/%s.ir", furi_string_get_cstr(name));
  86. FURI_LOG_I(TAG, "Writing new IR file: %s", furi_string_get_cstr(file_name));
  87. if(!flipper_format_file_open_new(fff_data_file, furi_string_get_cstr(file_name))) {
  88. FURI_LOG_E(
  89. TAG, "Error creating new file: %s", furi_string_get_cstr(file_name));
  90. break;
  91. }
  92. if(!infrared_utils_write_signal(fff_data_file, signal, name)) {
  93. FURI_LOG_E(TAG, "Failed to write signal!");
  94. break;
  95. }
  96. flipper_format_file_close(fff_data_file);
  97. FURI_LOG_I(TAG, "Imported %s", furi_string_get_cstr(name));
  98. num_imported++;
  99. }
  100. if(num_imported == (end - start)) {
  101. // Import successful!
  102. notification_message(app->notifications, &sequence_success);
  103. } else {
  104. FURI_LOG_E(
  105. TAG,
  106. "Error importing IR command(s) from %s",
  107. furi_string_get_cstr(app->temp_str));
  108. notification_message(app->notifications, &sequence_error);
  109. }
  110. // Leave the user on this scene, in case they want to import
  111. // more commands from this IR file
  112. } while(false);
  113. // cleanup
  114. flipper_format_file_close(fff_data_file);
  115. flipper_format_free(fff_data_file);
  116. furi_string_free(name);
  117. furi_string_free(file_name);
  118. infrared_utils_signal_free(signal);
  119. }
  120. return consumed;
  121. }
  122. void scene_action_ir_list_on_exit(void* context) {
  123. App* app = context;
  124. submenu_reset(app->sub_menu);
  125. }