2023-10-30 19:20:35 +03:00
|
|
|
#include "infrared_app_i.h"
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2024-03-25 15:47:34 +03:00
|
|
|
#include <furi_hal_power.h>
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
#include <string.h>
|
2023-10-30 19:20:35 +03:00
|
|
|
#include <toolbox/path.h>
|
2024-03-25 15:47:34 +03:00
|
|
|
#include <toolbox/saved_struct.h>
|
2022-06-21 15:45:50 +03:00
|
|
|
#include <dolphin/dolphin.h>
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
#define TAG "InfraredApp"
|
|
|
|
|
2024-03-11 20:35:51 +03:00
|
|
|
#define INFRARED_TX_MIN_INTERVAL_MS (50U)
|
2024-07-15 20:02:45 +03:00
|
|
|
#define INFRARED_TASK_STACK_SIZE (2048UL)
|
2023-03-03 15:37:02 +03:00
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
static const NotificationSequence*
|
|
|
|
infrared_notification_sequences[InfraredNotificationMessageCount] = {
|
|
|
|
&sequence_success,
|
|
|
|
&sequence_set_only_green_255,
|
|
|
|
&sequence_reset_green,
|
|
|
|
&sequence_solid_yellow,
|
|
|
|
&sequence_reset_rgb,
|
|
|
|
&sequence_blink_start_cyan,
|
|
|
|
&sequence_blink_start_magenta,
|
|
|
|
&sequence_blink_stop,
|
2022-08-04 15:11:01 +03:00
|
|
|
};
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
static void infrared_make_app_folder(InfraredApp* infrared) {
|
2022-06-21 15:45:50 +03:00
|
|
|
if(!storage_simply_mkdir(infrared->storage, INFRARED_APP_FOLDER)) {
|
2023-10-30 19:20:35 +03:00
|
|
|
infrared_show_error_message(infrared, "Cannot create\napp folder");
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool infrared_custom_event_callback(void* context, uint32_t event) {
|
|
|
|
furi_assert(context);
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2022-06-21 15:45:50 +03:00
|
|
|
return scene_manager_handle_custom_event(infrared->scene_manager, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool infrared_back_event_callback(void* context) {
|
|
|
|
furi_assert(context);
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2022-06-21 15:45:50 +03:00
|
|
|
return scene_manager_handle_back_event(infrared->scene_manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void infrared_tick_event_callback(void* context) {
|
|
|
|
furi_assert(context);
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2022-06-21 15:45:50 +03:00
|
|
|
scene_manager_handle_tick_event(infrared->scene_manager);
|
|
|
|
}
|
|
|
|
|
2023-11-10 10:22:34 +03:00
|
|
|
static void infrared_rpc_command_callback(const RpcAppSystemEvent* event, void* context) {
|
2022-07-04 16:09:46 +03:00
|
|
|
furi_assert(context);
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2022-08-02 15:54:12 +03:00
|
|
|
furi_assert(infrared->rpc_ctx);
|
2022-07-04 16:09:46 +03:00
|
|
|
|
2023-11-10 10:22:34 +03:00
|
|
|
if(event->type == RpcAppEventTypeSessionClose) {
|
2022-07-04 16:09:46 +03:00
|
|
|
view_dispatcher_send_custom_event(
|
2022-08-02 15:54:12 +03:00
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypeRpcSessionClose);
|
2022-08-11 18:37:23 +03:00
|
|
|
rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
|
|
|
|
infrared->rpc_ctx = NULL;
|
2023-11-10 10:22:34 +03:00
|
|
|
} else if(event->type == RpcAppEventTypeAppExit) {
|
2022-07-04 16:09:46 +03:00
|
|
|
view_dispatcher_send_custom_event(
|
2022-08-02 15:54:12 +03:00
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypeRpcExit);
|
2023-11-10 10:22:34 +03:00
|
|
|
} else if(event->type == RpcAppEventTypeLoadFile) {
|
|
|
|
furi_assert(event->data.type == RpcAppSystemEventDataTypeString);
|
|
|
|
furi_string_set(infrared->file_path, event->data.string);
|
2022-08-02 15:54:12 +03:00
|
|
|
view_dispatcher_send_custom_event(
|
2023-11-10 10:22:34 +03:00
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypeRpcLoadFile);
|
|
|
|
} else if(event->type == RpcAppEventTypeButtonPress) {
|
|
|
|
furi_assert(
|
|
|
|
event->data.type == RpcAppSystemEventDataTypeString ||
|
|
|
|
event->data.type == RpcAppSystemEventDataTypeInt32);
|
|
|
|
if(event->data.type == RpcAppSystemEventDataTypeString) {
|
|
|
|
furi_string_set(infrared->button_name, event->data.string);
|
|
|
|
view_dispatcher_send_custom_event(
|
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonPressName);
|
|
|
|
} else {
|
|
|
|
infrared->app_state.current_button_index = event->data.i32;
|
|
|
|
view_dispatcher_send_custom_event(
|
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonPressIndex);
|
|
|
|
}
|
|
|
|
} else if(event->type == RpcAppEventTypeButtonRelease) {
|
2022-08-02 15:54:12 +03:00
|
|
|
view_dispatcher_send_custom_event(
|
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonRelease);
|
|
|
|
} else {
|
2023-11-10 10:22:34 +03:00
|
|
|
rpc_system_app_confirm(infrared->rpc_ctx, false);
|
2022-07-04 16:09:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
static void infrared_find_vacant_remote_name(FuriString* name, const char* path) {
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* base_path;
|
|
|
|
base_path = furi_string_alloc_set(path);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
if(furi_string_end_with(base_path, INFRARED_APP_EXTENSION)) {
|
|
|
|
size_t filename_start = furi_string_search_rchar(base_path, '/');
|
|
|
|
furi_string_left(base_path, filename_start);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
|
|
|
base_path, "%s/%s%s", path, furi_string_get_cstr(name), INFRARED_APP_EXTENSION);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FS_Error status = storage_common_stat(storage, furi_string_get_cstr(base_path), NULL);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
if(status == FSE_OK) {
|
|
|
|
/* If the suggested name is occupied, try another one (name2, name3, etc) */
|
2022-10-05 18:15:23 +03:00
|
|
|
size_t dot = furi_string_search_rchar(base_path, '.');
|
|
|
|
furi_string_left(base_path, dot);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* path_temp;
|
|
|
|
path_temp = furi_string_alloc();
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
uint32_t i = 1;
|
|
|
|
do {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
2022-10-07 16:35:15 +03:00
|
|
|
path_temp, "%s%lu%s", furi_string_get_cstr(base_path), ++i, INFRARED_APP_EXTENSION);
|
2022-10-05 18:15:23 +03:00
|
|
|
status = storage_common_stat(storage, furi_string_get_cstr(path_temp), NULL);
|
2022-06-21 15:45:50 +03:00
|
|
|
} while(status == FSE_OK);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(path_temp);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
if(status == FSE_NOT_EXIST) {
|
2022-10-07 16:35:15 +03:00
|
|
|
furi_string_cat_printf(name, "%lu", i);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(base_path);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
2024-03-25 13:53:32 +03:00
|
|
|
static InfraredApp* infrared_alloc(void) {
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = malloc(sizeof(InfraredApp));
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2024-03-11 20:35:51 +03:00
|
|
|
infrared->task_thread =
|
|
|
|
furi_thread_alloc_ex("InfraredTask", INFRARED_TASK_STACK_SIZE, NULL, infrared);
|
2022-10-05 18:15:23 +03:00
|
|
|
infrared->file_path = furi_string_alloc();
|
2023-11-10 10:22:34 +03:00
|
|
|
infrared->button_name = furi_string_alloc();
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
InfraredAppState* app_state = &infrared->app_state;
|
|
|
|
app_state->is_learning_new_remote = false;
|
|
|
|
app_state->is_debug_enabled = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);
|
|
|
|
app_state->edit_target = InfraredEditTargetNone;
|
|
|
|
app_state->edit_mode = InfraredEditModeNone;
|
|
|
|
app_state->current_button_index = InfraredButtonIndexNone;
|
|
|
|
|
|
|
|
infrared->scene_manager = scene_manager_alloc(&infrared_scene_handlers, infrared);
|
|
|
|
infrared->view_dispatcher = view_dispatcher_alloc();
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
infrared->gui = furi_record_open(RECORD_GUI);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
ViewDispatcher* view_dispatcher = infrared->view_dispatcher;
|
|
|
|
view_dispatcher_set_event_callback_context(view_dispatcher, infrared);
|
|
|
|
view_dispatcher_set_custom_event_callback(view_dispatcher, infrared_custom_event_callback);
|
|
|
|
view_dispatcher_set_navigation_event_callback(view_dispatcher, infrared_back_event_callback);
|
|
|
|
view_dispatcher_set_tick_event_callback(view_dispatcher, infrared_tick_event_callback, 100);
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
infrared->storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
infrared->dialogs = furi_record_open(RECORD_DIALOGS);
|
|
|
|
infrared->notifications = furi_record_open(RECORD_NOTIFICATION);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
infrared->worker = infrared_worker_alloc();
|
|
|
|
infrared->remote = infrared_remote_alloc();
|
2023-10-30 19:20:35 +03:00
|
|
|
infrared->current_signal = infrared_signal_alloc();
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared->brute_force = infrared_brute_force_alloc();
|
|
|
|
|
|
|
|
infrared->submenu = submenu_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
view_dispatcher, InfraredViewSubmenu, submenu_get_view(infrared->submenu));
|
|
|
|
|
|
|
|
infrared->text_input = text_input_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
view_dispatcher, InfraredViewTextInput, text_input_get_view(infrared->text_input));
|
|
|
|
|
|
|
|
infrared->dialog_ex = dialog_ex_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
view_dispatcher, InfraredViewDialogEx, dialog_ex_get_view(infrared->dialog_ex));
|
|
|
|
|
|
|
|
infrared->button_menu = button_menu_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
view_dispatcher, InfraredViewButtonMenu, button_menu_get_view(infrared->button_menu));
|
|
|
|
|
|
|
|
infrared->popup = popup_alloc();
|
|
|
|
view_dispatcher_add_view(view_dispatcher, InfraredViewPopup, popup_get_view(infrared->popup));
|
|
|
|
|
2024-03-25 15:47:34 +03:00
|
|
|
infrared->var_item_list = variable_item_list_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
view_dispatcher,
|
|
|
|
InfraredViewVariableList,
|
|
|
|
variable_item_list_get_view(infrared->var_item_list));
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared->view_stack = view_stack_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
view_dispatcher, InfraredViewStack, view_stack_get_view(infrared->view_stack));
|
|
|
|
|
2023-08-07 12:18:46 +03:00
|
|
|
infrared->move_view = infrared_move_view_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
view_dispatcher, InfraredViewMove, infrared_move_view_get_view(infrared->move_view));
|
|
|
|
|
2024-03-12 19:45:22 +03:00
|
|
|
infrared->loading = loading_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
view_dispatcher, InfraredViewLoading, loading_get_view(infrared->loading));
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
if(app_state->is_debug_enabled) {
|
|
|
|
infrared->debug_view = infrared_debug_view_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
view_dispatcher,
|
|
|
|
InfraredViewDebugView,
|
|
|
|
infrared_debug_view_get_view(infrared->debug_view));
|
|
|
|
}
|
|
|
|
|
|
|
|
infrared->button_panel = button_panel_alloc();
|
|
|
|
infrared->progress = infrared_progress_view_alloc();
|
|
|
|
|
|
|
|
return infrared;
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
static void infrared_free(InfraredApp* infrared) {
|
2022-06-21 15:45:50 +03:00
|
|
|
furi_assert(infrared);
|
2024-03-11 20:35:51 +03:00
|
|
|
|
|
|
|
furi_thread_join(infrared->task_thread);
|
|
|
|
furi_thread_free(infrared->task_thread);
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
ViewDispatcher* view_dispatcher = infrared->view_dispatcher;
|
|
|
|
InfraredAppState* app_state = &infrared->app_state;
|
|
|
|
|
2022-07-04 16:09:46 +03:00
|
|
|
if(infrared->rpc_ctx) {
|
|
|
|
rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
|
2022-07-25 17:16:45 +03:00
|
|
|
rpc_system_app_send_exited(infrared->rpc_ctx);
|
2022-07-04 16:09:46 +03:00
|
|
|
infrared->rpc_ctx = NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewSubmenu);
|
|
|
|
submenu_free(infrared->submenu);
|
|
|
|
|
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewTextInput);
|
|
|
|
text_input_free(infrared->text_input);
|
|
|
|
|
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewDialogEx);
|
|
|
|
dialog_ex_free(infrared->dialog_ex);
|
|
|
|
|
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewButtonMenu);
|
|
|
|
button_menu_free(infrared->button_menu);
|
|
|
|
|
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewPopup);
|
|
|
|
popup_free(infrared->popup);
|
|
|
|
|
2024-03-25 15:47:34 +03:00
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewVariableList);
|
|
|
|
variable_item_list_free(infrared->var_item_list);
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewStack);
|
|
|
|
view_stack_free(infrared->view_stack);
|
|
|
|
|
2023-08-07 12:18:46 +03:00
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewMove);
|
|
|
|
infrared_move_view_free(infrared->move_view);
|
|
|
|
|
2024-03-12 19:45:22 +03:00
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewLoading);
|
|
|
|
loading_free(infrared->loading);
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
if(app_state->is_debug_enabled) {
|
|
|
|
view_dispatcher_remove_view(view_dispatcher, InfraredViewDebugView);
|
|
|
|
infrared_debug_view_free(infrared->debug_view);
|
|
|
|
}
|
|
|
|
|
|
|
|
button_panel_free(infrared->button_panel);
|
|
|
|
infrared_progress_view_free(infrared->progress);
|
|
|
|
|
|
|
|
view_dispatcher_free(view_dispatcher);
|
|
|
|
scene_manager_free(infrared->scene_manager);
|
|
|
|
|
|
|
|
infrared_brute_force_free(infrared->brute_force);
|
2023-10-30 19:20:35 +03:00
|
|
|
infrared_signal_free(infrared->current_signal);
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared_remote_free(infrared->remote);
|
|
|
|
infrared_worker_free(infrared->worker);
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_NOTIFICATION);
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared->notifications = NULL;
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_DIALOGS);
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared->dialogs = NULL;
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_GUI);
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared->gui = NULL;
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(infrared->file_path);
|
2023-11-10 10:22:34 +03:00
|
|
|
furi_string_free(infrared->button_name);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
free(infrared);
|
|
|
|
}
|
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode infrared_add_remote_with_button(
|
2023-10-30 19:20:35 +03:00
|
|
|
const InfraredApp* infrared,
|
2022-06-21 15:45:50 +03:00
|
|
|
const char* button_name,
|
2023-10-30 19:20:35 +03:00
|
|
|
const InfraredSignal* signal) {
|
2022-06-21 15:45:50 +03:00
|
|
|
InfraredRemote* remote = infrared->remote;
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
FuriString* new_name = furi_string_alloc_set(INFRARED_DEFAULT_REMOTE_NAME);
|
|
|
|
FuriString* new_path = furi_string_alloc_set(INFRARED_APP_FOLDER);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
infrared_find_vacant_remote_name(new_name, furi_string_get_cstr(new_path));
|
|
|
|
furi_string_cat_printf(
|
|
|
|
new_path, "/%s%s", furi_string_get_cstr(new_name), INFRARED_APP_EXTENSION);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode error = InfraredErrorCodeNone;
|
2023-10-30 19:20:35 +03:00
|
|
|
|
|
|
|
do {
|
2024-09-06 12:52:00 +03:00
|
|
|
error = infrared_remote_create(remote, furi_string_get_cstr(new_path));
|
|
|
|
if(INFRARED_ERROR_PRESENT(error)) break;
|
|
|
|
|
|
|
|
error = infrared_remote_append_signal(remote, signal, button_name);
|
2023-10-30 19:20:35 +03:00
|
|
|
} while(false);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(new_name);
|
|
|
|
furi_string_free(new_path);
|
2023-10-30 19:20:35 +03:00
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
return error;
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode
|
|
|
|
infrared_rename_current_remote(const InfraredApp* infrared, const char* new_name) {
|
2022-06-21 15:45:50 +03:00
|
|
|
InfraredRemote* remote = infrared->remote;
|
2023-10-30 19:20:35 +03:00
|
|
|
const char* old_path = infrared_remote_get_path(remote);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
if(!strcmp(infrared_remote_get_name(remote), new_name)) {
|
2022-06-21 15:45:50 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
FuriString* new_name_fstr = furi_string_alloc_set(new_name);
|
|
|
|
FuriString* new_path_fstr = furi_string_alloc_set(old_path);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
infrared_find_vacant_remote_name(new_name_fstr, old_path);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
if(furi_string_end_with(new_path_fstr, INFRARED_APP_EXTENSION)) {
|
|
|
|
path_extract_dirname(old_path, new_path_fstr);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
path_append(new_path_fstr, furi_string_get_cstr(new_name_fstr));
|
|
|
|
furi_string_cat(new_path_fstr, INFRARED_APP_EXTENSION);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
const InfraredErrorCode error =
|
|
|
|
infrared_remote_rename(remote, furi_string_get_cstr(new_path_fstr));
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
furi_string_free(new_name_fstr);
|
|
|
|
furi_string_free(new_path_fstr);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
return error;
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
void infrared_tx_start(InfraredApp* infrared) {
|
2022-08-12 16:50:04 +03:00
|
|
|
if(infrared->app_state.is_transmitting) {
|
|
|
|
return;
|
2023-03-03 15:37:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t time_elapsed = furi_get_tick() - infrared->app_state.last_transmit_time;
|
|
|
|
|
|
|
|
if(time_elapsed < INFRARED_TX_MIN_INTERVAL_MS) {
|
|
|
|
return;
|
2022-08-12 16:50:04 +03:00
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
if(infrared_signal_is_raw(infrared->current_signal)) {
|
|
|
|
const InfraredRawSignal* raw = infrared_signal_get_raw_signal(infrared->current_signal);
|
2023-05-25 17:38:56 +03:00
|
|
|
infrared_worker_set_raw_signal(
|
|
|
|
infrared->worker, raw->timings, raw->timings_size, raw->frequency, raw->duty_cycle);
|
2022-06-21 15:45:50 +03:00
|
|
|
} else {
|
2023-10-30 19:20:35 +03:00
|
|
|
const InfraredMessage* message = infrared_signal_get_message(infrared->current_signal);
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared_worker_set_decoded_signal(infrared->worker, message);
|
|
|
|
}
|
|
|
|
|
2023-06-09 14:02:47 +03:00
|
|
|
dolphin_deed(DolphinDeedIrSend);
|
2022-08-04 15:11:01 +03:00
|
|
|
infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
|
2022-08-12 16:50:04 +03:00
|
|
|
|
|
|
|
infrared_worker_tx_set_get_signal_callback(
|
|
|
|
infrared->worker, infrared_worker_tx_get_signal_steady_callback, infrared);
|
|
|
|
infrared_worker_tx_start(infrared->worker);
|
2023-03-03 15:37:02 +03:00
|
|
|
|
|
|
|
infrared->app_state.is_transmitting = true;
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode infrared_tx_start_button_index(InfraredApp* infrared, size_t button_index) {
|
2023-10-30 19:20:35 +03:00
|
|
|
furi_assert(button_index < infrared_remote_get_signal_count(infrared->remote));
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode error =
|
2024-09-05 22:54:49 +03:00
|
|
|
infrared_remote_load_signal(infrared->remote, infrared->current_signal, button_index);
|
2024-09-06 12:52:00 +03:00
|
|
|
|
|
|
|
if(!INFRARED_ERROR_PRESENT(error)) {
|
2023-10-30 19:20:35 +03:00
|
|
|
infrared_tx_start(infrared);
|
|
|
|
}
|
2024-09-06 12:52:00 +03:00
|
|
|
return error;
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
void infrared_tx_stop(InfraredApp* infrared) {
|
2022-08-12 16:50:04 +03:00
|
|
|
if(!infrared->app_state.is_transmitting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared_worker_tx_stop(infrared->worker);
|
2022-08-12 16:50:04 +03:00
|
|
|
infrared_worker_tx_set_get_signal_callback(infrared->worker, NULL, NULL);
|
|
|
|
|
2022-08-04 15:11:01 +03:00
|
|
|
infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStop);
|
2023-03-03 15:37:02 +03:00
|
|
|
|
|
|
|
infrared->app_state.is_transmitting = false;
|
|
|
|
infrared->app_state.last_transmit_time = furi_get_tick();
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
2024-03-11 20:35:51 +03:00
|
|
|
void infrared_blocking_task_start(InfraredApp* infrared, FuriThreadCallback callback) {
|
2024-03-12 19:45:22 +03:00
|
|
|
view_dispatcher_switch_to_view(infrared->view_dispatcher, InfraredViewLoading);
|
2024-03-11 20:35:51 +03:00
|
|
|
furi_thread_set_callback(infrared->task_thread, callback);
|
|
|
|
furi_thread_start(infrared->task_thread);
|
|
|
|
}
|
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode infrared_blocking_task_finalize(InfraredApp* infrared) {
|
2024-03-11 20:35:51 +03:00
|
|
|
furi_thread_join(infrared->task_thread);
|
|
|
|
return furi_thread_get_return_code(infrared->task_thread);
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
void infrared_text_store_set(InfraredApp* infrared, uint32_t bank, const char* fmt, ...) {
|
2022-06-21 15:45:50 +03:00
|
|
|
va_list args;
|
2023-10-30 19:20:35 +03:00
|
|
|
va_start(args, fmt);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
vsnprintf(infrared->text_store[bank], INFRARED_TEXT_STORE_SIZE, fmt, args);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
void infrared_text_store_clear(InfraredApp* infrared, uint32_t bank) {
|
2022-12-26 15:13:30 +03:00
|
|
|
memset(infrared->text_store[bank], 0, INFRARED_TEXT_STORE_SIZE + 1);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
void infrared_play_notification_message(
|
|
|
|
const InfraredApp* infrared,
|
|
|
|
InfraredNotificationMessage message) {
|
|
|
|
furi_assert(message < InfraredNotificationMessageCount);
|
2022-06-21 15:45:50 +03:00
|
|
|
notification_message(infrared->notifications, infrared_notification_sequences[message]);
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
void infrared_show_error_message(const InfraredApp* infrared, const char* fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
FuriString* message = furi_string_alloc_vprintf(fmt, args);
|
|
|
|
dialog_message_show_storage_error(infrared->dialogs, furi_string_get_cstr(message));
|
|
|
|
|
|
|
|
furi_string_free(message);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2024-03-25 15:47:34 +03:00
|
|
|
void infrared_set_tx_pin(InfraredApp* infrared, FuriHalInfraredTxPin tx_pin) {
|
|
|
|
if(tx_pin < FuriHalInfraredTxPinMax) {
|
|
|
|
furi_hal_infrared_set_tx_output(tx_pin);
|
|
|
|
} else {
|
|
|
|
FuriHalInfraredTxPin tx_pin_detected = furi_hal_infrared_detect_tx_output();
|
|
|
|
furi_hal_infrared_set_tx_output(tx_pin_detected);
|
|
|
|
if(tx_pin_detected != FuriHalInfraredTxPinInternal) {
|
|
|
|
infrared_enable_otg(infrared, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
infrared->app_state.tx_pin = tx_pin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void infrared_enable_otg(InfraredApp* infrared, bool enable) {
|
|
|
|
if(enable) {
|
2024-09-09 05:12:18 +03:00
|
|
|
if(!furi_hal_power_is_otg_enabled()) furi_hal_power_enable_otg();
|
2024-03-25 15:47:34 +03:00
|
|
|
} else {
|
2024-09-09 05:12:18 +03:00
|
|
|
if(furi_hal_power_is_otg_enabled()) furi_hal_power_disable_otg();
|
2024-03-25 15:47:34 +03:00
|
|
|
}
|
|
|
|
infrared->app_state.is_otg_enabled = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void infrared_load_settings(InfraredApp* infrared) {
|
|
|
|
InfraredSettings settings = {0};
|
|
|
|
|
|
|
|
if(!saved_struct_load(
|
|
|
|
INFRARED_SETTINGS_PATH,
|
|
|
|
&settings,
|
|
|
|
sizeof(InfraredSettings),
|
|
|
|
INFRARED_SETTINGS_MAGIC,
|
|
|
|
INFRARED_SETTINGS_VERSION)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed to load settings, using defaults");
|
|
|
|
infrared_save_settings(infrared);
|
|
|
|
}
|
|
|
|
|
|
|
|
infrared_set_tx_pin(infrared, settings.tx_pin);
|
2024-04-01 16:42:47 +03:00
|
|
|
if(settings.tx_pin < FuriHalInfraredTxPinMax) {
|
|
|
|
infrared_enable_otg(infrared, settings.otg_enabled);
|
|
|
|
}
|
2024-03-25 15:47:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void infrared_save_settings(InfraredApp* infrared) {
|
|
|
|
InfraredSettings settings = {
|
|
|
|
.tx_pin = infrared->app_state.tx_pin,
|
2024-04-01 16:42:47 +03:00
|
|
|
.otg_enabled = infrared->app_state.is_otg_enabled,
|
2024-03-25 15:47:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!saved_struct_save(
|
|
|
|
INFRARED_SETTINGS_PATH,
|
|
|
|
&settings,
|
|
|
|
sizeof(InfraredSettings),
|
|
|
|
INFRARED_SETTINGS_MAGIC,
|
|
|
|
INFRARED_SETTINGS_VERSION)) {
|
|
|
|
FURI_LOG_E(TAG, "Failed to save settings");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
void infrared_signal_received_callback(void* context, InfraredWorkerSignal* received_signal) {
|
|
|
|
furi_assert(context);
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
if(infrared_worker_signal_is_decoded(received_signal)) {
|
|
|
|
infrared_signal_set_message(
|
2023-10-30 19:20:35 +03:00
|
|
|
infrared->current_signal, infrared_worker_get_decoded_signal(received_signal));
|
2022-06-21 15:45:50 +03:00
|
|
|
} else {
|
|
|
|
const uint32_t* timings;
|
|
|
|
size_t timings_size;
|
|
|
|
infrared_worker_get_raw_signal(received_signal, &timings, &timings_size);
|
|
|
|
infrared_signal_set_raw_signal(
|
2023-10-30 19:20:35 +03:00
|
|
|
infrared->current_signal,
|
2022-06-21 15:45:50 +03:00
|
|
|
timings,
|
|
|
|
timings_size,
|
|
|
|
INFRARED_COMMON_CARRIER_FREQUENCY,
|
|
|
|
INFRARED_COMMON_DUTY_CYCLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
view_dispatcher_send_custom_event(
|
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypeSignalReceived);
|
|
|
|
}
|
|
|
|
|
|
|
|
void infrared_text_input_callback(void* context) {
|
|
|
|
furi_assert(context);
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2022-06-21 15:45:50 +03:00
|
|
|
view_dispatcher_send_custom_event(
|
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypeTextEditDone);
|
|
|
|
}
|
|
|
|
|
2022-06-21 17:45:55 +03:00
|
|
|
void infrared_popup_closed_callback(void* context) {
|
2022-06-21 15:45:50 +03:00
|
|
|
furi_assert(context);
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2022-06-21 15:45:50 +03:00
|
|
|
view_dispatcher_send_custom_event(
|
2022-06-21 17:45:55 +03:00
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypePopupClosed);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t infrared_app(void* p) {
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = infrared_alloc();
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2024-03-25 15:47:34 +03:00
|
|
|
infrared_load_settings(infrared);
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared_make_app_folder(infrared);
|
|
|
|
|
|
|
|
bool is_remote_loaded = false;
|
2022-07-04 16:09:46 +03:00
|
|
|
bool is_rpc_mode = false;
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2022-08-03 18:47:10 +03:00
|
|
|
if(p && strlen(p)) {
|
2022-07-04 16:09:46 +03:00
|
|
|
uint32_t rpc_ctx = 0;
|
|
|
|
if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
|
|
|
|
infrared->rpc_ctx = (void*)rpc_ctx;
|
|
|
|
rpc_system_app_set_callback(
|
|
|
|
infrared->rpc_ctx, infrared_rpc_command_callback, infrared);
|
2022-07-25 17:16:45 +03:00
|
|
|
rpc_system_app_send_started(infrared->rpc_ctx);
|
2022-07-04 16:09:46 +03:00
|
|
|
is_rpc_mode = true;
|
|
|
|
} else {
|
2023-10-30 19:20:35 +03:00
|
|
|
const char* file_path = (const char*)p;
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode error = infrared_remote_load(infrared->remote, file_path);
|
|
|
|
|
|
|
|
if(!INFRARED_ERROR_PRESENT(error)) {
|
|
|
|
is_remote_loaded = true;
|
|
|
|
} else {
|
|
|
|
is_remote_loaded = false;
|
|
|
|
bool wrong_file_type = INFRARED_ERROR_CHECK(error, InfraredErrorCodeWrongFileType);
|
|
|
|
const char* format = wrong_file_type ?
|
|
|
|
"Library file\n\"%s\" can't be openned as a remote" :
|
|
|
|
"Failed to load\n\"%s\"";
|
|
|
|
|
|
|
|
infrared_show_error_message(infrared, format, file_path);
|
2022-07-04 16:09:46 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2023-10-30 19:20:35 +03:00
|
|
|
|
|
|
|
furi_string_set(infrared->file_path, file_path);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 16:09:46 +03:00
|
|
|
if(is_rpc_mode) {
|
2022-07-25 17:16:45 +03:00
|
|
|
view_dispatcher_attach_to_gui(
|
|
|
|
infrared->view_dispatcher, infrared->gui, ViewDispatcherTypeDesktop);
|
2022-07-04 16:09:46 +03:00
|
|
|
scene_manager_next_scene(infrared->scene_manager, InfraredSceneRpc);
|
2022-06-21 15:45:50 +03:00
|
|
|
} else {
|
2022-07-25 17:16:45 +03:00
|
|
|
view_dispatcher_attach_to_gui(
|
|
|
|
infrared->view_dispatcher, infrared->gui, ViewDispatcherTypeFullscreen);
|
2022-12-26 15:13:30 +03:00
|
|
|
if(is_remote_loaded) { //-V547
|
2022-07-25 17:16:45 +03:00
|
|
|
scene_manager_next_scene(infrared->scene_manager, InfraredSceneRemote);
|
|
|
|
} else {
|
|
|
|
scene_manager_next_scene(infrared->scene_manager, InfraredSceneStart);
|
|
|
|
}
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
view_dispatcher_run(infrared->view_dispatcher);
|
|
|
|
|
2024-03-25 15:47:34 +03:00
|
|
|
infrared_set_tx_pin(infrared, FuriHalInfraredTxPinInternal);
|
|
|
|
infrared_enable_otg(infrared, false);
|
2022-06-21 15:45:50 +03:00
|
|
|
infrared_free(infrared);
|
2024-03-25 15:47:34 +03:00
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
return 0;
|
|
|
|
}
|