scene_action_ir_list.c 4.2 KB

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