infrared_app.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "infrared_app.h"
  2. #include "m-string.h"
  3. #include <infrared_worker.h>
  4. #include <furi.h>
  5. #include <gui/gui.h>
  6. #include <input/input.h>
  7. #include <stdio.h>
  8. #include <callback-connector.h>
  9. int32_t InfraredApp::run(void* args) {
  10. InfraredAppEvent event;
  11. bool consumed;
  12. bool exit = false;
  13. if(args) {
  14. string_t path;
  15. string_init_set_str(path, (char*)args);
  16. if(string_end_with_str_p(path, InfraredApp::infrared_extension)) {
  17. bool result = remote_manager.load(path);
  18. if(result) {
  19. current_scene = InfraredApp::Scene::Remote;
  20. } else {
  21. printf("Failed to load remote \'%s\'\r\n", string_get_cstr(path));
  22. return -1;
  23. }
  24. }
  25. string_clear(path);
  26. }
  27. scenes[current_scene]->on_enter(this);
  28. while(!exit) {
  29. view_manager.receive_event(&event);
  30. if(event.type == InfraredAppEvent::Type::Exit) break;
  31. consumed = scenes[current_scene]->on_event(this, &event);
  32. if(!consumed) {
  33. if(event.type == InfraredAppEvent::Type::Back) {
  34. exit = switch_to_previous_scene();
  35. }
  36. }
  37. };
  38. scenes[current_scene]->on_exit(this);
  39. return 0;
  40. };
  41. InfraredApp::InfraredApp() {
  42. furi_check(InfraredAppRemoteManager::max_button_name_length < get_text_store_size());
  43. string_init_set_str(file_path, InfraredApp::infrared_directory);
  44. notification = static_cast<NotificationApp*>(furi_record_open("notification"));
  45. dialogs = static_cast<DialogsApp*>(furi_record_open("dialogs"));
  46. infrared_worker = infrared_worker_alloc();
  47. }
  48. InfraredApp::~InfraredApp() {
  49. infrared_worker_free(infrared_worker);
  50. furi_record_close("notification");
  51. furi_record_close("dialogs");
  52. string_clear(file_path);
  53. for(auto& [key, scene] : scenes) delete scene;
  54. }
  55. InfraredAppViewManager* InfraredApp::get_view_manager() {
  56. return &view_manager;
  57. }
  58. void InfraredApp::set_learn_new_remote(bool value) {
  59. learn_new_remote = value;
  60. }
  61. bool InfraredApp::get_learn_new_remote() {
  62. return learn_new_remote;
  63. }
  64. void InfraredApp::switch_to_next_scene(Scene next_scene) {
  65. previous_scenes_list.push_front(current_scene);
  66. switch_to_next_scene_without_saving(next_scene);
  67. }
  68. void InfraredApp::switch_to_next_scene_without_saving(Scene next_scene) {
  69. if(next_scene != Scene::Exit) {
  70. scenes[current_scene]->on_exit(this);
  71. current_scene = next_scene;
  72. scenes[current_scene]->on_enter(this);
  73. view_manager.clear_events();
  74. }
  75. }
  76. void InfraredApp::search_and_switch_to_previous_scene(
  77. const std::initializer_list<Scene>& scenes_list) {
  78. Scene previous_scene = Scene::Start;
  79. bool scene_found = false;
  80. while(!scene_found) {
  81. previous_scene = get_previous_scene();
  82. if(previous_scene == Scene::Exit) break;
  83. for(Scene element : scenes_list) {
  84. if(previous_scene == element) {
  85. scene_found = true;
  86. break;
  87. }
  88. }
  89. }
  90. if(previous_scene == Scene::Exit) {
  91. InfraredAppEvent event;
  92. event.type = InfraredAppEvent::Type::Exit;
  93. view_manager.send_event(&event);
  94. } else {
  95. scenes[current_scene]->on_exit(this);
  96. current_scene = previous_scene;
  97. scenes[current_scene]->on_enter(this);
  98. view_manager.clear_events();
  99. }
  100. }
  101. bool InfraredApp::switch_to_previous_scene(uint8_t count) {
  102. Scene previous_scene = Scene::Start;
  103. for(uint8_t i = 0; i < count; i++) previous_scene = get_previous_scene();
  104. if(previous_scene == Scene::Exit) return true;
  105. scenes[current_scene]->on_exit(this);
  106. current_scene = previous_scene;
  107. scenes[current_scene]->on_enter(this);
  108. view_manager.clear_events();
  109. return false;
  110. }
  111. InfraredApp::Scene InfraredApp::get_previous_scene() {
  112. Scene scene = Scene::Exit;
  113. if(!previous_scenes_list.empty()) {
  114. scene = previous_scenes_list.front();
  115. previous_scenes_list.pop_front();
  116. }
  117. return scene;
  118. }
  119. InfraredAppRemoteManager* InfraredApp::get_remote_manager() {
  120. return &remote_manager;
  121. }
  122. void InfraredApp::set_text_store(uint8_t index, const char* text...) {
  123. furi_check(index < text_store_max);
  124. va_list args;
  125. va_start(args, text);
  126. vsnprintf(text_store[index], text_store_size, text, args);
  127. va_end(args);
  128. }
  129. char* InfraredApp::get_text_store(uint8_t index) {
  130. furi_check(index < text_store_max);
  131. return text_store[index];
  132. }
  133. uint8_t InfraredApp::get_text_store_size() {
  134. return text_store_size;
  135. }
  136. void InfraredApp::text_input_callback(void* context) {
  137. InfraredApp* app = static_cast<InfraredApp*>(context);
  138. InfraredAppEvent event;
  139. event.type = InfraredAppEvent::Type::TextEditDone;
  140. app->get_view_manager()->send_event(&event);
  141. }
  142. void InfraredApp::popup_callback(void* context) {
  143. InfraredApp* app = static_cast<InfraredApp*>(context);
  144. InfraredAppEvent event;
  145. event.type = InfraredAppEvent::Type::PopupTimer;
  146. app->get_view_manager()->send_event(&event);
  147. }
  148. void InfraredApp::set_edit_element(InfraredApp::EditElement value) {
  149. element = value;
  150. }
  151. InfraredApp::EditElement InfraredApp::get_edit_element(void) {
  152. return element;
  153. }
  154. void InfraredApp::set_edit_action(InfraredApp::EditAction value) {
  155. action = value;
  156. }
  157. InfraredApp::EditAction InfraredApp::get_edit_action(void) {
  158. return action;
  159. }
  160. void InfraredApp::set_current_button(int value) {
  161. current_button = value;
  162. }
  163. int InfraredApp::get_current_button() {
  164. return current_button;
  165. }
  166. void InfraredApp::notify_success() {
  167. notification_message(notification, &sequence_success);
  168. }
  169. void InfraredApp::notify_blink_read() {
  170. notification_message(notification, &sequence_blink_cyan_10);
  171. }
  172. void InfraredApp::notify_blink_send() {
  173. notification_message(notification, &sequence_blink_magenta_10);
  174. }
  175. DialogsApp* InfraredApp::get_dialogs() {
  176. return dialogs;
  177. }
  178. void InfraredApp::notify_green_on() {
  179. notification_message(notification, &sequence_set_only_green_255);
  180. }
  181. void InfraredApp::notify_green_off() {
  182. notification_message(notification, &sequence_reset_green);
  183. }
  184. InfraredWorker* InfraredApp::get_infrared_worker() {
  185. return infrared_worker;
  186. }
  187. const InfraredAppSignal& InfraredApp::get_received_signal() const {
  188. return received_signal;
  189. }
  190. void InfraredApp::set_received_signal(const InfraredAppSignal& signal) {
  191. received_signal = signal;
  192. }
  193. void InfraredApp::signal_sent_callback(void* context) {
  194. InfraredApp* app = static_cast<InfraredApp*>(context);
  195. app->notify_blink_send();
  196. }