infrared.c 14 KB

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