MiLightClient.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 < NUM_RADIOS; 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. }
  21. return NULL;
  22. }
  23. void MiLightClient::prepare(MiLightRadioConfig& config,
  24. const uint16_t deviceId,
  25. const uint8_t groupId) {
  26. switchRadio(config.type);
  27. if (deviceId >= 0 && groupId >= 0) {
  28. formatter->prepare(deviceId, groupId);
  29. }
  30. }
  31. void MiLightClient::setResendCount(const unsigned int resendCount) {
  32. this->resendCount = resendCount;
  33. }
  34. bool MiLightClient::available() {
  35. if (currentRadio == NULL) {
  36. return false;
  37. }
  38. return currentRadio->getRadio()->available();
  39. }
  40. void MiLightClient::read(uint8_t packet[]) {
  41. if (currentRadio == NULL) {
  42. return;
  43. }
  44. size_t length;
  45. currentRadio->getRadio()->read(packet, length);
  46. }
  47. void MiLightClient::write(uint8_t packet[]) {
  48. if (currentRadio == NULL) {
  49. return;
  50. }
  51. for (int i = 0; i < this->resendCount; i++) {
  52. currentRadio->getRadio()->write(packet, currentRadio->config.packetLength);
  53. }
  54. }
  55. void MiLightClient::updateColorRaw(const uint8_t color) {
  56. formatter->updateColorRaw(color);
  57. flushPacket();
  58. }
  59. void MiLightClient::updateHue(const uint16_t hue) {
  60. formatter->updateHue(hue);
  61. flushPacket();
  62. }
  63. void MiLightClient::updateBrightness(const uint8_t brightness) {
  64. const MiLightRadioType type = currentRadio->config.type;
  65. if (type == CCT) {
  66. const unsigned int oldResend = resendCount;
  67. setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
  68. for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
  69. decreaseBrightness();
  70. }
  71. for (int i = 0; i < brightness/(100/MILIGHT_CCT_INTERVALS); i++) {
  72. increaseBrightness();
  73. }
  74. setResendCount(oldResend);
  75. } else {
  76. formatter->updateBrightness(brightness);
  77. flushPacket();
  78. }
  79. }
  80. void MiLightClient::updateStatus(MiLightStatus status, uint8_t groupId) {
  81. formatter->updateStatus(status, groupId);
  82. flushPacket();
  83. }
  84. void MiLightClient::updateStatus(MiLightStatus status) {
  85. formatter->updateStatus(status);
  86. flushPacket();
  87. }
  88. void MiLightClient::updateSaturation(const uint8_t value) {
  89. formatter->updateSaturation(value);
  90. flushPacket();
  91. }
  92. void MiLightClient::updateColorWhite() {
  93. formatter->updateColorWhite();
  94. flushPacket();
  95. }
  96. void MiLightClient::pair() {
  97. for (size_t i = 0; i < 5; i++) {
  98. formatter->updateStatus(ON);
  99. flushPacket();
  100. delay(1);
  101. }
  102. }
  103. void MiLightClient::unpair() {
  104. const MiLightRadioType type = currentRadio->config.type;
  105. if (type == RGBW) {
  106. formatter->updateStatus(ON);
  107. flushPacket();
  108. yield();
  109. formatter->updateColorWhite();
  110. flushPacket();
  111. } else if (type == CCT) {
  112. for (int i = 0; i < 5; i++) {
  113. formatter->updateStatus(ON);
  114. flushPacket();
  115. delay(1);
  116. }
  117. } else if (type == RGB_CCT) {
  118. for (int i = 0; i < 5; i++) {
  119. formatter->updateStatus(ON, 0);
  120. flushPacket();
  121. delay(1);
  122. }
  123. }
  124. }
  125. void MiLightClient::increaseBrightness() {
  126. formatter->increaseBrightness();
  127. flushPacket();
  128. }
  129. void MiLightClient::decreaseBrightness() {
  130. formatter->decreaseBrightness();
  131. flushPacket();
  132. }
  133. void MiLightClient::increaseTemperature() {
  134. formatter->increaseTemperature();
  135. flushPacket();
  136. }
  137. void MiLightClient::decreaseTemperature() {
  138. formatter->decreaseTemperature();
  139. flushPacket();
  140. }
  141. void MiLightClient::updateTemperature(const uint8_t temperature) {
  142. MiLightRadioType type = currentRadio->config.type;
  143. if (type == CCT) {
  144. const unsigned int oldResend = resendCount;
  145. setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
  146. for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
  147. decreaseTemperature();
  148. }
  149. for (int i = 0; i < temperature/(100/MILIGHT_CCT_INTERVALS); i++) {
  150. increaseTemperature();
  151. }
  152. setResendCount(oldResend);
  153. } else {
  154. formatter->updateTemperature(temperature);
  155. flushPacket();
  156. }
  157. }
  158. void MiLightClient::command(uint8_t command, uint8_t arg) {
  159. formatter->command(command, arg);
  160. flushPacket();
  161. }
  162. void MiLightClient::formatPacket(uint8_t* packet, char* buffer) {
  163. formatter->format(packet, buffer);
  164. }
  165. void MiLightClient::flushPacket() {
  166. write(formatter->buildPacket());
  167. formatter->reset();
  168. }