irda-app.cpp 6.9 KB

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