infrared_app.h 9.0 KB

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