MiLightClient.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. formatter->updateBrightness(brightness);
  65. flushPacket();
  66. }
  67. void MiLightClient::updateStatus(MiLightStatus status, uint8_t groupId) {
  68. formatter->updateStatus(status, groupId);
  69. flushPacket();
  70. }
  71. void MiLightClient::updateStatus(MiLightStatus status) {
  72. formatter->updateStatus(status);
  73. flushPacket();
  74. }
  75. void MiLightClient::updateSaturation(const uint8_t value) {
  76. formatter->updateSaturation(value);
  77. flushPacket();
  78. }
  79. void MiLightClient::updateColorWhite() {
  80. formatter->updateColorWhite();
  81. flushPacket();
  82. }
  83. void MiLightClient::pair() {
  84. for (size_t i = 0; i < 5; i++) {
  85. formatter->updateStatus(ON);
  86. flushPacket();
  87. delay(1);
  88. }
  89. }
  90. void MiLightClient::unpair() {
  91. MiLightRadioType type = currentRadio->config.type;
  92. if (type == RGBW) {
  93. formatter->updateStatus(ON);
  94. flushPacket();
  95. yield();
  96. formatter->updateColorWhite();
  97. flushPacket();
  98. } else if (type == CCT) {
  99. for (int i = 0; i < 5; i++) {
  100. formatter->updateStatus(ON);
  101. flushPacket();
  102. delay(1);
  103. }
  104. } else if (type == RGB_CCT) {
  105. for (int i = 0; i < 5; i++) {
  106. formatter->updateStatus(ON, 0);
  107. flushPacket();
  108. delay(1);
  109. }
  110. }
  111. }
  112. void MiLightClient::increaseBrightness() {
  113. formatter->increaseBrightness();
  114. flushPacket();
  115. }
  116. void MiLightClient::decreaseBrightness() {
  117. formatter->decreaseBrightness();
  118. flushPacket();
  119. }
  120. void MiLightClient::increaseTemperature() {
  121. formatter->increaseTemperature();
  122. flushPacket();
  123. }
  124. void MiLightClient::decreaseTemperature() {
  125. formatter->decreaseTemperature();
  126. flushPacket();
  127. }
  128. void MiLightClient::updateTemperature(const uint8_t temperature) {
  129. formatter->updateTemperature(temperature);
  130. flushPacket();
  131. }
  132. void MiLightClient::command(uint8_t command, uint8_t arg) {
  133. formatter->command(command, arg);
  134. flushPacket();
  135. }
  136. void MiLightClient::formatPacket(MiLightRadioConfig& config, uint8_t* packet, char* buffer) {
  137. if (config.type == RGBW || config.type == CCT) {
  138. String format = String("Request type : %02X\n")
  139. + "Device ID : %02X%02X\n"
  140. + "b1 : %02X\n"
  141. + "b2 : %02X\n"
  142. + "b3 : %02X\n"
  143. + "Sequence Num. : %02X";
  144. sprintf(
  145. buffer,
  146. format.c_str(),
  147. packet[0],
  148. packet[1], packet[2],
  149. packet[3],
  150. packet[4],
  151. packet[5],
  152. packet[6]
  153. );
  154. } else {
  155. for (int i = 0; i < config.packetLength; i++) {
  156. sprintf(buffer, "%02X ", packet[i]);
  157. buffer += 3;
  158. }
  159. sprintf(buffer, "\n\n");
  160. }
  161. }
  162. void MiLightClient::flushPacket() {
  163. write(formatter->buildPacket());
  164. formatter->reset();
  165. }