infrared_app.cpp 7.0 KB

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