MiLightClient.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <MiLightClient.h>
  2. #include <MiLightRadioConfig.h>
  3. #include <Arduino.h>
  4. MiLightClient::MiLightClient(MiLightRadioFactory* radioFactory)
  5. : resendCount(MILIGHT_DEFAULT_RESEND_COUNT),
  6. currentRadio(NULL),
  7. numRadios(MiLightRadioConfig::NUM_CONFIGS)
  8. {
  9. radios = new MiLightRadio*[numRadios];
  10. for (size_t i = 0; i < numRadios; i++) {
  11. radios[i] = radioFactory->create(*MiLightRadioConfig::ALL_CONFIGS[i]);
  12. }
  13. this->currentRadio = radios[0];
  14. this->currentRadio->configure();
  15. }
  16. void MiLightClient::begin() {
  17. for (size_t i = 0; i < numRadios; i++) {
  18. radios[i]->begin();
  19. }
  20. }
  21. MiLightRadio* MiLightClient::switchRadio(const MiLightRadioType type) {
  22. MiLightRadio* radio = NULL;
  23. for (int i = 0; i < numRadios; i++) {
  24. if (this->radios[i]->config().type == type) {
  25. radio = radios[i];
  26. break;
  27. }
  28. }
  29. if (radio != NULL) {
  30. if (currentRadio != radio) {
  31. radio->configure();
  32. }
  33. this->currentRadio = radio;
  34. this->formatter = radio->config().packetFormatter;
  35. return radio;
  36. } else {
  37. Serial.print(F("MiLightClient - tried to get radio for unknown type: "));
  38. Serial.println(type);
  39. }
  40. return NULL;
  41. }
  42. void MiLightClient::prepare(MiLightRadioConfig& config,
  43. const uint16_t deviceId,
  44. const uint8_t groupId) {
  45. switchRadio(config.type);
  46. if (deviceId >= 0 && groupId >= 0) {
  47. formatter->prepare(deviceId, groupId);
  48. }
  49. }
  50. void MiLightClient::setResendCount(const unsigned int resendCount) {
  51. this->resendCount = resendCount;
  52. }
  53. bool MiLightClient::available() {
  54. if (currentRadio == NULL) {
  55. return false;
  56. }
  57. return currentRadio->available();
  58. }
  59. void MiLightClient::read(uint8_t packet[]) {
  60. if (currentRadio == NULL) {
  61. return;
  62. }
  63. size_t length = currentRadio->config().getPacketLength();
  64. currentRadio->read(packet, length);
  65. }
  66. void MiLightClient::write(uint8_t packet[]) {
  67. if (currentRadio == NULL) {
  68. return;
  69. }
  70. #ifdef DEBUG_PRINTF
  71. printf("Sending packet: ");
  72. for (int i = 0; i < currentRadio->config().getPacketLength(); i++) {
  73. printf("%02X", packet[i]);
  74. }
  75. printf("\n");
  76. int iStart = millis();
  77. #endif
  78. for (int i = 0; i < this->resendCount; i++) {
  79. currentRadio->write(packet, currentRadio->config().getPacketLength());
  80. }
  81. #ifdef DEBUG_PRINTF
  82. int iElapsed = millis() - iStart;
  83. Serial.print("Elapsed: ");
  84. Serial.println(iElapsed);
  85. #endif
  86. }
  87. void MiLightClient::updateColorRaw(const uint8_t color) {
  88. formatter->updateColorRaw(color);
  89. flushPacket();
  90. }
  91. void MiLightClient::updateHue(const uint16_t hue) {
  92. formatter->updateHue(hue);
  93. flushPacket();
  94. }
  95. void MiLightClient::updateBrightness(const uint8_t brightness) {
  96. formatter->updateBrightness(brightness);
  97. flushPacket();
  98. }
  99. void MiLightClient::updateMode(uint8_t mode) {
  100. formatter->updateMode(mode);
  101. flushPacket();
  102. }
  103. void MiLightClient::nextMode() {
  104. formatter->nextMode();
  105. flushPacket();
  106. }
  107. void MiLightClient::previousMode() {
  108. formatter->previousMode();
  109. flushPacket();
  110. }
  111. void MiLightClient::modeSpeedDown() {
  112. formatter->modeSpeedDown();
  113. flushPacket();
  114. }
  115. void MiLightClient::modeSpeedUp() {
  116. formatter->modeSpeedUp();
  117. flushPacket();
  118. }
  119. void MiLightClient::updateStatus(MiLightStatus status, uint8_t groupId) {
  120. formatter->updateStatus(status, groupId);
  121. flushPacket();
  122. }
  123. void MiLightClient::updateStatus(MiLightStatus status) {
  124. formatter->updateStatus(status);
  125. flushPacket();
  126. }
  127. void MiLightClient::updateSaturation(const uint8_t value) {
  128. formatter->updateSaturation(value);
  129. flushPacket();
  130. }
  131. void MiLightClient::updateColorWhite() {
  132. formatter->updateColorWhite();
  133. flushPacket();
  134. }
  135. void MiLightClient::pair() {
  136. formatter->pair();
  137. flushPacket();
  138. }
  139. void MiLightClient::unpair() {
  140. formatter->unpair();
  141. flushPacket();
  142. }
  143. void MiLightClient::increaseBrightness() {
  144. formatter->increaseBrightness();
  145. flushPacket();
  146. }
  147. void MiLightClient::decreaseBrightness() {
  148. formatter->decreaseBrightness();
  149. flushPacket();
  150. }
  151. void MiLightClient::increaseTemperature() {
  152. formatter->increaseTemperature();
  153. flushPacket();
  154. }
  155. void MiLightClient::decreaseTemperature() {
  156. formatter->decreaseTemperature();
  157. flushPacket();
  158. }
  159. void MiLightClient::updateTemperature(const uint8_t temperature) {
  160. formatter->updateTemperature(temperature);
  161. flushPacket();
  162. }
  163. void MiLightClient::command(uint8_t command, uint8_t arg) {
  164. formatter->command(command, arg);
  165. flushPacket();
  166. }
  167. void MiLightClient::formatPacket(uint8_t* packet, char* buffer) {
  168. formatter->format(packet, buffer);
  169. }
  170. void MiLightClient::flushPacket() {
  171. PacketStream& stream = formatter->buildPackets();
  172. const size_t prevNumRepeats = this->resendCount;
  173. // When sending multiple packets, normalize the number of repeats
  174. if (stream.numPackets > 1) {
  175. setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
  176. }
  177. while (stream.hasNext()) {
  178. write(stream.next());
  179. if (stream.hasNext()) {
  180. delay(10);
  181. }
  182. }
  183. setResendCount(prevNumRepeats);
  184. formatter->reset();
  185. }