infrared.c 16 KB

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