irda-app-remote-manager.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "irda-app-remote-manager.hpp"
  2. #include "furi.h"
  3. #include <string>
  4. #include <utility>
  5. IrdaAppRemoteManager::IrdaAppRemoteManager() {
  6. // Read from api-hal-storage, and fill remotes
  7. }
  8. static const std::string default_remote_name = "remote";
  9. void IrdaAppRemoteManager::add_button(const char* button_name, const IrdaMessage* message) {
  10. remotes[current_remote_index].buttons.emplace_back(button_name, message);
  11. }
  12. void IrdaAppRemoteManager::add_remote_with_button(
  13. const char* button_name,
  14. const IrdaMessage* message) {
  15. bool found = true;
  16. int i = 0;
  17. // find first free common name for remote
  18. do {
  19. found = false;
  20. ++i;
  21. for(const auto& it : remotes) {
  22. if(it.name == (default_remote_name + std::to_string(i))) {
  23. found = true;
  24. break;
  25. }
  26. }
  27. } while(found);
  28. remotes.emplace_back(default_remote_name + std::to_string(i));
  29. current_remote_index = remotes.size() - 1;
  30. add_button(button_name, message);
  31. }
  32. IrdaAppRemote::IrdaAppRemote(std::string name)
  33. : name(name) {
  34. }
  35. std::vector<std::string> IrdaAppRemoteManager::get_button_list(void) const {
  36. std::vector<std::string> name_vector;
  37. auto remote = remotes[current_remote_index];
  38. name_vector.reserve(remote.buttons.size());
  39. for(const auto& it : remote.buttons) {
  40. name_vector.emplace_back(it.name);
  41. }
  42. // copy elision
  43. return name_vector;
  44. }
  45. std::vector<std::string> IrdaAppRemoteManager::get_remote_list() const {
  46. std::vector<std::string> name_vector;
  47. name_vector.reserve(remotes.size());
  48. for(const auto& it : remotes) {
  49. name_vector.push_back(it.name);
  50. }
  51. // copy elision
  52. return name_vector;
  53. }
  54. size_t IrdaAppRemoteManager::get_current_remote(void) const {
  55. return current_remote_index;
  56. }
  57. size_t IrdaAppRemoteManager::get_current_button(void) const {
  58. return current_button_index;
  59. }
  60. void IrdaAppRemote::add_button(
  61. size_t remote_index,
  62. const char* button_name,
  63. const IrdaMessage* message) {
  64. buttons.emplace_back(button_name, message);
  65. }
  66. const IrdaMessage* IrdaAppRemoteManager::get_button_data(size_t button_index) const {
  67. furi_check(remotes[current_remote_index].buttons.size() > button_index);
  68. auto& b = remotes[current_remote_index].buttons.at(button_index);
  69. return &b.message;
  70. }
  71. void IrdaAppRemoteManager::set_current_remote(size_t index) {
  72. furi_check(index < remotes.size());
  73. current_remote_index = index;
  74. }
  75. void IrdaAppRemoteManager::set_current_button(size_t index) {
  76. furi_check(current_remote_index < remotes.size());
  77. furi_check(index < remotes[current_remote_index].buttons.size());
  78. current_button_index = index;
  79. }
  80. void IrdaAppRemoteManager::delete_current_remote() {
  81. remotes.erase(remotes.begin() + current_remote_index);
  82. current_remote_index = 0;
  83. }
  84. void IrdaAppRemoteManager::delete_current_button() {
  85. auto& buttons = remotes[current_remote_index].buttons;
  86. buttons.erase(buttons.begin() + current_button_index);
  87. current_button_index = 0;
  88. }
  89. std::string IrdaAppRemoteManager::get_current_button_name() {
  90. auto buttons = remotes[current_remote_index].buttons;
  91. return buttons[current_button_index].name;
  92. }
  93. std::string IrdaAppRemoteManager::get_current_remote_name() {
  94. return remotes[current_remote_index].name;
  95. }
  96. void IrdaAppRemoteManager::rename_remote(const char* str) {
  97. remotes[current_remote_index].name = str;
  98. }
  99. void IrdaAppRemoteManager::rename_button(const char* str) {
  100. remotes[current_remote_index].buttons[current_button_index].name = str;
  101. }
  102. size_t IrdaAppRemoteManager::get_current_remote_buttons_number() {
  103. return remotes[current_remote_index].buttons.size();
  104. }