MiLightClient.cpp 4.7 KB

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