infrared_app_remote_manager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "m-string.h"
  10. #include <infrared_worker.h>
  11. #include <infrared.h>
  12. #include <cstdint>
  13. #include <string>
  14. #include <memory>
  15. #include <vector>
  16. /** Class to handle remote button */
  17. class InfraredAppRemoteButton {
  18. /** Allow field access */
  19. friend class InfraredAppRemoteManager;
  20. /** Name of signal */
  21. std::string name;
  22. /** Signal data */
  23. InfraredAppSignal signal;
  24. public:
  25. /** Initialize remote button
  26. *
  27. * @param name - button name
  28. * @param signal - signal to copy for remote button
  29. */
  30. InfraredAppRemoteButton(const char* name, const InfraredAppSignal& signal)
  31. : name(name)
  32. , signal(signal) {
  33. }
  34. /** Initialize remote button
  35. *
  36. * @param name - button name
  37. * @param signal - signal to move for remote button
  38. */
  39. InfraredAppRemoteButton(const char* name, InfraredAppSignal&& signal)
  40. : name(name)
  41. , signal(std::move(signal)) {
  42. }
  43. /** Deinitialize remote button */
  44. ~InfraredAppRemoteButton() {
  45. }
  46. };
  47. /** Class to handle remote */
  48. class InfraredAppRemote {
  49. /** Allow field access */
  50. friend class InfraredAppRemoteManager;
  51. /** Button container */
  52. std::vector<InfraredAppRemoteButton> buttons;
  53. /** Name of remote */
  54. std::string name;
  55. /** Path to remote file */
  56. string_t path;
  57. public:
  58. /** Initialize new remote
  59. *
  60. * @param path - remote file path
  61. */
  62. InfraredAppRemote(string_t file_path) {
  63. string_init_set(path, file_path);
  64. }
  65. ~InfraredAppRemote() {
  66. string_clear(path);
  67. }
  68. };
  69. /** Class to handle remote manager */
  70. class InfraredAppRemoteManager {
  71. /** Remote instance. There can be 1 remote loaded at a time. */
  72. std::unique_ptr<InfraredAppRemote> remote;
  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. * @param path - remote file path
  108. */
  109. void find_vacant_remote_name(string_t name, string_t path);
  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 path - path to remote file
  158. * @retval true if success, false otherwise
  159. */
  160. bool load(string_t path);
  161. };