infrared_app.cpp 7.0 KB

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