Skip to content

Commit

Permalink
add Import All for ir files
Browse files Browse the repository at this point in the history
  • Loading branch information
rdefeo committed Sep 6, 2024
1 parent fe03a61 commit 75fd1a4
Showing 1 changed file with 51 additions and 31 deletions.
82 changes: 51 additions & 31 deletions scenes/scene_action_ir_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#include <flipper_format/flipper_format.h>

// ehhhh
uint32_t num_ir_commands;

void scene_action_ir_list_callback(void* context, uint32_t index) {
App* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, index);
Expand All @@ -27,10 +30,14 @@ void scene_action_ir_list_on_enter(void* context) {
// Our selected IR File is app->temp_str
submenu_set_header(menu, "Select IR Command");

uint32_t index = 0;

// Add an entry for IMPORT ALL
submenu_add_item(menu, "* IMPORT ALL *", index++, scene_action_ir_list_callback, app);

// read the IR file and load the names of all of the commands
FuriString* name = furi_string_alloc();

uint32_t index = 0;
FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
if(flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(app->temp_str))) {
while(flipper_format_read_string(fff_data_file, "name", name)) {
Expand All @@ -39,9 +46,11 @@ void scene_action_ir_list_on_enter(void* context) {
index++;
}
}
num_ir_commands = index - 1;

if(index == 0) {
if(index == 1) { // '1' to account for 'IMPORT ALL'
FURI_LOG_E(TAG, "Failed to get commands from %s", furi_string_get_cstr(app->temp_str));
submenu_change_item_label(menu, 0, "No IR cmds!");
}

flipper_format_file_close(fff_data_file);
Expand All @@ -66,37 +75,48 @@ bool scene_action_ir_list_on_event(void* context, SceneManagerEvent event) {
FuriString* file_name = furi_string_alloc(); // new IR file name

do {
if(!flipper_format_file_open_existing(
fff_data_file, furi_string_get_cstr(app->temp_str))) {
FURI_LOG_E(TAG, "Failed to open %s", furi_string_get_cstr(app->temp_str));
break;
uint32_t start = index - 1;
uint32_t end = index;
if(index == 0) {
start = 0;
end = num_ir_commands;
}
if(!infrared_utils_read_signal_at_index(fff_data_file, index, signal, name)) {
FURI_LOG_E(TAG, "Failed to read signal at %lu", index);
break;
for(uint32_t ir_index = start; ir_index < end; ir_index++) {
if(!flipper_format_file_open_existing(
fff_data_file, furi_string_get_cstr(app->temp_str))) {
FURI_LOG_E(TAG, "Failed to open %s", furi_string_get_cstr(app->temp_str));
break;
}

if(!infrared_utils_read_signal_at_index(fff_data_file, ir_index, signal, name)) {
FURI_LOG_E(TAG, "Failed to read signal at %lu", index);
break;
}
FURI_LOG_I(TAG, "Read IR signal: %s", furi_string_get_cstr(name));
flipper_format_file_close(fff_data_file);

// generate the new path, based on current item's dir and new command name
if(app->selected_item != EMPTY_ACTION_INDEX) {
Item* item = ItemArray_get(app->items_view->items, app->selected_item);
path_extract_dirname(furi_string_get_cstr(item->path), file_name);
} else {
furi_string_set(file_name, app->items_view->path);
}
furi_string_cat_printf(file_name, "/%s.ir", furi_string_get_cstr(name));

FURI_LOG_I(TAG, "Writing new IR file: %s", furi_string_get_cstr(file_name));
if(!flipper_format_file_open_new(fff_data_file, furi_string_get_cstr(file_name))) {
FURI_LOG_E(
TAG, "Error creating new file: %s", furi_string_get_cstr(file_name));
break;
}
if(!infrared_utils_write_signal(fff_data_file, signal, name)) {
FURI_LOG_E(TAG, "Failed to write signal!");
break;
}
flipper_format_file_close(fff_data_file);
FURI_LOG_I(TAG, "Imported %s", furi_string_get_cstr(name));
}
FURI_LOG_I(TAG, "Read IR signal: %s", furi_string_get_cstr(name));
flipper_format_file_close(fff_data_file);

// generate the new path, based on current item's dir and new command name
if(app->selected_item != EMPTY_ACTION_INDEX) {
Item* item = ItemArray_get(app->items_view->items, app->selected_item);
path_extract_dirname(furi_string_get_cstr(item->path), file_name);
} else {
furi_string_set(file_name, app->items_view->path);
}
furi_string_cat_printf(file_name, "/%s.ir", furi_string_get_cstr(name));

FURI_LOG_I(TAG, "Writing new IR file: %s", furi_string_get_cstr(file_name));
if(!flipper_format_file_open_new(fff_data_file, furi_string_get_cstr(file_name))) {
FURI_LOG_E(TAG, "Error creating new file: %s", furi_string_get_cstr(file_name));
break;
}
if(!infrared_utils_write_signal(fff_data_file, signal, name)) {
FURI_LOG_E(TAG, "Failed to write signal!");
break;
}

// Import successful!
// Leave the user on this scene, in case they want to import
// more commands from this IR file
Expand Down

0 comments on commit 75fd1a4

Please sign in to comment.