infrared_app.cpp 6.5 KB

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