infrared_app_remote_manager.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @file infrared_app_remote_manager.h
  3. * Infrared: Remote manager class.
  4. * It holds remote, can load/save/rename remote,
  5. * add/remove/rename buttons.
  6. */
  7. #pragma once
  8. #include "infrared_app_signal.h"
  9. #include <infrared_worker.h>
  10. #include <infrared.h>
  11. #include <cstdint>
  12. #include <string>
  13. #include <memory>
  14. #include <vector>
  15. /** Class to handle remote button */
  16. class InfraredAppRemoteButton {
  17. /** Allow field access */
  18. friend class InfraredAppRemoteManager;
  19. /** Name of signal */
  20. std::string name;
  21. /** Signal data */
  22. InfraredAppSignal signal;
  23. public:
  24. /** Initialize remote button
  25. *
  26. * @param name - button name
  27. * @param signal - signal to copy for remote button
  28. */
  29. InfraredAppRemoteButton(const char* name, const InfraredAppSignal& signal)
  30. : name(name)
  31. , signal(signal) {
  32. }
  33. /** Initialize remote button
  34. *
  35. * @param name - button name
  36. * @param signal - signal to move for remote button
  37. */
  38. InfraredAppRemoteButton(const char* name, InfraredAppSignal&& signal)
  39. : name(name)
  40. , signal(std::move(signal)) {
  41. }
  42. /** Deinitialize remote button */
  43. ~InfraredAppRemoteButton() {
  44. }
  45. };
  46. /** Class to handle remote */
  47. class InfraredAppRemote {
  48. /** Allow field access */
  49. friend class InfraredAppRemoteManager;
  50. /** Button container */
  51. std::vector<InfraredAppRemoteButton> buttons;
  52. /** Name of remote */
  53. std::string name;
  54. /** Path to remote file */
  55. std::string path;
  56. public:
  57. /** Initialize new remote
  58. *
  59. * @param path - remote file path
  60. * @param name - new remote name
  61. */
  62. InfraredAppRemote(const std::string& path, const std::string& name)
  63. : name(name)
  64. , path(path) {
  65. }
  66. };
  67. /** Class to handle remote manager */
  68. class InfraredAppRemoteManager {
  69. /** Remote instance. There can be 1 remote loaded at a time. */
  70. std::unique_ptr<InfraredAppRemote> remote;
  71. /** Make full name from remote name
  72. *
  73. * @param remote_name name of remote
  74. * @retval full name of remote on disk
  75. */
  76. std::string make_full_name(const std::string& path, const std::string& remote_name) const;
  77. public:
  78. /** Restriction to button name length. Buttons larger are ignored. */
  79. static constexpr const uint32_t max_button_name_length = 22;
  80. /** Restriction to remote name length. Remotes larger are ignored. */
  81. static constexpr const uint32_t max_remote_name_length = 22;
  82. /** Construct button from signal, and create remote
  83. *
  84. * @param button_name - name of button to create
  85. * @param signal - signal to create button from
  86. * @retval true for success, false otherwise
  87. * */
  88. bool add_remote_with_button(const char* button_name, const InfraredAppSignal& signal);
  89. /** Add button to current remote
  90. *
  91. * @param button_name - name of button to create
  92. * @param signal - signal to create button from
  93. * @retval true for success, false otherwise
  94. * */
  95. bool add_button(const char* button_name, const InfraredAppSignal& signal);
  96. /** Rename button in current remote
  97. *
  98. * @param index - index of button to rename
  99. * @param str - new button name
  100. */
  101. bool rename_button(uint32_t index, const char* str);
  102. /** Rename current remote
  103. *
  104. * @param str - new remote name
  105. */
  106. bool rename_remote(const char* str);
  107. /** Find vacant remote name. If suggested name is occupied,
  108. * incremented digit(2,3,4,etc) added to name and check repeated.
  109. *
  110. * @param name - suggested remote name
  111. * @retval garanteed free remote name, prefixed with suggested
  112. */
  113. std::string find_vacant_remote_name(const std::string& name);
  114. /** Get button list
  115. *
  116. * @retval container of button names
  117. */
  118. std::vector<std::string> get_button_list() const;
  119. /** Get button name by index
  120. *
  121. * @param index - index of button to get name from
  122. * @retval button name
  123. */
  124. std::string get_button_name(uint32_t index);
  125. /** Get remote name
  126. *
  127. * @retval remote name
  128. */
  129. std::string get_remote_name();
  130. /** Get number of buttons
  131. *
  132. * @retval number of buttons
  133. */
  134. size_t get_number_of_buttons();
  135. /** Get button's signal
  136. *
  137. * @param index - index of interested button
  138. * @retval signal
  139. */
  140. const InfraredAppSignal& get_button_data(size_t index) const;
  141. /** Delete button
  142. *
  143. * @param index - index of interested button
  144. * @retval true if success, false otherwise
  145. */
  146. bool delete_button(uint32_t index);
  147. /** Delete remote
  148. *
  149. * @retval true if success, false otherwise
  150. */
  151. bool delete_remote();
  152. /** Clean all loaded info in current remote */
  153. void reset_remote();
  154. /** Store current remote data on disk
  155. *
  156. * @retval true if success, false otherwise
  157. */
  158. bool store();
  159. /** Load data from disk into current remote
  160. *
  161. * @param name - name of remote to load
  162. * @retval true if success, false otherwise
  163. */
  164. bool load(const std::string& path, const std::string& name);
  165. };