infrared.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #include "infrared_i.h"
  2. #include <string.h>
  3. #include <dolphin/dolphin.h>
  4. static const NotificationSequence* infrared_notification_sequences[] = {
  5. &sequence_success,
  6. &sequence_set_only_green_255,
  7. &sequence_reset_green,
  8. &sequence_solid_yellow,
  9. &sequence_reset_rgb,
  10. &sequence_blink_start_cyan,
  11. &sequence_blink_start_magenta,
  12. &sequence_blink_stop,
  13. };
  14. static void infrared_make_app_folder(Infrared* infrared) {
  15. if(!storage_simply_mkdir(infrared->storage, INFRARED_APP_FOLDER)) {
  16. dialog_message_show_storage_error(infrared->dialogs, "Cannot create\napp folder");
  17. }
  18. }
  19. static bool infrared_custom_event_callback(void* context, uint32_t event) {
  20. furi_assert(context);
  21. Infrared* infrared = context;
  22. return scene_manager_handle_custom_event(infrared->scene_manager, event);
  23. }
  24. static bool infrared_back_event_callback(void* context) {
  25. furi_assert(context);
  26. Infrared* infrared = context;
  27. return scene_manager_handle_back_event(infrared->scene_manager);
  28. }
  29. static void infrared_tick_event_callback(void* context) {
  30. furi_assert(context);
  31. Infrared* infrared = context;
  32. scene_manager_handle_tick_event(infrared->scene_manager);
  33. }
  34. static void infrared_rpc_command_callback(RpcAppSystemEvent event, void* context) {
  35. furi_assert(context);
  36. Infrared* infrared = context;
  37. furi_assert(infrared->rpc_ctx);
  38. if(event == RpcAppEventSessionClose) {
  39. view_dispatcher_send_custom_event(
  40. infrared->view_dispatcher, InfraredCustomEventTypeRpcSessionClose);
  41. rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
  42. infrared->rpc_ctx = NULL;
  43. } else if(event == RpcAppEventAppExit) {
  44. view_dispatcher_send_custom_event(
  45. infrared->view_dispatcher, InfraredCustomEventTypeRpcExit);
  46. } else if(event == RpcAppEventLoadFile) {
  47. view_dispatcher_send_custom_event(
  48. infrared->view_dispatcher, InfraredCustomEventTypeRpcLoad);
  49. } else if(event == RpcAppEventButtonPress) {
  50. view_dispatcher_send_custom_event(
  51. infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonPress);
  52. } else if(event == RpcAppEventButtonRelease) {
  53. view_dispatcher_send_custom_event(
  54. infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonRelease);
  55. } else {
  56. rpc_system_app_confirm(infrared->rpc_ctx, event, false);
  57. }
  58. }
  59. static void infrared_find_vacant_remote_name(string_t name, const char* path) {
  60. Storage* storage = furi_record_open(RECORD_STORAGE);
  61. string_t base_path;
  62. string_init_set_str(base_path, path);
  63. if(string_end_with_str_p(base_path, INFRARED_APP_EXTENSION)) {
  64. size_t filename_start = string_search_rchar(base_path, '/');
  65. string_left(base_path, filename_start);
  66. }
  67. string_printf(base_path, "%s/%s%s", path, string_get_cstr(name), INFRARED_APP_EXTENSION);
  68. FS_Error status = storage_common_stat(storage, string_get_cstr(base_path), NULL);
  69. if(status == FSE_OK) {
  70. /* If the suggested name is occupied, try another one (name2, name3, etc) */
  71. size_t dot = string_search_rchar(base_path, '.');
  72. string_left(base_path, dot);
  73. string_t path_temp;
  74. string_init(path_temp);
  75. uint32_t i = 1;
  76. do {
  77. string_printf(
  78. path_temp, "%s%u%s", string_get_cstr(base_path), ++i, INFRARED_APP_EXTENSION);
  79. status = storage_common_stat(storage, string_get_cstr(path_temp), NULL);
  80. } while(status == FSE_OK);
  81. string_clear(path_temp);
  82. if(status == FSE_NOT_EXIST) {
  83. string_cat_printf(name, "%u", i);
  84. }
  85. }
  86. string_clear(base_path);
  87. furi_record_close(RECORD_STORAGE);
  88. }
  89. static Infrared* infrared_alloc() {
  90. Infrared* infrared = malloc(sizeof(Infrared));
  91. string_init(infrared->file_path);
  92. InfraredAppState* app_state = &infrared->app_state;
  93. app_state->is_learning_new_remote = false;
  94. app_state->is_debug_enabled = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);
  95. app_state->edit_target = InfraredEditTargetNone;
  96. app_state->edit_mode = InfraredEditModeNone;
  97. app_state->current_button_index = InfraredButtonIndexNone;
  98. infrared->scene_manager = scene_manager_alloc(&infrared_scene_handlers, infrared);
  99. infrared->view_dispatcher = view_dispatcher_alloc();
  100. infrared->gui = furi_record_open(RECORD_GUI);
  101. ViewDispatcher* view_dispatcher = infrared->view_dispatcher;
  102. view_dispatcher_enable_queue(view_dispatcher);
  103. view_dispatcher_set_event_callback_context(view_dispatcher, infrared);
  104. view_dispatcher_set_custom_event_callback(view_dispatcher, infrared_custom_event_callback);
  105. view_dispatcher_set_navigation_event_callback(view_dispatcher, infrared_back_event_callback);
  106. view_dispatcher_set_tick_event_callback(view_dispatcher, infrared_tick_event_callback, 100);
  107. infrared->storage = furi_record_open(RECORD_STORAGE);
  108. infrared->dialogs = furi_record_open(RECORD_DIALOGS);
  109. infrared->notifications = furi_record_open(RECORD_NOTIFICATION);
  110. infrared->worker = infrared_worker_alloc();
  111. infrared->remote = infrared_remote_alloc();
  112. infrared->received_signal = infrared_signal_alloc();
  113. infrared->brute_force = infrared_brute_force_alloc();
  114. infrared->submenu = submenu_alloc();
  115. view_dispatcher_add_view(
  116. view_dispatcher, InfraredViewSubmenu, submenu_get_view(infrared->submenu));
  117. infrared->text_input = text_input_alloc();
  118. view_dispatcher_add_view(
  119. view_dispatcher, InfraredViewTextInput, text_input_get_view(infrared->text_input));
  120. infrared->dialog_ex = dialog_ex_alloc();
  121. view_dispatcher_add_view(
  122. view_dispatcher, InfraredViewDialogEx, dialog_ex_get_view(infrared->dialog_ex));
  123. infrared->button_menu = button_menu_alloc();
  124. view_dispatcher_add_view(
  125. view_dispatcher, InfraredViewButtonMenu, button_menu_get_view(infrared->button_menu));
  126. infrared->popup = popup_alloc();
  127. view_dispatcher_add_view(view_dispatcher, InfraredViewPopup, popup_get_view(infrared->popup));
  128. infrared->view_stack = view_stack_alloc();
  129. view_dispatcher_add_view(
  130. view_dispatcher, InfraredViewStack, view_stack_get_view(infrared->view_stack));
  131. if(app_state->is_debug_enabled) {
  132. infrared->debug_view = infrared_debug_view_alloc();
  133. view_dispatcher_add_view(
  134. view_dispatcher,
  135. InfraredViewDebugView,
  136. infrared_debug_view_get_view(infrared->debug_view));
  137. }
  138. infrared->button_panel = button_panel_alloc();
  139. infrared->loading = loading_alloc();
  140. infrared->progress = infrared_progress_view_alloc();
  141. return infrared;
  142. }
  143. static void infrared_free(Infrared* infrared) {
  144. furi_assert(infrared);
  145. ViewDispatcher* view_dispatcher = infrared->view_dispatcher;
  146. InfraredAppState* app_state = &infrared->app_state;
  147. if(infrared->rpc_ctx) {
  148. rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
  149. rpc_system_app_send_exited(infrared->rpc_ctx);
  150. infrared->rpc_ctx = NULL;
  151. }
  152. view_dispatcher_remove_view(view_dispatcher, InfraredViewSubmenu);
  153. submenu_free(infrared->submenu);
  154. view_dispatcher_remove_view(view_dispatcher, InfraredViewTextInput);
  155. text_input_free(infrared->text_input);
  156. view_dispatcher_remove_view(view_dispatcher, InfraredViewDialogEx);
  157. dialog_ex_free(infrared->dialog_ex);
  158. view_dispatcher_remove_view(view_dispatcher, InfraredViewButtonMenu);
  159. button_menu_free(infrared->button_menu);
  160. view_dispatcher_remove_view(view_dispatcher, InfraredViewPopup);
  161. popup_free(infrared->popup);
  162. view_dispatcher_remove_view(view_dispatcher, InfraredViewStack);
  163. view_stack_free(infrared->view_stack);
  164. if(app_state->is_debug_enabled) {
  165. view_dispatcher_remove_view(view_dispatcher, InfraredViewDebugView);
  166. infrared_debug_view_free(infrared->debug_view);
  167. }
  168. button_panel_free(infrared->button_panel);
  169. loading_free(infrared->loading);
  170. infrared_progress_view_free(infrared->progress);
  171. view_dispatcher_free(view_dispatcher);
  172. scene_manager_free(infrared->scene_manager);
  173. infrared_brute_force_free(infrared->brute_force);
  174. infrared_signal_free(infrared->received_signal);
  175. infrared_remote_free(infrared->remote);
  176. infrared_worker_free(infrared->worker);
  177. furi_record_close(RECORD_NOTIFICATION);
  178. infrared->notifications = NULL;
  179. furi_record_close(RECORD_DIALOGS);
  180. infrared->dialogs = NULL;
  181. furi_record_close(RECORD_GUI);
  182. infrared->gui = NULL;
  183. string_clear(infrared->file_path);
  184. free(infrared);
  185. }
  186. bool infrared_add_remote_with_button(
  187. Infrared* infrared,
  188. const char* button_name,
  189. InfraredSignal* signal) {
  190. InfraredRemote* remote = infrared->remote;
  191. string_t new_name, new_path;
  192. string_init_set_str(new_name, INFRARED_DEFAULT_REMOTE_NAME);
  193. string_init_set_str(new_path, INFRARED_APP_FOLDER);
  194. infrared_find_vacant_remote_name(new_name, string_get_cstr(new_path));
  195. string_cat_printf(new_path, "/%s%s", string_get_cstr(new_name), INFRARED_APP_EXTENSION);
  196. infrared_remote_reset(remote);
  197. infrared_remote_set_name(remote, string_get_cstr(new_name));
  198. infrared_remote_set_path(remote, string_get_cstr(new_path));
  199. string_clear(new_name);
  200. string_clear(new_path);
  201. return infrared_remote_add_button(remote, button_name, signal);
  202. }
  203. bool infrared_rename_current_remote(Infrared* infrared, const char* name) {
  204. InfraredRemote* remote = infrared->remote;
  205. const char* remote_path = infrared_remote_get_path(remote);
  206. if(!strcmp(infrared_remote_get_name(remote), name)) {
  207. return true;
  208. }
  209. string_t new_name;
  210. string_init_set_str(new_name, name);
  211. infrared_find_vacant_remote_name(new_name, remote_path);
  212. string_t new_path;
  213. string_init_set(new_path, infrared_remote_get_path(remote));
  214. if(string_end_with_str_p(new_path, INFRARED_APP_EXTENSION)) {
  215. size_t filename_start = string_search_rchar(new_path, '/');
  216. string_left(new_path, filename_start);
  217. }
  218. string_cat_printf(new_path, "/%s%s", string_get_cstr(new_name), INFRARED_APP_EXTENSION);
  219. Storage* storage = furi_record_open(RECORD_STORAGE);
  220. FS_Error status = storage_common_rename(
  221. storage, infrared_remote_get_path(remote), string_get_cstr(new_path));
  222. infrared_remote_set_name(remote, string_get_cstr(new_name));
  223. infrared_remote_set_path(remote, string_get_cstr(new_path));
  224. string_clear(new_name);
  225. string_clear(new_path);
  226. furi_record_close(RECORD_STORAGE);
  227. return (status == FSE_OK || status == FSE_EXIST);
  228. }
  229. void infrared_tx_start_signal(Infrared* infrared, InfraredSignal* signal) {
  230. if(infrared->app_state.is_transmitting) {
  231. FURI_LOG_D(INFRARED_LOG_TAG, "Transmitter is already active");
  232. return;
  233. } else {
  234. infrared->app_state.is_transmitting = true;
  235. }
  236. if(infrared_signal_is_raw(signal)) {
  237. InfraredRawSignal* raw = infrared_signal_get_raw_signal(signal);
  238. infrared_worker_set_raw_signal(infrared->worker, raw->timings, raw->timings_size);
  239. } else {
  240. InfraredMessage* message = infrared_signal_get_message(signal);
  241. infrared_worker_set_decoded_signal(infrared->worker, message);
  242. }
  243. DOLPHIN_DEED(DolphinDeedIrSend);
  244. infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
  245. infrared_worker_tx_set_get_signal_callback(
  246. infrared->worker, infrared_worker_tx_get_signal_steady_callback, infrared);
  247. infrared_worker_tx_start(infrared->worker);
  248. }
  249. void infrared_tx_start_button_index(Infrared* infrared, size_t button_index) {
  250. furi_assert(button_index < infrared_remote_get_button_count(infrared->remote));
  251. InfraredRemoteButton* button = infrared_remote_get_button(infrared->remote, button_index);
  252. InfraredSignal* signal = infrared_remote_button_get_signal(button);
  253. infrared_tx_start_signal(infrared, signal);
  254. infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
  255. }
  256. void infrared_tx_start_received(Infrared* infrared) {
  257. infrared_tx_start_signal(infrared, infrared->received_signal);
  258. infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
  259. }
  260. void infrared_tx_stop(Infrared* infrared) {
  261. if(!infrared->app_state.is_transmitting) {
  262. FURI_LOG_D(INFRARED_LOG_TAG, "Transmitter is already stopped");
  263. return;
  264. } else {
  265. infrared->app_state.is_transmitting = false;
  266. }
  267. infrared_worker_tx_stop(infrared->worker);
  268. infrared_worker_tx_set_get_signal_callback(infrared->worker, NULL, NULL);
  269. infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStop);
  270. }
  271. void infrared_text_store_set(Infrared* infrared, uint32_t bank, const char* text, ...) {
  272. va_list args;
  273. va_start(args, text);
  274. vsnprintf(infrared->text_store[bank], INFRARED_TEXT_STORE_SIZE, text, args);
  275. va_end(args);
  276. }
  277. void infrared_text_store_clear(Infrared* infrared, uint32_t bank) {
  278. memset(infrared->text_store[bank], 0, INFRARED_TEXT_STORE_SIZE);
  279. }
  280. void infrared_play_notification_message(Infrared* infrared, uint32_t message) {
  281. furi_assert(message < sizeof(infrared_notification_sequences) / sizeof(NotificationSequence*));
  282. notification_message(infrared->notifications, infrared_notification_sequences[message]);
  283. }
  284. void infrared_show_loading_popup(Infrared* infrared, bool show) {
  285. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  286. ViewStack* view_stack = infrared->view_stack;
  287. Loading* loading = infrared->loading;
  288. if(show) {
  289. // Raise timer priority so that animations can play
  290. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  291. view_stack_add_view(view_stack, loading_get_view(loading));
  292. } else {
  293. view_stack_remove_view(view_stack, loading_get_view(loading));
  294. // Restore default timer priority
  295. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  296. }
  297. }
  298. void infrared_signal_received_callback(void* context, InfraredWorkerSignal* received_signal) {
  299. furi_assert(context);
  300. Infrared* infrared = context;
  301. if(infrared_worker_signal_is_decoded(received_signal)) {
  302. infrared_signal_set_message(
  303. infrared->received_signal, infrared_worker_get_decoded_signal(received_signal));
  304. } else {
  305. const uint32_t* timings;
  306. size_t timings_size;
  307. infrared_worker_get_raw_signal(received_signal, &timings, &timings_size);
  308. infrared_signal_set_raw_signal(
  309. infrared->received_signal,
  310. timings,
  311. timings_size,
  312. INFRARED_COMMON_CARRIER_FREQUENCY,
  313. INFRARED_COMMON_DUTY_CYCLE);
  314. }
  315. view_dispatcher_send_custom_event(
  316. infrared->view_dispatcher, InfraredCustomEventTypeSignalReceived);
  317. }
  318. void infrared_text_input_callback(void* context) {
  319. furi_assert(context);
  320. Infrared* infrared = context;
  321. view_dispatcher_send_custom_event(
  322. infrared->view_dispatcher, InfraredCustomEventTypeTextEditDone);
  323. }
  324. void infrared_popup_closed_callback(void* context) {
  325. furi_assert(context);
  326. Infrared* infrared = context;
  327. view_dispatcher_send_custom_event(
  328. infrared->view_dispatcher, InfraredCustomEventTypePopupClosed);
  329. }
  330. int32_t infrared_app(void* p) {
  331. Infrared* infrared = infrared_alloc();
  332. infrared_make_app_folder(infrared);
  333. bool is_remote_loaded = false;
  334. bool is_rpc_mode = false;
  335. if(p && strlen(p)) {
  336. uint32_t rpc_ctx = 0;
  337. if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
  338. infrared->rpc_ctx = (void*)rpc_ctx;
  339. rpc_system_app_set_callback(
  340. infrared->rpc_ctx, infrared_rpc_command_callback, infrared);
  341. rpc_system_app_send_started(infrared->rpc_ctx);
  342. is_rpc_mode = true;
  343. } else {
  344. string_set_str(infrared->file_path, (const char*)p);
  345. is_remote_loaded = infrared_remote_load(infrared->remote, infrared->file_path);
  346. if(!is_remote_loaded) {
  347. dialog_message_show_storage_error(
  348. infrared->dialogs, "Failed to load\nselected remote");
  349. return -1;
  350. }
  351. }
  352. }
  353. if(is_rpc_mode) {
  354. view_dispatcher_attach_to_gui(
  355. infrared->view_dispatcher, infrared->gui, ViewDispatcherTypeDesktop);
  356. scene_manager_next_scene(infrared->scene_manager, InfraredSceneRpc);
  357. } else {
  358. view_dispatcher_attach_to_gui(
  359. infrared->view_dispatcher, infrared->gui, ViewDispatcherTypeFullscreen);
  360. if(is_remote_loaded) {
  361. scene_manager_next_scene(infrared->scene_manager, InfraredSceneRemote);
  362. } else {
  363. scene_manager_next_scene(infrared->scene_manager, InfraredSceneStart);
  364. }
  365. }
  366. view_dispatcher_run(infrared->view_dispatcher);
  367. infrared_free(infrared);
  368. return 0;
  369. }