infrared_app.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * @file infrared_app.h
  3. * Infrared: Main infrared application class
  4. */
  5. #pragma once
  6. #include <map>
  7. #include <infrared.h>
  8. #include <furi.h>
  9. #include <forward_list>
  10. #include <stdint.h>
  11. #include <notification/notification_messages.h>
  12. #include <dialogs/dialogs.h>
  13. #include <infrared_worker.h>
  14. #include "scene/infrared_app_scene.h"
  15. #include "scene/infrared_app_scene.h"
  16. #include "infrared_app_view_manager.h"
  17. #include "infrared_app_remote_manager.h"
  18. #include "infrared_app_view_manager.h"
  19. /** Main Infrared application class */
  20. class InfraredApp {
  21. public:
  22. /** Enum to save scene state: edit element */
  23. enum class EditElement : uint8_t {
  24. Button,
  25. Remote,
  26. };
  27. /** Enum to save scene state: edit action */
  28. enum class EditAction : uint8_t {
  29. Rename,
  30. Delete,
  31. };
  32. /** List of scenes for Infrared application */
  33. enum class Scene : uint8_t {
  34. Exit,
  35. Start,
  36. Universal,
  37. UniversalTV,
  38. UniversalAudio,
  39. UniversalAirConditioner,
  40. Learn,
  41. LearnSuccess,
  42. LearnEnterName,
  43. LearnDone,
  44. AskBack,
  45. Remote,
  46. RemoteList,
  47. Edit,
  48. EditKeySelect,
  49. EditRename,
  50. EditDelete,
  51. EditRenameDone,
  52. EditDeleteDone,
  53. };
  54. /** Start application
  55. *
  56. * @param args - application arguments.
  57. * Allowed argument is path to remote file.
  58. * @retval 0 on success, error code otherwise
  59. */
  60. int32_t run(void* args);
  61. /** Switch to next scene. Put current scene number on stack.
  62. * Doesn't save scene state.
  63. *
  64. * @param index - next scene index
  65. */
  66. void switch_to_next_scene(Scene index);
  67. /** Switch to next scene, but don't put current scene on
  68. * stack. Thus calling switch_to_previous_scene() doesn't return
  69. * to current scene.
  70. *
  71. * @param index - next scene index
  72. */
  73. void switch_to_next_scene_without_saving(Scene index);
  74. /** Switch to previous scene. Pop scenes from stack and switch to last one.
  75. *
  76. * @param count - how many scenes should be popped
  77. * @retval false on failed, true on success
  78. */
  79. bool switch_to_previous_scene(uint8_t count = 1);
  80. /** Get previous scene in scene stack
  81. *
  82. * @retval previous scene
  83. */
  84. Scene get_previous_scene();
  85. /** Get view manager instance
  86. *
  87. * @retval view manager instance
  88. */
  89. InfraredAppViewManager* get_view_manager();
  90. /** Set one of text stores
  91. *
  92. * @param index - index of text store
  93. * @param text - text to set
  94. */
  95. void set_text_store(uint8_t index, const char* text...);
  96. /** Get value in text store
  97. *
  98. * @param index - index of text store
  99. * @retval value in text_store
  100. */
  101. char* get_text_store(uint8_t index);
  102. /** Get text store size
  103. *
  104. * @retval size of text store
  105. */
  106. uint8_t get_text_store_size();
  107. /** Get remote manager instance
  108. *
  109. * @retval remote manager instance
  110. */
  111. InfraredAppRemoteManager* get_remote_manager();
  112. /** Get infrared worker instance
  113. *
  114. * @retval infrared worker instance
  115. */
  116. InfraredWorker* get_infrared_worker();
  117. /** Get signal, previously got on Learn scene
  118. *
  119. * @retval received signal
  120. */
  121. const InfraredAppSignal& get_received_signal() const;
  122. /** Set received signal
  123. *
  124. * @param signal - signal
  125. */
  126. void set_received_signal(const InfraredAppSignal& signal);
  127. /** Switch to previous scene in one of provided in list.
  128. * Pop scene stack, and find first scene from list.
  129. *
  130. * @param scenes_list - list of scenes
  131. */
  132. void search_and_switch_to_previous_scene(const std::initializer_list<Scene>& scenes_list);
  133. /** Set edit element value. It is used on edit scene to determine
  134. * what should be deleted - remote or button.
  135. *
  136. * @param value - value to set
  137. */
  138. void set_edit_element(EditElement value);
  139. /** Get edit element
  140. *
  141. * @retval edit element value
  142. */
  143. EditElement get_edit_element(void);
  144. /** Set edit action value. It is used on edit scene to determine
  145. * what action to perform - deletion or renaming.
  146. *
  147. * @param value - value to set
  148. */
  149. void set_edit_action(EditAction value);
  150. /** Get edit action
  151. *
  152. * @retval edit action value
  153. */
  154. EditAction get_edit_action(void);
  155. /** Get state of learning new signal.
  156. * Adding new remote with 1 button from start scene and
  157. * learning 1 additional button to remote have very similar
  158. * flow, so they are joined. Difference in flow is handled
  159. * by this boolean flag.
  160. *
  161. * @retval false if flow is in learning new remote, true if
  162. * adding signal to created remote
  163. *
  164. */
  165. bool get_learn_new_remote();
  166. /** Set state of learning new signal.
  167. * Adding new remote with 1 button from start scene and
  168. * learning 1 additional button to remote have very similar
  169. * flow, so they are joined. Difference in flow is handled
  170. * by this boolean flag.
  171. *
  172. * @param value - false if flow is in learning new remote, true if
  173. * adding signal to created remote
  174. */
  175. void set_learn_new_remote(bool value);
  176. /** Button is not assigned value
  177. */
  178. enum : int {
  179. ButtonNA = -1,
  180. };
  181. /** Get current button index
  182. *
  183. * @retval current button index
  184. */
  185. int get_current_button();
  186. /** Set current button index
  187. *
  188. * @param current button index
  189. */
  190. void set_current_button(int value);
  191. /** Play success notification */
  192. void notify_success();
  193. /** Play red blink notification */
  194. void notify_blink_read();
  195. /** Light green */
  196. void notify_green_on();
  197. /** Disable green light */
  198. void notify_green_off();
  199. /** Blink on send */
  200. void notify_blink_send();
  201. /** Get Dialogs instance */
  202. DialogsApp* get_dialogs();
  203. /** Text input callback
  204. *
  205. * @param context - context to pass to callback
  206. */
  207. static void text_input_callback(void* context);
  208. /** Popup callback
  209. *
  210. * @param context - context to pass to callback
  211. */
  212. static void popup_callback(void* context);
  213. /** Signal sent callback
  214. *
  215. * @param context - context to pass to callback
  216. */
  217. static void signal_sent_callback(void* context);
  218. /** Main class constructor, initializes all critical objects */
  219. InfraredApp();
  220. /** Main class destructor, deinitializes all critical objects */
  221. ~InfraredApp();
  222. string_t file_path;
  223. /** Path to Infrared directory */
  224. static constexpr const char* infrared_directory = "/any/infrared";
  225. /** Infrared files extension (remote files and universal databases) */
  226. static constexpr const char* infrared_extension = ".ir";
  227. /** Max Raw timings in signal */
  228. static constexpr const uint32_t max_raw_timings_in_signal = 512;
  229. /** Max line length in Infrared file */
  230. static constexpr const uint32_t max_line_length =
  231. (9 + 1) * InfraredApp::max_raw_timings_in_signal + 100;
  232. private:
  233. /** Text store size */
  234. static constexpr const uint8_t text_store_size = 128;
  235. /** Amount of text stores */
  236. static constexpr const uint8_t text_store_max = 2;
  237. /** Store text here, for some views, because they doesn't
  238. * hold ownership of text */
  239. char text_store[text_store_max][text_store_size + 1];
  240. /**
  241. * Flag to control adding new signal flow.
  242. * Adding new remote with 1 button from start scene and
  243. * learning 1 additional button to remote have very similar
  244. * flow, so they are joined. Difference in flow is handled
  245. * by this boolean flag.
  246. */
  247. bool learn_new_remote;
  248. /** Value to control edit scene */
  249. EditElement element;
  250. /** Value to control edit scene */
  251. EditAction action;
  252. /** Selected button index */
  253. uint32_t current_button;
  254. /** Notification instance */
  255. NotificationApp* notification;
  256. /** Dialogs instance */
  257. DialogsApp* dialogs;
  258. /** View manager instance */
  259. InfraredAppViewManager view_manager;
  260. /** Remote manager instance */
  261. InfraredAppRemoteManager remote_manager;
  262. /** Infrared worker instance */
  263. InfraredWorker* infrared_worker;
  264. /** Signal received on Learn scene */
  265. InfraredAppSignal received_signal;
  266. /** Stack of previous scenes */
  267. std::forward_list<Scene> previous_scenes_list;
  268. /** Now acting scene */
  269. Scene current_scene = Scene::Start;
  270. /** Map of index/scene objects */
  271. std::map<Scene, InfraredAppScene*> scenes = {
  272. {Scene::Start, new InfraredAppSceneStart()},
  273. {Scene::Universal, new InfraredAppSceneUniversal()},
  274. {Scene::UniversalTV, new InfraredAppSceneUniversalTV()},
  275. {Scene::Learn, new InfraredAppSceneLearn()},
  276. {Scene::LearnSuccess, new InfraredAppSceneLearnSuccess()},
  277. {Scene::LearnEnterName, new InfraredAppSceneLearnEnterName()},
  278. {Scene::LearnDone, new InfraredAppSceneLearnDone()},
  279. {Scene::AskBack, new InfraredAppSceneAskBack()},
  280. {Scene::Remote, new InfraredAppSceneRemote()},
  281. {Scene::RemoteList, new InfraredAppSceneRemoteList()},
  282. {Scene::Edit, new InfraredAppSceneEdit()},
  283. {Scene::EditKeySelect, new InfraredAppSceneEditKeySelect()},
  284. {Scene::EditRename, new InfraredAppSceneEditRename()},
  285. {Scene::EditDelete, new InfraredAppSceneEditDelete()},
  286. {Scene::EditRenameDone, new InfraredAppSceneEditRenameDone()},
  287. {Scene::EditDeleteDone, new InfraredAppSceneEditDeleteDone()},
  288. };
  289. };