2023-10-30 19:20:35 +03:00
|
|
|
#include "../infrared_app_i.h"
|
2023-02-07 19:33:05 +03:00
|
|
|
#include <gui/canvas.h>
|
2022-07-04 16:09:46 +03:00
|
|
|
|
2023-11-10 10:22:34 +03:00
|
|
|
#define TAG "InfraredApp"
|
|
|
|
|
2022-08-11 18:37:23 +03:00
|
|
|
typedef enum {
|
|
|
|
InfraredRpcStateIdle,
|
|
|
|
InfraredRpcStateLoaded,
|
|
|
|
InfraredRpcStateSending,
|
|
|
|
} InfraredRpcState;
|
|
|
|
|
2024-03-11 20:35:51 +03:00
|
|
|
static int32_t infrared_scene_rpc_task_callback(void* context) {
|
|
|
|
InfraredApp* infrared = context;
|
2024-09-06 12:52:00 +03:00
|
|
|
const InfraredErrorCode error =
|
2024-03-11 20:35:51 +03:00
|
|
|
infrared_remote_load(infrared->remote, furi_string_get_cstr(infrared->file_path));
|
|
|
|
view_dispatcher_send_custom_event(
|
|
|
|
infrared->view_dispatcher, InfraredCustomEventTypeTaskFinished);
|
2024-09-06 12:52:00 +03:00
|
|
|
return error;
|
2024-03-11 20:35:51 +03:00
|
|
|
}
|
|
|
|
|
2022-07-04 16:09:46 +03:00
|
|
|
void infrared_scene_rpc_on_enter(void* context) {
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2024-09-05 22:54:49 +03:00
|
|
|
scene_manager_set_scene_state(infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateIdle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void infrared_scene_rpc_show(InfraredApp* infrared) {
|
2022-07-04 16:09:46 +03:00
|
|
|
Popup* popup = infrared->popup;
|
|
|
|
|
2022-08-11 18:37:23 +03:00
|
|
|
popup_set_header(popup, "Infrared", 89, 42, AlignCenter, AlignBottom);
|
|
|
|
popup_set_text(popup, "RPC mode", 89, 44, AlignCenter, AlignTop);
|
2024-09-05 22:54:49 +03:00
|
|
|
popup_set_text(popup, infrared->text_store[0], 89, 44, AlignCenter, AlignTop);
|
2022-07-25 17:16:45 +03:00
|
|
|
|
2022-08-11 18:37:23 +03:00
|
|
|
popup_set_icon(popup, 0, 12, &I_RFIDDolphinSend_97x61);
|
2022-07-04 16:09:46 +03:00
|
|
|
|
2024-09-05 22:54:49 +03:00
|
|
|
popup_set_context(popup, infrared);
|
2022-07-04 16:09:46 +03:00
|
|
|
popup_set_callback(popup, infrared_popup_closed_callback);
|
|
|
|
|
2024-03-20 17:54:07 +03:00
|
|
|
view_dispatcher_switch_to_view(infrared->view_dispatcher, InfraredViewPopup);
|
2024-09-05 22:54:49 +03:00
|
|
|
scene_manager_set_scene_state(
|
|
|
|
infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateSending);
|
2022-07-04 16:09:46 +03:00
|
|
|
notification_message(infrared->notifications, &sequence_display_backlight_on);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool infrared_scene_rpc_on_event(void* context, SceneManagerEvent event) {
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2022-07-04 16:09:46 +03:00
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
if(event.type == SceneManagerEventTypeCustom) {
|
2024-03-11 20:35:51 +03:00
|
|
|
InfraredAppState* app_state = &infrared->app_state;
|
|
|
|
InfraredRpcState rpc_state =
|
2022-08-11 18:37:23 +03:00
|
|
|
scene_manager_get_scene_state(infrared->scene_manager, InfraredSceneRpc);
|
2024-03-11 20:35:51 +03:00
|
|
|
|
|
|
|
if(event.event == InfraredCustomEventTypeRpcLoadFile) {
|
|
|
|
if(rpc_state == InfraredRpcStateIdle) {
|
|
|
|
// Load the remote in a separate thread
|
|
|
|
infrared_blocking_task_start(infrared, infrared_scene_rpc_task_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(event.event == InfraredCustomEventTypeTaskFinished) {
|
2024-09-06 12:52:00 +03:00
|
|
|
const InfraredErrorCode task_error = infrared_blocking_task_finalize(infrared);
|
|
|
|
|
|
|
|
if(!INFRARED_ERROR_PRESENT(task_error)) {
|
|
|
|
const char* remote_name = infrared_remote_get_name(infrared->remote);
|
|
|
|
infrared_text_store_set(infrared, 0, "loaded\n%s", remote_name);
|
2024-03-11 20:35:51 +03:00
|
|
|
scene_manager_set_scene_state(
|
|
|
|
infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateLoaded);
|
|
|
|
} else {
|
2024-09-05 22:54:49 +03:00
|
|
|
FuriString* str = furi_string_alloc();
|
|
|
|
furi_string_printf(
|
|
|
|
str, "Failed to load\n%s", furi_string_get_cstr(infrared->file_path));
|
2022-07-25 17:16:45 +03:00
|
|
|
|
2024-09-05 22:54:49 +03:00
|
|
|
rpc_system_app_set_error_code(infrared->rpc_ctx, RpcAppSystemErrorCodeParseFile);
|
|
|
|
rpc_system_app_set_error_text(infrared->rpc_ctx, furi_string_get_cstr(str));
|
2022-08-02 15:54:12 +03:00
|
|
|
|
2024-09-05 22:54:49 +03:00
|
|
|
furi_string_free(str);
|
|
|
|
}
|
2024-09-06 12:52:00 +03:00
|
|
|
rpc_system_app_confirm(infrared->rpc_ctx, !INFRARED_ERROR_PRESENT(task_error));
|
2023-11-10 10:22:34 +03:00
|
|
|
} else if(
|
|
|
|
event.event == InfraredCustomEventTypeRpcButtonPressName ||
|
|
|
|
event.event == InfraredCustomEventTypeRpcButtonPressIndex) {
|
2022-08-02 15:54:12 +03:00
|
|
|
bool result = false;
|
2024-03-11 20:35:51 +03:00
|
|
|
if(rpc_state == InfraredRpcStateLoaded) {
|
2023-11-10 10:22:34 +03:00
|
|
|
if(event.event == InfraredCustomEventTypeRpcButtonPressName) {
|
|
|
|
const char* button_name = furi_string_get_cstr(infrared->button_name);
|
|
|
|
size_t index;
|
|
|
|
const bool index_found =
|
|
|
|
infrared_remote_get_signal_index(infrared->remote, button_name, &index);
|
2024-03-11 20:35:51 +03:00
|
|
|
app_state->current_button_index = index_found ? (signed)index :
|
|
|
|
InfraredButtonIndexNone;
|
2023-11-10 10:22:34 +03:00
|
|
|
FURI_LOG_D(TAG, "Sending signal with name \"%s\"", button_name);
|
|
|
|
} else {
|
|
|
|
FURI_LOG_D(
|
2024-03-11 20:35:51 +03:00
|
|
|
TAG, "Sending signal with index \"%ld\"", app_state->current_button_index);
|
2023-11-10 10:22:34 +03:00
|
|
|
}
|
|
|
|
if(infrared->app_state.current_button_index != InfraredButtonIndexNone) {
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode error =
|
|
|
|
infrared_tx_start_button_index(infrared, app_state->current_button_index);
|
|
|
|
if(!INFRARED_ERROR_PRESENT(error)) {
|
2024-09-05 22:54:49 +03:00
|
|
|
const char* remote_name = infrared_remote_get_name(infrared->remote);
|
|
|
|
infrared_text_store_set(infrared, 0, "emulating\n%s", remote_name);
|
|
|
|
|
|
|
|
infrared_scene_rpc_show(infrared);
|
|
|
|
result = true;
|
|
|
|
} else {
|
|
|
|
rpc_system_app_set_error_code(
|
|
|
|
infrared->rpc_ctx, RpcAppSystemErrorCodeInternalParse);
|
|
|
|
rpc_system_app_set_error_text(
|
|
|
|
infrared->rpc_ctx, "Cannot load button data");
|
|
|
|
result = false;
|
|
|
|
}
|
2022-08-02 15:54:12 +03:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 10:22:34 +03:00
|
|
|
rpc_system_app_confirm(infrared->rpc_ctx, result);
|
2024-03-11 20:35:51 +03:00
|
|
|
|
2022-08-02 15:54:12 +03:00
|
|
|
} else if(event.event == InfraredCustomEventTypeRpcButtonRelease) {
|
2022-08-11 18:37:23 +03:00
|
|
|
bool result = false;
|
2024-03-11 20:35:51 +03:00
|
|
|
|
|
|
|
if(rpc_state == InfraredRpcStateSending) {
|
2022-08-11 18:37:23 +03:00
|
|
|
infrared_tx_stop(infrared);
|
|
|
|
result = true;
|
|
|
|
scene_manager_set_scene_state(
|
|
|
|
infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateLoaded);
|
|
|
|
}
|
2024-03-11 20:35:51 +03:00
|
|
|
|
2023-11-10 10:22:34 +03:00
|
|
|
rpc_system_app_confirm(infrared->rpc_ctx, result);
|
2024-03-11 20:35:51 +03:00
|
|
|
|
|
|
|
} else if(
|
|
|
|
event.event == InfraredCustomEventTypeRpcExit ||
|
|
|
|
event.event == InfraredCustomEventTypeRpcSessionClose ||
|
|
|
|
event.event == InfraredCustomEventTypePopupClosed) {
|
2022-08-11 18:37:23 +03:00
|
|
|
scene_manager_stop(infrared->scene_manager);
|
2022-08-02 15:54:12 +03:00
|
|
|
view_dispatcher_stop(infrared->view_dispatcher);
|
2024-03-11 20:35:51 +03:00
|
|
|
|
|
|
|
if(event.event == InfraredCustomEventTypeRpcExit) {
|
|
|
|
rpc_system_app_confirm(infrared->rpc_ctx, true);
|
|
|
|
}
|
2022-07-04 16:09:46 +03:00
|
|
|
}
|
2024-03-11 20:35:51 +03:00
|
|
|
|
|
|
|
consumed = true;
|
2022-07-04 16:09:46 +03:00
|
|
|
}
|
2024-03-11 20:35:51 +03:00
|
|
|
|
2022-07-04 16:09:46 +03:00
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void infrared_scene_rpc_on_exit(void* context) {
|
2023-10-30 19:20:35 +03:00
|
|
|
InfraredApp* infrared = context;
|
2022-08-11 18:37:23 +03:00
|
|
|
if(scene_manager_get_scene_state(infrared->scene_manager, InfraredSceneRpc) ==
|
|
|
|
InfraredRpcStateSending) {
|
|
|
|
infrared_tx_stop(infrared);
|
|
|
|
}
|
2024-03-11 20:35:51 +03:00
|
|
|
|
2022-07-04 16:09:46 +03:00
|
|
|
popup_reset(infrared->popup);
|
|
|
|
}
|