irda-app.cpp 6.1 KB

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