infrared_app_remote_manager.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. public:
  55. /** Initialize new remote
  56. *
  57. * @param name - new remote name
  58. */
  59. InfraredAppRemote(const std::string& name)
  60. : name(name) {
  61. }
  62. };
  63. /** Class to handle remote manager */
  64. class InfraredAppRemoteManager {
  65. /** Remote instance. There can be 1 remote loaded at a time. */
  66. std::unique_ptr<InfraredAppRemote> remote;
  67. /** Make full name from remote name
  68. *
  69. * @param remote_name name of remote
  70. * @retval full name of remote on disk
  71. */
  72. std::string make_full_name(const std::string& remote_name) const;
  73. public:
  74. /** Restriction to button name length. Buttons larger are ignored. */
  75. static constexpr const uint32_t max_button_name_length = 22;
  76. /** Restriction to remote name length. Remotes larger are ignored. */
  77. static constexpr const uint32_t max_remote_name_length = 22;
  78. /** Construct button from signal, and create remote
  79. *
  80. * @param button_name - name of button to create
  81. * @param signal - signal to create button from
  82. * @retval true for success, false otherwise
  83. * */
  84. bool add_remote_with_button(const char* button_name, const InfraredAppSignal& signal);
  85. /** Add button to current remote
  86. *
  87. * @param button_name - name of button to create
  88. * @param signal - signal to create button from
  89. * @retval true for success, false otherwise
  90. * */
  91. bool add_button(const char* button_name, const InfraredAppSignal& signal);
  92. /** Rename button in current remote
  93. *
  94. * @param index - index of button to rename
  95. * @param str - new button name
  96. */
  97. bool rename_button(uint32_t index, const char* str);
  98. /** Rename current remote
  99. *
  100. * @param str - new remote name
  101. */
  102. bool rename_remote(const char* str);
  103. /** Find vacant remote name. If suggested name is occupied,
  104. * incremented digit(2,3,4,etc) added to name and check repeated.
  105. *
  106. * @param name - suggested remote name
  107. * @retval garanteed free remote name, prefixed with suggested
  108. */
  109. std::string find_vacant_remote_name(const std::string& name);
  110. /** Get button list
  111. *
  112. * @retval container of button names
  113. */
  114. std::vector<std::string> get_button_list() const;
  115. /** Get button name by index
  116. *
  117. * @param index - index of button to get name from
  118. * @retval button name
  119. */
  120. std::string get_button_name(uint32_t index);
  121. /** Get remote name
  122. *
  123. * @retval remote name
  124. */
  125. std::string get_remote_name();
  126. /** Get number of buttons
  127. *
  128. * @retval number of buttons
  129. */
  130. size_t get_number_of_buttons();
  131. /** Get button's signal
  132. *
  133. * @param index - index of interested button
  134. * @retval signal
  135. */
  136. const InfraredAppSignal& get_button_data(size_t index) const;
  137. /** Delete button
  138. *
  139. * @param index - index of interested button
  140. * @retval true if success, false otherwise
  141. */
  142. bool delete_button(uint32_t index);
  143. /** Delete remote
  144. *
  145. * @retval true if success, false otherwise
  146. */
  147. bool delete_remote();
  148. /** Clean all loaded info in current remote */
  149. void reset_remote();
  150. /** Store current remote data on disk
  151. *
  152. * @retval true if success, false otherwise
  153. */
  154. bool store();
  155. /** Load data from disk into current remote
  156. *
  157. * @param name - name of remote to load
  158. * @retval true if success, false otherwise
  159. */
  160. bool load(const std::string& name);
  161. };