MiLightClient.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. for (int i = 0; i < this->resendCount; i++) {
  55. currentRadio->getRadio()->write(packet, currentRadio->config.getPacketLength());
  56. }
  57. }
  58. void MiLightClient::updateColorRaw(const uint8_t color) {
  59. formatter->updateColorRaw(color);
  60. flushPacket();
  61. }
  62. void MiLightClient::updateHue(const uint16_t hue) {
  63. formatter->updateHue(hue);
  64. flushPacket();
  65. }
  66. void MiLightClient::updateBrightness(const uint8_t brightness) {
  67. const MiLightRadioType type = currentRadio->config.type;
  68. if (type == CCT || type == RGB) {
  69. const unsigned int oldResend = resendCount;
  70. setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
  71. for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
  72. decreaseBrightness();
  73. }
  74. for (int i = 0; i < brightness/(100/MILIGHT_CCT_INTERVALS); i++) {
  75. increaseBrightness();
  76. }
  77. setResendCount(oldResend);
  78. } else {
  79. formatter->updateBrightness(brightness);
  80. flushPacket();
  81. }
  82. }
  83. void MiLightClient::updateStatus(MiLightStatus status, uint8_t groupId) {
  84. formatter->updateStatus(status, groupId);
  85. flushPacket();
  86. }
  87. void MiLightClient::updateStatus(MiLightStatus status) {
  88. formatter->updateStatus(status);
  89. flushPacket();
  90. }
  91. void MiLightClient::updateSaturation(const uint8_t value) {
  92. formatter->updateSaturation(value);
  93. flushPacket();
  94. }
  95. void MiLightClient::updateColorWhite() {
  96. formatter->updateColorWhite();
  97. flushPacket();
  98. }
  99. void MiLightClient::pair() {
  100. for (size_t i = 0; i < 5; i++) {
  101. formatter->updateStatus(ON);
  102. flushPacket();
  103. delay(1);
  104. }
  105. }
  106. void MiLightClient::unpair() {
  107. const MiLightRadioType type = currentRadio->config.type;
  108. if (type == RGBW) {
  109. formatter->updateStatus(ON);
  110. flushPacket();
  111. yield();
  112. formatter->updateColorWhite();
  113. flushPacket();
  114. } else if (type == CCT) {
  115. for (int i = 0; i < 5; i++) {
  116. formatter->updateStatus(ON);
  117. flushPacket();
  118. delay(1);
  119. }
  120. } else if (type == RGB_CCT) {
  121. for (int i = 0; i < 5; i++) {
  122. formatter->updateStatus(ON, 0);
  123. flushPacket();
  124. delay(1);
  125. }
  126. }
  127. }
  128. void MiLightClient::increaseBrightness() {
  129. formatter->increaseBrightness();
  130. flushPacket();
  131. }
  132. void MiLightClient::decreaseBrightness() {
  133. formatter->decreaseBrightness();
  134. flushPacket();
  135. }
  136. void MiLightClient::increaseTemperature() {
  137. formatter->increaseTemperature();
  138. flushPacket();
  139. }
  140. void MiLightClient::decreaseTemperature() {
  141. formatter->decreaseTemperature();
  142. flushPacket();
  143. }
  144. void MiLightClient::updateTemperature(const uint8_t temperature) {
  145. MiLightRadioType type = currentRadio->config.type;
  146. if (type == CCT) {
  147. const unsigned int oldResend = resendCount;
  148. setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
  149. for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
  150. decreaseTemperature();
  151. }
  152. for (int i = 0; i < temperature/(100/MILIGHT_CCT_INTERVALS); i++) {
  153. increaseTemperature();
  154. }
  155. setResendCount(oldResend);
  156. } else {
  157. formatter->updateTemperature(temperature);
  158. flushPacket();
  159. }
  160. }
  161. void MiLightClient::command(uint8_t command, uint8_t arg) {
  162. formatter->command(command, arg);
  163. flushPacket();
  164. }
  165. void MiLightClient::formatPacket(uint8_t* packet, char* buffer) {
  166. formatter->format(packet, buffer);
  167. }
  168. void MiLightClient::flushPacket() {
  169. Serial.print("Flushing packets");
  170. PacketStream& stream = formatter->buildPackets();
  171. while (stream.hasNext()) {
  172. Serial.print(".");
  173. write(stream.next());
  174. if (stream.hasNext()) {
  175. delay(10);
  176. }
  177. }
  178. Serial.println();
  179. formatter->reset();
  180. }