infrared_app.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. remote_name.erase(remote_name.find_last_of('.'));
  16. path.erase(path.find_last_of('/'));
  17. bool result = remote_manager.load(path, remote_name);
  18. if(result) {
  19. current_scene = InfraredApp::Scene::Remote;
  20. } else {
  21. printf("Failed to load remote \'%s\'\r\n", remote_name.c_str());
  22. return -1;
  23. }
  24. }
  25. scenes[current_scene]->on_enter(this);
  26. while(!exit) {
  27. view_manager.receive_event(&event);
  28. if(event.type == InfraredAppEvent::Type::Exit) break;
  29. consumed = scenes[current_scene]->on_event(this, &event);
  30. if(!consumed) {
  31. if(event.type == InfraredAppEvent::Type::Back) {
  32. exit = switch_to_previous_scene();
  33. }
  34. }
  35. };
  36. scenes[current_scene]->on_exit(this);
  37. return 0;
  38. };
  39. InfraredApp::InfraredApp() {
  40. furi_check(InfraredAppRemoteManager::max_button_name_length < get_text_store_size());
  41. notification = static_cast<NotificationApp*>(furi_record_open("notification"));
  42. dialogs = static_cast<DialogsApp*>(furi_record_open("dialogs"));
  43. infrared_worker = infrared_worker_alloc();
  44. }
  45. InfraredApp::~InfraredApp() {
  46. infrared_worker_free(infrared_worker);
  47. furi_record_close("notification");
  48. furi_record_close("dialogs");
  49. for(auto& [key, scene] : scenes) delete scene;
  50. }
  51. InfraredAppViewManager* InfraredApp::get_view_manager() {
  52. return &view_manager;
  53. }
  54. void InfraredApp::set_learn_new_remote(bool value) {
  55. learn_new_remote = value;
  56. }
  57. bool InfraredApp::get_learn_new_remote() {
  58. return learn_new_remote;
  59. }
  60. void InfraredApp::switch_to_next_scene(Scene next_scene) {
  61. previous_scenes_list.push_front(current_scene);
  62. switch_to_next_scene_without_saving(next_scene);
  63. }
  64. void InfraredApp::switch_to_next_scene_without_saving(Scene next_scene) {
  65. if(next_scene != Scene::Exit) {
  66. scenes[current_scene]->on_exit(this);
  67. current_scene = next_scene;
  68. scenes[current_scene]->on_enter(this);
  69. view_manager.clear_events();
  70. }
  71. }
  72. void InfraredApp::search_and_switch_to_previous_scene(
  73. const std::initializer_list<Scene>& scenes_list) {
  74. Scene previous_scene = Scene::Start;
  75. bool scene_found = false;
  76. while(!scene_found) {
  77. previous_scene = get_previous_scene();
  78. if(previous_scene == Scene::Exit) break;
  79. for(Scene element : scenes_list) {
  80. if(previous_scene == element) {
  81. scene_found = true;
  82. break;
  83. }
  84. }
  85. }
  86. if(previous_scene == Scene::Exit) {
  87. InfraredAppEvent event;
  88. event.type = InfraredAppEvent::Type::Exit;
  89. view_manager.send_event(&event);
  90. } else {
  91. scenes[current_scene]->on_exit(this);
  92. current_scene = previous_scene;
  93. scenes[current_scene]->on_enter(this);
  94. view_manager.clear_events();
  95. }
  96. }
  97. bool InfraredApp::switch_to_previous_scene(uint8_t count) {
  98. Scene previous_scene = Scene::Start;
  99. for(uint8_t i = 0; i < count; i++) previous_scene = get_previous_scene();
  100. if(previous_scene == Scene::Exit) return true;
  101. scenes[current_scene]->on_exit(this);
  102. current_scene = previous_scene;
  103. scenes[current_scene]->on_enter(this);
  104. view_manager.clear_events();
  105. return false;
  106. }
  107. InfraredApp::Scene InfraredApp::get_previous_scene() {
  108. Scene scene = Scene::Exit;
  109. if(!previous_scenes_list.empty()) {
  110. scene = previous_scenes_list.front();
  111. previous_scenes_list.pop_front();
  112. }
  113. return scene;
  114. }
  115. InfraredAppRemoteManager* InfraredApp::get_remote_manager() {
  116. return &remote_manager;
  117. }
  118. void InfraredApp::set_text_store(uint8_t index, const char* text...) {
  119. furi_check(index < text_store_max);
  120. va_list args;
  121. va_start(args, text);
  122. vsnprintf(text_store[index], text_store_size, text, args);
  123. va_end(args);
  124. }
  125. char* InfraredApp::get_text_store(uint8_t index) {
  126. furi_check(index < text_store_max);
  127. return text_store[index];
  128. }
  129. uint8_t InfraredApp::get_text_store_size() {
  130. return text_store_size;
  131. }
  132. void InfraredApp::text_input_callback(void* context) {
  133. InfraredApp* app = static_cast<InfraredApp*>(context);
  134. InfraredAppEvent event;
  135. event.type = InfraredAppEvent::Type::TextEditDone;
  136. app->get_view_manager()->send_event(&event);
  137. }
  138. void InfraredApp::popup_callback(void* context) {
  139. InfraredApp* app = static_cast<InfraredApp*>(context);
  140. InfraredAppEvent event;
  141. event.type = InfraredAppEvent::Type::PopupTimer;
  142. app->get_view_manager()->send_event(&event);
  143. }
  144. void InfraredApp::set_edit_element(InfraredApp::EditElement value) {
  145. element = value;
  146. }
  147. InfraredApp::EditElement InfraredApp::get_edit_element(void) {
  148. return element;
  149. }
  150. void InfraredApp::set_edit_action(InfraredApp::EditAction value) {
  151. action = value;
  152. }
  153. InfraredApp::EditAction InfraredApp::get_edit_action(void) {
  154. return action;
  155. }
  156. void InfraredApp::set_current_button(int value) {
  157. current_button = value;
  158. }
  159. int InfraredApp::get_current_button() {
  160. return current_button;
  161. }
  162. void InfraredApp::notify_success() {
  163. notification_message(notification, &sequence_success);
  164. }
  165. void InfraredApp::notify_red_blink() {
  166. notification_message(notification, &sequence_blink_red_10);
  167. }
  168. void InfraredApp::notify_click() {
  169. static const NotificationSequence sequence = {
  170. &message_click,
  171. &message_delay_1,
  172. &message_sound_off,
  173. NULL,
  174. };
  175. notification_message_block(notification, &sequence);
  176. }
  177. void InfraredApp::notify_click_and_green_blink() {
  178. static const NotificationSequence sequence = {
  179. &message_click,
  180. &message_delay_1,
  181. &message_sound_off,
  182. &message_green_255,
  183. &message_delay_10,
  184. &message_green_0,
  185. &message_do_not_reset,
  186. NULL,
  187. };
  188. notification_message_block(notification, &sequence);
  189. }
  190. void InfraredApp::notify_blink_green() {
  191. static const NotificationSequence sequence = {
  192. &message_green_255,
  193. &message_delay_10,
  194. &message_green_0,
  195. &message_do_not_reset,
  196. NULL,
  197. };
  198. notification_message(notification, &sequence);
  199. }
  200. DialogsApp* InfraredApp::get_dialogs() {
  201. return dialogs;
  202. }
  203. void InfraredApp::notify_green_on() {
  204. notification_message(notification, &sequence_set_only_green_255);
  205. }
  206. void InfraredApp::notify_green_off() {
  207. notification_message(notification, &sequence_reset_green);
  208. }
  209. InfraredWorker* InfraredApp::get_infrared_worker() {
  210. return infrared_worker;
  211. }
  212. const InfraredAppSignal& InfraredApp::get_received_signal() const {
  213. return received_signal;
  214. }
  215. void InfraredApp::set_received_signal(const InfraredAppSignal& signal) {
  216. received_signal = signal;
  217. }
  218. void InfraredApp::signal_sent_callback(void* context) {
  219. InfraredApp* app = static_cast<InfraredApp*>(context);
  220. app->notify_blink_green();
  221. }