MiLightClient.cpp 4.4 KB

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