irda-app.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. IrdaAppViewManager* IrdaApp::get_view_manager() {
  42. return &view_manager;
  43. }
  44. void IrdaApp::set_learn_new_remote(bool value) {
  45. learn_new_remote = value;
  46. }
  47. bool IrdaApp::get_learn_new_remote() {
  48. return learn_new_remote;
  49. }
  50. void IrdaApp::switch_to_next_scene(Scene next_scene) {
  51. previous_scenes_list.push_front(current_scene);
  52. switch_to_next_scene_without_saving(next_scene);
  53. }
  54. void IrdaApp::switch_to_next_scene_without_saving(Scene next_scene) {
  55. if(next_scene != Scene::Exit) {
  56. scenes[current_scene]->on_exit(this);
  57. current_scene = next_scene;
  58. scenes[current_scene]->on_enter(this);
  59. view_manager.clear_events();
  60. }
  61. }
  62. void IrdaApp::search_and_switch_to_previous_scene(const std::initializer_list<Scene>& scenes_list) {
  63. Scene previous_scene = Scene::Start;
  64. bool scene_found = false;
  65. while(!scene_found) {
  66. previous_scene = get_previous_scene();
  67. if(previous_scene == Scene::Exit) break;
  68. for(Scene element : scenes_list) {
  69. if(previous_scene == element) {
  70. scene_found = true;
  71. break;
  72. }
  73. }
  74. }
  75. if(previous_scene == Scene::Exit) {
  76. IrdaAppEvent event;
  77. event.type = IrdaAppEvent::Type::Exit;
  78. view_manager.send_event(&event);
  79. } else {
  80. scenes[current_scene]->on_exit(this);
  81. current_scene = previous_scene;
  82. scenes[current_scene]->on_enter(this);
  83. view_manager.clear_events();
  84. }
  85. }
  86. bool IrdaApp::switch_to_previous_scene(uint8_t count) {
  87. Scene previous_scene = Scene::Start;
  88. for(uint8_t i = 0; i < count; i++) previous_scene = get_previous_scene();
  89. if(previous_scene == Scene::Exit) return true;
  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. return false;
  95. }
  96. IrdaApp::Scene IrdaApp::get_previous_scene() {
  97. Scene scene = Scene::Exit;
  98. if(!previous_scenes_list.empty()) {
  99. scene = previous_scenes_list.front();
  100. previous_scenes_list.pop_front();
  101. }
  102. return scene;
  103. }
  104. IrdaAppRemoteManager* IrdaApp::get_remote_manager() {
  105. return &remote_manager;
  106. }
  107. void IrdaApp::set_text_store(uint8_t index, const char* text...) {
  108. furi_check(index < text_store_max);
  109. va_list args;
  110. va_start(args, text);
  111. vsnprintf(text_store[index], text_store_size, text, args);
  112. va_end(args);
  113. }
  114. char* IrdaApp::get_text_store(uint8_t index) {
  115. furi_check(index < text_store_max);
  116. return text_store[index];
  117. }
  118. uint8_t IrdaApp::get_text_store_size() {
  119. return text_store_size;
  120. }
  121. void IrdaApp::text_input_callback(void* context) {
  122. IrdaApp* app = static_cast<IrdaApp*>(context);
  123. IrdaAppEvent event;
  124. event.type = IrdaAppEvent::Type::TextEditDone;
  125. app->get_view_manager()->send_event(&event);
  126. }
  127. void IrdaApp::popup_callback(void* context) {
  128. IrdaApp* app = static_cast<IrdaApp*>(context);
  129. IrdaAppEvent event;
  130. event.type = IrdaAppEvent::Type::PopupTimer;
  131. app->get_view_manager()->send_event(&event);
  132. }
  133. void IrdaApp::set_edit_element(IrdaApp::EditElement value) {
  134. element = value;
  135. }
  136. IrdaApp::EditElement IrdaApp::get_edit_element(void) {
  137. return element;
  138. }
  139. void IrdaApp::set_edit_action(IrdaApp::EditAction value) {
  140. action = value;
  141. }
  142. IrdaApp::EditAction IrdaApp::get_edit_action(void) {
  143. return action;
  144. }
  145. void IrdaApp::set_current_button(int value) {
  146. current_button = value;
  147. }
  148. int IrdaApp::get_current_button() {
  149. return current_button;
  150. }
  151. void IrdaApp::notify_success() {
  152. notification_message(notification, &sequence_success);
  153. }
  154. void IrdaApp::notify_red_blink() {
  155. notification_message(notification, &sequence_blink_red_10);
  156. }
  157. void IrdaApp::notify_space_blink() {
  158. static const NotificationSequence sequence = {
  159. &message_green_0,
  160. &message_delay_50,
  161. &message_green_255,
  162. &message_do_not_reset,
  163. NULL,
  164. };
  165. notification_message_block(notification, &sequence);
  166. }
  167. void IrdaApp::notify_click() {
  168. static const NotificationSequence sequence = {
  169. &message_click,
  170. &message_delay_1,
  171. &message_sound_off,
  172. NULL,
  173. };
  174. notification_message_block(notification, &sequence);
  175. }
  176. void IrdaApp::notify_click_and_green_blink() {
  177. static const NotificationSequence sequence = {
  178. &message_click,
  179. &message_delay_1,
  180. &message_sound_off,
  181. &message_green_255,
  182. &message_delay_10,
  183. &message_green_0,
  184. &message_do_not_reset,
  185. NULL,
  186. };
  187. notification_message_block(notification, &sequence);
  188. }
  189. void IrdaApp::notify_blink_green() {
  190. static const NotificationSequence sequence = {
  191. &message_green_255,
  192. &message_delay_10,
  193. &message_green_0,
  194. &message_do_not_reset,
  195. NULL,
  196. };
  197. notification_message(notification, &sequence);
  198. }
  199. void IrdaApp::notify_double_vibro() {
  200. notification_message(notification, &sequence_double_vibro);
  201. }
  202. void IrdaApp::notify_green_on() {
  203. notification_message(notification, &sequence_set_only_green_255);
  204. }
  205. void IrdaApp::notify_green_off() {
  206. notification_message(notification, &sequence_reset_green);
  207. }
  208. IrdaWorker* IrdaApp::get_irda_worker() {
  209. return irda_worker;
  210. }
  211. const IrdaAppSignal& IrdaApp::get_received_signal() const {
  212. return received_signal;
  213. }
  214. void IrdaApp::set_received_signal(const IrdaAppSignal& signal) {
  215. received_signal = signal;
  216. }