RadioSwitchboard.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <RadioSwitchboard.h>
  2. RadioSwitchboard::RadioSwitchboard(
  3. std::shared_ptr<MiLightRadioFactory> radioFactory,
  4. GroupStateStore* stateStore,
  5. Settings& settings
  6. ) {
  7. for (size_t i = 0; i < MiLightRadioConfig::NUM_CONFIGS; i++) {
  8. std::shared_ptr<MiLightRadio> radio = radioFactory->create(MiLightRadioConfig::ALL_CONFIGS[i]);
  9. radio->begin();
  10. radios.push_back(radio);
  11. }
  12. for (size_t i = 0; i < MiLightRemoteConfig::NUM_REMOTES; i++) {
  13. MiLightRemoteConfig::ALL_REMOTES[i]->packetFormatter->initialize(stateStore, &settings);
  14. }
  15. }
  16. size_t RadioSwitchboard::getNumRadios() const {
  17. return radios.size();
  18. }
  19. std::shared_ptr<MiLightRadio> RadioSwitchboard::switchRadio(size_t radioIx) {
  20. if (radioIx >= getNumRadios()) {
  21. return NULL;
  22. }
  23. if (this->currentRadio != radios[radioIx]) {
  24. this->currentRadio = radios[radioIx];
  25. this->currentRadio->configure();
  26. }
  27. return this->currentRadio;
  28. }
  29. std::shared_ptr<MiLightRadio> RadioSwitchboard::switchRadio(const MiLightRemoteConfig* remote) {
  30. std::shared_ptr<MiLightRadio> radio = NULL;
  31. for (size_t i = 0; i < radios.size(); i++) {
  32. if (&this->radios[i]->config() == &remote->radioConfig) {
  33. radio = switchRadio(i);
  34. break;
  35. }
  36. }
  37. return radio;
  38. }
  39. void RadioSwitchboard::write(uint8_t* packet, size_t len) {
  40. if (this->currentRadio == nullptr) {
  41. return;
  42. }
  43. this->currentRadio->write(packet, len);
  44. }
  45. size_t RadioSwitchboard::read(uint8_t* packet) {
  46. if (currentRadio == nullptr) {
  47. return 0;
  48. }
  49. size_t length;
  50. currentRadio->read(packet, length);
  51. return length;
  52. }
  53. bool RadioSwitchboard::available() {
  54. if (currentRadio == nullptr) {
  55. return false;
  56. }
  57. return currentRadio->available();
  58. }